11package com .github .jikoo .planarenchanting .anvil ;
22
3+ import io .papermc .paper .registry .RegistryAccess ;
4+ import io .papermc .paper .registry .RegistryKey ;
5+ import io .papermc .paper .registry .TypedKey ;
6+ import io .papermc .paper .registry .keys .tags .ItemTypeTagKeys ;
7+ import io .papermc .paper .registry .tag .Tag ;
8+ import io .papermc .paper .registry .tag .TagKey ;
39import java .util .HashMap ;
4- import java .util .List ;
510import java .util .Map ;
6- import org . bukkit . Material ;
7- import org .bukkit .Tag ;
11+ import java . util . function . Predicate ;
12+ import org .bukkit .NamespacedKey ;
813import org .bukkit .inventory .ItemStack ;
9- import org .bukkit .inventory .RecipeChoice ;
10- import org .bukkit .inventory .RecipeChoice .MaterialChoice ;
14+ import org .bukkit .inventory .ItemType ;
1115import org .jetbrains .annotations .NotNull ;
12- import org .jetbrains .annotations .VisibleForTesting ;
1316
1417/**
1518 * Definitions of materials used in anvil repair operations.
@@ -26,69 +29,69 @@ public final class RepairMaterial {
2629 * @return whether the addition material can repair the base material
2730 */
2831 public static boolean repairs (@ NotNull ItemStack base , @ NotNull ItemStack addition ) {
29- RecipeChoice recipeChoice = MATERIALS_TO_REPAIRABLE .get (base .getType ());
30- return recipeChoice != null && recipeChoice .test (addition );
32+ Predicate < NamespacedKey > predicate = MATERIALS_TO_REPAIRABLE .get (base .getType (). getKey ());
33+ return predicate != null && predicate .test (addition . getType (). getKey () );
3134 }
3235
33- private static final Map <Material , RecipeChoice > MATERIALS_TO_REPAIRABLE = new HashMap <>();
36+ private static final Map <NamespacedKey , Predicate < NamespacedKey > > MATERIALS_TO_REPAIRABLE = new HashMap <>();
3437
3538 static {
36- String [] armor = new String [] { "_HELMET" , "_CHESTPLATE" , "_LEGGINGS" , "_BOOTS" };
37- String [] tools = new String [] { "_AXE" , "_SHOVEL" , "_PICKAXE" , "_HOE" , "_SWORD" };
38- String [] armortools = new String [armor .length + tools .length ];
39- System .arraycopy (armor , 0 , armortools , 0 , armor .length );
40- System .arraycopy (tools , 0 , armortools , armor .length , tools .length );
41-
42- // Leather armor
43- addGear ("LEATHER" , armor , Material .LEATHER );
44-
45- // Stone tools
46- addGear ("STONE" , tools , new RecipeChoice .MaterialChoice (Tag .ITEMS_STONE_TOOL_MATERIALS ));
47-
48- // Wooden tools, shields
49- MaterialChoice choicePlanks = new MaterialChoice (Tag .PLANKS );
50- addGear ("WOODEN" , tools , choicePlanks );
51- MATERIALS_TO_REPAIRABLE .put (Material .SHIELD , choicePlanks );
52-
53- // Chainmail, iron armor and tools
54- RecipeChoice choiceIronIngot = singleChoice (Material .IRON_INGOT );
55- addGear ("CHAINMAIL" , armor , choiceIronIngot );
56- addGear ("IRON" , armortools , choiceIronIngot );
57-
58- // Gold, diamond, and netherite armor and tools
59- addGear ("GOLDEN" , armortools , Material .GOLD_INGOT );
60- addGear ("DIAMOND" , armortools , Material .DIAMOND );
61- addGear ("NETHERITE" , armortools , Material .NETHERITE_INGOT );
39+ // Note that for all choices, we want to use a MaterialChoice.
40+ // ExactChoice also does a full meta match.
41+
42+ // See net.minecraft.world.item.equipment.ArmorMaterials
43+ String [] armor = new String [] { "_helmet" , "_chestplate" , "_leggings" , "_boots" };
44+ addGear ("leather" , armor , ItemTypeTagKeys .REPAIRS_LEATHER_ARMOR );
45+ addGear ("copper" , armor , ItemTypeTagKeys .REPAIRS_COPPER_ARMOR );
46+ addGear ("chainmail" , armor , ItemTypeTagKeys .REPAIRS_CHAIN_ARMOR );
47+ addGear ("iron" , armor , ItemTypeTagKeys .REPAIRS_IRON_ARMOR );
48+ addGear ("golden" , armor , ItemTypeTagKeys .REPAIRS_GOLD_ARMOR );
49+ addGear ("diamond" , armor , ItemTypeTagKeys .REPAIRS_DIAMOND_ARMOR );
50+ MATERIALS_TO_REPAIRABLE .put (ItemType .TURTLE_HELMET .getKey (), new TagPredicate (ItemTypeTagKeys .REPAIRS_TURTLE_HELMET ));
51+ addGear ("netherite" , armor , ItemTypeTagKeys .REPAIRS_NETHERITE_ARMOR );
52+ MATERIALS_TO_REPAIRABLE .put (ItemType .WOLF_ARMOR .getKey (), new TagPredicate (ItemTypeTagKeys .REPAIRS_WOLF_ARMOR ));
53+
54+ // See net.minecraft.world.item.ToolMaterial
55+ String [] tools = new String [] { "_axe" , "_shovel" , "_pickaxe" , "_hoe" , "_sword" , "_spear" };
56+ addGear ("stone" , tools , ItemTypeTagKeys .STONE_TOOL_MATERIALS );
57+ Predicate <NamespacedKey > woodToolMats = new TagPredicate (ItemTypeTagKeys .WOODEN_TOOL_MATERIALS );
58+ addGear ("wooden" , tools , woodToolMats );
59+ MATERIALS_TO_REPAIRABLE .put (ItemType .SHIELD .getKey (), woodToolMats );
60+ addGear ("iron" , tools , ItemTypeTagKeys .IRON_TOOL_MATERIALS );
61+ addGear ("golden" , tools , ItemTypeTagKeys .GOLD_TOOL_MATERIALS );
62+ addGear ("diamond" , tools , ItemTypeTagKeys .DIAMOND_TOOL_MATERIALS );
63+ addGear ("netherite" , tools , ItemTypeTagKeys .NETHERITE_TOOL_MATERIALS );
6264
6365 // Misc. repairable items
64- MATERIALS_TO_REPAIRABLE .put (Material .TURTLE_HELMET , singleChoice (Material .TURTLE_SCUTE ));
65- MATERIALS_TO_REPAIRABLE .put (Material .ELYTRA , singleChoice (Material .PHANTOM_MEMBRANE ));
66- MATERIALS_TO_REPAIRABLE .put (Material .MACE , singleChoice (Material .BREEZE_ROD ));
66+ MATERIALS_TO_REPAIRABLE .put (ItemType .ELYTRA .getKey (), key -> ItemType .PHANTOM_MEMBRANE .getKey ().equals (key ));
67+ MATERIALS_TO_REPAIRABLE .put (ItemType .MACE .getKey (), key -> ItemType .BREEZE_ROD .getKey ().equals (key ));
6768 }
6869
69- private static void addGear (String type , String [] gearType , RecipeChoice repairChoice ) {
70+ private static void addGear (String type , String [] gearType , Predicate < NamespacedKey > repairChoice ) {
7071 for (String toolType : gearType ) {
71- Material material = Material .getMaterial (type + toolType );
72- if (material != null ) {
73- MATERIALS_TO_REPAIRABLE .put (material , repairChoice );
74- }
72+ MATERIALS_TO_REPAIRABLE .put (NamespacedKey .minecraft (type + toolType ), repairChoice );
7573 }
7674 }
7775
78- private static void addGear (String type , String [] gearType , Material repairMaterial ) {
79- addGear (type , gearType , singleChoice ( repairMaterial ));
76+ private static void addGear (String type , String [] gearType , TagKey < ItemType > tag ) {
77+ addGear (type , gearType , new TagPredicate ( tag ));
8078 }
8179
82- private static RecipeChoice singleChoice (Material material ) {
83- // RecipeChoice.ExactChoice is a full meta match, which isn't what we want.
84- return new RecipeChoice .MaterialChoice (List .of (material ));
80+ private RepairMaterial () {
81+ throw new IllegalStateException ("Cannot instantiate static helper method container." );
8582 }
8683
87- @ VisibleForTesting
88- static boolean hasEntry (@ NotNull Material material ) {
89- return MATERIALS_TO_REPAIRABLE .containsKey (material );
90- }
84+ private record TagPredicate (Tag <ItemType > tag ) implements Predicate <NamespacedKey > {
9185
92- private RepairMaterial () {}
86+ private TagPredicate (TagKey <ItemType > tag ) {
87+ this (RegistryAccess .registryAccess ().getRegistry (RegistryKey .ITEM ).getTag (tag ));
88+ }
89+
90+ @ Override
91+ public boolean test (NamespacedKey key ) {
92+ return tag .contains (TypedKey .create (RegistryKey .ITEM , key ));
93+ }
94+
95+ }
9396
9497}
0 commit comments