From 9f2ae5b34c08f8fab9c52c26f56df399229a2435 Mon Sep 17 00:00:00 2001 From: DoKM Date: Sat, 7 Aug 2021 21:01:12 +0200 Subject: Fix being able to drop slot locked items --- .../notenoughupdates/miscfeatures/SlotLocking.java | 32 ++++++++++++++++++++++ .../mixins/MixinEntityPlayerSP.java | 2 +- .../mixins/MixinInventoryPlayer.java | 12 +++++++- .../notenoughupdates/mixins/MixinMinecraft.java | 11 +++++++- .../options/seperateSections/SlotLocking.java | 13 +++++++++ src/main/resources/mixins.notenoughupdates.json | 1 + 6 files changed, 68 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java index 1925811d..256cf03f 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/SlotLocking.java @@ -26,6 +26,7 @@ import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.event.GuiScreenEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; import org.apache.commons.lang3.tuple.Triple; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; @@ -87,6 +88,36 @@ public class SlotLocking { } } + public void changedSlot(int slotNumber){ + int pingModifier = NotEnoughUpdates.INSTANCE.config.slotLocking.slotLockSwapDelay; + if(pingModifier == 0){ return; }; + long currentTimeMilis = System.currentTimeMillis(); + + for (int i = 0; i < slotChanges.length; i++) { + if(i != slotNumber && slotChanges[i] != 0 && slotChanges[i] < (currentTimeMilis+pingModifier)){ + slotChanges[i] = 0; + } + } + slotChanges[slotNumber] = currentTimeMilis; + } + + public boolean isSwapedSlotLocked(){ + int pingModifier = NotEnoughUpdates.INSTANCE.config.slotLocking.slotLockSwapDelay; + if(pingModifier == 0){ return false; }; + long currentTimeMilis = System.currentTimeMillis(); + + for (int i = 0; i < slotChanges.length; i++) { + if(slotChanges[i] != 0 && slotChanges[i] < (currentTimeMilis+pingModifier)){ + if(isSlotIndexLocked(i)){ + return true; + } + } + } + return false; + } + + private long[] slotChanges = new long[9]; + public void saveConfig(File file) { try { file.createNewFile(); @@ -576,5 +607,6 @@ public class SlotLocking { return locked != null && (locked.locked || (NotEnoughUpdates.INSTANCE.config.slotLocking.bindingAlsoLocks && locked.boundTo != -1)); } + } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinEntityPlayerSP.java b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinEntityPlayerSP.java index f82cc88f..35ee3a63 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinEntityPlayerSP.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinEntityPlayerSP.java @@ -22,7 +22,7 @@ public class MixinEntityPlayerSP { } int slot = Minecraft.getMinecraft().thePlayer.inventory.currentItem; - if(SlotLocking.getInstance().isSlotIndexLocked(slot)) { + if(SlotLocking.getInstance().isSlotIndexLocked(slot) || SlotLocking.getInstance().isSwapedSlotLocked()) { ci.cancel(); Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED+ "NotEnoughUpdates has prevented you from dropping that locked item!")); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinInventoryPlayer.java b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinInventoryPlayer.java index d31ab00a..f60a4839 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinInventoryPlayer.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinInventoryPlayer.java @@ -1,5 +1,6 @@ package io.github.moulberry.notenoughupdates.mixins; +import io.github.moulberry.notenoughupdates.miscfeatures.SlotLocking; import io.github.moulberry.notenoughupdates.miscgui.InventoryStorageSelector; import net.minecraft.entity.player.InventoryPlayer; import org.spongepowered.asm.mixin.Mixin; @@ -11,10 +12,19 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinInventoryPlayer { @Inject(method="changeCurrentItem", at=@At("RETURN")) - public void changeCurrentItem(int direction, CallbackInfo ci) { + public void changeCurrentItemReturn(int direction, CallbackInfo ci) { InventoryPlayer $this = (InventoryPlayer)(Object)this; $this.currentItem = InventoryStorageSelector.getInstance().onScroll(direction, $this.currentItem); + + + } + + @Inject(method="changeCurrentItem", at=@At("HEAD")) + public void changeCurrentItemHead(int direction, CallbackInfo ci) { + InventoryPlayer $this = (InventoryPlayer)(Object)this; + + SlotLocking.getInstance().changedSlot($this.currentItem); } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinMinecraft.java b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinMinecraft.java index 81ab8ec0..e2b4a156 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinMinecraft.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/mixins/MixinMinecraft.java @@ -1,9 +1,12 @@ package io.github.moulberry.notenoughupdates.mixins; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.miscfeatures.SlotLocking; + import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.renderer.EntityRenderer; +import org.spongepowered.asm.lib.Opcodes; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -14,7 +17,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(Minecraft.class) public class MixinMinecraft { - @Shadow public WorldClient theWorld; + //Commented as they were'nt being loaded before +/* @Shadow public WorldClient theWorld; @Shadow public EntityRenderer entityRenderer; @@ -33,6 +37,11 @@ public class MixinMinecraft { @Redirect(method="loadWorld(Lnet/minecraft/client/multiplayer/WorldClient;Ljava/lang/String;)V", at=@At(value = "INVOKE", target = "Ljava/lang/System;gc()V")) public void loadWorld_gc() { + }*/ + + @Inject(method = "runTick", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/player/InventoryPlayer;currentItem:I", opcode = Opcodes.PUTFIELD)) + public void currentItemMixin(CallbackInfo ci){ + SlotLocking.getInstance().changedSlot(Minecraft.getMinecraft().thePlayer.inventory.currentItem); } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/SlotLocking.java b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/SlotLocking.java index 3da885a0..98358d83 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/SlotLocking.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/seperateSections/SlotLocking.java @@ -49,6 +49,19 @@ public class SlotLocking { @ConfigEditorBoolean public boolean lockSlotsInTrade = true; + @Expose + @ConfigOption( + name = "Item Swap drop delay", + desc = "Set the delay between swapping to another item and being able to drop it.\n"+ + "This is to fix a bug that allowed you to drop slot locked items." + ) + @ConfigEditorSlider( + minValue = 0, + maxValue = 500, + minStep = 5 + ) + public int slotLockSwapDelay = 100; + @Expose @ConfigOption( name = "Slot Lock Sound", diff --git a/src/main/resources/mixins.notenoughupdates.json b/src/main/resources/mixins.notenoughupdates.json index 0d415976..f82f6311 100644 --- a/src/main/resources/mixins.notenoughupdates.json +++ b/src/main/resources/mixins.notenoughupdates.json @@ -3,6 +3,7 @@ "refmap": "mixins.notenoughupdates.refmap.json", "compatibilityLevel": "JAVA_8", "mixins": [ + "MixinMinecraft", "MixinAbstractClientPlayer", "MixinContainer", "MixinEffectRenderer", -- cgit