From 848b6f495bf19d07a8fbb859fe265bff53af8d8a Mon Sep 17 00:00:00 2001 From: zontreck Date: Tue, 2 Jan 2024 00:28:17 -0700 Subject: [PATCH] Finish implementing initial chest-gui interface API --- .../dev/zontreck/libzontreck/LibZontreck.java | 2 - .../libzontreck/chestgui/ChestGUI.java | 35 +++++++++++- .../libzontreck/chestgui/ChestGUIButton.java | 27 ++++++++- .../chestgui/ChestGUIRegistry.java | 34 ------------ .../libzontreck/commands/CreditsCommand.java | 24 +++++++- .../events/ForgeEventHandlers.java | 2 - .../libzontreck/menus/ChestGUIMenu.java | 32 +++++++++-- .../libzontreck/networking/ModMessages.java | 7 --- .../packets/C2SChestGUIButtonClicked.java | 55 ------------------- 9 files changed, 108 insertions(+), 110 deletions(-) delete mode 100644 src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIRegistry.java delete mode 100644 src/main/java/dev/zontreck/libzontreck/networking/packets/C2SChestGUIButtonClicked.java diff --git a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java index e375f0f..56b732d 100644 --- a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java +++ b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java @@ -10,7 +10,6 @@ import java.util.UUID; import dev.zontreck.ariaslib.util.DelayedExecutorService; import dev.zontreck.eventsbus.Bus; -import dev.zontreck.libzontreck.chestgui.ChestGUIRegistry; import dev.zontreck.libzontreck.currency.Bank; import dev.zontreck.libzontreck.currency.CurrencyHelper; import dev.zontreck.libzontreck.menus.ChestGUIScreen; @@ -84,7 +83,6 @@ public class LibZontreck { MinecraftForge.EVENT_BUS.register(new ForgeEventHandlers()); MinecraftForge.EVENT_BUS.register(new Commands()); MinecraftForge.EVENT_BUS.register(new NetworkEvents()); - MinecraftForge.EVENT_BUS.register(new ChestGUIRegistry()); Bus.Reset(); diff --git a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java index 86595e1..258b955 100644 --- a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java +++ b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUI.java @@ -1,14 +1,20 @@ package dev.zontreck.libzontreck.chestgui; import dev.zontreck.libzontreck.LibZontreck; +import dev.zontreck.libzontreck.menus.ChestGUIMenu; import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S; +import dev.zontreck.libzontreck.util.ServerUtilities; import dev.zontreck.libzontreck.vectors.Vector2; +import dev.zontreck.libzontreck.vectors.Vector2i; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.item.Item; import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.items.ItemStackHandler; +import net.minecraftforge.network.NetworkHooks; import java.util.ArrayList; import java.util.List; @@ -54,7 +60,7 @@ public class ChestGUI { if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER) { - + NetworkHooks.openScreen(ServerUtilities.getPlayerByID(player.toString()), new SimpleMenuProvider(ChestGUIMenu.getServerMenu(this), Component.literal(MenuTitle))); } } @@ -87,4 +93,31 @@ public class ChestGUI } } } + + public boolean hasSlot(Vector2i slot) + { + for(ChestGUIButton btn : buttons) + { + if(btn.matchesSlot(slot)) + { + return true; + } + } + + return false; + } + + public ChestGUIButton getSlot(Vector2i slot) { + if(hasSlot(slot)) + { + for(ChestGUIButton btn : buttons) + { + if(btn.matchesSlot(slot)) + { + return btn; + } + } + } + return null; + } } diff --git a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java index e0c9942..99ac7b0 100644 --- a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java +++ b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIButton.java @@ -4,10 +4,12 @@ import dev.zontreck.libzontreck.lore.LoreContainer; import dev.zontreck.libzontreck.lore.LoreEntry; import dev.zontreck.libzontreck.util.ChatHelpers; import dev.zontreck.libzontreck.vectors.Vector2; +import dev.zontreck.libzontreck.vectors.Vector2i; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtUtils; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraftforge.items.ItemStackHandler; import java.util.ArrayList; import java.util.List; @@ -24,9 +26,9 @@ public class ChestGUIButton /** * Position is Row (X), Column (Y) */ - private Vector2 position; + private Vector2i position; - public ChestGUIButton(Item icon, String name, Runnable callback, Vector2 position) + public ChestGUIButton(Item icon, String name, Runnable callback, Vector2i position) { this.icon = icon; this.name = name; @@ -35,7 +37,7 @@ public class ChestGUIButton tooltipInfo = new ArrayList<>(); } - public ChestGUIButton(ItemStack existing, Runnable callback, Vector2 position) + public ChestGUIButton(ItemStack existing, Runnable callback, Vector2i position) { this.callback = callback; this.position = position; @@ -76,6 +78,15 @@ public class ChestGUIButton return ret; } + public ItemStackHandler buildIconStack() + { + ItemStack stack = buildIcon(); + ItemStackHandler st = new ItemStackHandler(1); + st.setStackInSlot(0, stack); + + return st; + } + /** * Adds a line to the Lore (Tooltip) of the button * @param line The line to add @@ -88,6 +99,16 @@ public class ChestGUIButton return this; } + /** + * Check if the slot's row and column match (X,Y) + * @param slot + * @return True if matches + */ + public boolean matchesSlot(Vector2i slot) + { + return position.same(slot); + } + /** * Returns the slot number in the 32 item grid * @return Slot number from vector (row, column) diff --git a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIRegistry.java b/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIRegistry.java deleted file mode 100644 index eaec6d1..0000000 --- a/src/main/java/dev/zontreck/libzontreck/chestgui/ChestGUIRegistry.java +++ /dev/null @@ -1,34 +0,0 @@ -package dev.zontreck.libzontreck.chestgui; - -import dev.zontreck.libzontreck.events.GUIButtonClickedEvent; -import dev.zontreck.libzontreck.vectors.Vector2; -import net.minecraft.nbt.CompoundTag; -import net.minecraftforge.eventbus.api.SubscribeEvent; - -import java.util.ArrayList; -import java.util.List; - -public class ChestGUIRegistry -{ - public static List ActiveGUIs = new ArrayList<>(); - - @SubscribeEvent - public void onChestGUIButtonClicked(GUIButtonClickedEvent event) - { - for(ChestGUI gui : ActiveGUIs) - { - if(gui.isPlayer(event.player)) - { - if(gui.matches(event.id)) - { - // Handle the click now - CompoundTag tag = event.stack.getTag(); - Vector2 pos = new Vector2(tag.getCompound("pos")); - int slot = tag.getInt("slot"); - gui.handleButtonClicked(slot, pos, event.stack.getItem()); - } - - } - } - } -} diff --git a/src/main/java/dev/zontreck/libzontreck/commands/CreditsCommand.java b/src/main/java/dev/zontreck/libzontreck/commands/CreditsCommand.java index 1d9477b..358be8b 100644 --- a/src/main/java/dev/zontreck/libzontreck/commands/CreditsCommand.java +++ b/src/main/java/dev/zontreck/libzontreck/commands/CreditsCommand.java @@ -3,8 +3,14 @@ package dev.zontreck.libzontreck.commands; import com.mojang.brigadier.CommandDispatcher; +import dev.zontreck.libzontreck.chestgui.ChestGUI; +import dev.zontreck.libzontreck.chestgui.ChestGUIButton; +import dev.zontreck.libzontreck.util.heads.CreditsEntry; +import dev.zontreck.libzontreck.util.heads.HeadCache; +import dev.zontreck.libzontreck.vectors.Vector2i; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; public class CreditsCommand { @@ -17,7 +23,23 @@ public class CreditsCommand { // Open the credits GUI if(source.getEntity() instanceof Player) { - // OK. + // OK. + ChestGUI gui = ChestGUI.builder().withGUIId(new ResourceLocation("ariasmods", "credits-gui")).withPlayer(source.getEntity().getUUID()); + + Vector2i pos = new Vector2i(); + for(CreditsEntry entry : HeadCache.CREDITS) + { + gui = gui.withButton(new ChestGUIButton(entry.compile(), ()->{}, pos)); + + pos.y++; + if(pos.y>=9) + { + pos.x++; + pos.y=0; + } + } + + gui.open(); diff --git a/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java b/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java index 4bf560e..db5f5bb 100644 --- a/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java +++ b/src/main/java/dev/zontreck/libzontreck/events/ForgeEventHandlers.java @@ -3,8 +3,6 @@ package dev.zontreck.libzontreck.events; import dev.zontreck.ariaslib.terminal.Task; import dev.zontreck.ariaslib.util.DelayedExecutorService; import dev.zontreck.libzontreck.LibZontreck; -import dev.zontreck.libzontreck.currency.Account; -import dev.zontreck.libzontreck.currency.Bank; import dev.zontreck.libzontreck.exceptions.InvalidSideException; import dev.zontreck.libzontreck.memory.PlayerContainer; import dev.zontreck.libzontreck.networking.ModMessages; diff --git a/src/main/java/dev/zontreck/libzontreck/menus/ChestGUIMenu.java b/src/main/java/dev/zontreck/libzontreck/menus/ChestGUIMenu.java index cae9d28..09e6e93 100644 --- a/src/main/java/dev/zontreck/libzontreck/menus/ChestGUIMenu.java +++ b/src/main/java/dev/zontreck/libzontreck/menus/ChestGUIMenu.java @@ -2,7 +2,9 @@ package dev.zontreck.libzontreck.menus; import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUIButton; +import dev.zontreck.libzontreck.dynamicchest.ReadOnlyItemStackHandler; import dev.zontreck.libzontreck.types.ModMenuTypes; +import dev.zontreck.libzontreck.vectors.Vector2i; import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.entity.player.Inventory; @@ -12,22 +14,42 @@ import net.minecraft.world.inventory.MenuConstructor; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.item.ItemStack; import net.minecraftforge.items.ItemStackHandler; +import net.minecraftforge.items.SlotItemHandler; import org.jetbrains.annotations.Nullable; public class ChestGUIMenu extends AbstractContainerMenu { - private ChestGUI gui; + public final ChestGUI gui; public ChestGUIMenu(int id, Inventory playerInv, FriendlyByteBuf buf) { - this(id, playerInv, new ItemStackHandler(3*9), BlockPos.ZERO, playerInv.player, null); + this(id, playerInv, BlockPos.ZERO, playerInv.player, null); } - public ChestGUIMenu(int id, Inventory playerInv, ItemStackHandler inv, BlockPos position, Player player, ChestGUI gui) + public ChestGUIMenu(int id, Inventory playerInv, BlockPos position, Player player, ChestGUI gui) { super(ModMenuTypes.CHEST_GUI_MENU.get(), id); this.gui = gui; + + if(gui == null)return; + + int slotSize = 18; + int startX = 15; + int startY = 15; + + for (int row = 0; row < 3; row++) + { + for(int column=0;column<9;column++) + { + Vector2i slot = new Vector2i(row, column); + ChestGUIButton btn = gui.getSlot(slot); + if(gui.hasSlot(slot)) + { + addSlot(new SlotItemHandler(new ReadOnlyItemStackHandler(btn.buildIconStack(), btn::clicked), row*9 + column, startX + column * slotSize, startY + row * slotSize)); + } + } + } } @Override @@ -40,8 +62,8 @@ public class ChestGUIMenu extends AbstractContainerMenu return true; } - public static MenuConstructor getServerMenu(ItemStackHandler inventory) + public static MenuConstructor getServerMenu(ChestGUI gui) { - return (id, playerInv, player) -> new ChestGUIMenu(id, playerInv, inventory, BlockPos.ZERO, player, null); + return (id, playerInv, player) -> new ChestGUIMenu(id, playerInv, BlockPos.ZERO, player, gui); } } diff --git a/src/main/java/dev/zontreck/libzontreck/networking/ModMessages.java b/src/main/java/dev/zontreck/libzontreck/networking/ModMessages.java index a8e5875..ec110e0 100644 --- a/src/main/java/dev/zontreck/libzontreck/networking/ModMessages.java +++ b/src/main/java/dev/zontreck/libzontreck/networking/ModMessages.java @@ -2,7 +2,6 @@ package dev.zontreck.libzontreck.networking; import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.events.RegisterPacketsEvent; -import dev.zontreck.libzontreck.networking.packets.C2SChestGUIButtonClicked; import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S; import dev.zontreck.libzontreck.networking.packets.IPacket; import net.minecraft.resources.ResourceLocation; @@ -52,12 +51,6 @@ public class ModMessages { .consumerMainThread(ChestGUIOpenC2S::handle) .add(); - net.messageBuilder(C2SChestGUIButtonClicked.class, PACKET_ID.getAndIncrement(), NetworkDirection.PLAY_TO_SERVER) - .decoder(C2SChestGUIButtonClicked::new) - .encoder(C2SChestGUIButtonClicked::toBytes) - .consumerMainThread(C2SChestGUIButtonClicked::handle) - .add(); - } diff --git a/src/main/java/dev/zontreck/libzontreck/networking/packets/C2SChestGUIButtonClicked.java b/src/main/java/dev/zontreck/libzontreck/networking/packets/C2SChestGUIButtonClicked.java deleted file mode 100644 index dfafd8e..0000000 --- a/src/main/java/dev/zontreck/libzontreck/networking/packets/C2SChestGUIButtonClicked.java +++ /dev/null @@ -1,55 +0,0 @@ -package dev.zontreck.libzontreck.networking.packets; - -import dev.zontreck.libzontreck.events.GUIButtonClickedEvent; -import dev.zontreck.libzontreck.vectors.Vector2; -import net.minecraft.client.Minecraft; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.NbtUtils; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; -import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.network.NetworkEvent; - -import java.util.UUID; -import java.util.function.Supplier; - -public class C2SChestGUIButtonClicked -{ - private ItemStack stack; - private ResourceLocation id; - private UUID player; - - public C2SChestGUIButtonClicked(ItemStack stack, ResourceLocation id) - { - this.stack = stack; - - this.id = id; - player = Minecraft.getInstance().player.getUUID(); - } - - public C2SChestGUIButtonClicked(FriendlyByteBuf buf) - { - stack = buf.readItem(); - id = buf.readResourceLocation(); - } - - public void toBytes(FriendlyByteBuf buf) - { - buf.writeItem(stack); - buf.writeResourceLocation(id); - } - - public boolean handle(Supplier ctx) - { - NetworkEvent.Context context = ctx.get(); - - context.enqueueWork(()->{ - // We're on the server now. - - MinecraftForge.EVENT_BUS.post(new GUIButtonClickedEvent(stack, id, player)); - }); - - return true; - } -}