Skip to content

Commit 87d4b71

Browse files
committed
Move baked repair data into point of use
1 parent 46d8709 commit 87d4b71

5 files changed

Lines changed: 183 additions & 248 deletions

File tree

enchanting-common/src/test/java/com/github/jikoo/planarenchanting/table/EnchantingTableTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
class EnchantingTableTest {
6060

6161
private MockedStatic<Bukkit> bukkit;
62-
private final Random random = new Random(0); // TODO reset seed before each? Consistency
62+
private final Random random = new Random(0);
6363
private Collection<Enchantment> toolEnchants;
6464

6565
@BeforeAll
@@ -90,6 +90,11 @@ void tearDown() {
9090
bukkit.close();
9191
}
9292

93+
@BeforeEach
94+
void setUpEach() {
95+
random.setSeed(0);
96+
}
97+
9398
static void setUpToolEnchants() {
9499
EnchantData data = EnchantData.Service.PROVIDER.of(Enchantment.EFFICIENCY);
95100
doReturn(10).when(data).getWeight();

enchanting-meta/src/main/java/com/github/jikoo/planarenchanting/anvil/MetaVanillaBehavior.java

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.github.jikoo.planarenchanting.anvil;
22

3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.function.Predicate;
8+
import org.bukkit.Bukkit;
39
import org.bukkit.Material;
10+
import org.bukkit.NamespacedKey;
11+
import org.bukkit.Registry;
12+
import org.bukkit.Tag;
413
import org.bukkit.enchantments.Enchantment;
14+
import org.bukkit.inventory.ItemType;
515
import org.jspecify.annotations.NullMarked;
616

717
/**
@@ -24,7 +34,83 @@ public boolean itemsCombineEnchants(MetaCachedStack base, MetaCachedStack additi
2434

2535
@Override
2636
public boolean itemRepairedBy(MetaCachedStack repaired, MetaCachedStack repairMat) {
27-
return RepairMaterial.repairs(repaired.getItem(), repairMat.getItem());
37+
Predicate<Material> predicate = MATERIALS_TO_REPAIRABLE.get(repaired.getItem().getType());
38+
return predicate != null && predicate.test(repairMat.getItem().getType());
39+
}
40+
41+
private static final Map<Material, Predicate<Material>> MATERIALS_TO_REPAIRABLE = new HashMap<>();
42+
43+
static {
44+
loadTags();
45+
loadLists();
46+
}
47+
48+
private static void loadTags() {
49+
Map<NamespacedKey, Predicate<Material>> tags = new HashMap<>();
50+
for (var entry : BakedRepairableData.getTags().entrySet()) {
51+
if (entry.getKey() == null || entry.getValue() == null) {
52+
continue;
53+
}
54+
55+
Material mat = Registry.MATERIAL.get(entry.getKey());
56+
if (mat == null) {
57+
continue;
58+
}
59+
60+
Predicate<Material> predicate = tags.computeIfAbsent(
61+
entry.getValue(),
62+
tagKey -> {
63+
// Prefer Material tags as they're officially supported.
64+
Tag<Material> matTag = Bukkit.getTag(Tag.REGISTRY_ITEMS, entry.getValue(), Material.class);
65+
if (matTag != null) {
66+
return matTag::isTagged;
67+
}
68+
69+
// Fall through to ItemType tags.
70+
Tag<ItemType> typeTag = Bukkit.getTag(Tag.REGISTRY_ITEMS, entry.getValue(), ItemType.class);
71+
if (typeTag == null) {
72+
return null;
73+
}
74+
return localMat -> {
75+
ItemType localType = localMat.asItemType();
76+
return localType != null && typeTag.isTagged(localType);
77+
};
78+
79+
}
80+
);
81+
82+
if (predicate != null) {
83+
MATERIALS_TO_REPAIRABLE.put(mat, predicate);
84+
}
85+
}
86+
}
87+
88+
private static void loadLists() {
89+
for (var entry : BakedRepairableData.getLists().entrySet()) {
90+
if (entry.getKey() == null) {
91+
continue;
92+
}
93+
94+
Material type = Registry.MATERIAL.get(entry.getKey());
95+
if (type == null || !type.isItem()) {
96+
continue;
97+
}
98+
99+
Set<Material> values = new HashSet<>();
100+
for (NamespacedKey key : entry.getValue()) {
101+
if (key == null) {
102+
continue;
103+
}
104+
Material value = Registry.MATERIAL.get(key);
105+
if (value != null) {
106+
values.add(value);
107+
}
108+
}
109+
110+
if (!values.isEmpty()) {
111+
MATERIALS_TO_REPAIRABLE.put(type, values::contains);
112+
}
113+
}
28114
}
29115

30116
}

enchanting-meta/src/main/java/com/github/jikoo/planarenchanting/anvil/RepairMaterial.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

enchanting-meta/src/test/java/com/github/jikoo/planarenchanting/anvil/MetaVanillaBehaviorTest.java

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.ArgumentMatchers.argThat;
7+
import static org.mockito.ArgumentMatchers.eq;
8+
import static org.mockito.Mockito.doAnswer;
69
import static org.mockito.Mockito.doReturn;
710
import static org.mockito.Mockito.mock;
811
import static org.mockito.Mockito.mockStatic;
@@ -11,9 +14,12 @@
1114

1215
import org.bukkit.Bukkit;
1316
import org.bukkit.Material;
17+
import org.bukkit.NamespacedKey;
1418
import org.bukkit.Registry;
19+
import org.bukkit.Tag;
1520
import org.bukkit.enchantments.Enchantment;
1621
import org.bukkit.inventory.ItemStack;
22+
import org.bukkit.inventory.ItemType;
1723
import org.junit.jupiter.api.AfterAll;
1824
import org.junit.jupiter.api.BeforeAll;
1925
import org.junit.jupiter.api.BeforeEach;
@@ -22,26 +28,49 @@
2228
import org.junit.jupiter.params.ParameterizedTest;
2329
import org.junit.jupiter.params.provider.CsvSource;
2430
import org.junit.jupiter.params.provider.ValueSource;
31+
import org.mockito.ArgumentMatcher;
2532
import org.mockito.MockedStatic;
2633

2734
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2835
class MetaVanillaBehaviorTest {
2936

3037
private MockedStatic<Bukkit> bukkit;
31-
private MockedStatic<RepairMaterial> repairMaterial;
3238
private MetaVanillaBehavior behavior;
3339

3440
@BeforeAll
3541
void setUp() {
3642
bukkit = mockStatic();
43+
// Set up for Material tag for all diamond stuff
44+
ArgumentMatcher<NamespacedKey> isDiamondTag = key ->
45+
key != null && key.getKey().contains("diamond");
46+
bukkit.when(() -> Bukkit.getTag(eq("items"), argThat(isDiamondTag), eq(Material.class)))
47+
.thenAnswer(invocation -> {
48+
Tag<Material> tag = mock();
49+
doAnswer(invIsTagged -> {
50+
Material argument = invIsTagged.getArgument(0);
51+
return argument.getKey().getKey().contains("diamond");
52+
}).when(tag).isTagged(any());
53+
return tag;
54+
});
55+
// Set up for fallthrough to ItemType tag for all gold stuff
56+
ArgumentMatcher<NamespacedKey> isGoldTag = key ->
57+
key != null && key.getKey().contains("gold");
58+
bukkit.when(() -> Bukkit.getTag(eq("items"), argThat(isGoldTag), eq(ItemType.class)))
59+
.thenAnswer(invocation -> {
60+
Tag<ItemType> tag = mock();
61+
doAnswer(invIsTagged -> {
62+
ItemType argument = invIsTagged.getArgument(0);
63+
return argument.getKey().getKey().contains("gold");
64+
}).when(tag).isTagged(any());
65+
return tag;
66+
});
67+
// All other tags will be nonexistent
68+
// Set up other registries (Registry.MATERIAL is backed by the enum and doesn't need mocking)
3769
bukkit.when(() -> Bukkit.getRegistry(any())).thenAnswer(invocation -> mock(Registry.class));
38-
repairMaterial = mockStatic();
39-
repairMaterial.when(() -> RepairMaterial.repairs(any(), any())).thenReturn(true);
4070
}
4171

4272
@AfterAll
4373
void tearDownAll() {
44-
repairMaterial.close();
4574
bukkit.close();
4675
}
4776

@@ -91,15 +120,65 @@ void itemsCombineEnchants(Material baseMat, Material additionMat, boolean result
91120

92121
@Test
93122
void itemRepairedBy() {
123+
ItemStack baseStack = mock();
124+
doReturn(Material.DIAMOND_PICKAXE).when(baseStack).getType();
94125
MetaCachedStack base = mock();
126+
doReturn(baseStack).when(base).getItem();
127+
ItemStack additionStack = mock();
128+
doReturn(Material.DIAMOND).when(additionStack).getType();
95129
MetaCachedStack addition = mock();
130+
doReturn(additionStack).when(addition).getItem();
96131

97-
assertThat(
98-
"Delegates to RepairMaterial",
99-
behavior.itemRepairedBy(base, addition),
100-
is(true)
101-
);
102-
repairMaterial.verify(() -> RepairMaterial.repairs(any(), any()));
132+
assertThat("Item is repairable", behavior.itemRepairedBy(base, addition), is(true));
133+
}
134+
135+
@Test
136+
void repairsNotRepairMat() {
137+
ItemStack baseStack = mock();
138+
doReturn(Material.DIAMOND_PICKAXE).when(baseStack).getType();
139+
MetaCachedStack base = mock();
140+
doReturn(baseStack).when(base).getItem();
141+
ItemStack additionStack = mock();
142+
doReturn(Material.DIRT).when(additionStack).getType();
143+
MetaCachedStack addition = mock();
144+
doReturn(additionStack).when(addition).getItem();
145+
146+
assertThat("Item is not repairable", behavior.itemRepairedBy(base, addition), is(false));
147+
}
148+
149+
@Test
150+
void repairsItemTypeFallthrough() {
151+
ItemStack baseStack = mock();
152+
doReturn(Material.GOLDEN_PICKAXE).when(baseStack).getType();
153+
MetaCachedStack base = mock();
154+
doReturn(baseStack).when(base).getItem();
155+
156+
NamespacedKey key = mock();
157+
doReturn("gold").when(key).getKey();
158+
ItemType type = mock();
159+
doReturn(key).when(type).getKey();
160+
Material material = mock();
161+
doReturn(type).when(material).asItemType();
162+
ItemStack additionStack = mock();
163+
doReturn(material).when(additionStack).getType();
164+
MetaCachedStack addition = mock();
165+
doReturn(additionStack).when(addition).getItem();
166+
167+
assertThat("Item is repairable", behavior.itemRepairedBy(base, addition), is(true));
168+
}
169+
170+
@Test
171+
void repairsNotRepairable() {
172+
ItemStack baseStack = mock();
173+
doReturn(Material.WOODEN_PICKAXE).when(baseStack).getType();
174+
MetaCachedStack base = mock();
175+
doReturn(baseStack).when(base).getItem();
176+
ItemStack additionStack = mock();
177+
doReturn(Material.DIRT).when(additionStack).getType();
178+
MetaCachedStack addition = mock();
179+
doReturn(baseStack).when(addition).getItem();
180+
181+
assertThat("Item is not repairable", behavior.itemRepairedBy(base, addition), is(false));
103182
}
104183

105-
}
184+
}

0 commit comments

Comments
 (0)