11package com .artillexstudios .axsmithing .gui .impl ;
22
3+ import com .artillexstudios .axapi .libs .boostedyaml .boostedyaml .YamlDocument ;
4+ import com .artillexstudios .axapi .libs .boostedyaml .boostedyaml .block .implementation .Section ;
5+ import com .artillexstudios .axapi .scheduler .Scheduler ;
36import com .artillexstudios .axsmithing .AxSmithingPlugin ;
47import com .artillexstudios .axsmithing .gui .SmithingTable ;
58import com .artillexstudios .axsmithing .utils .ItemBuilder ;
69import com .artillexstudios .axsmithing .utils .StringUtils ;
7- import dev .dejvokep .boostedyaml .YamlDocument ;
8- import dev .dejvokep .boostedyaml .block .implementation .Section ;
910import org .bukkit .Bukkit ;
1011import org .bukkit .Material ;
1112import org .bukkit .entity .Player ;
1617import org .bukkit .inventory .InventoryHolder ;
1718import org .bukkit .inventory .ItemStack ;
1819import org .bukkit .inventory .Recipe ;
20+ import org .bukkit .inventory .RecipeChoice ;
21+ import org .bukkit .inventory .SmithingRecipe ;
1922import org .bukkit .inventory .SmithingTransformRecipe ;
2023import org .bukkit .inventory .SmithingTrimRecipe ;
2124import org .bukkit .inventory .meta .ArmorMeta ;
2629import org .jetbrains .annotations .NotNull ;
2730import org .jetbrains .annotations .Nullable ;
2831
32+ import java .lang .reflect .Field ;
2933import java .util .ArrayList ;
3034import java .util .Iterator ;
3135import java .util .List ;
3236import java .util .Map ;
3337import java .util .Set ;
38+ import java .util .stream .IntStream ;
3439
3540public class SmithingTable_V1_20 implements SmithingTable , InventoryHolder {
3641 private final int outputSlot ;
@@ -39,6 +44,10 @@ public class SmithingTable_V1_20 implements SmithingTable, InventoryHolder {
3944 private final int templateSlot ;
4045 private final boolean needNetheriteTemplate ;
4146 private final boolean dontConvertWithModelData ;
47+ private final Field templateField ;
48+ private final Field baseField ;
49+ private final Field additionField ;
50+ private final Field resultField ;
4251
4352 public SmithingTable_V1_20 () {
4453 YamlDocument config = AxSmithingPlugin .getConfiguration ();
@@ -48,6 +57,33 @@ public SmithingTable_V1_20() {
4857 itemSlot = config .getInt ("menu.1_20.item-slot" );
4958 needNetheriteTemplate = config .getBoolean ("menu.1_20.need-netherite-template" );
5059 dontConvertWithModelData = config .getBoolean ("menu.1_20.dont-convert-with-modeldata" );
60+ try {
61+ templateField = SmithingTransformRecipe .class .getDeclaredField ("template" );
62+ templateField .setAccessible (true );
63+ } catch (NoSuchFieldException e ) {
64+ throw new RuntimeException (e );
65+ }
66+
67+ try {
68+ baseField = SmithingRecipe .class .getDeclaredField ("base" );
69+ baseField .setAccessible (true );
70+ } catch (NoSuchFieldException e ) {
71+ throw new RuntimeException (e );
72+ }
73+
74+ try {
75+ additionField = SmithingRecipe .class .getDeclaredField ("addition" );
76+ additionField .setAccessible (true );
77+ } catch (NoSuchFieldException e ) {
78+ throw new RuntimeException (e );
79+ }
80+
81+ try {
82+ resultField = SmithingRecipe .class .getDeclaredField ("result" );
83+ resultField .setAccessible (true );
84+ } catch (NoSuchFieldException e ) {
85+ throw new RuntimeException (e );
86+ }
5187 }
5288
5389 @ Override
@@ -188,7 +224,7 @@ public Inventory getInventory() {
188224 }
189225
190226 private void updateGui (Inventory inv ) {
191- Bukkit . getScheduler ().runTaskLater ( AxSmithingPlugin . getInstance (), t -> {
227+ Scheduler . get ().runLater ( t -> {
192228 ItemStack template = inv .getItem (templateSlot );
193229 ItemStack base = inv .getItem (itemSlot );
194230 ItemStack addition = inv .getItem (upgradeSlot );
@@ -230,7 +266,7 @@ private void updateGui(Inventory inv) {
230266 checkRecipe (inv , finalAddition , finalTemplate , finalBase );
231267 }
232268
233- }, 0L );
269+ }, 1L );
234270 }
235271
236272 private boolean checkRecipe (Inventory inventory , ItemStack finalTemplate , ItemStack finalBase , ItemStack finalAddition ) {
@@ -262,18 +298,28 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt
262298 }
263299
264300 if (recipe instanceof SmithingTransformRecipe transformRecipe ) {
265- boolean test1 = transformRecipe .getTemplate ().test (finalTemplate );
266- boolean test2 = transformRecipe .getBase ().test (finalBase );
301+ RecipeChoice template = getTemplate (transformRecipe );
302+ RecipeChoice base = getBase (transformRecipe );
303+ RecipeChoice addition = getAddition (transformRecipe );
304+ if (template == null || base == null || addition == null ) {
305+ return false ;
306+ }
307+
308+ boolean test1 = template .test (finalTemplate );
309+ boolean test2 = base .test (finalBase );
267310 ItemMeta baseItemMeta = finalBase .getItemMeta ();
268311 if (baseItemMeta == null ) return false ;
269- boolean test3 = transformRecipe . getAddition () .test (finalAddition );
312+ boolean test3 = addition .test (finalAddition );
270313
271314 if (dontConvertWithModelData && baseItemMeta .hasCustomModelData ()) {
272315 return false ;
273316 }
274317
275318 if (test1 && test2 && test3 ) {
276- ItemStack item = transformRecipe .getResult ();
319+ ItemStack item = getResult (transformRecipe );
320+ if (item == null ) {
321+ return false ;
322+ }
277323 item .setItemMeta (baseItemMeta );
278324 inventory .setItem (outputSlot , item );
279325 return true ;
@@ -286,6 +332,62 @@ private boolean checkRecipe(Inventory inventory, ItemStack finalTemplate, ItemSt
286332 return false ;
287333 }
288334
335+ public RecipeChoice getTemplate (SmithingTransformRecipe recipe ) {
336+ RecipeChoice recipeChoice ;
337+ try {
338+ recipeChoice = (RecipeChoice ) templateField .get (recipe );
339+ if (recipeChoice == null ) {
340+ return null ;
341+ }
342+ } catch (IllegalAccessException e ) {
343+ throw new RuntimeException (e );
344+ }
345+
346+ return recipeChoice .clone ();
347+ }
348+
349+ public RecipeChoice getBase (SmithingTransformRecipe recipe ) {
350+ RecipeChoice recipeChoice ;
351+ try {
352+ recipeChoice = (RecipeChoice ) baseField .get (recipe );
353+ if (recipeChoice == null ) {
354+ return null ;
355+ }
356+ } catch (IllegalAccessException e ) {
357+ throw new RuntimeException (e );
358+ }
359+
360+ return recipeChoice .clone ();
361+ }
362+
363+ public RecipeChoice getAddition (SmithingTransformRecipe recipe ) {
364+ RecipeChoice recipeChoice ;
365+ try {
366+ recipeChoice = (RecipeChoice ) additionField .get (recipe );
367+ if (recipeChoice == null ) {
368+ return null ;
369+ }
370+ } catch (IllegalAccessException e ) {
371+ throw new RuntimeException (e );
372+ }
373+
374+ return recipeChoice .clone ();
375+ }
376+
377+ public ItemStack getResult (SmithingTransformRecipe recipe ) {
378+ ItemStack result ;
379+ try {
380+ result = (ItemStack ) resultField .get (recipe );
381+ if (result == null ) {
382+ return null ;
383+ }
384+ } catch (IllegalAccessException e ) {
385+ throw new RuntimeException (e );
386+ }
387+
388+ return result .clone ();
389+ }
390+
289391 @ Nullable
290392 private ArmorTrim armorTrim (@ NotNull ItemStack finalAddition , @ NotNull ItemStack finalTemplate ) {
291393 TrimMaterial material ;
0 commit comments