From 220391f6d46609b30a754ce60c2cce943d699f89 Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Tue, 16 Apr 2024 20:56:35 +0300 Subject: [PATCH] Possibly better inventory capture for chest guis? idk --- .../mods/kubejs/core/ServerPlayerKJS.java | 28 ++++++++++++------- .../mods/kubejs/gui/chest/ChestMenuData.java | 5 ++-- .../kubejs/gui/chest/CustomChestMenu.java | 7 ++--- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/ServerPlayerKJS.java b/common/src/main/java/dev/latvian/mods/kubejs/core/ServerPlayerKJS.java index b00671d30..cf51863d8 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/ServerPlayerKJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/ServerPlayerKJS.java @@ -21,7 +21,9 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.players.UserBanListEntry; import net.minecraft.util.Mth; +import net.minecraft.world.Container; import net.minecraft.world.MenuProvider; +import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; @@ -186,20 +188,22 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe }); } - default ItemStack[] kjs$captureInventory(boolean autoRestore) { + default Container kjs$captureInventory(boolean autoRestore) { var playerItems = kjs$self().getInventory().items; - var captured = new ItemStack[playerItems.size()]; + var captured = new SimpleContainer(playerItems.size()); var map = new HashMap(); - for (int i = 0; i < captured.length; i++) { + for (int i = 0; i < playerItems.size(); i++) { var c = playerItems.set(i, ItemStack.EMPTY); - if (autoRestore && !c.isEmpty()) { - map.put(i, c); - } + if (!c.isEmpty()) { + if (autoRestore) { + map.put(i, c); + } - captured[i] = c.copy(); + captured.setItem(i, c.copy()); + } } if (autoRestore && !map.isEmpty()) { @@ -213,12 +217,16 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe var data = new ChestMenuData(kjs$self(), title, Mth.clamp(rows, 1, 6)); gui.accept(data); - if (kjs$self().containerMenu instanceof CustomChestMenu open && open.data.rows == data.rows && open.data.title.equals(title)) { + if (kjs$self().containerMenu instanceof CustomChestMenu open) { data.capturedInventory = open.data.capturedInventory; + } else { + data.capturedInventory = kjs$captureInventory(true); + } + + if (kjs$self().containerMenu instanceof CustomChestMenu open && open.data.rows == data.rows && open.data.title.equals(title)) { open.data = data; data.sync(); } else { - data.capturedInventory = data.playerSlots ? new ItemStack[0] : kjs$captureInventory(true); data.sync(); kjs$self().openMenu(new MenuProvider() { @@ -229,7 +237,7 @@ public Component getDisplayName() { @Override public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { - return new CustomChestMenu(i, inventory, data); + return new CustomChestMenu(i, data); } }); } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/ChestMenuData.java b/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/ChestMenuData.java index 6359abe5b..15ee30c91 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/ChestMenuData.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/ChestMenuData.java @@ -2,6 +2,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.Container; import net.minecraft.world.inventory.ClickType; import net.minecraft.world.item.ItemStack; @@ -9,7 +10,7 @@ public class ChestMenuData { public final ServerPlayer player; - public final Component title; + public Component title; public final int rows; public final ChestMenuSlot[] slots; public ChestMenuClickEvent.Callback anyClicked; @@ -17,7 +18,7 @@ public class ChestMenuData { public boolean playerSlots; public Runnable closed; public ItemStack mouseItem; - public ItemStack[] capturedInventory; + public Container capturedInventory; public ChestMenuData(ServerPlayer player, Component title, int rows) { this.player = player; diff --git a/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/CustomChestMenu.java b/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/CustomChestMenu.java index a086318c4..61eb24b9f 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/CustomChestMenu.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/gui/chest/CustomChestMenu.java @@ -1,7 +1,6 @@ package dev.latvian.mods.kubejs.gui.chest; import dev.latvian.mods.kubejs.util.ConsoleJS; -import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.ClickType; @@ -23,7 +22,7 @@ public class CustomChestMenu extends AbstractContainerMenu { public ChestMenuData data; - public CustomChestMenu(int containerId, Inventory inventory, ChestMenuData data) { + public CustomChestMenu(int containerId, ChestMenuData data) { super(TYPES[data.rows - 1], containerId); this.data = data; @@ -38,12 +37,12 @@ public CustomChestMenu(int containerId, Inventory inventory, ChestMenuData data) if (data.playerSlots) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 9; x++) { - addSlot(new Slot(inventory, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + k)); + addSlot(new Slot(data.capturedInventory, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + k)); } } for (int x = 0; x < 9; x++) { - addSlot(new Slot(inventory, x, 8 + x * 18, 161 + k)); + addSlot(new Slot(data.capturedInventory, x, 8 + x * 18, 161 + k)); } } else { for (int y = 0; y < 3; y++) {