blob: 131b0677a5a830aa9cba7edb5f700695c7a3f91a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
package de.hype.bbsentials.client;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.hype.bbsentials.chat.Chat;
import de.hype.bbsentials.chat.Sender;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import java.io.*;
import java.lang.reflect.Field;
import java.net.Socket;
import java.time.LocalDate;
import java.util.ArrayList;
public class Config implements Serializable {
// Helper class for sending chat messages
public int version = 1;
public transient final Sender sender = new Sender();
public transient boolean highlightitem = false;
public transient String lastChatPromptAnswer = null;
// Automatically set, no need for config
private transient String username;
private boolean overrideBingoTime = false;
// Set in-game
private transient boolean isLeader;
private transient String alreadyReported = "";
private String bbServerURL = "static.204.177.34.188.clients.your-server.de";
public String[] bbsentialsRoles = {""};
public static ArrayList<String> partyMembers = new ArrayList<>();
// Set via load / default
private String bbsentialsCommandPrefix = ".";
private String apiKey = "";
private boolean leaveDungeonAutomatically;
public boolean showBingoChat = true;
private boolean allowBBinviteMe = true;
private boolean leaveKuudraAutomatically;
private boolean devMode = false;
private boolean detailedDevMode = false;
private boolean acceptReparty;
private String nickname;
private String getNotifForParty;
private final int apiVersion = 1;
public transient ToDisplayConfig toDisplayConfig = ToDisplayConfig.loadFromFile();
// Set default attribute values
private void setDefaults() {
username = MinecraftClient.getInstance().player.getName().getString();
leaveKuudraAutomatically = true;
leaveDungeonAutomatically = true;
acceptReparty = true;
if (username.equals("Hype_the_Time")) {
nickname = "Hype";
getNotifForParty = "nick";
}
else {
nickname = "";
getNotifForParty = "none";
}
}
// Method to send the config to a server using sockets
public void sendConfig(Config config, String host, int port) {
Socket socket = null;
ObjectOutputStream objectOutputStream = null;
try {
socket = new Socket(host, port);
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(config);
objectOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Method to replace the current config with a new one
public void replaceConfig(Config newConfig) {
try {
// Get the class of the current config
Class<? extends Config> currentClass = this.getClass();
// Get the fields of the current config class
Field[] currentFields = currentClass.getDeclaredFields();
// Iterate through the fields
for (Field field : currentFields) {
// Exclude the socket field from being updated
if (field.getName().equals("serverSocket") || field.getName().equals("clientSocket")) {
continue;
}
// Make the field accessible to modify its value
field.setAccessible(true);
// Get the corresponding field from the new config class
Field newField = newConfig.getClass().getDeclaredField(field.getName());
// Make the new field accessible to read its value
newField.setAccessible(true);
// Get the current value of the field
Object currentValue = field.get(this);
// Get the new value of the field
Object newValue = newField.get(newConfig);
// Update the field only if it is defined or explicitly overridden
if (newValue != null || currentValue == null) {
field.set(this, newValue);
}
}
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
// Gson object for serialization
private final transient Gson GSON = new GsonBuilder().setPrettyPrinting().create();
// File object for storing the config
private final transient File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), "BBsential_settings.json");
// Constructor
public Config() {
setDefaults();
}
// Load the config from file
public static Config load() {
Config settings;
File CONFIG_FILE = new File(FabricLoader.getInstance().getConfigDir().toFile(), "BBsential_settings.json");
Gson GSON = new GsonBuilder().setPrettyPrinting().create();
if (CONFIG_FILE.exists()) {
try (FileReader reader = new FileReader(CONFIG_FILE)) {
settings = GSON.fromJson(reader, Config.class);
} catch (IOException e) {
e.printStackTrace();
settings = new Config(); // Use default values if loading fails
settings.save();
}catch (IllegalStateException e){
System.out.println("Error loading config. Resetting it.");
settings = new Config();
settings.save();
}
}
else {
settings = new Config(); // Use default values if the file doesn't exist
settings.username = MinecraftClient.getInstance().player.getName().getString();
}
if (!settings.hasBBRoles("dev")) {
settings.detailedDevMode = false;
settings.devMode = false;
}
settings.save();
return settings;
}
// Save the config to file
public void save() {
try (FileWriter writer = new FileWriter(CONFIG_FILE)) {
GSON.toJson(this, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
// Getter methods for various config attributes
public String getUsername() {
return username;
}
public boolean isLeader() {
return isLeader;
}
public void setIsLeader(boolean value) {
isLeader = value;
}
public String getNickname() {
return nickname;
}
public String getNotifForParty() {
return getNotifForParty;
}
public boolean isLeaveDungeonAutomatically() {
return leaveDungeonAutomatically;
}
public boolean isLeaveKuudraAutomatically() {
return leaveKuudraAutomatically;
}
public boolean isDevModeEnabled() {
return devMode;
}
public boolean isDetailedDevModeEnabled() {
return detailedDevMode;
}
public String[] getPlayersInParty() {
return partyMembers.toArray(new String[0]);
}
public boolean messageFromAlreadyReported(String message) {
return alreadyReported.contains(Chat.getPlayerNameFromMessage(message));
}
public void addReported(String playerName) {
alreadyReported = alreadyReported + " , " + playerName;
}
public String getApiKey() {
return apiKey;
}
public String getBBServerURL() {
return bbServerURL;
}
public String getCommandPrefix(String type) {
if (type.equals("BBsentials")) {
System.out.println("Registered command with: " + bbsentialsCommandPrefix);
return bbsentialsCommandPrefix;
}
else {
return "/";
}
}
public static boolean isBingoTime() {
LocalDate currentDate = LocalDate.now();
LocalDate lastDayOfMonth = currentDate.withDayOfMonth(currentDate.lengthOfMonth());
LocalDate firstDayOfMonth = currentDate.withDayOfMonth(1);
Boolean isBefore = currentDate.isAfter(lastDayOfMonth.minusDays(4));
Boolean isInRange = currentDate.isBefore(firstDayOfMonth.plusDays(15));
return isBefore || isInRange;
}
public boolean overrideBingoTime() {
return overrideBingoTime;
}
public boolean isHighlightitem() {
return highlightitem;
}
public String getLastChatPromptAnswer() {
return lastChatPromptAnswer;
}
public boolean allowBBinviteMe() {
return allowBBinviteMe;
}
public void setLastChatPromptAnswer(String lastChatPromptAnswer) {
this.lastChatPromptAnswer = lastChatPromptAnswer;
}
public int getVersion() {
return version;
}
public boolean hasBBRoles(String roleName) {
for (String role : bbsentialsRoles) {
if (role.equalsIgnoreCase(roleName)) {
return true;
}
}
return false;
}
public int getApiVersion() {
return apiVersion;
}
}
|