-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathMainConfiguration.java
More file actions
405 lines (338 loc) · 14.8 KB
/
Copy pathMainConfiguration.java
File metadata and controls
405 lines (338 loc) · 14.8 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package fr.maxlego08.essentials;
import fr.maxlego08.essentials.api.Configuration;
import fr.maxlego08.essentials.api.chat.ChatCooldown;
import fr.maxlego08.essentials.api.commands.CommandCooldown;
import fr.maxlego08.essentials.api.commands.CommandRestriction;
import fr.maxlego08.essentials.api.config.models.NearDirectionReplacements;
import fr.maxlego08.essentials.api.configuration.NonLoadable;
import fr.maxlego08.essentials.api.configuration.ReplacePlaceholder;
import fr.maxlego08.essentials.api.configuration.ReplacePlaceholderElement;
import fr.maxlego08.essentials.api.configuration.ReplacePlaceholderType;
import fr.maxlego08.essentials.api.configuration.placeholders.NumberPlaceholder;
import fr.maxlego08.essentials.api.configuration.placeholders.StringPlaceholder;
import fr.maxlego08.essentials.api.server.RedisConfiguration;
import fr.maxlego08.essentials.api.server.ServerType;
import fr.maxlego08.essentials.api.storage.StorageType;
import fr.maxlego08.essentials.api.user.Option;
import fr.maxlego08.essentials.api.utils.MessageColor;
import fr.maxlego08.essentials.api.utils.NearDistance;
import fr.maxlego08.essentials.api.utils.TransformMaterial;
import fr.maxlego08.essentials.api.worldedit.Cuboid;
import fr.maxlego08.essentials.zutils.utils.YamlLoader;
import fr.maxlego08.sarah.DatabaseConfiguration;
import fr.maxlego08.sarah.database.DatabaseType;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.permissions.Permissible;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.LongStream;
public class MainConfiguration extends YamlLoader implements Configuration {
private final ZEssentialsPlugin plugin;
private final List<CommandCooldown> commandCooldowns = new ArrayList<>();
private final List<TransformMaterial> compactMaterials = new ArrayList<>();
private final List<TransformMaterial> smeltableMaterials = new ArrayList<>();
private final StorageType storageType = StorageType.JSON;
private final List<MessageColor> messageColors = new ArrayList<>();
private final List<ChatCooldown> cooldowns = new ArrayList<>();
private double nearDistance;
@NonLoadable
private NearDirectionReplacements nearDirectionReplacements = NearDirectionReplacements.DEFAULT;
private final List<NearDistance> nearPermissions = new ArrayList<>();
private long dayTime = 1000;
private long nightTime = 13000;
private boolean timeSmoothChange = false;
private final List<String> disableFlyWorld = new ArrayList<>();
private final List<String> disableBackWorld = new ArrayList<>();
@NonLoadable
private final List<CommandRestriction> commandRestrictions = new ArrayList<>();
private final Map<Option, Boolean> defaultOptionValues = new HashMap<>();
private long[] cooldownCommands;
private boolean enableDebug;
private boolean enableCooldownBypass;
private boolean enableCommandLog;
private boolean tempFlyTask;
private int trashSize;
private String globalDateFormat;
@NonLoadable
private DatabaseConfiguration databaseConfiguration;
private ServerType serverType;
private RedisConfiguration redisConfiguration;
private SimpleDateFormat simpleDateFormat;
private List<ReplacePlaceholder> replacePlaceholders = new ArrayList<>();
private List<String> randomWords;
private boolean enableOfflinePlayerNames;
private boolean enableFlyReturn;
private long batchAutoSave;
private List<UUID> blacklistUuids;
private List<Long> flyTaskAnnounce;
private String placeholderEmpty;
private final List<String> forceCommands = new ArrayList<>();
public MainConfiguration(ZEssentialsPlugin plugin) {
this.plugin = plugin;
}
@Override
public boolean isEnableDebug() {
return this.enableDebug;
}
@Override
public boolean isEnableCooldownBypass() {
return this.enableCooldownBypass;
}
@Override
public List<CommandCooldown> getCommandCooldown() {
return this.commandCooldowns;
}
@Override
public Optional<Integer> getCooldown(Permissible permissible, String command) {
return this.commandCooldowns.stream().filter(commandCooldown -> commandCooldown.command().equalsIgnoreCase(command)).map(commandCooldown -> {
List<Map<String, Object>> permissions = commandCooldown.permissions() == null ? new ArrayList<>() : commandCooldown.permissions();
return permissions.stream().filter(e -> permissible.hasPermission((String) e.get("permission"))).mapToInt(e -> ((Number) e.get("cooldown")).intValue()).min().orElse(commandCooldown.cooldown());
}).findFirst();
}
@Override
public String getPlaceholderEmpty() {
return this.placeholderEmpty == null || this.placeholderEmpty.isEmpty() ? "Empty" : this.placeholderEmpty;
}
@Override
public void load() {
this.plugin.reloadConfig();
YamlConfiguration configuration = (YamlConfiguration) this.plugin.getConfig();
this.loadYamlConfirmation(this.plugin, configuration);
this.commandRestrictions.clear();
for (Map<?, ?> map : configuration.getMapList("command-restrictions")) {
List<String> commands = (List<String>) map.get("commands");
String bypass = (String) map.get("bypass-permission");
List<String> worlds = map.get("worlds") == null ? new ArrayList<>() : (List<String>) map.get("worlds");
List<Cuboid> cuboids = new ArrayList<>();
List<String> cuboidStrings = (List<String>) map.get("cuboids");
if (cuboidStrings != null) {
for (String cuboidString : cuboidStrings) {
String[] parts = cuboidString.split(",");
if (parts.length == 7) {
try {
String worldName = parts[0];
int x1 = Integer.parseInt(parts[1]);
int y1 = Integer.parseInt(parts[2]);
int z1 = Integer.parseInt(parts[3]);
int x2 = Integer.parseInt(parts[4]);
int y2 = Integer.parseInt(parts[5]);
int z2 = Integer.parseInt(parts[6]);
World world = Bukkit.getWorld(worldName);
if (world != null) {
cuboids.add(new Cuboid(world, x1, y1, z1, x2, y2, z2));
}
} catch (NumberFormatException ignored) {
}
}
}
}
this.commandRestrictions.add(new CommandRestriction(commands, bypass, worlds, cuboids));
}
this.databaseConfiguration = new DatabaseConfiguration(
configuration.getString("database-configuration.tablePrefix", configuration.getString("database-configuration.table-prefix")),
configuration.getString("database-configuration.user"),
configuration.getString("database-configuration.password"),
configuration.getInt("database-configuration.port", this.storageType == StorageType.POSTGRESQL ? 5432 : 3306),
configuration.getString("database-configuration.host"),
configuration.getString("database-configuration.database"),
configuration.getBoolean("database-configuration.debug"),
getDatabaseType()
);
final ConfigurationSection nearDirSection = configuration.getConfigurationSection("near-direction-replacements");
if (nearDirSection != null) {
this.nearDirectionReplacements = new NearDirectionReplacements(
configuration.getString("forward", "↑"),
configuration.getString("forward-right", "↗"),
configuration.getString("right", "→"),
configuration.getString("back-right", "↘"),
configuration.getString("back", "↓"),
configuration.getString("back-left", "↙"),
configuration.getString("left", "←"),
configuration.getString("forward-left", "↖")
);
}
this.cooldownCommands = this.cooldowns.stream().flatMapToLong(cooldown -> LongStream.of(cooldown.cooldown(), cooldown.messages())).toArray();
this.simpleDateFormat = this.globalDateFormat == null ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") : new SimpleDateFormat(this.globalDateFormat);
this.flyTaskAnnounce = configuration.getLongList("fly-task-announce");
this.loadReplacePlaceholders();
this.defaultOptionValues.clear();
for (Map<?, ?> map : configuration.getMapList("default-options")) {
this.defaultOptionValues.put(Option.valueOf((String) map.get("option")), (Boolean) map.get("value"));
}
}
private DatabaseType getDatabaseType() {
return switch (this.storageType) {
case SQLITE -> DatabaseType.SQLITE;
case MARIADB -> DatabaseType.MARIADB;
case POSTGRESQL -> DatabaseType.POSTGRESQL;
default -> DatabaseType.MYSQL;
};
}
private void loadReplacePlaceholders() {
YamlConfiguration configuration = (YamlConfiguration) this.plugin.getConfig();
this.replacePlaceholders = new ArrayList<>();
List<Map<?, ?>> placeholdersList = configuration.getMapList("replace-placeholders");
for (Map<?, ?> placeholderMap : placeholdersList) {
if (!placeholderMap.containsKey("placeholder") || !placeholderMap.containsKey("default")) {
this.plugin.getLogger().severe("Your replace-placeholders is wrong ! Please check your configuration.");
continue;
}
String placeholder = (String) placeholderMap.get("placeholder");
String defaultValue = (String) placeholderMap.get("default");
List<ReplacePlaceholderElement> elements = new ArrayList<>();
List<Map<?, ?>> replacesList = (List<Map<?, ?>>) placeholderMap.get("replaces");
for (Map<?, ?> replaceMap : replacesList) {
if (!replaceMap.containsKey("type") || !replaceMap.containsKey("value")) {
this.plugin.getLogger().severe("Your replace-placeholders is wrong ! Please check your configuration.");
continue;
}
ReplacePlaceholderType type = ReplacePlaceholderType.valueOf((String) replaceMap.get("type"));
String value = (String) replaceMap.get("value");
if (type == ReplacePlaceholderType.NUMBER) {
int max = (int) replaceMap.get("max");
elements.add(new NumberPlaceholder(value, max));
} else if (type == ReplacePlaceholderType.STRING) {
String string = String.valueOf(replaceMap.get("equalsTo"));
elements.add(new StringPlaceholder(value, string));
}
}
this.replacePlaceholders.add(new ReplacePlaceholder(placeholder, defaultValue, elements));
}
;
}
@Override
public int getTrashSize() {
return this.trashSize;
}
@Override
public List<TransformMaterial> getCompactMaterials() {
return this.compactMaterials;
}
@Override
public StorageType getStorageType() {
return this.storageType;
}
@Override
public DatabaseConfiguration getDatabaseConfiguration() {
return this.databaseConfiguration;
}
@Override
public ServerType getServerType() {
return serverType;
}
@Override
public RedisConfiguration getRedisConfiguration() {
return redisConfiguration;
}
@Override
public List<MessageColor> getMessageColors() {
return messageColors;
}
@Override
public List<ChatCooldown> getCooldowns() {
return this.cooldowns;
}
@Override
public long[] getCooldownCommands() {
return cooldownCommands;
}
@Override
public List<TransformMaterial> getSmeltableMaterials() {
return this.smeltableMaterials;
}
@Override
public double getDefaultNearDistance() {
return nearDistance;
}
@Override
public NearDirectionReplacements getNearDirectionReplacements() {
return this.nearDirectionReplacements;
}
@Override
public double getNearDistance(Permissible permissible) {
return this.nearPermissions.stream().filter(nearDistance -> permissible.hasPermission(nearDistance.permission())).map(NearDistance::distance).findFirst().orElse(this.nearDistance);
}
@Override
public List<NearDistance> getNearPermissions() {
return nearPermissions;
}
@Override
public long getDayTime() {
return this.dayTime;
}
@Override
public long getNightTime() {
return this.nightTime;
}
@Override
public boolean isTimeSmoothChangeEnabled() {
return this.timeSmoothChange;
}
@Override
public boolean isEnableCommandLog() {
return this.enableCommandLog;
}
@Override
public SimpleDateFormat getGlobalDateFormat() {
return this.simpleDateFormat;
}
@Override
public List<ReplacePlaceholder> getReplacePlaceholders() {
return this.replacePlaceholders;
}
@Override
public Optional<ReplacePlaceholder> getReplacePlaceholder(String placeholder) {
return this.replacePlaceholders.stream().filter(replacePlaceholder -> replacePlaceholder.placeholder().equalsIgnoreCase(placeholder)).findFirst();
}
@Override
public boolean isTempFlyTask() {
return tempFlyTask;
}
@Override
public List<String> getDisableFlyWorld() {
return disableFlyWorld;
}
@Override
public List<String> getRandomWords() {
return randomWords;
}
@Override
public boolean isEnableOfflinePlayersName() {
return this.enableOfflinePlayerNames;
}
@Override
public boolean isEnableFlyReturn() {
return this.enableFlyReturn;
}
@Override
public long getBatchAutoSave() {
return this.batchAutoSave;
}
@Override
public List<UUID> getBlacklistUuids() {
return blacklistUuids;
}
@Override
public List<Long> getFlyTaskAnnounce() {
return this.flyTaskAnnounce;
}
@Override
public List<String> getDisableBackWorld() {
return disableBackWorld;
}
@Override
public Map<Option, Boolean> getDefaultOptionValues() {
return defaultOptionValues;
}
@Override
public List<CommandRestriction> getCommandRestrictions() {
return this.commandRestrictions;
}
@Override
public List<String> getForceCommands() {
return this.forceCommands;
}
}