-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathKitManager.java
More file actions
522 lines (465 loc) · 19.5 KB
/
KitManager.java
File metadata and controls
522 lines (465 loc) · 19.5 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright 2022-2025 Noah Ross
*
* This file is part of PerPlayerKit.
*
* PerPlayerKit is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* PerPlayerKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PerPlayerKit. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.noah.perplayerkit;
import dev.noah.perplayerkit.util.BroadcastManager;
import dev.noah.perplayerkit.util.IDUtil;
import dev.noah.perplayerkit.util.Serializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import dev.noah.perplayerkit.util.SoundManager;
import org.bukkit.inventory.ItemStack;
import java.io.IOException;
import java.util.*;
public class KitManager {
private static KitManager instance;
private final PerPlayerKit plugin;
private final HashMap<String, ItemStack[]> kitByKitIDMap;
private final HashMap<UUID, Integer> lastKitUsedByPlayer;
private final List<PublicKit> publicKitList;
public KitManager(PerPlayerKit plugin) {
this.plugin = plugin;
lastKitUsedByPlayer = new HashMap<>();
publicKitList = new ArrayList<>();
kitByKitIDMap = new HashMap<>();
instance = this;
}
public static KitManager get() {
if (instance == null) {
throw new IllegalStateException("KitManager not initialized");
}
return instance;
}
public ItemStack[] getItemStackArrayById(String id) {
return kitByKitIDMap.get(id);
}
public List<PublicKit> getPublicKitList() {
return publicKitList;
}
public int getLastKitLoaded(UUID uuid) {
if (lastKitUsedByPlayer.containsKey(uuid)) {
return lastKitUsedByPlayer.get(uuid);
}
return -1;
}
public boolean savekit(UUID uuid, int slot, ItemStack[] kit) {
if (Bukkit.getPlayer(uuid) != null) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
boolean notEmpty = false;
for (ItemStack i : kit) {
if (i != null) {
if (!notEmpty) {
notEmpty = true;
}
}
}
if (notEmpty) {
if (kit[36] != null) {
if (!kit[36].getType().toString().contains("BOOTS")) {
kit[36] = null;
}
}
if (kit[37] != null) {
if (!kit[37].getType().toString().contains("LEGGINGS")) {
kit[37] = null;
}
}
if (kit[38] != null) {
if (!(kit[38].getType().toString().contains("CHESTPLATE") || kit[38].getType().toString().contains("ELYTRA"))) {
kit[38] = null;
}
}
if (kit[39] != null) {
if (!kit[39].getType().toString().contains("HELMET")) {
kit[39] = null;
}
}
kitByKitIDMap.put(IDUtil.getPlayerKitId(uuid, slot), kit);
player.sendMessage(ChatColor.GREEN + "Kit " + slot + " saved!");
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> savePlayerKitToDB(uuid, slot));
return true;
} else {
player.sendMessage(ChatColor.RED + "You cant save an empty kit!");
}
}
}
return false;
}
public boolean savePublicKit(Player player, String publickit, ItemStack[] kit) {
boolean notEmpty = false;
for (ItemStack i : kit) {
if (i != null) {
if (!notEmpty) {
notEmpty = true;
}
}
}
if (notEmpty) {
if (kit[36] != null) {
if (!kit[36].getType().toString().contains("BOOTS")) {
kit[36] = null;
}
}
if (kit[37] != null) {
if (!kit[37].getType().toString().contains("LEGGINGS")) {
kit[37] = null;
}
}
if (kit[38] != null) {
if (!(kit[38].getType().toString().contains("CHESTPLATE") || kit[38].getType().toString().contains("ELYTRA"))) {
kit[38] = null;
}
}
if (kit[39] != null) {
if (!kit[39].getType().toString().contains("HELMET")) {
kit[39] = null;
}
}
kitByKitIDMap.put(IDUtil.getPublicKitId(publickit), kit);
player.sendMessage(ChatColor.GREEN + "Public Kit " + publickit + " saved!");
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> savePublicKitToDB(publickit));
return true;
} else {
player.sendMessage(ChatColor.RED + "You cant save an empty kit!");
}
return false;
}
public boolean savePublicKit(String id, ItemStack[] kit) {
boolean notEmpty = false;
for (ItemStack i : kit) {
if (i != null) {
if (!notEmpty) {
notEmpty = true;
}
}
}
if (notEmpty) {
if (kit[36] != null) {
if (!kit[36].getType().toString().contains("BOOTS")) {
kit[36] = null;
}
}
if (kit[37] != null) {
if (!kit[37].getType().toString().contains("LEGGINGS")) {
kit[37] = null;
}
}
if (kit[38] != null) {
if (!(kit[38].getType().toString().contains("CHESTPLATE") || kit[38].getType().toString().contains("ELYTRA"))) {
kit[38] = null;
}
}
if (kit[39] != null) {
if (!kit[39].getType().toString().contains("HELMET")) {
kit[39] = null;
}
}
kitByKitIDMap.put(IDUtil.getPublicKitId(id), kit);
return true;
}
return false;
}
public boolean saveEC(UUID uuid, int slot, ItemStack[] kit) {
if (Bukkit.getPlayer(uuid) != null) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
boolean notEmpty = false;
for (ItemStack i : kit) {
if (i != null) {
if (!notEmpty) {
notEmpty = true;
}
}
}
if (notEmpty) {
kitByKitIDMap.put(IDUtil.getECId(uuid, slot), kit);
player.sendMessage(ChatColor.GREEN + "Enderchest " + slot + " saved!");
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> saveEnderchestToDB(uuid, slot));
return true;
} else {
player.sendMessage(ChatColor.RED + "You cant save an empty enderchest!");
}
}
}
return false;
}
public boolean savekit(UUID uuid, int slot, ItemStack[] kit, boolean silent) {
if (silent) {
if (Bukkit.getPlayer(uuid) != null) {
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
boolean notEmpty = false;
for (ItemStack i : kit) {
if (i != null) {
if (!notEmpty) {
notEmpty = true;
}
}
}
if (notEmpty) {
if (kit[36] != null) {
if (!kit[36].getType().toString().contains("BOOTS")) {
kit[36] = null;
}
}
if (kit[37] != null) {
if (!kit[37].getType().toString().contains("LEGGINGS")) {
kit[37] = null;
}
}
if (kit[38] != null) {
if (!(kit[38].getType().toString().contains("CHESTPLATE") || kit[38].getType().toString().contains("ELYTRA"))) {
kit[38] = null;
}
}
if (kit[39] != null) {
if (!kit[39].getType().toString().contains("HELMET")) {
kit[39] = null;
}
}
kitByKitIDMap.put(IDUtil.getPlayerKitId(uuid, slot), ItemFilter.get().filterItemStack(kit));
return true;
} else {
player.sendMessage(ChatColor.RED + "You cant save an empty kit!");
}
}
}
return false;
} else {
return savekit(uuid, slot, kit);
}
}
public boolean regearKit(Player player, int slot) {
UUID uuid = player.getUniqueId();
if (kitByKitIDMap.get(IDUtil.getPlayerKitId(uuid, slot)) == null) {
return false;
}
boolean invertWhitelist = plugin.getConfig().getBoolean("regear.invert-whitelist", false);
Set<String> whitelist = new HashSet<>(plugin.getConfig().getStringList("regear.whitelist"));
ItemStack[] kit = kitByKitIDMap.get(IDUtil.getPlayerKitId(uuid, slot));
ItemStack[] playerInventory = player.getInventory().getContents();
for (int i = 0; i < Math.min(playerInventory.length, kit.length); i++) {
if (kit[i] == null) {
continue;
}
if (invertWhitelist) {
if (whitelist.contains(kit[i].getType().toString())) {
continue;
}
} else {
if (!whitelist.contains(kit[i].getType().toString())) {
continue;
}
}
if (playerInventory[i] == null || playerInventory[i].getType().isAir() || playerInventory[i].getType() == kit[i].getType()) {
playerInventory[i] = kit[i];
continue;
}
}
player.getInventory().setContents(playerInventory);
return true;
}
private boolean loadKitInternal(Player player, String kitId, String notFoundMessage, boolean isEnderChest, Runnable afterLoad) {
if (player == null) {
return false;
}
ItemStack[] kit = kitByKitIDMap.get(kitId);
if (kit == null) {
if (notFoundMessage != null) {
player.sendMessage(ChatColor.RED + notFoundMessage);
SoundManager.playFailure(player);
}
return false;
}
if (isEnderChest) {
player.getEnderChest().setContents(kit);
} else {
player.getInventory().setContents(kit);
}
if (afterLoad != null) {
afterLoad.run();
}
SoundManager.playSuccess(player);
applyKitLoadEffects(player, isEnderChest);
return true;
}
public boolean loadKit(Player player, int slot) {
return loadKitInternal(player, IDUtil.getPlayerKitId(player.getUniqueId(), slot), "Kit " + slot + " does not exist!", false, () -> {
BroadcastManager.get().broadcastPlayerLoadedPrivateKit(player, "Kit " + slot);
player.sendMessage(ChatColor.GREEN + "Kit " + slot + " loaded!");
lastKitUsedByPlayer.put(player.getUniqueId(), slot);
});
}
public boolean loadKitSilent(Player player, int slot) {
return loadKitInternal(player, IDUtil.getPlayerKitId(player.getUniqueId(), slot), null, false, null);
}
public boolean loadPublicKit(Player player, String id) {
String kitDisplayName = publicKitList.stream()
.filter(k -> k.id.equals(id))
.map(k -> k.name)
.findFirst()
.orElse(id);
return loadKitInternal(player, IDUtil.getPublicKitId(id), "Kit does not exist!", false, () -> {
BroadcastManager.get().broadcastPlayerLoadedPublicKit(player, kitDisplayName);
player.sendMessage(ChatColor.GREEN + "Public Kit loaded!");
player.sendMessage(ChatColor.GRAY + "You can save a custom version this kit by importing into the kit editor");
});
}
public boolean loadPublicKitSilent(Player player, String id) {
return loadKitInternal(player, IDUtil.getPublicKitId(id), null, false, null);
}
public boolean loadEnderchest(Player player, int slot) {
return loadKitInternal(player, IDUtil.getECId(player.getUniqueId(), slot), "Enderchest " + slot + " does not exist!", true, () -> {
BroadcastManager.get().broadcastPlayerLoadedEnderChest(player);
player.sendMessage(ChatColor.GREEN + "Enderchest " + slot + " loaded!");
});
}
public boolean loadEnderchestSilent(Player player, int slot) {
return loadKitInternal(player, IDUtil.getECId(player.getUniqueId(), slot), null, true, null);
}
public boolean loadLastKit(Player player) {
if (lastKitUsedByPlayer.containsKey(player.getUniqueId())) {
return loadKit(player, lastKitUsedByPlayer.get(player.getUniqueId()));
}
return false;
}
public boolean hasKit(UUID uuid, int slot) {
return kitByKitIDMap.get(IDUtil.getPlayerKitId(uuid, slot)) != null;
}
public boolean hasEC(UUID uuid, int slot) {
return kitByKitIDMap.get(IDUtil.getECId(uuid, slot)) != null;
}
public ItemStack[] getPlayerEC(UUID uuid, int slot) {
return kitByKitIDMap.get(IDUtil.getECId(uuid, slot));
}
public ItemStack[] getPlayerKit(UUID uuid, int slot) {
return kitByKitIDMap.get(IDUtil.getPlayerKitId(uuid, slot));
}
public boolean hasPublicKit(String id) {
return kitByKitIDMap.get(IDUtil.getPublicKitId(id)) != null;
}
public ItemStack[] getPublicKit(String id) {
return kitByKitIDMap.get(IDUtil.getPublicKitId(id));
}
public void loadPlayerDataFromDB(UUID uuid) {
for (int slot = 1; slot < 10; slot++) {
String data = PerPlayerKit.storageManager.getKitDataByID(IDUtil.getPlayerKitId(uuid, slot));
if (!data.equalsIgnoreCase("error")) {
try {
ItemStack[] kit = Serializer.itemStackArrayFromBase64(data);
kitByKitIDMap.put(IDUtil.getPlayerKitId(uuid, slot), ItemFilter.get().filterItemStack(Serializer.itemStackArrayFromBase64(data)));
} catch (IOException ignored) {
}
}
}
for (int slot = 1; slot < 10; slot++) {
String data = PerPlayerKit.storageManager.getKitDataByID(IDUtil.getECId(uuid, slot));
if (!data.equalsIgnoreCase("error")) {
try {
ItemStack[] kit = Serializer.itemStackArrayFromBase64(data);
kitByKitIDMap.put(IDUtil.getECId(uuid, slot), ItemFilter.get().filterItemStack(Serializer.itemStackArrayFromBase64(data)));
} catch (IOException ignored) {
}
}
}
}
public void savePlayerKitsToDB(UUID uuid) {
for (int i = 1; i < 10; i++) {
saveKitToDB(IDUtil.getPlayerKitId(uuid, i), true);
saveKitToDB(IDUtil.getECId(uuid, i), true);
}
}
public void savePlayerKitToDB(UUID uuid, int slot) {
saveKitToDB(IDUtil.getPlayerKitId(uuid, slot), false);
}
public void saveEnderchestToDB(UUID uuid, int slot) {
saveKitToDB(IDUtil.getECId(uuid, slot), false);
}
public void savePublicKitToDB(String id) {
saveKitToDB(IDUtil.getPublicKitId(id), false);
}
private void saveKitToDB(String key, boolean removeAfterSave) {
if (kitByKitIDMap.get(key) != null) {
PerPlayerKit.storageManager.saveKitDataByID(key, Serializer.itemStackArrayToBase64(ItemFilter.get().filterItemStack(kitByKitIDMap.get(key))));
if (removeAfterSave) {
kitByKitIDMap.remove(key);
}
}
}
public void loadPublicKitFromDB(String id) {
String data = PerPlayerKit.storageManager.getKitDataByID(IDUtil.getPublicKitId(id));
if (!data.equalsIgnoreCase("error")) {
try {
ItemStack[] kit = Serializer.itemStackArrayFromBase64(data);
kitByKitIDMap.put(IDUtil.getPublicKitId(id), ItemFilter.get().filterItemStack(kit));
} catch (IOException ignored) {
plugin.getLogger().info("Error loading public kit " + id);
}
}
}
public boolean deleteKit(UUID uuid, int slot) {
if (hasKit(uuid, slot)) {
kitByKitIDMap.remove(IDUtil.getPlayerKitId(uuid, slot));
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> PerPlayerKit.storageManager.deleteKitByID(IDUtil.getPlayerKitId(uuid, slot)));
return true;
}
return false;
}
public boolean deleteEnderchest(UUID uuid, int slot) {
if (hasEC(uuid, slot)) {
kitByKitIDMap.remove(IDUtil.getECId(uuid, slot));
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> PerPlayerKit.storageManager.deleteKitByID(IDUtil.getECId(uuid, slot)));
return true;
}
return false;
}
private void applyKitLoadEffects(Player player, boolean isEnderChest) {
if (player.isDead()) {
return;
}
if (isEnderChest) {
if (plugin.getConfig().getBoolean("feature.heal-on-enderchest-load", false)) {
player.setHealth(20);
}
if (plugin.getConfig().getBoolean("feature.feed-on-enderchest-load", false)) {
player.setFoodLevel(20);
}
if (plugin.getConfig().getBoolean("feature.set-saturation-on-enderchest-load", false)) {
player.setSaturation(20);
}
if (plugin.getConfig().getBoolean("feature.remove-potion-effects-on-enderchest-load", false)) {
player.getActivePotionEffects().forEach(potionEffect -> player.removePotionEffect(potionEffect.getType()));
}
} else {
if (plugin.getConfig().getBoolean("feature.set-health-on-kit-load", false)) {
player.setHealth(20);
}
if (plugin.getConfig().getBoolean("feature.set-hunger-on-kit-load", false)) {
player.setFoodLevel(20);
}
if (plugin.getConfig().getBoolean("feature.set-saturation-on-kit-load", false)) {
player.setSaturation(20);
}
if (plugin.getConfig().getBoolean("feature.remove-potion-effects-on-kit-load", false)) {
player.getActivePotionEffects().forEach(potionEffect -> player.removePotionEffect(potionEffect.getType()));
}
}
}
}