Skip to content

Commit 366e7cd

Browse files
committed
fix: fix issues on older versions aswell
1 parent 8d2dfe0 commit 366e7cd

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_16.java

+81-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import org.bukkit.inventory.InventoryHolder;
1818
import org.bukkit.inventory.ItemStack;
1919
import org.bukkit.inventory.Recipe;
20+
import org.bukkit.inventory.RecipeChoice;
2021
import org.bukkit.inventory.SmithingRecipe;
2122
import org.bukkit.inventory.meta.ItemMeta;
2223
import org.jetbrains.annotations.NotNull;
2324

25+
import java.lang.reflect.Field;
2426
import java.util.ArrayList;
2527
import java.util.Iterator;
2628
import java.util.List;
@@ -32,13 +34,37 @@ public class SmithingTable_V1_16 implements SmithingTable, InventoryHolder {
3234
private final int upgradeSlot;
3335
private final int itemSlot;
3436
private final boolean dontConvertWithModelData;
37+
private final Field baseField;
38+
private final Field additionField;
39+
private final Field resultField;
3540

3641
public SmithingTable_V1_16() {
3742
YamlDocument config = AxSmithingPlugin.getConfiguration();
3843
outputSlot = config.getInt("menu.1_16.output-slot");
3944
upgradeSlot = config.getInt("menu.1_16.upgrade-slot");
4045
itemSlot = config.getInt("menu.1_16.item-slot");
4146
dontConvertWithModelData = config.getBoolean("menu.1_16.dont-convert-with-modeldata");
47+
48+
try {
49+
baseField = SmithingRecipe.class.getDeclaredField("base");
50+
baseField.setAccessible(true);
51+
} catch (NoSuchFieldException e) {
52+
throw new RuntimeException(e);
53+
}
54+
55+
try {
56+
additionField = SmithingRecipe.class.getDeclaredField("addition");
57+
additionField.setAccessible(true);
58+
} catch (NoSuchFieldException e) {
59+
throw new RuntimeException(e);
60+
}
61+
62+
try {
63+
resultField = SmithingRecipe.class.getDeclaredField("result");
64+
resultField.setAccessible(true);
65+
} catch (NoSuchFieldException e) {
66+
throw new RuntimeException(e);
67+
}
4268
}
4369

4470
@Override
@@ -189,17 +215,27 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack
189215
Recipe recipe = recipeIterator.next();
190216

191217
if (recipe instanceof SmithingRecipe smithingRecipe) {
192-
boolean test1 = smithingRecipe.getBase().test(finalBase);
218+
RecipeChoice base = getBase(smithingRecipe);
219+
RecipeChoice addition = getAddition(smithingRecipe);
220+
if (base == null || addition == null) {
221+
return false;
222+
}
223+
224+
boolean test1 = base.test(finalBase);
193225
ItemMeta baseItemMeta = finalBase.getItemMeta();
194226
if (baseItemMeta == null) return false;
195-
boolean test2 = smithingRecipe.getAddition().test(finalAddition);
227+
boolean test2 = addition.test(finalAddition);
196228

197229
if (dontConvertWithModelData && baseItemMeta.hasCustomModelData()) {
198230
return false;
199231
}
200232

201233
if (test1 && test2) {
202-
ItemStack item = smithingRecipe.getResult();
234+
ItemStack item = getResult(smithingRecipe);
235+
if (item == null) {
236+
return false;
237+
}
238+
203239
item.setItemMeta(baseItemMeta);
204240
inventory.setItem(outputSlot, item);
205241
return true;
@@ -211,4 +247,46 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack
211247

212248
return false;
213249
}
250+
251+
public RecipeChoice getBase(SmithingRecipe recipe) {
252+
RecipeChoice recipeChoice;
253+
try {
254+
recipeChoice = (RecipeChoice) baseField.get(recipe);
255+
if (recipeChoice == null) {
256+
return null;
257+
}
258+
} catch (IllegalAccessException e) {
259+
throw new RuntimeException(e);
260+
}
261+
262+
return recipeChoice.clone();
263+
}
264+
265+
public RecipeChoice getAddition(SmithingRecipe recipe) {
266+
RecipeChoice recipeChoice;
267+
try {
268+
recipeChoice = (RecipeChoice) additionField.get(recipe);
269+
if (recipeChoice == null) {
270+
return null;
271+
}
272+
} catch (IllegalAccessException e) {
273+
throw new RuntimeException(e);
274+
}
275+
276+
return recipeChoice.clone();
277+
}
278+
279+
public ItemStack getResult(SmithingRecipe recipe) {
280+
ItemStack result;
281+
try {
282+
result = (ItemStack) resultField.get(recipe);
283+
if (result == null) {
284+
return null;
285+
}
286+
} catch (IllegalAccessException e) {
287+
throw new RuntimeException(e);
288+
}
289+
290+
return result.clone();
291+
}
214292
}

src/main/java/com/artillexstudios/axsmithing/gui/impl/SmithingTable_V1_20.java

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.List;
3636
import java.util.Map;
3737
import java.util.Set;
38-
import java.util.stream.IntStream;
3938

4039
public class SmithingTable_V1_20 implements SmithingTable, InventoryHolder {
4140
private final int outputSlot;

0 commit comments

Comments
 (0)