From 95c931757248cde19e512c6829a70c56b5ab4ecf Mon Sep 17 00:00:00 2001 From: Wynd Date: Fri, 12 Jan 2024 15:29:02 +0200 Subject: [PATCH] Slabs fix and some cleaner reflection helpers --- .../xyz/pixelatedw/finalbeta/ModConfig.java | 2 + .../xyz/pixelatedw/finalbeta/WyHelper.java | 76 +++++++++---------- .../finalbeta/mixin/RecipeRegistryMixin.java | 35 +++++++++ src/main/resources/finalbeta.mixins.json | 3 +- 4 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 src/main/java/xyz/pixelatedw/finalbeta/mixin/RecipeRegistryMixin.java diff --git a/src/main/java/xyz/pixelatedw/finalbeta/ModConfig.java b/src/main/java/xyz/pixelatedw/finalbeta/ModConfig.java index f08bb4c..7b6eea0 100644 --- a/src/main/java/xyz/pixelatedw/finalbeta/ModConfig.java +++ b/src/main/java/xyz/pixelatedw/finalbeta/ModConfig.java @@ -59,6 +59,8 @@ public class ModConfig { "Drops 3 books when breaking a bookshelf"); public static final Option FIX_DOUBLE_DOORS = make("Fix Double Doors", true, "Fixes double doors not being in their correct state when pressure plates are used to open them"); + public static final Option FIX_SLABS_RECIPE = make("Fix Slabs Recipe", true, + "Givs two slabs for every block, since 1 block = 2 slabs => 3 blocks = 6 slabs (instead of 3)"); private static ModConfig instance = new ModConfig(); public static final ModConfig instance() { diff --git a/src/main/java/xyz/pixelatedw/finalbeta/WyHelper.java b/src/main/java/xyz/pixelatedw/finalbeta/WyHelper.java index 075ff36..dfbe2dc 100644 --- a/src/main/java/xyz/pixelatedw/finalbeta/WyHelper.java +++ b/src/main/java/xyz/pixelatedw/finalbeta/WyHelper.java @@ -33,36 +33,42 @@ public class WyHelper { INSTANCE = getInstance(); } + public static Field getField(Class clz, String... names) { + for (String name : names) { + try { + Field field = clz.getDeclaredField(name); + field.setAccessible(true); + return field; + } catch (NoSuchFieldException ex) { + continue; + } + } + return null; + } + + public static Method getMethod(Class clz, String[] names, Class... params) { + for (String name : names) { + try { + Method method = clz.getDeclaredMethod(name, params); + method.setAccessible(true); + return method; + } catch (NoSuchMethodException ex) { + continue; + } + } + return null; + } + public static Minecraft getInstance() { if (INSTANCE != null) { return INSTANCE; } else { try { - Field field = null; - - try { - field = Minecraft.class.getDeclaredField("instance"); - } catch (NoSuchFieldException ex) { - try { - field = Minecraft.class.getDeclaredField("field_2791"); - } catch (NoSuchFieldException | SecurityException e) { - e.printStackTrace(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - if (field == null) { - return null; - } - - field.setAccessible(true); - return (Minecraft) field.get(null); + return (Minecraft) getField(Minecraft.class, "instance", "field_2791").get(null); } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); + return null; } - - return null; } } @@ -89,24 +95,13 @@ public class WyHelper { } public static void registerWhiteWoolRecipe() { - Method method = null; - try { - method = RecipeRegistry.class.getDeclaredMethod("addShapelessRecipe", ItemInstance.class, Object[].class); - } catch (NoSuchMethodException ex) { - try { - method = RecipeRegistry.class.getDeclaredMethod("method_542", ItemInstance.class, Object[].class); - } catch (NoSuchMethodException | SecurityException e) { - e.printStackTrace(); - } - } + Method method = getMethod(RecipeRegistry.class, new String[] { "addShapelessRecipe", "method_542" }, ItemInstance.class, Object[].class); if (method == null) { return; } try { - method.setAccessible(true); - // Makes new recipes for all colored wools so they can be dyed back // white using bone meal for (int colorId = 0; colorId < 16; ++colorId) { @@ -119,10 +114,13 @@ public class WyHelper { } public static void cheatCommand(Player player) { -// player.level.setLevelTime(0); -// player.level.getProperties().setRaining(false); -// player.level.getProperties().setRainTime(0); -// player.level.getProperties().setThundering(false); -// player.level.getProperties().setThunderTime(0); + + player.dropItem(new ItemInstance(ItemType.redstone, 64)); + + player.level.setLevelTime(0); + player.level.getProperties().setRaining(false); + player.level.getProperties().setRainTime(0); + player.level.getProperties().setThundering(false); + player.level.getProperties().setThunderTime(0); } } diff --git a/src/main/java/xyz/pixelatedw/finalbeta/mixin/RecipeRegistryMixin.java b/src/main/java/xyz/pixelatedw/finalbeta/mixin/RecipeRegistryMixin.java new file mode 100644 index 0000000..ae775e3 --- /dev/null +++ b/src/main/java/xyz/pixelatedw/finalbeta/mixin/RecipeRegistryMixin.java @@ -0,0 +1,35 @@ +package xyz.pixelatedw.finalbeta.mixin; + +import java.util.List; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.item.ItemInstance; +import net.minecraft.item.StoneSlabItem; +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeRegistry; +import xyz.pixelatedw.finalbeta.ModConfig; + +@Mixin(RecipeRegistry.class) +public class RecipeRegistryMixin { + + @Shadow + private List recipes; + + @Inject(method = "", at = @At("TAIL")) + private void recipesChanges(CallbackInfo ci) { + if(ModConfig.FIX_SLABS_RECIPE.get()) { + for (Recipe recipe : this.recipes) { + ItemInstance itemStack = recipe.getOutput(); + if (itemStack.getType() instanceof StoneSlabItem) { + itemStack.count = 6; + } + } + } + } + +} diff --git a/src/main/resources/finalbeta.mixins.json b/src/main/resources/finalbeta.mixins.json index 57a1a91..3f3c8ce 100644 --- a/src/main/resources/finalbeta.mixins.json +++ b/src/main/resources/finalbeta.mixins.json @@ -38,7 +38,8 @@ "LevelMixin", "DoorTileMixin", "CraftingContainerMixin", - "CraftingInventoryMixin" + "CraftingInventoryMixin", + "RecipeRegistryMixin" ], "injectors": { "defaultRequire": -1