From d6a324a93fe3412fbdd8fb1c0e1c6328cd848119 Mon Sep 17 00:00:00 2001 From: skippyall <> Date: Tue, 17 Sep 2024 10:46:43 +0200 Subject: [PATCH] e --- .../skippyall/minions/gui/MinionGui.java | 6 +- .../minions/gui/ModuleInventory.java | 10 +++ .../gui/ModuleInventoryScreenHandler.java | 80 +++++++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/skippyall/minions/gui/ModuleInventoryScreenHandler.java diff --git a/src/main/java/io/github/skippyall/minions/gui/MinionGui.java b/src/main/java/io/github/skippyall/minions/gui/MinionGui.java index d369a19..7ba218c 100644 --- a/src/main/java/io/github/skippyall/minions/gui/MinionGui.java +++ b/src/main/java/io/github/skippyall/minions/gui/MinionGui.java @@ -40,7 +40,7 @@ public class MinionGui { .setItem(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE) .setName(Text.translatable("minions.gui.main.modules")) .setCallback((i, clickType, slotActionType) -> { - openModuleInventory(player, minion); + ModuleInventory.openModuleInventory(player, minion); }) ); gui.setSlot(5, new GuiElementBuilder() @@ -61,10 +61,6 @@ public class MinionGui { } - public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) { - player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> GenericContainerScreenHandler.createGeneric9x3(syncId, playerInventory, minion.getModuleInventory()), Text.translatable(""))); - } - public static void openMinionInventory(ServerPlayerEntity player, MinionFakePlayer minion) { SimpleGui gui = new SimpleGui(ScreenHandlerType.GENERIC_9X5, player, false); gui.setTitle(Text.translatable("minions.gui.inventory.title", minion.getName())); diff --git a/src/main/java/io/github/skippyall/minions/gui/ModuleInventory.java b/src/main/java/io/github/skippyall/minions/gui/ModuleInventory.java index cd67870..45f65ee 100644 --- a/src/main/java/io/github/skippyall/minions/gui/ModuleInventory.java +++ b/src/main/java/io/github/skippyall/minions/gui/ModuleInventory.java @@ -1,13 +1,19 @@ package io.github.skippyall.minions.gui; import io.github.skippyall.minions.command.Command; +import io.github.skippyall.minions.fakeplayer.MinionFakePlayer; import io.github.skippyall.minions.module.ModuleItem; import io.github.skippyall.minions.program.block.CodeBlock; +import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventories; import net.minecraft.inventory.SimpleInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.registry.RegistryWrapper; +import net.minecraft.screen.GenericContainerScreenHandler; +import net.minecraft.screen.SimpleNamedScreenHandlerFactory; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; import java.util.ArrayList; import java.util.List; @@ -17,6 +23,10 @@ public class ModuleInventory extends SimpleInventory { super(27); } + public static void openModuleInventory(ServerPlayerEntity player, MinionFakePlayer minion) { + player.openHandledScreen(new SimpleNamedScreenHandlerFactory((syncId, playerInventory, player2) -> new ModuleInventoryScreenHandler(syncId, playerInventory, minion.getModuleInventory()), Text.translatable("minions.gui.modules.title", minion.getName()))); + } + @Override public int getMaxCountPerStack() { return 1; diff --git a/src/main/java/io/github/skippyall/minions/gui/ModuleInventoryScreenHandler.java b/src/main/java/io/github/skippyall/minions/gui/ModuleInventoryScreenHandler.java new file mode 100644 index 0000000..c3bf640 --- /dev/null +++ b/src/main/java/io/github/skippyall/minions/gui/ModuleInventoryScreenHandler.java @@ -0,0 +1,80 @@ +package io.github.skippyall.minions.gui; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.GenericContainerScreenHandler; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; +import net.minecraft.screen.slot.Slot; + +public class ModuleInventoryScreenHandler extends ScreenHandler { + private final int rows = 3; + private final ModuleInventory inventory; + public ModuleInventoryScreenHandler(int syncId, ModuleInventory inventory) { + super(ScreenHandlerType.GENERIC_9X3, syncId); + this.inventory = inventory; + } + + public ModuleInventoryScreenHandler(int syncId, PlayerInventory playerInventory, ModuleInventory inventory) { + super(ScreenHandlerType.GENERIC_9X3, syncId); + int k; + int j; + GenericContainerScreenHandler.checkSize(inventory, 3 * 9); + this.inventory = inventory; + inventory.onOpen(playerInventory.player); + int i = (rows - 4) * 18; + for (j = 0; j < rows; ++j) { + for (k = 0; k < 9; ++k) { + this.addSlot(new Slot(inventory, k + j * 9, 8 + k * 18, 18 + j * 18) { + @Override + public boolean canInsert(ItemStack stack) { + return super.canInsert(stack) && inventory.isValid(getIndex(), stack); + } + }); + } + } + for (j = 0; j < 3; ++j) { + for (k = 0; k < 9; ++k) { + this.addSlot(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i)); + } + } + for (j = 0; j < 9; ++j) { + this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 161 + i)); + } + } + + @Override + public boolean canUse(PlayerEntity player) { + return this.inventory.canPlayerUse(player); + } + + @Override + public ItemStack quickMove(PlayerEntity player, int slot) { + ItemStack itemStack = ItemStack.EMPTY; + Slot slot2 = (Slot)this.slots.get(slot); + if (slot2 != null && slot2.hasStack()) { + ItemStack itemStack2 = slot2.getStack(); + itemStack = itemStack2.copy(); + if (slot < this.rows * 9 ? !this.insertItem(itemStack2, this.rows * 9, this.slots.size(), true) : !this.insertItem(itemStack2, 0, this.rows * 9, false)) { + return ItemStack.EMPTY; + } + if (itemStack2.isEmpty()) { + slot2.setStack(ItemStack.EMPTY); + } else { + slot2.markDirty(); + } + } + return itemStack; + } + + @Override + public void onClosed(PlayerEntity player) { + super.onClosed(player); + this.inventory.onClose(player); + } + + public ModuleInventory getInventory() { + return inventory; + } +}