diff --git a/dependencies.gradle b/dependencies.gradle index 55e717b6..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") - compileOnly("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/BogoSorter.java b/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java index 74934121..c2634e3d 100644 --- a/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java +++ b/src/main/java/com/cleanroommc/bogosorter/BogoSorter.java @@ -61,7 +61,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/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/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/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..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,8 @@ 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; import it.unimi.dsi.fastutil.ints.IntListIterator; @@ -13,6 +15,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; @@ -22,18 +25,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,31 +54,19 @@ public class RefillHandler { @SubscribeEvent - public static void onDestroyItem(PlayerDestroyItemEvent event) { + public void onDestroyItem(PlayerDestroyItemEvent event) { if (event.entityPlayer == null || event.entityPlayer.worldObj == null || event.entityPlayer.worldObj.isRemote || !PlayerConfig.get(event.entityPlayer).enableAutoRefill) return; - if (event.original.getItem() instanceof ItemBlock && shouldHandleRefill(event.entityPlayer, event.original)) { - int index = event.entityPlayer.getHeldItem() == null ? event.entityPlayer.inventory.currentItem : 40; + 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 asm - */ - public static void onDestroyItem(EntityPlayer player, ItemStack brokenItem) { - if (!PlayerConfig.get(player).enableAutoRefill) return; - - if (shouldHandleRefill(player, brokenItem)) { - int index = player.getHeldItem() != null ? player.inventory.currentItem : 40; - 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(); } @@ -88,14 +82,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 +103,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); } @@ -114,13 +111,13 @@ public RefillHandler(int hotbarIndex, ItemStack brokenItem, EntityPlayer player) 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)); - }; + } 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; + return OreDictHelper.getGtToolMaterial(stack).equals(OreDictHelper.getGtToolMaterial(stack2)); + }; } else { similarItemMatcher = (stack, stack2) -> stack.getItem() == stack2.getItem(); } @@ -131,9 +128,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; @@ -212,6 +209,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 @@ -235,10 +233,6 @@ 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); } 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..a0c949f8 100644 --- a/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java +++ b/src/main/java/com/cleanroommc/bogosorter/mixinplugin/Mixins.java @@ -17,7 +17,8 @@ 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.SlotMixin" )), Vanilla_CLIENT(new Builder(" Vanilla").addTargetedMod(TargetedMod.VANILLA) 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..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 @@ -1,73 +1,59 @@ 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 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) +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; - @Shadow - private int itemInUseCount; + @Shadow + private ItemStack itemInUse; + - /** + /** * 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 - ), - locals = LocalCapture.CAPTURE_FAILHARD // Capture the local variables + value = "INVOKE", + target = "Lnet/minecraft/entity/player/EntityPlayer;clearItemInUse()V", + shift = At.Shift.BEFORE + ) ) - 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, @Local(name = "itemstack") ItemStack returnedItem) { + EntityPlayer player = bogosorter$EntityPlayer(); if (!PlayerConfig.get(player).enableAutoRefill) 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)) { + // used in cases where a modded item returns itself with a different durability (AA coffee, Botania Vials, etc) + // bug: once it empties, it wont refill with the same type + if (returnedItem != null && (returnedItem.getItem() == itemInUse.getItem())){ 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 (didSwap && itemstack != null) { - if (!player.inventory.addItemStackToInventory(itemstack)) { + //new RefillHandler(player.inventory.currentItem, activeItemStackCopy, player).handleRefill(); + if (didSwap && returnedItem != null) { + if (!player.inventory.addItemStackToInventory(returnedItem)) { 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); + player.func_146097_a(returnedItem, true, false); } } } } + + 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); + } +} 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);