From e6c1978d7cad52d282e50f4282d68412e46e48b3 Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 03:21:19 +0300 Subject: [PATCH 01/10] First try at Refill --- .../cleanroommc/bogosorter/BogoSorter.java | 2 +- .../common/refill/DamageHelper.java | 14 +-- .../common/refill/RefillHandler.java | 72 ++++++++------ .../core/BogoSorterTransformer.java | 27 ------ .../core/visitor/EntityPlayerVisitor.java | 95 ------------------ .../bogosorter/core/visitor/PIMVisitor.java | 96 ------------------- .../bogosorter/mixinplugin/Mixins.java | 4 +- .../minecraft/ItemInWorldManagerMixin.java | 53 ++++++++++ .../early/minecraft/MixinEntityPlayer.java | 74 +++++++------- .../early/minecraft/MixinItemStack.java | 19 ++++ 10 files changed, 153 insertions(+), 303 deletions(-) delete mode 100644 src/main/java/com/cleanroommc/bogosorter/core/BogoSorterTransformer.java delete mode 100644 src/main/java/com/cleanroommc/bogosorter/core/visitor/EntityPlayerVisitor.java delete mode 100644 src/main/java/com/cleanroommc/bogosorter/core/visitor/PIMVisitor.java create mode 100644 src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java create mode 100644 src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinItemStack.java diff --git a/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java b/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java index 525222f5..3d4d7203 100644 --- a/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java +++ b/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java @@ -58,7 +58,7 @@ public void onPreInit(FMLPreInitializationEvent event) { DefaultRules.init(BogoSortAPI.INSTANCE); DefaultCompat.init(BogoSortAPI.INSTANCE); Serializer.loadConfig(); - MinecraftForge.EVENT_BUS.register(RefillHandler.class); + MinecraftForge.EVENT_BUS.register(new RefillHandler()); if (NetworkUtils.isDedicatedClient()) { MinecraftForge.EVENT_BUS.post(new SortConfigChangeEvent()); FMLCommonHandler.instance().bus().register(new ClientEventHandler()); diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/DamageHelper.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/DamageHelper.java index c58f7110..b81231d3 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/DamageHelper.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/DamageHelper.java @@ -17,15 +17,6 @@ public static boolean damageItemHook(ItemStack itemStack) { if (!playerConfig.enableAutoRefill || playerConfig.autoRefillDamageThreshold <= 0) return false; if (RefillHandler.shouldHandleRefill(player, itemStack) && isNotArmor(itemStack)) { - ItemStack handItem = player.getHeldItem(); - if (handItem != itemStack) { -// handItem = player.getHeldItemOffhand(); -// if (handItem != itemStack) { -// return false; -// } - return false; - } - int durabilityLeft = itemStack.getMaxDamage() - itemStack.getItemDamage(); if (durabilityLeft >= 0 && durabilityLeft < playerConfig.autoRefillDamageThreshold) { return RefillHandler.handle(player.inventory.currentItem, itemStack, player, true); @@ -34,11 +25,10 @@ public static boolean damageItemHook(ItemStack itemStack) { return false; } + private static boolean isNotArmor(ItemStack itemStack) { if (itemStack.getItem() instanceof ItemArmor || itemStack.getItem() instanceof ISpecialArmor) return false; -// EntityEquipmentSlot slot = itemStack.getItem().getEquipmentSlot(itemStack); -// return slot == null || slot == EntityEquipmentSlot.MAINHAND || slot == EntityEquipmentSlot.OFFHAND; - return false; + return true; } public static int getDurability(ItemStack item) { diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 0592893d..52a28f8d 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -6,6 +6,7 @@ import com.cleanroommc.bogosorter.common.network.NetworkUtils; import com.cleanroommc.bogosorter.common.network.SRefillSound; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gregtech.api.items.MetaGeneratedTool; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; import it.unimi.dsi.fastutil.ints.IntListIterator; @@ -22,18 +23,21 @@ public class RefillHandler { - private static final Class gtToolClass; - - static { - Class clazz; - try { - clazz = Class.forName("gregtech.api.items.toolitem.IGTTool", false, RefillHandler.class.getClassLoader()); - } catch (Exception ignored) { - clazz = null; - } - gtToolClass = clazz; + public RefillHandler(){ } +// private static final Class gtToolClass; +// +// static { +// Class clazz; +// try { +// clazz = Class.forName("gregtech.api.items.MetaGeneratedTool", false, RefillHandler.class.getClassLoader()); +// } catch (Exception ignored) { +// clazz = null; +// } +// gtToolClass = clazz; +// } + private static final int[][] INVENTORY_PROXIMITY_MAP = { {1, 2, 3, 4, 5, 6, 7, 8, 27, 18, 9, 28, 19, 10, 29, 20, 11, 30, 21, 12, 31, 22, 13, 32, 23, 14, 33, 24, 15, 34, 25, 16, 35, 26, 17}, {0, 2, 3, 4, 5, 6, 7, 8, 28, 19, 10, 27, 18, 9, 29, 20, 11, 30, 21, 12, 31, 22, 13, 32, 23, 14, 33, 24, 15, 34, 25, 16, 35, 26, 17}, @@ -48,7 +52,8 @@ public class RefillHandler { @SubscribeEvent - public static void onDestroyItem(PlayerDestroyItemEvent event) { + public void onDestroyItem(PlayerDestroyItemEvent event) { + System.out.println(event); if (event.entityPlayer == null || event.entityPlayer.worldObj == null || event.entityPlayer.worldObj.isRemote || @@ -56,19 +61,20 @@ public static void onDestroyItem(PlayerDestroyItemEvent event) { return; if (event.original.getItem() instanceof ItemBlock && shouldHandleRefill(event.entityPlayer, event.original)) { - int index = event.entityPlayer.getHeldItem() == null ? event.entityPlayer.inventory.currentItem : 40; + int index = event.entityPlayer.inventory.currentItem; handle(index, event.original, event.entityPlayer, false); } } /** - * Called via asm + * Called via Mixin */ public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) { + System.out.println(brokenItem); if (!PlayerConfig.get(player).enableAutoRefill) return; if (shouldHandleRefill(player, brokenItem)) { - int index = player.getHeldItem() != null ? player.inventory.currentItem : 40; + int index = player.inventory.currentItem; handle(index, brokenItem, player, false); } } @@ -88,14 +94,16 @@ public static boolean shouldHandleRefill(EntityPlayer player, ItemStack brokenIt private BiPredicate similarItemMatcher = (stack, stack2) -> stack.getItem() == stack2.getItem() && stack.getItemDamage() == stack2.getItemDamage(); private BiPredicate exactItemMatcher = RefillHandler::matchTags; - private final int hotbarIndex; - private final IntList slots; - private final ItemStack brokenItem; - private final EntityPlayer player; - private final InventoryPlayer inventory; - private final PlayerConfig playerConfig; - private final boolean swapItems; - private boolean isDamageable = false; + private int hotbarIndex; + private IntList slots; + private ItemStack brokenItem; + private EntityPlayer player; + private InventoryPlayer inventory; + private PlayerConfig playerConfig; + private boolean swapItems; + private boolean isDamageable; + + public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, boolean swapItems) { this.hotbarIndex = hotbarIndex; @@ -107,6 +115,7 @@ public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, this.swapItems = swapItems; } + public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player) { this(hotbarIndex, brokenItem, player, false); } @@ -115,12 +124,13 @@ public boolean handleRefill() { if (brokenItem.getItem() instanceof ItemBlock) { return findItem(false); } else if (brokenItem.isItemStackDamageable()) { - if (gtToolClass != null ) { // && isGTCEuTool(brokenItem)) - exactItemMatcher = (stack, stack2) -> { - if (stack.hasTagCompound() != stack2.hasTagCompound()) return false; - if (!stack.hasTagCompound()) return true; - return OreDictHelper.getGtToolMaterial(stack).equals(OreDictHelper.getGtToolMaterial(stack2)); - }; + System.out.println(isGT5uTool(brokenItem)); + if (isGT5uTool(brokenItem)) { + exactItemMatcher = (stack, stack2) -> { + if (stack.hasTagCompound() != stack2.hasTagCompound()) return false; + if (!stack.hasTagCompound()) return true; + return OreDictHelper.getGtToolMaterial(stack).equals(OreDictHelper.getGtToolMaterial(stack2)); + }; } else { similarItemMatcher = (stack, stack2) -> stack.getItem() == stack2.getItem(); } @@ -131,9 +141,9 @@ public boolean handleRefill() { } } -// private static boolean isGTCEuTool(ItemStack itemStack) { -// return itemStack.getItem() instanceof IGTTool; -// } + private static boolean isGT5uTool(ItemStack itemStack) { + return itemStack.getItem() instanceof MetaGeneratedTool; + } private boolean findItem(boolean exactOnly) { ItemStack firstItemMatch = null; diff --git a/src/main/java/com/cleanroommc/bogosorter/core/BogoSorterTransformer.java b/src/main/java/com/cleanroommc/bogosorter/core/BogoSorterTransformer.java deleted file mode 100644 index e0883b3e..00000000 --- a/src/main/java/com/cleanroommc/bogosorter/core/BogoSorterTransformer.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.cleanroommc.bogosorter.core; - -import com.cleanroommc.bogosorter.core.visitor.EntityPlayerVisitor; -import com.cleanroommc.bogosorter.core.visitor.PIMVisitor; -import net.minecraft.launchwrapper.IClassTransformer; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; - -public class BogoSorterTransformer implements IClassTransformer { - - @Override - public byte[] transform(String name, String transformedName, byte[] classBytes) { - switch (transformedName) { - case PIMVisitor.CLASS_NAME: { - ClassWriter classWriter = new ClassWriter(0); - new ClassReader(classBytes).accept(new PIMVisitor(classWriter), 0); - return classWriter.toByteArray(); - } - case EntityPlayerVisitor.CLASS_NAME: { - ClassWriter classWriter = new ClassWriter(0); - new ClassReader(classBytes).accept(new EntityPlayerVisitor(classWriter), 0); - return classWriter.toByteArray(); - } - } - return classBytes; - } -} diff --git a/src/main/java/com/cleanroommc/bogosorter/core/visitor/EntityPlayerVisitor.java b/src/main/java/com/cleanroommc/bogosorter/core/visitor/EntityPlayerVisitor.java deleted file mode 100644 index ef99f080..00000000 --- a/src/main/java/com/cleanroommc/bogosorter/core/visitor/EntityPlayerVisitor.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.cleanroommc.bogosorter.core.visitor; - -import com.cleanroommc.bogosorter.core.BogoSorterCore; -import com.cleanroommc.bogosorter.core.CatServerHelper; -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; - -public class EntityPlayerVisitor extends ClassVisitor implements Opcodes { - - public static final String CLASS_NAME = "net.minecraft.entity.player.EntityPlayer"; - public static final boolean DEOBF = BogoSorterCore.isDevEnv(); - private static final String METHOD_NAME = DEOBF ? "damageShield" : "func_184590_k"; - private static final String INTERACT_ON_FUNC = DEOBF ? "interactOn" : "func_190775_a"; - private static final String ATTACK_ENTITY_FUNC = DEOBF ? "attackTargetEntityWithCurrentItem" : "func_71059_n"; - private static final String FEF_CLASS = "net/minecraftforge/event/ForgeEventFactory"; - private static final String ON_DESTROY_ITEM_FUNC = "onPlayerDestroyItem"; - - public EntityPlayerVisitor(ClassVisitor cv) { - super(ASM5, cv); - } - - @Override - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); - if (access == ACC_PROTECTED && METHOD_NAME.equals(name) && "(F)V".equals(desc)) { - return new TargetMethodVisitor(mv); - } - if (access == ACC_PUBLIC && INTERACT_ON_FUNC.equals(name)) { - return new InteractOnVisitor(mv); - } - if (access == ACC_PUBLIC && ATTACK_ENTITY_FUNC.equals(name)) { - return new AttackTargetEntityWithCurrentItemVisitor(mv); - } - return mv; - } - - private static class TargetMethodVisitor extends MethodVisitor { - - public TargetMethodVisitor(MethodVisitor mv) { - super(ASM5, mv); - } - - @Override - public void visitFieldInsn(int opcode, String owner, String name, String desc) { - super.visitFieldInsn(opcode, owner, name, desc); - if (opcode == PUTFIELD && "activeItemStack".equals(name)) { - visitVarInsn(ALOAD, 0); - visitVarInsn(ALOAD, 2); - visitVarInsn(ALOAD, 4); - PIMVisitor.visitOnDestroy(this); - BogoSorterCore.LOGGER.info("Applied EntityPlayer damageShield ASM"); - } - } - } - - public static class InteractOnVisitor extends MethodVisitor { - - public InteractOnVisitor(MethodVisitor mv) { - super(ASM5, mv); - } - - @Override - public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { - super.visitMethodInsn(opcode, owner, name, desc, itf); - if ((opcode == INVOKEVIRTUAL && PIMVisitor.PLAYER_CLASS.equals(owner) && PIMVisitor.SET_HELD_ITEM_FUNC.equals(name)) || - (opcode == INVOKESTATIC && FEF_CLASS.equals(owner) && ON_DESTROY_ITEM_FUNC.equals(name))) { - visitVarInsn(ALOAD, 0); - visitVarInsn(ALOAD, 5); - visitVarInsn(ALOAD, 2); - PIMVisitor.visitOnDestroy(this); - BogoSorterCore.LOGGER.info("Applied EntityPlayer interactOn ASM"); - } - } - } - - public static class AttackTargetEntityWithCurrentItemVisitor extends MethodVisitor { - - public AttackTargetEntityWithCurrentItemVisitor(MethodVisitor mv) { - super(ASM5, mv); - } - - @Override - public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { - super.visitMethodInsn(opcode, owner, name, desc, itf); - if (opcode == INVOKEVIRTUAL && PIMVisitor.PLAYER_CLASS.equals(owner) && PIMVisitor.SET_HELD_ITEM_FUNC.equals(name)) { - visitVarInsn(ALOAD, 0); - visitVarInsn(ALOAD, CatServerHelper.isCatServerLoaded() ? 26 : 25); - visitFieldInsn(GETSTATIC, PIMVisitor.HAND_CLASS, "MAIN_HAND", "L" + PIMVisitor.HAND_CLASS + ";"); - PIMVisitor.visitOnDestroy(this); - BogoSorterCore.LOGGER.info("Applied EntityPlayer attackTargetEntityWithCurrentItem ASM"); - } - } - } -} diff --git a/src/main/java/com/cleanroommc/bogosorter/core/visitor/PIMVisitor.java b/src/main/java/com/cleanroommc/bogosorter/core/visitor/PIMVisitor.java deleted file mode 100644 index 0f0b33cf..00000000 --- a/src/main/java/com/cleanroommc/bogosorter/core/visitor/PIMVisitor.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.cleanroommc.bogosorter.core.visitor; - -import com.cleanroommc.bogosorter.core.BogoSorterCore; -import org.objectweb.asm.ClassVisitor; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; - -public class PIMVisitor extends ClassVisitor implements Opcodes { - - public static final String CLASS_NAME = "net.minecraft.server.management.PlayerInteractionManager"; - public static final boolean DEOBF = BogoSorterCore.isDevEnv(); - public static final String METHOD_NAME = DEOBF ? "processRightClick" : "func_187250_a"; - public static final String TRY_HARVEST_BLOCK_FUNC = DEOBF ? "tryHarvestBlock" : "func_180237_b"; - public static final String PLAYER_CLASS = "net/minecraft/entity/player/EntityPlayer"; - public static final String PLAYER_MP_CLASS = "net/minecraft/entity/player/EntityPlayerMP"; - public static final String SET_HELD_ITEM_FUNC = DEOBF ? "setHeldItem" : "func_184611_a"; - public static final String ITEM_STACK_CLASS = "net/minecraft/item/ItemStack"; - public static final String EMPTY = DEOBF ? "EMPTY" : "field_190927_a"; - public static final String REFILL_HANDLER_CLASS = "com/cleanroommc/bogosorter/common/refill/RefillHandler"; - public static final String ON_DESTROY_FUNC = "onDestroyItem"; - public static final String ON_DESTROY_FUNC_DESC = "(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/EnumHand;)V"; - public static final String FEF_CLASS = "net/minecraftforge/event/ForgeEventFactory"; - public static final String ON_DESTROY_ITEM_FUNC = "onPlayerDestroyItem"; - public static final String PIM_CLASS = "net/minecraft/server/management/PlayerInteractionManager"; - public static final String HAND_CLASS = "net/minecraft/util/EnumHand"; - - public PIMVisitor(ClassVisitor cv) { - super(ASM5, cv); - } - - public static void visitOnDestroy(MethodVisitor mv) { - mv.visitMethodInsn(INVOKESTATIC, REFILL_HANDLER_CLASS, ON_DESTROY_FUNC, ON_DESTROY_FUNC_DESC, false); - } - - @Override - public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { - MethodVisitor visitor = super.visitMethod(access, name, desc, signature, exceptions); - if (METHOD_NAME.equals(name)) { - return new TargetMethodVisitor(visitor); - } - if (access == ACC_PUBLIC && TRY_HARVEST_BLOCK_FUNC.equals(name)) { - return new TryHarvestBlockVisitor(visitor); - } - return visitor; - } - - public static class TargetMethodVisitor extends MethodVisitor { - - private boolean inject = false; - - public TargetMethodVisitor(MethodVisitor mv) { - super(ASM5, mv); - } - - @Override - public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { - super.visitMethodInsn(opcode, owner, name, desc, itf); - if (inject && opcode == INVOKEVIRTUAL && PLAYER_CLASS.equals(owner) && SET_HELD_ITEM_FUNC.equals(name)) { - visitVarInsn(ALOAD, 1); - visitVarInsn(ALOAD, 8); - visitVarInsn(ALOAD, 4); - visitOnDestroy(this); - inject = false; - BogoSorterCore.LOGGER.info("Applied PIM processRightClick ASM"); - } - } - - @Override - public void visitFieldInsn(int opcode, String owner, String name, String desc) { - super.visitFieldInsn(opcode, owner, name, desc); - if (opcode == GETSTATIC && ITEM_STACK_CLASS.equals(owner) && EMPTY.equals(name)) { - inject = true; - } - } - } - - public static class TryHarvestBlockVisitor extends MethodVisitor { - - public TryHarvestBlockVisitor(MethodVisitor mv) { - super(ASM5, mv); - } - - @Override - public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { - super.visitMethodInsn(opcode, owner, name, desc, itf); - if (opcode == INVOKESTATIC && FEF_CLASS.equals(owner) && ON_DESTROY_ITEM_FUNC.equals(name)) { - visitVarInsn(ALOAD, 0); - visitFieldInsn(GETFIELD, PIM_CLASS, DEOBF ? "player" : "field_73090_b", "L" + PLAYER_MP_CLASS + ";"); - visitVarInsn(ALOAD, 9); - visitFieldInsn(GETSTATIC, HAND_CLASS, "MAIN_HAND", "L" + HAND_CLASS + ";"); - visitOnDestroy(this); - BogoSorterCore.LOGGER.info("Applied PIM tryHarvestBlock ASM"); - } - } - } -} diff --git a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java index 208fba50..7f32c257 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java @@ -17,7 +17,9 @@ public enum Mixins { Vanilla_BOTH(new Builder(" Vanilla").addTargetedMod(TargetedMod.VANILLA) .setSide(Side.BOTH).setPhase(Phase.EARLY).addMixinClasses( "minecraft.ContainerHorseInventoryMixin", -// "minecraft.MixinEntityPlayer", + "minecraft.MixinEntityPlayer", + "minecraft.MixinItemStack", + "minecraft.ItemInWorldManagerMixin", "minecraft.SlotMixin" )), Vanilla_CLIENT(new Builder(" Vanilla").addTargetedMod(TargetedMod.VANILLA) diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java new file mode 100644 index 00000000..9061366a --- /dev/null +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java @@ -0,0 +1,53 @@ +package com.cleanroommc.bogosorter.mixins.early.minecraft; + + + +import com.cleanroommc.bogosorter.common.refill.RefillHandler; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.item.ItemStack; +import net.minecraft.server.management.ItemInWorldManager; +import net.minecraft.world.World; +import net.minecraftforge.event.entity.player.PlayerInteractEvent; +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.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +@Mixin(ItemInWorldManager.class) +public class ItemInWorldManagerMixin { + + @Shadow + public EntityPlayerMP thisPlayerMP; + + @Inject( + method = "tryUseItem", + at = @At( + value = "INVOKE", + target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after + shift = At.Shift.BY, by = 2 + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void tryUseItem(EntityPlayer p_73085_1_, World p_73085_2_, ItemStack p_73085_3_, CallbackInfoReturnable cir, int i, int j, ItemStack itemstack1) { + RefillHandler.onDestroyItem(this.thisPlayerMP, itemstack1); + } + + + @Inject( + method = "activateBlockOrUseItem", + at = @At( + value = "INVOKE", + target = "net/minecraftforge/event/ForgeEventFactory.onPlayerDestroyItem(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after + shift = At.Shift.AFTER + ), + locals = LocalCapture.CAPTURE_FAILHARD, + remap = false + ) + private void activateBlockOrUseItem(EntityPlayer player, World worldIn, ItemStack stack, int p_73078_4_, int p_73078_5_, int p_73078_6_, int p_73078_7_, float p_73078_8_, float p_73078_9_, float p_73078_10_, CallbackInfoReturnable cir, PlayerInteractEvent event) { + RefillHandler.onDestroyItem(this.thisPlayerMP, stack); + } +} + diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java index e6b1c700..dec01ffd 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java @@ -5,69 +5,63 @@ import com.cleanroommc.bogosorter.core.BogoSorterCore; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; - import net.minecraftforge.event.ForgeEventFactory; 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 org.spongepowered.asm.mixin.injection.callback.LocalCapture; - @Mixin(EntityPlayer.class) - public abstract class MixinEntityPlayer { - @Shadow - private ItemStack itemInUse; - - @Shadow - private int itemInUseCount; + import java.util.Random; - /** +@Mixin(EntityPlayer.class) + public abstract class MixinEntityPlayer { + /** * Inject custom logic into the `onItemUseFinish` method. * We capture the locals, such as `itemstack` and `activeItemStackCopy`, to implement custom logic. */ @Inject( - method = "onItemUseFinish", // Target the `onItemUseFinish` method + method = "onItemUseFinish", at = @At( - value = "INVOKE", // Look for an INVOKE instruction - target = "Lnet/minecraft/entity/player/EntityPlayer;clearItemInUse()V", // The target method to inject after - shift = At.Shift.AFTER // Inject after the target method + value = "INVOKE", + target = "Lnet/minecraft/entity/player/EntityPlayer;clearItemInUse()V", + shift = At.Shift.AFTER ), locals = LocalCapture.CAPTURE_FAILHARD // Capture the local variables ) - private void onItemUseFinish(CallbackInfo ci) { - // Get the current player instance - EntityPlayer player = (EntityPlayer) (Object) this; - - // Ensure that auto-refill is enabled for this player (this could be a config setting) + private void onItemUseFinish(CallbackInfo ci, int i, ItemStack itemstack) { + if (!(bogosorter$EntityPlayer() instanceof EntityPlayer)) return; + EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); if (!PlayerConfig.get(player).enableAutoRefill) return; +// // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) +// if (ItemStack.areItemsEqualIgnoreDurability(activeItemStackCopy, itemstack)) { +// return; +// } - // Capture the locals from the `onItemUseFinish` method in `EntityPlayer` - ItemStack itemstack = this.itemInUse.onFoodEaten(null, player); - itemstack = ForgeEventFactory.onItemUseFinish(player, this.itemInUse, this.itemInUseCount, itemstack); - - // You need to ensure that itemInUse and itemstack are set to appropriate values from the method - // Here, we assume they are captured correctly by the Mixin and set during the execution - - // Check if the item used is the same as the returned itemstack (e.g., no change in item) - if (ItemStack.areItemStacksEqual(this.itemInUse, itemstack)) { - return; - } - - // Handle refill logic (if necessary, based on your own refill handler logic) - if (RefillHandler.shouldHandleRefill(player, itemInUse, true)) { - boolean didSwap = RefillHandler.handle(player.inventory.currentItem, itemInUse, player, false); - - // Perform custom refill logic - new RefillHandler(player.inventory.currentItem, itemInUse, player).handleRefill(); - - // If an item was swapped and is non-null, attempt to add it to the player's inventory + if (RefillHandler.shouldHandleRefill(player, itemstack, true)) { + boolean didSwap = RefillHandler.handle(player.inventory.currentItem, itemstack, player, false); + //new RefillHandler(player.inventory.currentItem, activeItemStackCopy, player).handleRefill(); if (didSwap && itemstack != null) { if (!player.inventory.addItemStackToInventory(itemstack)) { BogoSorterCore.LOGGER.info("Dropping item that does not fit"); - // Drop the item if it doesn't fit in the inventory player.func_146097_a(itemstack, true, false); } } } } + @Inject(method = "destroyCurrentEquippedItem", + at = @At( + value = "INVOKE", + target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after + shift = At.Shift.BY , by = 2 + ), + locals = LocalCapture.CAPTURE_FAILHARD ) + private void destroyCurrentEquippedItem(CallbackInfo ci, ItemStack orig) { + System.out.println(orig); + EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); + RefillHandler.onDestroyItem(player, orig); + } + + private EntityPlayer bogosorter$EntityPlayer() { + return (EntityPlayer) (Object) this; + } } diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinItemStack.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinItemStack.java new file mode 100644 index 00000000..10b9d974 --- /dev/null +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinItemStack.java @@ -0,0 +1,19 @@ +package com.cleanroommc.bogosorter.mixins.early.minecraft; + +import com.cleanroommc.bogosorter.common.refill.DamageHelper; +import net.minecraft.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.Random; + +@Mixin(ItemStack.class) +public abstract class MixinItemStack { + + @Inject(method = "attemptDamageItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;setItemDamage(I)V", shift = At.Shift.AFTER), cancellable = true) + private void damageItemCount(int p_96631_1_, Random p_96631_2_, CallbackInfoReturnable cir) { + DamageHelper.damageItemHook((ItemStack) (Object) this); + } +} From c0192a4206e57852b13d8110cfc4b8e8ff2ef6e1 Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 03:26:50 +0300 Subject: [PATCH 02/10] Remove Debugging --- .../com/cleanroommc/bogosorter/common/refill/RefillHandler.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 52a28f8d..c5bf3a4b 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -53,7 +53,6 @@ public RefillHandler(){ @SubscribeEvent public void onDestroyItem(PlayerDestroyItemEvent event) { - System.out.println(event); if (event.entityPlayer == null || event.entityPlayer.worldObj == null || event.entityPlayer.worldObj.isRemote || @@ -70,7 +69,6 @@ public void onDestroyItem(PlayerDestroyItemEvent event) { * Called via Mixin */ public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) { - System.out.println(brokenItem); if (!PlayerConfig.get(player).enableAutoRefill) return; if (shouldHandleRefill(player, brokenItem)) { From 0de24e5bc35d37849fbf4ea8825a0a8050028bc9 Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 03:26:50 +0300 Subject: [PATCH 03/10] Remove Debugging --- .../com/cleanroommc/bogosorter/common/refill/RefillHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index c5bf3a4b..305147d5 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -122,7 +122,6 @@ public boolean handleRefill() { if (brokenItem.getItem() instanceof ItemBlock) { return findItem(false); } else if (brokenItem.isItemStackDamageable()) { - System.out.println(isGT5uTool(brokenItem)); if (isGT5uTool(brokenItem)) { exactItemMatcher = (stack, stack2) -> { if (stack.hasTagCompound() != stack2.hasTagCompound()) return false; From 41c9fd127c9854effd1965a5faf1cbe3c9c21a0c Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 07:42:53 +0300 Subject: [PATCH 04/10] Fixed Ghost item --- .../cleanroommc/bogosorter/common/refill/RefillHandler.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 305147d5..364e5be6 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -242,12 +242,10 @@ private void setAndSyncSlot(int index, ItemStack item) { inventory.armorInventory[index - 36] = item; slot += 5; } -// else { -// inventory.offHandInventory.set(0, item); -// slot = 45; -// } if (item != null) { player.inventoryContainer.inventoryItemStacks.set(slot, null); + } else { + player.inventoryContainer.detectAndSendChanges(); } } From f8dd76a9d8fcd26148cec2090dd8a04b4e272b0b Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 08:28:28 +0300 Subject: [PATCH 05/10] Remove activateBlockOrUseItem --- .../early/minecraft/ItemInWorldManagerMixin.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java index 9061366a..b30ed89f 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java @@ -34,20 +34,5 @@ public class ItemInWorldManagerMixin { private void tryUseItem(EntityPlayer p_73085_1_, World p_73085_2_, ItemStack p_73085_3_, CallbackInfoReturnable cir, int i, int j, ItemStack itemstack1) { RefillHandler.onDestroyItem(this.thisPlayerMP, itemstack1); } - - - @Inject( - method = "activateBlockOrUseItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/ForgeEventFactory.onPlayerDestroyItem(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.AFTER - ), - locals = LocalCapture.CAPTURE_FAILHARD, - remap = false - ) - private void activateBlockOrUseItem(EntityPlayer player, World worldIn, ItemStack stack, int p_73078_4_, int p_73078_5_, int p_73078_6_, int p_73078_7_, float p_73078_8_, float p_73078_9_, float p_73078_10_, CallbackInfoReturnable cir, PlayerInteractEvent event) { - RefillHandler.onDestroyItem(this.thisPlayerMP, stack); - } } From 6698bf3ae14d2a3a50d1c0125629bf3baf52a3cc Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 09:03:26 +0300 Subject: [PATCH 06/10] Remove ItemInWorldManagerMixin --- .../common/refill/RefillHandler.java | 13 +------ .../bogosorter/mixinplugin/Mixins.java | 1 - .../minecraft/ItemInWorldManagerMixin.java | 38 ------------------- .../early/minecraft/MixinEntityPlayer.java | 16 +------- 4 files changed, 3 insertions(+), 65 deletions(-) delete mode 100644 src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 364e5be6..fb69a7ef 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -59,23 +59,12 @@ public void onDestroyItem(PlayerDestroyItemEvent event) { !PlayerConfig.get(event.entityPlayer).enableAutoRefill) return; - if (event.original.getItem() instanceof ItemBlock && shouldHandleRefill(event.entityPlayer, event.original)) { + if (shouldHandleRefill(event.entityPlayer, event.original)) { int index = event.entityPlayer.inventory.currentItem; handle(index, event.original, event.entityPlayer, false); } } - /** - * Called via Mixin - */ - public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) { - if (!PlayerConfig.get(player).enableAutoRefill) return; - - if (shouldHandleRefill(player, brokenItem)) { - int index = player.inventory.currentItem; - handle(index, brokenItem, player, false); - } - } public static boolean handle(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, boolean swap) { return new RefillHandler(hotbarIndex, brokenItem, player, swap).handleRefill(); diff --git a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java index 7f32c257..a0c949f8 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java @@ -19,7 +19,6 @@ public enum Mixins { "minecraft.ContainerHorseInventoryMixin", "minecraft.MixinEntityPlayer", "minecraft.MixinItemStack", - "minecraft.ItemInWorldManagerMixin", "minecraft.SlotMixin" )), Vanilla_CLIENT(new Builder(" Vanilla").addTargetedMod(TargetedMod.VANILLA) diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java deleted file mode 100644 index b30ed89f..00000000 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.cleanroommc.bogosorter.mixins.early.minecraft; - - - -import com.cleanroommc.bogosorter.common.refill.RefillHandler; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraft.server.management.ItemInWorldManager; -import net.minecraft.world.World; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -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.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(ItemInWorldManager.class) -public class ItemInWorldManagerMixin { - - @Shadow - public EntityPlayerMP thisPlayerMP; - - @Inject( - method = "tryUseItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.BY, by = 2 - ), - locals = LocalCapture.CAPTURE_FAILHARD - ) - private void tryUseItem(EntityPlayer p_73085_1_, World p_73085_2_, ItemStack p_73085_3_, CallbackInfoReturnable cir, int i, int j, ItemStack itemstack1) { - RefillHandler.onDestroyItem(this.thisPlayerMP, itemstack1); - } -} - diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java index dec01ffd..69c22bfd 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java @@ -32,8 +32,8 @@ private void onItemUseFinish(CallbackInfo ci, int i, ItemStack itemstack) { if (!(bogosorter$EntityPlayer() instanceof EntityPlayer)) return; EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); if (!PlayerConfig.get(player).enableAutoRefill) return; -// // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) -// if (ItemStack.areItemsEqualIgnoreDurability(activeItemStackCopy, itemstack)) { + // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) +// if (ItemStack.areItemStackTagsEqual(activeItemStackCopy, itemstack)) { // return; // } @@ -48,18 +48,6 @@ private void onItemUseFinish(CallbackInfo ci, int i, ItemStack itemstack) { } } } - @Inject(method = "destroyCurrentEquippedItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.BY , by = 2 - ), - locals = LocalCapture.CAPTURE_FAILHARD ) - private void destroyCurrentEquippedItem(CallbackInfo ci, ItemStack orig) { - System.out.println(orig); - EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); - RefillHandler.onDestroyItem(player, orig); - } private EntityPlayer bogosorter$EntityPlayer() { return (EntityPlayer) (Object) this; From b8411c84c44d287452bb9d26e09c807d9b31ecc8 Mon Sep 17 00:00:00 2001 From: Caedis Date: Sat, 21 Dec 2024 00:08:16 -0600 Subject: [PATCH 07/10] Fix refilling bugs with tools and potions --- .../common/refill/RefillHandler.java | 18 +----- .../bogosorter/mixinplugin/Mixins.java | 1 - .../minecraft/ItemInWorldManagerMixin.java | 53 ---------------- .../early/minecraft/MixinEntityPlayer.java | 60 ++++++++----------- 4 files changed, 28 insertions(+), 104 deletions(-) delete mode 100644 src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 364e5be6..b4771e8b 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -14,6 +14,7 @@ import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; +import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent; @@ -59,24 +60,12 @@ public void onDestroyItem(PlayerDestroyItemEvent event) { !PlayerConfig.get(event.entityPlayer).enableAutoRefill) return; - if (event.original.getItem() instanceof ItemBlock && shouldHandleRefill(event.entityPlayer, event.original)) { + if (event.original.getItem() != null && shouldHandleRefill(event.entityPlayer, event.original)) { int index = event.entityPlayer.inventory.currentItem; handle(index, event.original, event.entityPlayer, false); } } - /** - * Called via Mixin - */ - public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) { - if (!PlayerConfig.get(player).enableAutoRefill) return; - - if (shouldHandleRefill(player, brokenItem)) { - int index = player.inventory.currentItem; - handle(index, brokenItem, player, false); - } - } - public static boolean handle(int hotbarIndex, ItemStack brokenItem, EntityPlayer player, boolean swap) { return new RefillHandler(hotbarIndex, brokenItem, player, swap).handleRefill(); } @@ -219,6 +208,7 @@ private void refillItem(ItemStack refill, int refillIndex) { if (!this.swapItems) current = getItem(this.hotbarIndex); setAndSyncSlot(hotbarIndex, refill.copy()); setAndSyncSlot(refillIndex, swapItems ? brokenItem.copy() : null); + player.inventoryContainer.detectAndSendChanges(); if (current != null) { // the broken item replaced itself with something // insert the item into another slot to prevent it from being lost @@ -244,8 +234,6 @@ private void setAndSyncSlot(int index, ItemStack item) { } if (item != null) { player.inventoryContainer.inventoryItemStacks.set(slot, null); - } else { - player.inventoryContainer.detectAndSendChanges(); } } diff --git a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java index 7f32c257..a0c949f8 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java @@ -19,7 +19,6 @@ public enum Mixins { "minecraft.ContainerHorseInventoryMixin", "minecraft.MixinEntityPlayer", "minecraft.MixinItemStack", - "minecraft.ItemInWorldManagerMixin", "minecraft.SlotMixin" )), Vanilla_CLIENT(new Builder(" Vanilla").addTargetedMod(TargetedMod.VANILLA) diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java deleted file mode 100644 index 9061366a..00000000 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/ItemInWorldManagerMixin.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.cleanroommc.bogosorter.mixins.early.minecraft; - - - -import com.cleanroommc.bogosorter.common.refill.RefillHandler; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemStack; -import net.minecraft.server.management.ItemInWorldManager; -import net.minecraft.world.World; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -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.CallbackInfoReturnable; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - -@Mixin(ItemInWorldManager.class) -public class ItemInWorldManagerMixin { - - @Shadow - public EntityPlayerMP thisPlayerMP; - - @Inject( - method = "tryUseItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.BY, by = 2 - ), - locals = LocalCapture.CAPTURE_FAILHARD - ) - private void tryUseItem(EntityPlayer p_73085_1_, World p_73085_2_, ItemStack p_73085_3_, CallbackInfoReturnable cir, int i, int j, ItemStack itemstack1) { - RefillHandler.onDestroyItem(this.thisPlayerMP, itemstack1); - } - - - @Inject( - method = "activateBlockOrUseItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/ForgeEventFactory.onPlayerDestroyItem(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.AFTER - ), - locals = LocalCapture.CAPTURE_FAILHARD, - remap = false - ) - private void activateBlockOrUseItem(EntityPlayer player, World worldIn, ItemStack stack, int p_73078_4_, int p_73078_5_, int p_73078_6_, int p_73078_7_, float p_73078_8_, float p_73078_9_, float p_73078_10_, CallbackInfoReturnable cir, PlayerInteractEvent event) { - RefillHandler.onDestroyItem(this.thisPlayerMP, stack); - } -} - diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java index dec01ffd..dda819ec 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java @@ -1,20 +1,24 @@ package com.cleanroommc.bogosorter.mixins.early.minecraft;// package com.cleanroommc.bogosorter.core.mixins.early.minecraft; - import com.cleanroommc.bogosorter.common.config.PlayerConfig; - import com.cleanroommc.bogosorter.common.refill.RefillHandler; - import com.cleanroommc.bogosorter.core.BogoSorterCore; - import net.minecraft.entity.player.EntityPlayer; - import net.minecraft.item.ItemStack; - import org.spongepowered.asm.mixin.Mixin; - import org.spongepowered.asm.mixin.injection.At; - import org.spongepowered.asm.mixin.injection.Inject; - import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - - import java.util.Random; +import com.cleanroommc.bogosorter.common.config.PlayerConfig; +import com.cleanroommc.bogosorter.common.refill.RefillHandler; +import com.cleanroommc.bogosorter.core.BogoSorterCore; +import com.llamalad7.mixinextras.sugar.Local; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +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; @Mixin(EntityPlayer.class) public abstract class MixinEntityPlayer { + + @Shadow + private ItemStack itemInUse; + + /** * Inject custom logic into the `onItemUseFinish` method. * We capture the locals, such as `itemstack` and `activeItemStackCopy`, to implement custom logic. @@ -24,42 +28,28 @@ public abstract class MixinEntityPlayer { at = @At( value = "INVOKE", target = "Lnet/minecraft/entity/player/EntityPlayer;clearItemInUse()V", - shift = At.Shift.AFTER - ), - locals = LocalCapture.CAPTURE_FAILHARD // Capture the local variables + shift = At.Shift.BEFORE + ) ) - private void onItemUseFinish(CallbackInfo ci, int i, ItemStack itemstack) { - if (!(bogosorter$EntityPlayer() instanceof EntityPlayer)) return; - EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); + private void onItemUseFinish(CallbackInfo ci, @Local(name = "itemstack") ItemStack returnedItem) { + EntityPlayer player = bogosorter$EntityPlayer(); if (!PlayerConfig.get(player).enableAutoRefill) return; // // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) // if (ItemStack.areItemsEqualIgnoreDurability(activeItemStackCopy, itemstack)) { // return; // } - if (RefillHandler.shouldHandleRefill(player, itemstack, true)) { - boolean didSwap = RefillHandler.handle(player.inventory.currentItem, itemstack, player, false); + if (RefillHandler.shouldHandleRefill(player, itemInUse, true)) { + boolean didSwap = RefillHandler.handle(player.inventory.currentItem, itemInUse, player, false); //new RefillHandler(player.inventory.currentItem, activeItemStackCopy, player).handleRefill(); - if (didSwap && itemstack != null) { - if (!player.inventory.addItemStackToInventory(itemstack)) { + if (didSwap && returnedItem != null) { + if (!player.inventory.addItemStackToInventory(returnedItem)) { BogoSorterCore.LOGGER.info("Dropping item that does not fit"); - player.func_146097_a(itemstack, true, false); + player.func_146097_a(returnedItem, true, false); } } } } - @Inject(method = "destroyCurrentEquippedItem", - at = @At( - value = "INVOKE", - target = "net/minecraftforge/event/entity/player/PlayerDestroyItemEvent.(Lnet/minecraft/entity/player/EntityPlayer;Lnet/minecraft/item/ItemStack;)V", // The target method to inject after - shift = At.Shift.BY , by = 2 - ), - locals = LocalCapture.CAPTURE_FAILHARD ) - private void destroyCurrentEquippedItem(CallbackInfo ci, ItemStack orig) { - System.out.println(orig); - EntityPlayer player = (EntityPlayer) bogosorter$EntityPlayer(); - RefillHandler.onDestroyItem(player, orig); - } private EntityPlayer bogosorter$EntityPlayer() { return (EntityPlayer) (Object) this; From 96530b153c9892b7fc2e0384818318332439228c Mon Sep 17 00:00:00 2001 From: Caedis Date: Sat, 21 Dec 2024 01:34:54 -0600 Subject: [PATCH 08/10] Fix duping --- .../cleanroommc/bogosorter/ShortcutHandler.java | 17 ++++++++++++----- .../early/minecraft/MixinEntityPlayer.java | 8 +++++--- .../ContainerAvaritiaddonsChestMixin.java | 8 ++++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/ShortcutHandler.java b/src/main/java/com/cleanroommc/bogosorter/ShortcutHandler.java index 2c82720c..90d8cc82 100644 --- a/src/main/java/com/cleanroommc/bogosorter/ShortcutHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/ShortcutHandler.java @@ -63,6 +63,9 @@ public static void moveItemStack(EntityPlayer player, Container container, ISlot if (toInsert == null) { toInsert = stack.copy(); toInsert.stackSize -= (amount); + if (toInsert.stackSize == 0){ + toInsert = null; + } slot.bogo$putStack(toInsert); // needed for crafting tables slot.bogo$onSlotChanged(stack, toInsert); @@ -100,9 +103,13 @@ public static void moveAllItems(EntityPlayer player, Container container, ISlot continue; ItemStack copy = stackInSlot.copy(); ItemStack remainder = BogoSortAPI.insert(container, otherSlots.getSlots(), copy); - int inserted = stackInSlot.stackSize - remainder.stackSize; - if (inserted > 0) { - slot1.bogo$putStack(remainder.stackSize > 0 ? remainder : null); + if (remainder == null){ + slot1.bogo$putStack(null); + } else { + int inserted = stackInSlot.stackSize - remainder.stackSize; + if (inserted > 0) { + slot1.bogo$putStack(remainder.stackSize > 0 ? remainder : null); + } } } } @@ -150,7 +157,7 @@ public static ItemStack insert(ISlot slot, ItemStack stack, boolean emptyOnly) { newStack.stackSize = (amount); stack.stackSize -= (amount); slot.bogo$putStack(newStack); - return stack == null ? null : stack; + return stack.stackSize == 0 ? null : stack; } if (stackInSlot != null && ItemHandlerHelper.canItemStacksStack(stackInSlot, stack)) { int amount = Math.min(slot.bogo$getItemStackLimit(stackInSlot), Math.min(stack.stackSize, slot.bogo$getMaxStackSize(stack) - stackInSlot.stackSize)); @@ -158,7 +165,7 @@ public static ItemStack insert(ISlot slot, ItemStack stack, boolean emptyOnly) { stack.stackSize -=(amount); stackInSlot.stackSize +=(amount); slot.bogo$putStack(stackInSlot); - return stack == null ? null : stack; + return stack.stackSize == 0 ? null : stack; } return stack; } diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java index e4c3739d..12e68aa2 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/early/minecraft/MixinEntityPlayer.java @@ -34,10 +34,12 @@ public abstract class MixinEntityPlayer { private void onItemUseFinish(CallbackInfo ci, @Local(name = "itemstack") ItemStack returnedItem) { EntityPlayer player = bogosorter$EntityPlayer(); if (!PlayerConfig.get(player).enableAutoRefill) return; + // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) -// if (ItemStack.areItemStackTagsEqual(activeItemStackCopy, itemstack)) { -// return; -// } + // bug: once it empties, it wont refill with the same type + if (returnedItem != null && (returnedItem.getItem() == itemInUse.getItem())){ + return; + } if (RefillHandler.shouldHandleRefill(player, itemInUse, true)) { boolean didSwap = RefillHandler.handle(player.inventory.currentItem, itemInUse, player, false); diff --git a/src/main/java/com/cleanroommc/bogosorter/mixins/late/avaritiaddons/ContainerAvaritiaddonsChestMixin.java b/src/main/java/com/cleanroommc/bogosorter/mixins/late/avaritiaddons/ContainerAvaritiaddonsChestMixin.java index 5212b03a..d416521c 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixins/late/avaritiaddons/ContainerAvaritiaddonsChestMixin.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixins/late/avaritiaddons/ContainerAvaritiaddonsChestMixin.java @@ -40,8 +40,12 @@ public void transferItem(EntityPlayer entityPlayer, int slotNumber, CallbackInfo } toInsert = BogoSortAPI.insert(container, otherSlots.getSlots(), toInsert); - stack.stackSize -= (amount - toInsert.stackSize); - slot.bogo$putStack(toInsert.stackSize > 0 ? toInsert : null); + if (toInsert == null){ + slot.bogo$putStack(null); + } else { + stack.stackSize -= (amount - toInsert.stackSize); + slot.bogo$putStack(toInsert.stackSize > 0 ? toInsert : null); + } cir.setReturnValue(toInsert); } cir.setReturnValue(null); From f06225643cae4a37f85e8d03d50e9e78a4cd05cc Mon Sep 17 00:00:00 2001 From: Abdulaziz Date: Sat, 21 Dec 2024 11:00:26 +0300 Subject: [PATCH 09/10] GT tools are "Working" --- .../com/cleanroommc/bogosorter/common/OreDictHelper.java | 7 ++++--- .../bogosorter/common/refill/RefillHandler.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cleanroommc/bogosorter/common/OreDictHelper.java b/src/main/java/com/cleanroommc/bogosorter/common/OreDictHelper.java index 483e6b7a..608d05e8 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/OreDictHelper.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/OreDictHelper.java @@ -87,12 +87,13 @@ public static String getMaterial(ItemStack item) { @Optional.Method(modid = "gregtech") @NotNull public static String getGtToolMaterial(ItemStack itemStack) { - NBTTagCompound statsTag = itemStack.stackTagCompound.getCompoundTag("GT.Tool"); + NBTTagCompound statsTag = itemStack.stackTagCompound.getCompoundTag("GT.ToolStats"); + System.out.println(statsTag); if (statsTag == null) { return ""; } - if (statsTag.hasKey("Material")) { - return statsTag.getString("Material"); + if (statsTag.hasKey("PrimaryMaterial")) { + return statsTag.getString("PrimaryMaterial"); } return ""; } diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index b4771e8b..9f91abda 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -110,7 +110,7 @@ public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player) public boolean handleRefill() { if (brokenItem.getItem() instanceof ItemBlock) { return findItem(false); - } else if (brokenItem.isItemStackDamageable()) { + } else if (brokenItem.isItemStackDamageable() || brokenItem.getItem() instanceof Item) { if (isGT5uTool(brokenItem)) { exactItemMatcher = (stack, stack2) -> { if (stack.hasTagCompound() != stack2.hasTagCompound()) return false; From ab463fa5d8afd22304ad0bb449e51af89614ab4e Mon Sep 17 00:00:00 2001 From: Caedis Date: Sat, 21 Dec 2024 13:59:57 -0600 Subject: [PATCH 10/10] Fix gt tool refilling --- dependencies.gradle | 5 +++-- .../cleanroommc/bogosorter/common/refill/RefillHandler.java | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 668acde7..bc1a390f 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -50,12 +50,13 @@ dependencies { compileOnly("com.github.GTNewHorizons:EnderIO:2.8.22:dev") compileOnly("com.github.GTNewHorizons:ironchest:6.0.87:dev") compileOnly("com.github.GTNewHorizons:Avaritiaddons:1.8.4-GTNH:dev") - compileOnly("com.github.GTNewHorizons:CookingForBlockheads:1.3.7-GTNH:dev") + compileOnly("com.github.GTNewHorizons:CookingForBlockheads:1.3.8-GTNH:dev") compileOnly("com.github.GTNewHorizons:ForestryMC:4.10.0:dev") compileOnly("com.github.GTNewHorizons:Draconic-Evolution:1.3.14-GTNH:dev") compileOnly("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-503-GTNH:dev") compileOnly("com.github.GTNewHorizons:EnderStorage:1.6.4:dev") - devOnlyNonPublishable("com.github.GTNewHorizons:GT5-Unofficial:5.09.51.13:dev") + devOnlyNonPublishable("com.github.GTNewHorizons:GT5-Unofficial:5.09.51.15:dev") + runtimeOnlyNonPublishable("com.github.GTNewHorizons:Botania:1.12.1-GTNH:dev") diff --git a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java index 9f91abda..78c58e6c 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/refill/RefillHandler.java @@ -6,6 +6,7 @@ import com.cleanroommc.bogosorter.common.network.NetworkUtils; import com.cleanroommc.bogosorter.common.network.SRefillSound; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gregtech.api.items.GTGenericItem; import gregtech.api.items.MetaGeneratedTool; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; @@ -110,8 +111,8 @@ public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player) public boolean handleRefill() { if (brokenItem.getItem() instanceof ItemBlock) { return findItem(false); - } else if (brokenItem.isItemStackDamageable() || brokenItem.getItem() instanceof Item) { - if (isGT5uTool(brokenItem)) { + } else if (brokenItem.isItemStackDamageable() || brokenItem.getItem() instanceof GTGenericItem) { + if (brokenItem.getItem() instanceof MetaGeneratedTool) { exactItemMatcher = (stack, stack2) -> { if (stack.hasTagCompound() != stack2.hasTagCompound()) return false; if (!stack.hasTagCompound()) return true;