/
ItemUtil.java
1088 lines (947 loc) · 38.5 KB
/
ItemUtil.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.sk89q.craftbook.util;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import org.bukkit.ChatColor;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.TreeSpecies;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
public final class ItemUtil {
/**
* Add an itemstack to an existing itemstack.
*
* @param base The itemstack to be added to.
* @param toAdd The itemstack to add to the base.
* @return The unaddable items.
*/
public static ItemStack addToStack(ItemStack base, ItemStack toAdd) {
if (!areItemsIdentical(base, toAdd)) return toAdd;
if (base.getAmount() + toAdd.getAmount() > base.getMaxStackSize()) {
toAdd.setAmount(base.getAmount() + toAdd.getAmount() - base.getMaxStackSize());
base.setAmount(base.getMaxStackSize());
return toAdd;
} else {
base.setAmount(base.getAmount() + toAdd.getAmount());
return null;
}
}
/**
* Filter a list of items by inclusions and exclusions.
*
* @param stacks The base list of items.
* @param inclusions The list of items to include, skipped if empty.
* @param exclusions The list of items to exclude, skipped if empty.
* @return The list of items that have been filtered.
*/
public static List<ItemStack> filterItems(List<ItemStack> stacks, HashSet<ItemStack> inclusions, HashSet<ItemStack> exclusions) {
List<ItemStack> ret = new ArrayList<>();
for(ItemStack stack : stacks) {
if(doesItemPassFilters(stack, inclusions, exclusions))
ret.add(stack);
}
return ret;
}
/**
* Check whether or not an item passes filters.
*
* @param stack The item to check if it passes.
* @param inclusions The list of items to include, skipped if empty.
* @param exclusions The list of items to exclude, skipped if empty.
* @return If the item passes the filters.
*/
public static boolean doesItemPassFilters(ItemStack stack, Set<ItemStack> inclusions, Set<ItemStack> exclusions) {
boolean passesFilters = true;
if(inclusions != null && inclusions.size() > 0) {
for (ItemStack fil : inclusions) {
if(!ItemUtil.isStackValid(fil))
continue;
if(ItemUtil.areItemsIdentical(fil, stack)) {
passesFilters = true;
break;
} else
passesFilters = false;
}
if(!passesFilters)
return false;
}
if(exclusions != null && exclusions.size() > 0) {
for (ItemStack fil : exclusions) {
if(!ItemUtil.isStackValid(fil))
continue;
if(ItemUtil.areItemsIdentical(fil, stack)) {
passesFilters = false;
break;
}
}
if(!passesFilters)
return false;
}
return true;
}
public static boolean areItemsSimilar(ItemStack item, Material type) {
return areItemsSimilar(item.getType(), type);
}
public static boolean areItemsSimilar(ItemStack item, ItemStack item2) {
return areItemsSimilar(item.getType(), item2.getType());
}
public static boolean areItemsSimilar(Material data, Material comparedData) {
return data == comparedData;
}
private static final Pattern STRIP_RESET_PATTERN = Pattern.compile("(?i)" + '\u00A7' + "[Rr]");
//TODO Move to a StringUtil.
public static String stripResetChar(String message) {
if (message == null)
return null;
return STRIP_RESET_PATTERN.matcher(message).replaceAll("");
}
public static boolean areRecipesIdentical(Recipe rec1, Recipe rec2) {
if(rec1 == null || rec2 == null)
return rec1 == rec2;
if(ItemUtil.areItemsIdentical(rec1.getResult(), rec2.getResult())) {
CraftBookPlugin.logDebugMessage("Recipes have same results!", "advanced-data.compare-recipes");
if(rec1 instanceof ShapedRecipe && rec2 instanceof ShapedRecipe) {
CraftBookPlugin.logDebugMessage("Shaped recipe!", "advanced-data.compare-recipes.shaped");
ShapedRecipe recipe1 = (ShapedRecipe) rec1;
ShapedRecipe recipe2 = (ShapedRecipe) rec2;
if(recipe1.getShape().length == recipe2.getShape().length) {
CraftBookPlugin.logDebugMessage("Same size!", "advanced-data.compare-recipes.shaped");
List<ItemStack> stacks1 = new ArrayList<>();
for(String s : recipe1.getShape())
for(char c : s.toCharArray())
for(Entry<Character, ItemStack> entry : recipe1.getIngredientMap().entrySet())
if(entry.getKey() == c)
stacks1.add(entry.getValue());
List<ItemStack> stacks2 = new ArrayList<>();
for(String s : recipe2.getShape())
for(char c : s.toCharArray())
for(Entry<Character, ItemStack> entry : recipe2.getIngredientMap().entrySet())
if(entry.getKey() == c)
stacks2.add(entry.getValue());
if(stacks2.size() != stacks1.size()) {
CraftBookPlugin.logDebugMessage("Recipes have different amounts of ingredients!", "advanced-data.compare-recipes.shaped");
return false;
}
List<ItemStack> test = new ArrayList<>(stacks1);
if(test.size() == 0) {
CraftBookPlugin.logDebugMessage("Recipes are the same!", "advanced-data.compare-recipes.shaped");
return true;
}
if(!test.removeAll(stacks2) && test.size() > 0) {
CraftBookPlugin.logDebugMessage("Recipes are NOT the same!", "advanced-data.compare-recipes.shaped");
return false;
}
if(test.size() > 0) {
CraftBookPlugin.logDebugMessage("Recipes are NOT the same!", "advanced-data.compare-recipes.shaped");
return false;
}
}
} else if(rec1 instanceof ShapelessRecipe && rec2 instanceof ShapelessRecipe) {
CraftBookPlugin.logDebugMessage("Shapeless Recipe!", "advanced-data.compare-recipes.shapeless");
ShapelessRecipe recipe1 = (ShapelessRecipe) rec1;
ShapelessRecipe recipe2 = (ShapelessRecipe) rec2;
if(VerifyUtil.withoutNulls(recipe1.getIngredientList()).size() != VerifyUtil.withoutNulls(recipe2.getIngredientList()).size()) {
CraftBookPlugin.logDebugMessage("Recipes have different amounts of ingredients!", "advanced-data.compare-recipes.shapeless");
return false;
}
CraftBookPlugin.logDebugMessage("Same Size!", "advanced-data.compare-recipes.shapeless");
List<ItemStack> test = new ArrayList<>(VerifyUtil.withoutNulls(recipe1.getIngredientList()));
if(test.size() == 0) {
CraftBookPlugin.logDebugMessage("Recipes are the same!", "advanced-data.compare-recipes.shapeless");
return true;
}
if(!test.removeAll(VerifyUtil.withoutNulls(recipe2.getIngredientList())) && test.size() > 0) {
CraftBookPlugin.logDebugMessage("Recipes are NOT the same!", "advanced-data.compare-recipes.shapeless");
return false;
}
if(test.size() > 0) {
CraftBookPlugin.logDebugMessage("Recipes are NOT the same!", "advanced-data.compare-recipes.shapeless");
return false;
}
}
CraftBookPlugin.logDebugMessage("Recipes are the same!", "advanced-data.compare-recipes");
return true;
}
return false;
}
public static boolean isValidItemMeta(ItemMeta meta) {
if(meta.hasDisplayName())
if(!meta.getDisplayName().equals("$IGNORE"))
return true;
if(meta.hasLore())
for(String lore : meta.getLore())
if(!lore.equals("$IGNORE"))
return true;
return meta.hasEnchants();
}
public static boolean areItemMetaIdentical(ItemMeta meta, ItemMeta meta2) {
return areItemMetaIdentical(meta, meta2, true);
}
public static boolean areItemMetaIdentical(ItemMeta meta, ItemMeta meta2, boolean checkEnchants) {
//Display Names
String displayName1;
if(meta.hasDisplayName())
displayName1 = ChatColor.translateAlternateColorCodes('&', stripResetChar(meta.getDisplayName().trim()));
else
displayName1 = "$IGNORE";
String displayName2;
if(meta2.hasDisplayName())
displayName2 = ChatColor.translateAlternateColorCodes('&', stripResetChar(meta2.getDisplayName().trim()));
else
displayName2 = "";
if(!displayName1.equals(displayName2)) {
if(!displayName1.equals("$IGNORE") && !displayName2.equals("$IGNORE"))
return false;
}
CraftBookPlugin.logDebugMessage("Display names are the same", "item-checks.meta.names");
//Lore
List<String> lore1 = new ArrayList<>();
if(meta.hasLore())
for(String lore : meta.getLore())
lore1.add(ChatColor.translateAlternateColorCodes('&', stripResetChar(lore.trim())));
List<String> lore2 = new ArrayList<>();
if(meta2.hasLore())
for(String lore : meta2.getLore())
lore2.add(ChatColor.translateAlternateColorCodes('&', stripResetChar(lore.trim())));
if(lore1.size() != lore2.size())
return false;
CraftBookPlugin.logDebugMessage("Has same lore lengths", "item-checks.meta.lores");
for(int i = 0; i < lore1.size(); i++) {
if(lore1.get(i).contains("$IGNORE") || lore2.get(i).contains("$IGNORE")) continue; //Ignore this line.
if(!lore1.get(i).equals(lore2.get(i)))
return false;
}
CraftBookPlugin.logDebugMessage("Lore is the same", "item-checks.meta.lores");
if(checkEnchants) {
//Enchants
List<Enchantment> ench1 = new ArrayList<>();
if(meta.hasEnchants())
ench1.addAll(meta.getEnchants().keySet());
List<Enchantment> ench2 = new ArrayList<>();
if(meta2.hasEnchants())
ench2.addAll(meta2.getEnchants().keySet());
if(ench1.size() != ench2.size())
return false;
CraftBookPlugin.logDebugMessage("Has same enchantment lengths", "item-checks.meta.enchants");
for(Enchantment ench : ench1) {
if(!ench2.contains(ench))
return false;
if(meta.getEnchantLevel(ench) != meta2.getEnchantLevel(ench))
return false;
}
CraftBookPlugin.logDebugMessage("Enchants are the same", "item-checks.meta.enchants");
//StoredEnchants
if (meta instanceof EnchantmentStorageMeta) {
if (!(meta2 instanceof EnchantmentStorageMeta))
return false; // meta type mismatch
EnchantmentStorageMeta storageMeta = (EnchantmentStorageMeta) meta;
List<Enchantment> storedEnchantments = new ArrayList<>();
if (storageMeta.hasStoredEnchants())
storedEnchantments.addAll(storageMeta.getStoredEnchants().keySet());
EnchantmentStorageMeta storageMeta2 = (EnchantmentStorageMeta) meta2;
List<Enchantment> storedEnchantments2 = new ArrayList<>();
if (storageMeta2.hasStoredEnchants())
storedEnchantments2.addAll(storageMeta2.getStoredEnchants().keySet());
if (storedEnchantments.size() != storedEnchantments2.size())
return false; // mismatch enchantment counts
CraftBookPlugin.logDebugMessage("Has same stored enchantment lengths", "item-checks.meta.enchants");
for (Enchantment ench : storedEnchantments) {
if (!storedEnchantments2.contains(ench))
return false; // mismatch stored enchantments
if (storageMeta.getStoredEnchantLevel(ench) != storageMeta2.getStoredEnchantLevel(ench))
return false; // mismatch stored enchantment levels
}
CraftBookPlugin.logDebugMessage("Stored enchants are the same", "item-checks.meta.enchants");
} else if (meta2 instanceof EnchantmentStorageMeta)
return false; // meta type mismatch
}
if (meta instanceof BookMeta) {
if (!(meta2 instanceof BookMeta))
return false;
BookMeta bookMeta = (BookMeta) meta;
BookMeta bookMeta2 = (BookMeta) meta2;
if (bookMeta.hasAuthor() != bookMeta2.hasAuthor())
return false;
if (bookMeta.hasAuthor() && !bookMeta.getAuthor().equals(bookMeta2.getAuthor()))
return false;
if (bookMeta.hasTitle() != bookMeta2.hasTitle())
return false;
if (bookMeta.hasTitle() && !bookMeta.getTitle().equals(bookMeta2.getTitle()))
return false;
if (bookMeta.hasPages() != bookMeta2.hasPages())
return false;
if (bookMeta.hasPages()) {
if (bookMeta.getPageCount() != bookMeta2.getPageCount())
return false;
for (int i = 1; i <= bookMeta.getPageCount(); i++) {
if (!bookMeta.getPage(i).equals(bookMeta2.getPage(i)))
return false;
}
}
} else if (meta2 instanceof BookMeta)
return false;
return true;
}
public static boolean areItemsIdentical(ItemStack item, ItemStack item2) {
if(!isStackValid(item) || !isStackValid(item2)) {
CraftBookPlugin.logDebugMessage("An invalid item was compared. Was first? " + !isStackValid(item), "item-checks");
return !isStackValid(item) && !isStackValid(item2);
}
else {
if(!areBaseItemsIdentical(item,item2))
return false;
CraftBookPlugin.logDebugMessage("The items are basically identical", "item-checks");
if(item.hasItemMeta() != item2.hasItemMeta()) {
if(item.hasItemMeta() && isValidItemMeta(item.getItemMeta())) return false;
else if(item2.hasItemMeta() && isValidItemMeta(item2.getItemMeta())) return false;
}
CraftBookPlugin.logDebugMessage("Both share the existance of metadata", "item-checks");
if(item.hasItemMeta()) {
CraftBookPlugin.logDebugMessage("Both have metadata", "item-checks.meta");
if(!areItemMetaIdentical(item.getItemMeta(), item2.getItemMeta())) {
CraftBookPlugin.logDebugMessage("Metadata is different", "item-checks.meta");
return false;
}
}
CraftBookPlugin.logDebugMessage("Items are identical", "item-checks");
return true;
}
}
public static boolean areBaseItemsIdentical(ItemStack item, ItemStack item2) {
if(!isStackValid(item) || !isStackValid(item2))
return !isStackValid(item) && !isStackValid(item2);
else {
return item.getType() == item2.getType();
}
}
public static boolean isStackValid(ItemStack item) {
if (item == null) {
CraftBookPlugin.logDebugMessage("item-checks", "Item is null.");
return false;
} else if (item.getAmount() <= 0) {
CraftBookPlugin.logDebugMessage("item-checks", "Item has amount of " + item.getAmount());
return false;
}
return true;
}
public static boolean hasDisplayNameOrLore(ItemStack item) {
if(item.hasItemMeta()) {
ItemMeta meta = item.getItemMeta();
return meta.hasDisplayName() || meta.hasLore();
}
return false;
}
/**
* Removes a specified amount from an item entity.
*
* @param item
* @return true if success, otherwise false.
*/
public static boolean takeFromItemEntity(Item item, int amount) {
if (item == null || item.isDead()) return false;
ItemStack newStack = item.getItemStack();
if (!isStackValid(newStack)) {
item.remove();
return false;
}
if(newStack.getAmount() < amount)
return false;
newStack.setAmount(newStack.getAmount() - amount);
if (!isStackValid(newStack))
item.remove();
else
item.setItemStack(newStack);
return true;
}
public static boolean isCookable(ItemStack item) {
return getCookedResult(item) != null;
}
public static ItemStack getCookedResult(ItemStack item) {
switch (item.getType()) {
case BEEF:
return new ItemStack(Material.COOKED_BEEF);
case CHICKEN:
return new ItemStack(Material.COOKED_CHICKEN);
case COD:
return new ItemStack(Material.COOKED_COD);
case SALMON:
return new ItemStack(Material.COOKED_SALMON);
case PORKCHOP:
return new ItemStack(Material.COOKED_PORKCHOP);
case POTATO:
return new ItemStack(Material.BAKED_POTATO);
case MUTTON:
return new ItemStack(Material.COOKED_MUTTON);
case RABBIT:
return new ItemStack(Material.COOKED_RABBIT);
case CHORUS_FRUIT:
return new ItemStack(Material.POPPED_CHORUS_FRUIT);
default:
return null;
}
}
public static boolean isSmeltable(ItemStack item) {
return getSmeletedResult(item) != null;
}
public static ItemStack getSmeletedResult(ItemStack item) {
switch (item.getType()) {
case COBBLESTONE:
return new ItemStack(Material.STONE);
case CACTUS:
return new ItemStack(Material.CACTUS_GREEN);
case IRON_ORE:
return new ItemStack(Material.IRON_INGOT);
case COAL_ORE:
return new ItemStack(Material.COAL);
case LAPIS_ORE:
return new ItemStack(Material.LAPIS_LAZULI);
case REDSTONE_ORE:
return new ItemStack(Material.REDSTONE, 4);
case EMERALD_ORE:
return new ItemStack(Material.EMERALD);
case GOLD_ORE:
return new ItemStack(Material.GOLD_INGOT);
case DIAMOND_ORE:
return new ItemStack(Material.DIAMOND);
case SAND:
return new ItemStack(Material.GLASS);
case CLAY_BALL:
return new ItemStack(Material.BRICK);
case NETHERRACK:
return new ItemStack(Material.NETHER_BRICK);
case CLAY:
return new ItemStack(Material.TERRACOTTA);
case NETHER_QUARTZ_ORE:
return new ItemStack(Material.QUARTZ);
case STONE_BRICKS:
return new ItemStack(Material.CRACKED_STONE_BRICKS);
case SPONGE:
if (item.getData().getData() == 0)
return null;
else return new ItemStack(Material.SPONGE);
case WHITE_TERRACOTTA:
return new ItemStack(Material.WHITE_GLAZED_TERRACOTTA);
case ORANGE_TERRACOTTA:
return new ItemStack(Material.ORANGE_GLAZED_TERRACOTTA);
case MAGENTA_TERRACOTTA:
return new ItemStack(Material.MAGENTA_GLAZED_TERRACOTTA);
case LIGHT_BLUE_TERRACOTTA:
return new ItemStack(Material.LIGHT_BLUE_GLAZED_TERRACOTTA);
case YELLOW_TERRACOTTA:
return new ItemStack(Material.YELLOW_GLAZED_TERRACOTTA);
case LIME_TERRACOTTA:
return new ItemStack(Material.LIME_GLAZED_TERRACOTTA);
case PINK_TERRACOTTA:
return new ItemStack(Material.PINK_GLAZED_TERRACOTTA);
case GRAY_TERRACOTTA:
return new ItemStack(Material.GRAY_GLAZED_TERRACOTTA);
case LIGHT_GRAY_TERRACOTTA:
return new ItemStack(Material.LIGHT_GRAY_GLAZED_TERRACOTTA);
case CYAN_TERRACOTTA:
return new ItemStack(Material.CYAN_GLAZED_TERRACOTTA);
case PURPLE_TERRACOTTA:
return new ItemStack(Material.PURPLE_GLAZED_TERRACOTTA);
case BLUE_TERRACOTTA:
return new ItemStack(Material.BLUE_GLAZED_TERRACOTTA);
case BROWN_TERRACOTTA:
return new ItemStack(Material.BROWN_GLAZED_TERRACOTTA);
case GREEN_TERRACOTTA:
return new ItemStack(Material.GREEN_GLAZED_TERRACOTTA);
case RED_TERRACOTTA:
return new ItemStack(Material.RED_GLAZED_TERRACOTTA);
case BLACK_TERRACOTTA:
return new ItemStack(Material.BLACK_GLAZED_TERRACOTTA);
case IRON_SWORD:
case IRON_PICKAXE:
case IRON_AXE:
case IRON_SHOVEL:
case IRON_HOE:
case CHAINMAIL_HELMET:
case CHAINMAIL_CHESTPLATE:
case CHAINMAIL_LEGGINGS:
case CHAINMAIL_BOOTS:
case IRON_HELMET:
case IRON_CHESTPLATE:
case IRON_LEGGINGS:
case IRON_BOOTS:
case IRON_HORSE_ARMOR:
return new ItemStack(Material.IRON_NUGGET);
case GOLDEN_SWORD:
case GOLDEN_PICKAXE:
case GOLDEN_AXE:
case GOLDEN_SHOVEL:
case GOLDEN_HOE:
case GOLDEN_HELMET:
case GOLDEN_CHESTPLATE:
case GOLDEN_LEGGINGS:
case GOLDEN_BOOTS:
case GOLDEN_HORSE_ARMOR:
return new ItemStack(Material.GOLD_NUGGET);
default:
if (Tag.LOGS.isTagged(item.getType())) {
return new ItemStack(Material.CHARCOAL);
}
return null;
}
}
public static Material getWoolFromColour(DyeColor color) {
switch (color) {
case WHITE:
return Material.WHITE_WOOL;
case ORANGE:
return Material.ORANGE_WOOL;
case MAGENTA:
return Material.MAGENTA_WOOL;
case LIGHT_BLUE:
return Material.LIGHT_BLUE_WOOL;
case YELLOW:
return Material.YELLOW_WOOL;
case LIME:
return Material.LIME_WOOL;
case PINK:
return Material.PINK_WOOL;
case GRAY:
return Material.GRAY_WOOL;
case LIGHT_GRAY:
return Material.LIGHT_GRAY_WOOL;
case CYAN:
return Material.CYAN_WOOL;
case PURPLE:
return Material.PURPLE_WOOL;
case BLUE:
return Material.BLUE_WOOL;
case BROWN:
return Material.BROWN_WOOL;
case GREEN:
return Material.GREEN_WOOL;
case RED:
return Material.RED_WOOL;
case BLACK:
return Material.BLACK_WOOL;
default:
return Material.WHITE_WOOL;
}
}
public static Material getBoatFromTree(TreeSpecies treeSpecies) {
switch (treeSpecies) {
case GENERIC:
return Material.OAK_BOAT;
case REDWOOD:
return Material.SPRUCE_BOAT;
case BIRCH:
return Material.BIRCH_BOAT;
case JUNGLE:
return Material.JUNGLE_BOAT;
case ACACIA:
return Material.ACACIA_BOAT;
case DARK_OAK:
return Material.DARK_OAK_BOAT;
default:
return Material.OAK_BOAT;
}
}
/**
* Checks whether the item is usable as a fuel in a furnace.
*
* @param item The item to check.
* @return Whether it is usable in a furnace.
*/
public static boolean isAFuel(ItemStack item) {
switch(item.getType()) {
case COAL:
case COAL_BLOCK:
case WOODEN_AXE:
case WOODEN_HOE:
case WOODEN_PICKAXE:
case WOODEN_SHOVEL:
case WOODEN_SWORD:
case STICK:
case OAK_FENCE:
case OAK_FENCE_GATE:
case OAK_TRAPDOOR:
case ACACIA_TRAPDOOR:
case BIRCH_TRAPDOOR:
case DARK_OAK_TRAPDOOR:
case JUNGLE_TRAPDOOR:
case SPRUCE_TRAPDOOR:
case CRAFTING_TABLE:
case CHEST:
case TRAPPED_CHEST:
case JUKEBOX:
case NOTE_BLOCK:
case BROWN_MUSHROOM_BLOCK:
case RED_MUSHROOM_BLOCK:
case BLAZE_ROD:
case LAVA_BUCKET:
case BOOKSHELF:
case ACACIA_STAIRS:
case DARK_OAK_STAIRS:
case SPRUCE_FENCE:
case JUNGLE_FENCE:
case BIRCH_FENCE:
case ACACIA_FENCE:
case DARK_OAK_FENCE:
case SPRUCE_FENCE_GATE:
case JUNGLE_FENCE_GATE:
case BIRCH_FENCE_GATE:
case ACACIA_FENCE_GATE:
case DARK_OAK_FENCE_GATE:
case SIGN:
case FISHING_ROD:
case BOW:
case LADDER:
return true;
default:
return Tag.ITEMS_BOATS.isTagged(item.getType())
|| Tag.WOODEN_DOORS.isTagged(item.getType())
|| Tag.CARPETS.isTagged(item.getType())
|| Tag.WOODEN_BUTTONS.isTagged(item.getType())
|| Tag.ITEMS_BANNERS.isTagged(item.getType())
|| Tag.LOGS.isTagged(item.getType())
|| Tag.LEAVES.isTagged(item.getType())
|| Tag.PLANKS.isTagged(item.getType())
|| Tag.WOODEN_STAIRS.isTagged(item.getType())
|| Tag.WOODEN_SLABS.isTagged(item.getType())
|| Tag.SAPLINGS.isTagged(item.getType())
|| Tag.WOOL.isTagged(item.getType())
|| Tag.WOODEN_PRESSURE_PLATES.isTagged(item.getType());
}
}
/**
* Checks whether an item is a potion ingredient.
*
* @param item The item to check.
* @return If the item is a potion ingredient.
*/
public static boolean isAPotionIngredient(ItemStack item) {
switch(item.getType()) {
case NETHER_WART:
case GLOWSTONE_DUST:
case REDSTONE:
case SPIDER_EYE:
case MAGMA_CREAM:
case SUGAR:
case GLISTERING_MELON_SLICE:
case GHAST_TEAR:
case BLAZE_POWDER:
case FERMENTED_SPIDER_EYE:
case GUNPOWDER:
case GOLDEN_CARROT:
case RABBIT_FOOT:
return true;
default:
return false;
}
}
public static boolean containsRawFood(Inventory inv) {
return getRawFood(inv).size() > 0;
}
public static List<ItemStack> getRawFood(Inventory inv) {
List<ItemStack> ret = new ArrayList<>();
for (ItemStack it : inv.getContents()) {
if (isStackValid(it) && isCookable(it))
ret.add(it);
}
return ret;
}
public static boolean containsRawMinerals(Inventory inv) {
for (ItemStack it : inv.getContents()) {
if (isStackValid(it) && isSmeltable(it))
return true;
}
return false;
}
public static List<ItemStack> getRawMinerals(Inventory inv) {
List<ItemStack> ret = new ArrayList<>();
for (ItemStack it : inv.getContents()) {
if (isStackValid(it) && isSmeltable(it))
ret.add(it);
}
return ret;
}
public static boolean containsRawMaterials(Inventory inv) {
for (ItemStack it : inv.getContents()) {
if (isStackValid(it) && isFurnacable(it))
return true;
}
return false;
}
public static List<ItemStack> getRawMaterials(Inventory inv) {
List<ItemStack> ret = new ArrayList<>();
for (ItemStack it : inv.getContents()) {
if (isStackValid(it) && isFurnacable(it))
ret.add(it);
}
return ret;
}
public static boolean isFurnacable(ItemStack item) {
return isCookable(item) || isSmeltable(item);
}
public static boolean isItemEdible(ItemStack item) {
return item.getType().isEdible();
}
public static ItemStack getUsedItem(ItemStack item) {
if (item.getType() == Material.MUSHROOM_STEW) {
item.setType(Material.BOWL); // Get your bowl back
} else if (item.getType() == Material.POTION) {
item.setType(Material.GLASS_BOTTLE); // Get your bottle back
} else if (item.getType() == Material.LAVA_BUCKET || item.getType() == Material.WATER_BUCKET || item.getType() == Material.MILK_BUCKET) {
item.setType(Material.BUCKET); // Get your bucket back
} else if (item.getAmount() == 1) {
item.setType(Material.AIR);
} else {
item.setAmount(item.getAmount() - 1);
}
return item;
}
public static ItemStack getSmallestStackOfType(ItemStack[] stacks, ItemStack item) {
ItemStack smallest = null;
for (ItemStack it : stacks) {
if (!ItemUtil.isStackValid(it)) {
continue;
}
if (ItemUtil.areItemsIdentical(it, item)) {
if (smallest == null) {
smallest = it;
}
if (it.getAmount() < smallest.getAmount()) {
smallest = it;
}
}
}
return smallest;
}
public static ItemStack makeItemValid(ItemStack invalid) {
if(invalid == null)
return new ItemStack(Material.STONE);
ItemStack valid = invalid.clone();
if(valid.getDurability() < 0)
valid.setDurability((short) 0);
if(valid.getType() == null || valid.getType() == Material.MOVING_PISTON)
valid.setType(Material.STONE);
if(valid.getAmount() < 1)
valid.setAmount(1);
return valid;
}
/**
* Gets all {@link Item}s at a certain {@link Block}.
*
* @param block The {@link Block} to check for items at.
* @return A {@link ArrayList} of {@link Item}s.
*/
public static List<Item> getItemsAtBlock(Block block) {
List<Item> items = new ArrayList<>();
for (Entity en : block.getChunk().getEntities()) {
if (!(en instanceof Item)) {
continue;
}
Item item = (Item) en;
if (item.isDead() || !item.isValid())
continue;
if (EntityUtil.isEntityInBlock(en, block)) {
items.add(item);
}
}
return items;
}
/**
* Returns the maximum durability that an item can have.
*
* @param type
* @return
*/
public static short getMaxDurability(Material type) {
switch(type) {
case DIAMOND_AXE:
case DIAMOND_HOE:
case DIAMOND_PICKAXE:
case DIAMOND_SHOVEL:
case DIAMOND_SWORD:
return 1562;
case IRON_AXE:
case IRON_HOE:
case IRON_PICKAXE:
case IRON_SHOVEL:
case IRON_SWORD:
return 251;
case STONE_AXE:
case STONE_HOE:
case STONE_PICKAXE:
case STONE_SHOVEL:
case STONE_SWORD:
return 132;
case WOODEN_AXE:
case WOODEN_HOE:
case WOODEN_PICKAXE:
case WOODEN_SHOVEL:
case WOODEN_SWORD:
return 60;
case GOLDEN_AXE:
case GOLDEN_HOE:
case GOLDEN_PICKAXE:
case GOLDEN_SHOVEL:
case GOLDEN_SWORD:
return 33;
case SHEARS:
return 238;
case FLINT_AND_STEEL:
case FISHING_ROD:
return 65;
case SHIELD:
return 337;
default:
return type.getMaxDurability();
}
}
public static void damageHeldItem(Player player) {
ItemStack heldItem = player.getItemInHand();
if(heldItem != null && getMaxDurability(heldItem.getType()) > 0) {
heldItem.setDurability((short) (heldItem.getDurability() + 1));
if(heldItem.getDurability() <= getMaxDurability(heldItem.getType()))
player.setItemInHand(heldItem);
else
player.setItemInHand(null);
}
}
public static boolean isStainedGlass(Material typeId) {
switch(typeId) {
case BLACK_STAINED_GLASS:
case BLUE_STAINED_GLASS:
case BROWN_STAINED_GLASS:
case CYAN_STAINED_GLASS:
case GRAY_STAINED_GLASS:
case GREEN_STAINED_GLASS:
case LIGHT_BLUE_STAINED_GLASS:
case LIGHT_GRAY_STAINED_GLASS:
case LIME_STAINED_GLASS:
case MAGENTA_STAINED_GLASS:
case ORANGE_STAINED_GLASS:
case PINK_STAINED_GLASS:
case PURPLE_STAINED_GLASS:
case RED_STAINED_GLASS:
case WHITE_STAINED_GLASS:
case YELLOW_STAINED_GLASS:
return true;
default:
return false;
}
}
public static boolean isStainedGlassPane(Material typeId) {
switch(typeId) {
case BLACK_STAINED_GLASS_PANE:
case BLUE_STAINED_GLASS_PANE:
case BROWN_STAINED_GLASS_PANE:
case CYAN_STAINED_GLASS_PANE:
case GRAY_STAINED_GLASS_PANE:
case GREEN_STAINED_GLASS_PANE:
case LIGHT_BLUE_STAINED_GLASS_PANE:
case LIGHT_GRAY_STAINED_GLASS_PANE:
case LIME_STAINED_GLASS_PANE:
case MAGENTA_STAINED_GLASS_PANE:
case ORANGE_STAINED_GLASS_PANE:
case PINK_STAINED_GLASS_PANE:
case PURPLE_STAINED_GLASS_PANE:
case RED_STAINED_GLASS_PANE: