-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigHandler.java
More file actions
253 lines (209 loc) · 9.09 KB
/
ConfigHandler.java
File metadata and controls
253 lines (209 loc) · 9.09 KB
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
package simplexity.simplepms.config;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import simplexity.simplepms.SimplePMs;
import simplexity.simplepms.hooks.DiscordWebHook;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConfigHandler {
private static ConfigHandler instance;
public static ConfigHandler getInstance() {
if (instance == null) instance = new ConfigHandler();
return instance;
}
private final Logger logger = SimplePMs.getInstance().getLogger();
private boolean mysqlEnabled, playersSendToConsole, playersSendToHiddenPlayers, consoleHasSocialSpy,
commandSpyEnabled, consoleHasCommandSpy, receiveSoundEnabled, sendSoundEnabled, spySoundEnabled, webhookEnabled;
private NamespacedKey receiveSound = Registry.SOUNDS.getKey(Sound.BLOCK_NOTE_BLOCK_XYLOPHONE);
private NamespacedKey sendSound = Registry.SOUNDS.getKey(Sound.ENTITY_ALLAY_ITEM_THROWN);
private NamespacedKey spySound = Registry.SOUNDS.getKey(Sound.ENTITY_ITEM_FRAME_ROTATE_ITEM);
private float receivePitch, receiveVolume, sendPitch, sendVolume, spyPitch, spyVolume;
private String mysqlIp, mysqlName, mysqlUsername, mysqlPassword, normalFormat, socialSpyFormat, webhookBody;
private URI webhookUri;
private final List<String> validNamesForConsole = new ArrayList<>();
private final HashSet<String> commandsToSpy = new HashSet<>();
public void loadConfigValues() {
SimplePMs.getInstance().reloadConfig();
FileConfiguration config = SimplePMs.getInstance().getConfig();
LocaleHandler.getInstance().reloadLocale();
List<String> commands = config.getStringList("command-spy.commands");
List<String> consoleNames = config.getStringList("valid-console-names");
updateHashSet(commandsToSpy, commands);
validateConsoleNames(consoleNames);
normalFormat = config.getString("format.normal", "<displayname>");
socialSpyFormat = config.getString("format.social-spy", "<username>");
mysqlEnabled = config.getBoolean("mysql.enabled", false);
commandSpyEnabled = config.getBoolean("command-spy.enabled", false);
mysqlIp = config.getString("mysql.ip", "localhost:3306");
mysqlName = config.getString("mysql.name", "homes");
mysqlUsername = config.getString("mysql.username", "username1");
mysqlPassword = config.getString("mysql.password", "badpassword!");
playersSendToConsole = config.getBoolean("allow-messaging.console", true);
playersSendToHiddenPlayers = config.getBoolean("allow-messaging.hidden-players", false);
consoleHasSocialSpy = config.getBoolean("console-has-social-spy", true);
consoleHasCommandSpy = config.getBoolean("console-has-command-spy", false);
receiveSoundEnabled = config.getBoolean("sounds.received.enabled", false);
sendSoundEnabled = config.getBoolean("sounds.sent.enabled", false);
spySoundEnabled = config.getBoolean("sounds.spy.enabled", false);
webhookEnabled = config.getBoolean("webhook.enabled", false);
if (receiveSoundEnabled) loadReceiveSoundInfo(config);
if (sendSoundEnabled) loadSendSoundInfo(config);
if (spySoundEnabled) loadSpySoundInfo(config);
if (webhookEnabled) loadWebhook(config);
}
private void updateHashSet(HashSet<String> set, List<String> list) {
set.clear();
set.addAll(list);
}
private void loadReceiveSoundInfo(FileConfiguration config) {
String soundString = config.getString("sounds.received.sound", "minecraft:block.note_block.xylophone");
receiveSound = getValidSound(soundString, Registry.SOUNDS.getKey(Sound.BLOCK_NOTE_BLOCK_XYLOPHONE));
receivePitch = getValidFloat(config.getDouble("sounds.received.pitch", 1.8));
receiveVolume = getValidFloat(config.getDouble("sounds.received.volume", 0.5));
}
private void loadSendSoundInfo(FileConfiguration config) {
String soundString = config.getString("sounds.sent.sound", "minecraft:entity.allay.item_thrown");
sendSound = getValidSound(soundString, Registry.SOUNDS.getKey(Sound.ENTITY_ALLAY_ITEM_THROWN));
sendPitch = getValidFloat(config.getDouble("sounds.sent.pitch", 1.8));
sendVolume = getValidFloat(config.getDouble("sounds.sent.volume", 0.5));
}
private void loadSpySoundInfo(FileConfiguration config) {
String soundString = config.getString("sounds.spy.sound", "minecraft:entity.item_frame.rotate_item");
spySound = getValidSound(soundString, Registry.SOUNDS.getKey(Sound.ENTITY_ITEM_FRAME_ROTATE_ITEM));
spyPitch = getValidFloat(config.getDouble("sounds.spy.pitch", 1.8));
spyVolume = getValidFloat(config.getDouble("sounds.spy.volume", 0.5));
}
private void loadWebhook(FileConfiguration config) {
webhookBody = config.getString("webhook.json-body", null);
String uri = config.getString("webhook.url", null);
if (uri == null || uri.isBlank()) {
logger.warning(LocaleMessage.LOG_ERROR_WEBHOOK_URL_BLANK.getMessage());
webhookEnabled = false;
return;
}
try {
webhookUri = URI.create(uri);
}
catch (IllegalArgumentException e) {
logger.log(Level.WARNING, LocaleMessage.LOG_ERROR_WEBHOOK_URL_MALFORMED.getMessage(), e);
webhookEnabled = false;
return;
}
if (webhookEnabled) DiscordWebHook.createClient();
}
private NamespacedKey getValidSound(String soundString, NamespacedKey defaultSound) {
NamespacedKey key = NamespacedKey.fromString(soundString);
if (key == null || Registry.SOUNDS.get(key) == null) {
String warning = LocaleMessage.LOG_ERROR_SOUND_NOT_VALID.getMessage().replace("%sound-string%", soundString);
String warning2 = LocaleMessage.LOG_ERROR_USING_DEFAULT_SOUND.getMessage().replace("%default-sound%", defaultSound.getKey());
logger.warning(warning);
logger.warning(warning2);
return defaultSound;
}
return key;
}
private float getValidFloat(double numberToCheck) {
if (numberToCheck <= 2 && numberToCheck >= 0) return (float) numberToCheck;
String warning = LocaleMessage.LOG_ERROR_FLOAT_OUT_OF_RANGE.getMessage().replace("%number%", String.valueOf(numberToCheck));
logger.warning(warning);
logger.warning(LocaleMessage.LOG_ERROR_USING_DEFAULT_FLOAT.getMessage());
return 1.0f;
}
private void validateConsoleNames(List<String> list) {
validNamesForConsole.clear();
for (String name : list) {
validNamesForConsole.add(name.toLowerCase());
}
}
public boolean isMysqlEnabled() {
return mysqlEnabled;
}
public String getMysqlIp() {
return mysqlIp;
}
public String getMysqlName() {
return mysqlName;
}
public String getMysqlUsername() {
return mysqlUsername;
}
public String getMysqlPassword() {
return mysqlPassword;
}
public boolean canPlayersSendToConsole() {
return playersSendToConsole;
}
public boolean canPlayersSendToHiddenPlayers() {
return playersSendToHiddenPlayers;
}
public List<String> getValidNamesForConsole() {
return validNamesForConsole;
}
public HashSet<String> getCommandsToSpy() {
return commandsToSpy;
}
public boolean doesConsoleHaveSocialSpy() {
return consoleHasSocialSpy;
}
public boolean doesConsoleHaveCommandSpy() {
return consoleHasCommandSpy;
}
public boolean isCommandSpyEnabled() {
return commandSpyEnabled;
}
public boolean receivingMessagePlaysSound() {
return receiveSoundEnabled;
}
public boolean sendingMessagePlaysSound() {
return sendSoundEnabled;
}
public boolean messagePlaysSoundForSpy() {
return spySoundEnabled;
}
public String getNormalFormat() {
return normalFormat;
}
public String getSocialSpyFormat() {
return socialSpyFormat;
}
@NotNull
public NamespacedKey getReceiveSound() {
return receiveSound;
}
@NotNull
public NamespacedKey getSendSound() {
return sendSound;
}
@NotNull
public NamespacedKey getSpySound() {
return spySound;
}
public float getReceivePitch() {
return receivePitch;
}
public float getReceiveVolume() {
return receiveVolume;
}
public float getSendPitch() {
return sendPitch;
}
public float getSendVolume() {
return sendVolume;
}
public float getSpyPitch() {
return spyPitch;
}
public float getSpyVolume() {
return spyVolume;
}
public boolean isWebhookEnabled() { return webhookEnabled; }
public URI getWebhookUri() { return webhookUri; }
public String getWebhookBody() { return webhookBody; }
}