/
ItemUtils.java
713 lines (616 loc) · 22.9 KB
/
ItemUtils.java
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package com.gmail.nossr50.util;
import com.gmail.nossr50.config.experience.ExperienceConfig;
import com.gmail.nossr50.config.party.ItemWeightConfig;
import com.gmail.nossr50.datatypes.treasure.EnchantmentWrapper;
import com.gmail.nossr50.datatypes.treasure.FishingTreasureBook;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.skills.smelting.Smelting;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
public final class ItemUtils {
/**
* This is a static utility class, therefore we don't want any instances of
* this class. Making the constructor private prevents accidents like that.
*/
private ItemUtils() {}
/**
* Checks if the item is a bow.
*
* @param item Item to check
* @return true if the item is a bow, false otherwise
*/
// TODO: Unit tests
public static boolean isBow(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isBow(item.getType().getKey().getKey());
}
/**
* Checks if a player has an item in their inventory or offhand.
*
* @param player Player to check
* @param material Material to check for
* @return true if the player has the item in their inventory or offhand, false otherwise
*/
public static boolean hasItemIncludingOffHand(Player player, Material material) {
// Checks main inventory / item bar
boolean containsInMain = player.getInventory().contains(material);
if (containsInMain) {
return true;
}
return player.getInventory().getItemInOffHand().getType() == material;
}
/**
* Removes an item from a player's inventory, including their offhand.
*
* @param player Player to remove the item from
* @param material Material to remove
* @param amount Amount of the material to remove
*/
public static void removeItemIncludingOffHand(@NotNull Player player, @NotNull Material material, int amount) {
// Checks main inventory / item bar
if (player.getInventory().contains(material)) {
player.getInventory().removeItem(new ItemStack(material, amount));
return;
}
// Check off-hand
final ItemStack offHandItem = player.getInventory().getItemInOffHand();
if (offHandItem.getType() == material) {
int newAmount = offHandItem.getAmount() - amount;
if (newAmount > 0) {
offHandItem.setAmount(newAmount);
} else {
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
}
}
}
// TODO: Unit tests
public static boolean isCrossbow(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isCrossbow(item.getType().getKey().getKey());
}
// TODO: Unit tests
public static boolean isBowOrCrossbow(@NotNull ItemStack item) {
return isBow(item) || isCrossbow(item);
}
// TODO: Unit tests
public static boolean isTrident(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isTrident(item.getType().getKey().getKey());
}
public static boolean hasItemInEitherHand(@NotNull Player player, Material material) {
return player.getInventory().getItemInMainHand().getType() == material || player.getInventory().getItemInOffHand().getType() == material;
}
public static boolean doesPlayerHaveEnchantmentOnArmor(@NotNull Player player, @NotNull String enchantmentByName) {
Enchantment enchantment = getEnchantment(enchantmentByName);
if(enchantment == null)
return false;
return doesPlayerHaveEnchantmentOnArmor(player, enchantment);
}
public static boolean doesPlayerHaveEnchantmentOnArmor(@NotNull Player player, @NotNull Enchantment enchantment) {
for(ItemStack itemStack : player.getInventory().getArmorContents()) {
if(itemStack != null) {
if(hasEnchantment(itemStack, enchantment))
return true;
}
}
return false;
}
public static boolean doesPlayerHaveEnchantmentOnArmorOrHands(@NotNull Player player, @NotNull String enchantmentName) {
Enchantment enchantment = getEnchantment(enchantmentName);
if(enchantment == null)
return false;
return doesPlayerHaveEnchantmentOnArmorOrHands(player, enchantment);
}
public static boolean doesPlayerHaveEnchantmentOnArmorOrHands(@NotNull Player player, @NotNull Enchantment enchantment) {
if(doesPlayerHaveEnchantmentOnArmor(player, enchantment))
return true;
if(doesPlayerHaveEnchantmentInHands(player, enchantment))
return true;
return false;
}
public static boolean doesPlayerHaveEnchantmentInHands(@NotNull Player player, @NotNull NamespacedKey enchantmentNameKey) {
Enchantment enchantment = Enchantment.getByKey(enchantmentNameKey);
if(enchantment == null)
return false;
return doesPlayerHaveEnchantmentInHands(player, enchantment);
}
public static boolean doesPlayerHaveEnchantmentInHands(@NotNull Player player, @NotNull String enchantmentName) {
Enchantment enchantment = getEnchantment(enchantmentName);
if(enchantment == null)
return false;
return doesPlayerHaveEnchantmentInHands(player, enchantment);
}
public static boolean doesPlayerHaveEnchantmentInHands(@NotNull Player player, @NotNull Enchantment enchantment) {
return hasEnchantment(player.getInventory().getItemInMainHand(), enchantment) ||
hasEnchantment(player.getInventory().getItemInOffHand(), enchantment);
}
public static boolean hasEnchantment(@NotNull ItemStack itemStack, @NotNull Enchantment enchantment) {
if(itemStack.getItemMeta() != null) {
return itemStack.getItemMeta().hasEnchant(enchantment);
}
return false;
}
public static @Nullable Enchantment getEnchantment(@NotNull String enchantmentName) {
for(Enchantment enchantment : Enchantment.values()) {
if(enchantment.getKey().getKey().equalsIgnoreCase(enchantmentName)) {
return enchantment;
}
}
return null;
}
/**
* Checks if the item is a sword.
*
* @param item Item to check
* @return true if the item is a sword, false otherwise
*/
public static boolean isSword(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isSword(item.getType().getKey().getKey());
}
/**
* Checks if the item is a hoe.
*
* @param item Item to check
* @return true if the item is a hoe, false otherwise
*/
public static boolean isHoe(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isHoe(item.getType().getKey().getKey());
}
/**
* Checks if the item is a shovel.
*
* @param item Item to check
* @return true if the item is a shovel, false otherwise
*/
public static boolean isShovel(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isShovel(item.getType().getKey().getKey());
}
/**
* Checks if the item is an axe.
*
* @param item Item to check
* @return true if the item is an axe, false otherwise
*/
public static boolean isAxe(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isAxe(item.getType().getKey().getKey());
}
/**
* Checks if the item is a pickaxe.
*
* @param item Item to check
* @return true if the item is a pickaxe, false otherwise
*/
public static boolean isPickaxe(@NotNull ItemStack item) {
return mcMMO.getMaterialMapStore().isPickAxe(item.getType().getKey().getKey());
}
/**
* Checks if the item counts as unarmed.
*
* @param item Item to check
* @return true if the item counts as unarmed, false otherwise
*/
public static boolean isUnarmed(ItemStack item) {
if (mcMMO.p.getGeneralConfig().getUnarmedItemsAsUnarmed()) {
return !isMinecraftTool(item);
}
return item.getType() == Material.AIR;
}
/**
* Checks to see if an item is a wearable armor piece.
*
* @param item Item to check
* @return true if the item is armor, false otherwise
*/
public static boolean isArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isArmor(item.getType());
}
/**
* Checks to see if an item is a leather armor piece.
*
* @param item Item to check
* @return true if the item is leather armor, false otherwise
*/
public static boolean isLeatherArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isLeatherArmor(item.getType());
}
/**
* Checks to see if an item is a gold armor piece.
*
* @param item Item to check
* @return true if the item is gold armor, false otherwise
*/
public static boolean isGoldArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isGoldArmor(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is an iron armor piece.
*
* @param item Item to check
* @return true if the item is iron armor, false otherwise
*/
public static boolean isIronArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isIronArmor(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a diamond armor piece.
*
* @param item Item to check
* @return true if the item is diamond armor, false otherwise
*/
public static boolean isDiamondArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isDiamondArmor(item.getType().getKey().getKey());
}
public static boolean isNetheriteArmor(ItemStack itemStack) {
return mcMMO.getMaterialMapStore().isNetheriteArmor(itemStack.getType().getKey().getKey());
}
public static boolean isNetheriteTool(ItemStack itemStack) {
return mcMMO.getMaterialMapStore().isNetheriteTool(itemStack.getType().getKey().getKey());
}
/**
* Checks to see if an item is a chainmail armor piece.
*
* @param item Item to check
* @return true if the item is chainmail armor, false otherwise
*/
public static boolean isChainmailArmor(ItemStack item) {
return mcMMO.getMaterialMapStore().isChainmailArmor(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a *vanilla* tool.
*
* @param item Item to check
* @return true if the item is a tool, false otherwise
*/
public static boolean isMinecraftTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a stone tool.
*
* @param item Item to check
* @return true if the item is a stone tool, false otherwise
*/
public static boolean isStoneTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isStoneTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a wooden tool.
*
* @param item Item to check
* @return true if the item is a wooden tool, false otherwise
*/
public static boolean isWoodTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isWoodTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a string tool.
*
* @param item Item to check
* @return true if the item is a string tool, false otherwise
*/
public static boolean isStringTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isStringTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a gold tool.
*
* @param item Item to check
* @return true if the item is a stone tool, false otherwise
*/
public static boolean isGoldTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isGoldTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is an iron tool.
*
* @param item Item to check
* @return true if the item is an iron tool, false otherwise
*/
public static boolean isIronTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isIronTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is a diamond tool.
*
* @param item Item to check
* @return true if the item is a diamond tool, false otherwise
*/
public static boolean isDiamondTool(ItemStack item) {
return mcMMO.getMaterialMapStore().isDiamondTool(item.getType().getKey().getKey());
}
/**
* Checks to see if an item is enchantable.
*
* @param item Item to check
* @return true if the item is enchantable, false otherwise
*/
public static boolean isEnchantable(ItemStack item) {
return mcMMO.getMaterialMapStore().isEnchantable(item.getType().getKey().getKey());
}
public static boolean isSmeltable(ItemStack item) {
return item != null && Smelting.getSmeltXP(item) >= 1;
}
public static boolean isSmelted(ItemStack item) {
if (item == null) {
return false;
}
for (Recipe recipe : mcMMO.p.getServer().getRecipesFor(item)) {
if (recipe instanceof FurnaceRecipe
&& ((FurnaceRecipe) recipe).getInput().getType().isBlock()
&& MaterialUtils.isOre(((FurnaceRecipe) recipe).getInput().getType())) {
return true;
}
}
return false;
}
/**
* Check if an item is sharable.
*
* @param item Item that will get shared
* @return True if the item can be shared.
*/
public static boolean isSharable(ItemStack item) {
if (item == null || item.getType() == Material.AIR) {
return false;
}
return isMiningDrop(item) || isWoodcuttingDrop(item) || isMobDrop(item) || isHerbalismDrop(item) || isMiscDrop(item);
}
/**
* Checks to see if an item is a mining drop.
*
* @param item Item to check
* @return true if the item is a mining drop, false otherwise
*/
public static boolean isMiningDrop(ItemStack item) {
//TODO: 1.14 This needs to be updated
switch (item.getType()) {
case COAL:
case COAL_ORE:
case DIAMOND:
case DIAMOND_ORE:
case EMERALD:
case EMERALD_ORE:
case GOLD_ORE:
case IRON_ORE:
case LAPIS_ORE:
case REDSTONE_ORE: // Should we also have Glowing Redstone Ore here?
case REDSTONE:
case GLOWSTONE_DUST: // Should we also have Glowstone here?
case QUARTZ:
case NETHER_QUARTZ_ORE:
case LAPIS_LAZULI:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a herbalism drop.
*
* @param item Item to check
* @return true if the item is a herbalism drop, false otherwise
*/
public static boolean isHerbalismDrop(ItemStack item) {
//TODO: 1.14 This needs to be updated
switch (item.getType().getKey().getKey().toLowerCase()) {
case "wheat":
case "wheat_seeds":
case "carrot":
case "chorus_fruit":
case "chorus_flower":
case "potato":
case "beetroot":
case "beetroots":
case "beetroot_seeds":
case "nether_wart":
case "brown_mushroom":
case "red_mushroom":
case "rose_bush":
case "dandelion":
case "cactus":
case "sugar_cane":
case "melon":
case "melon_seeds":
case "pumpkin":
case "pumpkin_seeds":
case "lily_pad":
case "vine":
case "tall_grass":
case "cocoa_beans":
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a mob drop.
*
* @param item Item to check
* @return true if the item is a mob drop, false otherwise
*/
public static boolean isMobDrop(ItemStack item) {
//TODO: 1.14 This needs to be updated
switch (item.getType()) {
case STRING:
case FEATHER:
case CHICKEN:
case COOKED_CHICKEN:
case LEATHER:
case BEEF:
case COOKED_BEEF:
case PORKCHOP:
case COOKED_PORKCHOP:
case WHITE_WOOL:
case BLACK_WOOL:
case BLUE_WOOL:
case BROWN_WOOL:
case CYAN_WOOL:
case GRAY_WOOL:
case GREEN_WOOL:
case LIGHT_BLUE_WOOL:
case LIGHT_GRAY_WOOL:
case LIME_WOOL:
case MAGENTA_WOOL:
case ORANGE_WOOL:
case PINK_WOOL:
case PURPLE_WOOL:
case RED_WOOL:
case YELLOW_WOOL:
case IRON_INGOT:
case SNOWBALL:
case BLAZE_ROD:
case SPIDER_EYE:
case GUNPOWDER:
case ENDER_PEARL:
case GHAST_TEAR:
case MAGMA_CREAM:
case BONE:
case ARROW:
case SLIME_BALL:
case NETHER_STAR:
case ROTTEN_FLESH:
case GOLD_NUGGET:
case EGG:
case ROSE_BUSH:
case COAL:
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a woodcutting drop.
*
* @param item Item to check
* @return true if the item is a woodcutting drop, false otherwise
*/
public static boolean isWoodcuttingDrop(ItemStack item) {
switch (item.getType().toString()) {
case "ACACIA_LOG":
case "BIRCH_LOG":
case "DARK_OAK_LOG":
case "JUNGLE_LOG":
case "OAK_LOG":
case "SPRUCE_LOG":
case "STRIPPED_ACACIA_LOG":
case "STRIPPED_BIRCH_LOG":
case "STRIPPED_DARK_OAK_LOG":
case "STRIPPED_JUNGLE_LOG":
case "STRIPPED_OAK_LOG":
case "STRIPPED_SPRUCE_LOG":
case "STRIPPED_MANGROVE_LOG":
case "ACACIA_SAPLING":
case "SPRUCE_SAPLING":
case "BIRCH_SAPLING":
case "DARK_OAK_SAPLING":
case "JUNGLE_SAPLING":
case "OAK_SAPLING":
case "ACACIA_LEAVES":
case "BIRCH_LEAVES":
case "DARK_OAK_LEAVES":
case "JUNGLE_LEAVES":
case "OAK_LEAVES":
case "SPRUCE_LEAVES":
case "BEE_NEST":
case "APPLE":
return true;
default:
return false;
}
}
/**
* Checks to see if an item is a miscellaneous drop. These items are read from the config file
*
* @param item Item to check
* @return true if the item is a miscellaneous drop, false otherwise
*/
public static boolean isMiscDrop(ItemStack item) {
return ItemWeightConfig.getInstance().getMiscItems().contains(item.getType());
}
public static boolean isMcMMOItem(ItemStack item) {
if (!item.hasItemMeta()) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
if(itemMeta == null)
return false;
return itemMeta.getLore() != null
&& itemMeta.getLore().contains("mcMMO Item");
}
public static boolean isChimaeraWing(ItemStack item) {
if (!isMcMMOItem(item)) {
return false;
}
ItemMeta itemMeta = item.getItemMeta();
if(itemMeta == null)
return false;
return itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals(ChatColor.GOLD + LocaleLoader.getString("Item.ChimaeraWing.Name"));
}
// public static void addAbilityLore(@NotNull ItemStack itemStack) {
// ItemMeta itemMeta = itemStack.getItemMeta();
// List<String> itemLore = new ArrayList<>();
//
// if(itemMeta == null)
// return;
//
// if (itemMeta.hasLore()) {
// itemLore = itemMeta.getLore();
// }
//
// itemLore.add("mcMMO Ability Tool");
//
// itemMeta.setLore(itemLore);
// itemStack.setItemMeta(itemMeta);
// }
public static void removeAbilityLore(@NotNull ItemStack itemStack) {
ItemMeta itemMeta = itemStack.getItemMeta();
if(itemMeta == null)
return;
if (itemMeta.hasLore()) {
List<String> itemLore = itemMeta.getLore();
if(itemLore == null)
return;
if (itemLore.remove("mcMMO Ability Tool")) {
itemMeta.setLore(itemLore);
itemStack.setItemMeta(itemMeta);
}
}
}
public static void addDigSpeedToItem(@NotNull ItemStack itemStack, int existingEnchantLevel) {
ItemMeta itemMeta = itemStack.getItemMeta();
if(itemMeta == null)
return;
itemMeta.addEnchant(Enchantment.DIG_SPEED, existingEnchantLevel + mcMMO.p.getAdvancedConfig().getEnchantBuff(), true);
itemStack.setItemMeta(itemMeta);
}
public static boolean canBeSuperAbilityDigBoosted(@NotNull ItemStack itemStack) {
return isShovel(itemStack) || isPickaxe(itemStack);
}
public static @NotNull ItemStack createEnchantBook(@NotNull FishingTreasureBook fishingTreasureBook) {
ItemStack itemStack = fishingTreasureBook.getDrop().clone();
EnchantmentWrapper enchantmentWrapper = getRandomEnchantment(fishingTreasureBook.getLegalEnchantments());
ItemMeta itemMeta = itemStack.getItemMeta();
if(itemMeta == null) {
return itemStack;
}
EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta;
enchantmentStorageMeta.addStoredEnchant(enchantmentWrapper.getEnchantment(), enchantmentWrapper.getEnchantmentLevel(), ExperienceConfig.getInstance().allowUnsafeEnchantments());
itemStack.setItemMeta(enchantmentStorageMeta);
return itemStack;
}
public static @NotNull EnchantmentWrapper getRandomEnchantment(@NotNull List<EnchantmentWrapper> enchantmentWrappers) {
Collections.shuffle(enchantmentWrappers, Misc.getRandom());
int randomIndex = Misc.getRandom().nextInt(enchantmentWrappers.size());
return enchantmentWrappers.get(randomIndex);
}
}