17
17
import org .bukkit .inventory .InventoryHolder ;
18
18
import org .bukkit .inventory .ItemStack ;
19
19
import org .bukkit .inventory .Recipe ;
20
+ import org .bukkit .inventory .RecipeChoice ;
20
21
import org .bukkit .inventory .SmithingRecipe ;
21
22
import org .bukkit .inventory .meta .ItemMeta ;
22
23
import org .jetbrains .annotations .NotNull ;
23
24
25
+ import java .lang .reflect .Field ;
24
26
import java .util .ArrayList ;
25
27
import java .util .Iterator ;
26
28
import java .util .List ;
@@ -32,13 +34,37 @@ public class SmithingTable_V1_16 implements SmithingTable, InventoryHolder {
32
34
private final int upgradeSlot ;
33
35
private final int itemSlot ;
34
36
private final boolean dontConvertWithModelData ;
37
+ private final Field baseField ;
38
+ private final Field additionField ;
39
+ private final Field resultField ;
35
40
36
41
public SmithingTable_V1_16 () {
37
42
YamlDocument config = AxSmithingPlugin .getConfiguration ();
38
43
outputSlot = config .getInt ("menu.1_16.output-slot" );
39
44
upgradeSlot = config .getInt ("menu.1_16.upgrade-slot" );
40
45
itemSlot = config .getInt ("menu.1_16.item-slot" );
41
46
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
+ }
42
68
}
43
69
44
70
@ Override
@@ -189,17 +215,27 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack
189
215
Recipe recipe = recipeIterator .next ();
190
216
191
217
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 );
193
225
ItemMeta baseItemMeta = finalBase .getItemMeta ();
194
226
if (baseItemMeta == null ) return false ;
195
- boolean test2 = smithingRecipe . getAddition () .test (finalAddition );
227
+ boolean test2 = addition .test (finalAddition );
196
228
197
229
if (dontConvertWithModelData && baseItemMeta .hasCustomModelData ()) {
198
230
return false ;
199
231
}
200
232
201
233
if (test1 && test2 ) {
202
- ItemStack item = smithingRecipe .getResult ();
234
+ ItemStack item = getResult (smithingRecipe );
235
+ if (item == null ) {
236
+ return false ;
237
+ }
238
+
203
239
item .setItemMeta (baseItemMeta );
204
240
inventory .setItem (outputSlot , item );
205
241
return true ;
@@ -211,4 +247,46 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalBase, ItemStack
211
247
212
248
return false ;
213
249
}
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
+ }
214
292
}
0 commit comments