From e848d808f51ad595cabae45c158b9a3272e56d0f Mon Sep 17 00:00:00 2001 From: TRMMax <48479483+TRMMax@users.noreply.github.com> Date: Fri, 28 Oct 2022 00:38:06 +0200 Subject: [PATCH] quicknum & ItemUtil.setContainerItem fix quicknum now works in all inventories, and ItemUtil.setContainerItem now works in all slots. --- .../mod/config/impl/MiscellaneousGroup.java | 1 + .../render/MAbstractContainerScreen.java | 13 ++ .../mod/mixin/render/MMouseHandler.java | 179 +++++++++--------- .../homchom/recode/sys/util/ItemUtil.java | 36 ++-- .../resources/assets/recode/lang/en_us.json | 4 +- src/main/resources/recodeLegacy.mixins.json | 1 + 6 files changed, 133 insertions(+), 101 deletions(-) create mode 100644 src/main/java/io/github/homchom/recode/mod/mixin/render/MAbstractContainerScreen.java diff --git a/src/main/java/io/github/homchom/recode/mod/config/impl/MiscellaneousGroup.java b/src/main/java/io/github/homchom/recode/mod/config/impl/MiscellaneousGroup.java index fe5ec126..5513bd58 100644 --- a/src/main/java/io/github/homchom/recode/mod/config/impl/MiscellaneousGroup.java +++ b/src/main/java/io/github/homchom/recode/mod/config/impl/MiscellaneousGroup.java @@ -27,6 +27,7 @@ public void initialize() { quickNum.register(new DoubleSetting("quicknumPrimaryAmount", 1.0)); quickNum.register(new DoubleSetting("quicknumSecondaryAmount", 10d)); quickNum.register(new DoubleSetting("quicknumTertiaryAmount", 0.1)); + quickNum.register(new DoubleSetting("quicknumDelay", 0.2)); this.register(quickNum); } diff --git a/src/main/java/io/github/homchom/recode/mod/mixin/render/MAbstractContainerScreen.java b/src/main/java/io/github/homchom/recode/mod/mixin/render/MAbstractContainerScreen.java new file mode 100644 index 00000000..8bd19bdf --- /dev/null +++ b/src/main/java/io/github/homchom/recode/mod/mixin/render/MAbstractContainerScreen.java @@ -0,0 +1,13 @@ +package io.github.homchom.recode.mod.mixin.render; + +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; +import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.inventory.Slot; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +@Mixin(AbstractContainerScreen.class) +public interface MAbstractContainerScreen { + @Invoker("isHovering") + boolean isHovering(Slot slot, double mouseX, double mouseY); +} diff --git a/src/main/java/io/github/homchom/recode/mod/mixin/render/MMouseHandler.java b/src/main/java/io/github/homchom/recode/mod/mixin/render/MMouseHandler.java index 92cf33f0..082b66f7 100644 --- a/src/main/java/io/github/homchom/recode/mod/mixin/render/MMouseHandler.java +++ b/src/main/java/io/github/homchom/recode/mod/mixin/render/MMouseHandler.java @@ -5,13 +5,15 @@ import io.github.homchom.recode.mod.config.Config; import io.github.homchom.recode.sys.util.ItemUtil; import net.minecraft.ChatFormatting; +import net.minecraft.client.Minecraft; import net.minecraft.client.MouseHandler; import net.minecraft.client.gui.screens.Screen; -import net.minecraft.client.gui.screens.inventory.ContainerScreen; +import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.*; import net.minecraft.sounds.*; import net.minecraft.world.inventory.*; +import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; import org.spongepowered.asm.mixin.*; import org.spongepowered.asm.mixin.injection.*; @@ -32,100 +34,101 @@ public class MMouseHandler { @Inject(method = "onScroll(JDD)V", at = @At("HEAD")) private void onScroll(long window, double horiz, double vertical, CallbackInfo ci) { - Screen screen = LegacyRecode.MC.screen; - if (screen instanceof ContainerScreen && Config.getBoolean("quicknum")) { - AbstractContainerMenu handler = ((ContainerScreen) screen).getMenu(); - List slotList = handler.slots; + Minecraft mc = LegacyRecode.MC; + Screen screen = mc.screen; - double scale = LegacyRecode.MC.getWindow().getGuiScale(); + if (screen instanceof MAbstractContainerScreen containerScreen && + mc.player != null && mc.gameMode != null && mc.player.isCreative() && + Config.getBoolean("quicknum") && System.currentTimeMillis() >= cd + ) { + List slotList = mc.player.containerMenu.slots; + double scale = mc.getWindow().getGuiScale(); - double mouseX = xpos; - double mouseY = ypos; + for (Slot slot : slotList) { + if (containerScreen.isHovering(slot, xpos / scale, ypos / scale) && ItemUtil.isVar(slot.getItem(), "num")) { + int slotIndex; + // Special case for when you're in the creative inventory + if (screen instanceof CreativeModeInventoryScreen creativeScreen) { + if (creativeScreen.getSelectedTab() != CreativeModeTab.TAB_INVENTORY.getId()) return; + if (slot.y >= 112) { + slotIndex = (slot.x - 9) / 18 + 36 + 2; + } else if (slot.y >= 54) { + slotIndex = (slot.y - 18) / 18 * 9 + (slot.x - 9) / 18 - 9 + 2; + } else { + return; + } + } else { + slotIndex = slot.index; + } + cd = System.currentTimeMillis() + (long)(Config.getDouble("quicknumDelay") * 1000); + ItemStack itemStack = slot.getItem().copy(); + CompoundTag tag = itemStack.getTag(); - for (Slot slot : slotList) { - double sX = Math.floor(((double) (screen.width - 176) / 2) + slot.x); - double sY = Math.floor(((double) (screen.height - 166) / 2) + slot.y); - sX *= scale; - sY *= scale; - - - if (sX < mouseX && mouseX < sX + (16 * scale)) { - if (sY < mouseY && mouseY < sY + (16 * scale)) { - if (System.currentTimeMillis() >= cd) { - if (LegacyRecode.MC.player != null && LegacyRecode.MC.gameMode != null && ItemUtil.isVar(slot.getItem(), "num")) { - if (LegacyRecode.MC.player.isCreative()) { - cd = System.currentTimeMillis() + 250; - ItemStack itemStack = slot.getItem().copy(); - - CompoundTag tag = itemStack.getTag(); - - if (tag == null) return; - - CompoundTag publicBukkitValues = tag.getCompound("PublicBukkitValues"); - String varItem = publicBukkitValues.getString("hypercube:varitem"); - - JsonObject parsedJson = JsonParser.parseString(varItem).getAsJsonObject(); - JsonObject data = parsedJson.get("data").getAsJsonObject(); - - String name = data.get("name").getAsString(); - - try { - BigDecimal bigDecimal = new BigDecimal(name); - - if (Screen.hasControlDown()) { - if (vertical > 0) { - bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumSecondaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); - } else { - bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumSecondaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); - } - } else if (Screen.hasShiftDown()) { - if (vertical > 0) { - bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumTertiaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); - } else { - bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumTertiaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); - } - } else { - if (vertical > 0) { - bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumPrimaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); - } else { - bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumPrimaryAmount"))); - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); - } - } - - name = bigDecimal.toString(); - if (name.endsWith(".0")) name = name.substring(0, name.length() - 2); - - data.addProperty("name", name); - parsedJson.add("data", data); - publicBukkitValues.putString("hypercube:varitem", parsedJson.toString()); - tag.put("PublicBukkitValues", publicBukkitValues); - - itemStack.setTag(tag); - itemStack.setHoverName(Component.literal(name) - .withStyle(style -> style.withColor(TextColor.fromLegacyFormat(ChatFormatting.RED)).withItalic(false))); - - ItemUtil.setContainerItem(slot.index, itemStack); - } catch (NumberFormatException e) { - if (Config.getBoolean("quicknumSound")) - LegacyRecode.MC.player.playNotifySound(SoundEvents.NOTE_BLOCK_DIDGERIDOO, SoundSource.PLAYERS, 1, 0); - } - } + if (tag == null) return; + + CompoundTag publicBukkitValues = tag.getCompound("PublicBukkitValues"); + String varItem = publicBukkitValues.getString("hypercube:varitem"); + + JsonObject parsedJson = JsonParser.parseString(varItem).getAsJsonObject(); + JsonObject data = parsedJson.get("data").getAsJsonObject(); + + String name = data.get("name").getAsString(); + + try { + BigDecimal bigDecimal = new BigDecimal(name); + + if (Screen.hasControlDown()) { + if (vertical > 0) { + bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumSecondaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); + } else { + bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumSecondaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); + } + } else if (Screen.hasShiftDown()) { + if (vertical > 0) { + bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumTertiaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); + } else { + bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumTertiaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); + } + } else { + if (vertical > 0) { + bigDecimal = bigDecimal.add(BigDecimal.valueOf(Config.getDouble("quicknumPrimaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 1); + } else { + bigDecimal = bigDecimal.subtract(BigDecimal.valueOf(Config.getDouble("quicknumPrimaryAmount"))); + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_HAT, SoundSource.PLAYERS, 1, 0); } } + + name = bigDecimal.toString(); + if (name.endsWith(".0")) name = name.substring(0, name.length() - 2); + + data.addProperty("name", name); + parsedJson.add("data", data); + publicBukkitValues.putString("hypercube:varitem", parsedJson.toString()); + tag.put("PublicBukkitValues", publicBukkitValues); + + itemStack.setTag(tag); + itemStack.setHoverName(Component.literal(name) + .withStyle(style -> style.withColor(TextColor.fromLegacyFormat(ChatFormatting.RED)).withItalic(false))); + + ItemUtil.setContainerItem(slotIndex, itemStack); + } catch (NumberFormatException e) { + if (Config.getBoolean("quicknumSound")) + mc.player.playNotifySound(SoundEvents.NOTE_BLOCK_DIDGERIDOO, SoundSource.PLAYERS, 1, 0); } + + return; } } } diff --git a/src/main/java/io/github/homchom/recode/sys/util/ItemUtil.java b/src/main/java/io/github/homchom/recode/sys/util/ItemUtil.java index fc1a0496..437e51d5 100644 --- a/src/main/java/io/github/homchom/recode/sys/util/ItemUtil.java +++ b/src/main/java/io/github/homchom/recode/sys/util/ItemUtil.java @@ -4,6 +4,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.github.homchom.recode.LegacyRecode; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.inventory.CreativeModeInventoryScreen; import net.minecraft.core.*; import net.minecraft.nbt.*; import net.minecraft.network.chat.Component; @@ -59,18 +60,29 @@ public static void setContainerItem(int slot, ItemStack itemStack) { if (!mc.player.isCreative()) { throw new IllegalStateException("Player is not in creative mode."); } - - // replace the 8th slot with the item we want to set. - ItemStack replacedItem = mc.player.getInventory().getItem(7); - LegacyRecode.MC.gameMode.handleCreativeModeItemAdd(itemStack, 43); - mc.player.getInventory().setItem(7, itemStack); - - // simulates pressing the 8 key on the slot we want to change. - LegacyRecode.MC.gameMode.handleInventoryMouseClick(mc.player.containerMenu.containerId, slot, 7, ClickType.SWAP, LegacyRecode.MC.player); - - // change the 8th slot back to what it was before. - LegacyRecode.MC.gameMode.handleCreativeModeItemAdd(replacedItem, 43); - mc.player.getInventory().setItem(7, replacedItem); + int slotCount = mc.player.containerMenu.slots.size(); + + // Check if you're trying to replace an item in your own inventory or in a container. + if (slot < slotCount - 36) { // Container case. + // replace the 8th slot with the item we want to set. + ItemStack replacedItem = mc.player.getInventory().getItem(7); + LegacyRecode.MC.gameMode.handleCreativeModeItemAdd(itemStack, 43); + mc.player.getInventory().setItem(7, itemStack); + + // simulates pressing the 8 key on the slot we want to change. + LegacyRecode.MC.gameMode.handleInventoryMouseClick(mc.player.containerMenu.containerId, slot, 7, ClickType.SWAP, LegacyRecode.MC.player); + + // change the 8th slot back to what it was before. + LegacyRecode.MC.gameMode.handleCreativeModeItemAdd(replacedItem, 43); + mc.player.getInventory().setItem(7, replacedItem); + } else { // Own inventory case. + // Transform index to in between 0 and 35. + int normalizedSlot = slot - slotCount + 36; + + // Replace the slot with the item we want to set. + LegacyRecode.MC.gameMode.handleCreativeModeItemAdd(itemStack, normalizedSlot + 9); + mc.player.getInventory().setItem((normalizedSlot + 9) % 36, itemStack); + } } public static List fromItemContainer(ItemStack container) { diff --git a/src/main/resources/assets/recode/lang/en_us.json b/src/main/resources/assets/recode/lang/en_us.json index 6b2030be..0d37ad82 100644 --- a/src/main/resources/assets/recode/lang/en_us.json +++ b/src/main/resources/assets/recode/lang/en_us.json @@ -183,8 +183,10 @@ "config.recode.option.quicknumPrimaryAmount.tooltip": "Change how much a scroll changes the number.", "config.recode.option.quicknumSecondaryAmount": "Secondary Amount", "config.recode.option.quicknumSecondaryAmount.tooltip": "Change how much a scroll changes the number. (When holding CTRL)", - "config.recode.option.quicknumTertiaryAmount": "Secondary Amount", + "config.recode.option.quicknumTertiaryAmount": "Tertiary Amount", "config.recode.option.quicknumTertiaryAmount.tooltip": "Change how much a scroll changes the number. (When holding SHIFT)", + "config.recode.option.quicknumDelay": "Delay", + "config.recode.option.quicknumDelay.tooltip": "Minimum delay between each number change.\nToo low values might cause issues.", "config.recode.option.chestToolTip": "Chest Tooltip", "config.recode.option.chestToolTip.tooltip": "Display chest params when you are in dev", diff --git a/src/main/resources/recodeLegacy.mixins.json b/src/main/resources/recodeLegacy.mixins.json index f6397d59..8b4287f9 100644 --- a/src/main/resources/recodeLegacy.mixins.json +++ b/src/main/resources/recodeLegacy.mixins.json @@ -24,6 +24,7 @@ "render.MStitcher", "render.MWindow", "render.TextureMapAccessor", + "render.MAbstractContainerScreen", "render.screen.MCreativeInventoryScreen", "render.screen.MOptionsScreen" ],