Get the menu working, button click broken

This commit is contained in:
zontreck 2024-01-02 03:36:08 -07:00
parent 848b6f495b
commit 01a6f1ddc7
8 changed files with 86 additions and 24 deletions

View file

@ -86,6 +86,8 @@ public class LibZontreck {
Bus.Reset(); Bus.Reset();
ModMenuTypes.REGISTRY.register(bus);
Bus.Register(CurrencyHelper.class, null); Bus.Register(CurrencyHelper.class, null);
Bus.Register(Bank.class, null); Bus.Register(Bank.class, null);
} }

View file

@ -1,17 +1,16 @@
package dev.zontreck.libzontreck.chestgui; package dev.zontreck.libzontreck.chestgui;
import dev.zontreck.libzontreck.LibZontreck; import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.events.OpenGUIEvent;
import dev.zontreck.libzontreck.menus.ChestGUIMenu; import dev.zontreck.libzontreck.menus.ChestGUIMenu;
import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S;
import dev.zontreck.libzontreck.util.ServerUtilities; import dev.zontreck.libzontreck.util.ServerUtilities;
import dev.zontreck.libzontreck.vectors.Vector2; import dev.zontreck.libzontreck.vectors.Vector2;
import dev.zontreck.libzontreck.vectors.Vector2i; 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.network.chat.Component;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.SimpleMenuProvider; import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.NetworkHooks;
@ -23,9 +22,9 @@ import java.util.UUID;
public class ChestGUI public class ChestGUI
{ {
private ItemStackHandler container = new ItemStackHandler((9*3)); private ItemStackHandler container = new ItemStackHandler((9*3));
private String MenuTitle; private String MenuTitle = "";
private UUID player; private UUID player;
private List<ChestGUIButton> buttons = new ArrayList<>(); public List<ChestGUIButton> buttons = new ArrayList<>();
private ResourceLocation id; private ResourceLocation id;
public ChestGUI withButton(ChestGUIButton button) public ChestGUI withButton(ChestGUIButton button)
{ {
@ -60,7 +59,8 @@ public class ChestGUI
{ {
if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER) if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER)
{ {
NetworkHooks.openScreen(ServerUtilities.getPlayerByID(player.toString()), new SimpleMenuProvider(ChestGUIMenu.getServerMenu(this), Component.literal(MenuTitle))); MinecraftForge.EVENT_BUS.post(new OpenGUIEvent(id, player));
NetworkHooks.openScreen(ServerUtilities.getPlayerByID(player.toString()), new SimpleMenuProvider(ChestGUIMenu.getServerMenu(this), Component.literal((MenuTitle != "") ? MenuTitle : "No Title")));
} }
} }

View file

@ -3,6 +3,7 @@ package dev.zontreck.libzontreck.commands;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUI;
import dev.zontreck.libzontreck.chestgui.ChestGUIButton; import dev.zontreck.libzontreck.chestgui.ChestGUIButton;
import dev.zontreck.libzontreck.util.heads.CreditsEntry; import dev.zontreck.libzontreck.util.heads.CreditsEntry;
@ -24,18 +25,21 @@ public class CreditsCommand {
if(source.getEntity() instanceof Player) if(source.getEntity() instanceof Player)
{ {
// OK. // OK.
ChestGUI gui = ChestGUI.builder().withGUIId(new ResourceLocation("ariasmods", "credits-gui")).withPlayer(source.getEntity().getUUID()); ChestGUI gui = ChestGUI.builder().withGUIId(new ResourceLocation("ariasmods", "creditsgui")).withPlayer(source.getEntity().getUUID()).withTitle("Aria's Mods - Credits");
Vector2i pos = new Vector2i(); int x = 0;
int y = 0;
for(CreditsEntry entry : HeadCache.CREDITS) for(CreditsEntry entry : HeadCache.CREDITS)
{ {
gui = gui.withButton(new ChestGUIButton(entry.compile(), ()->{}, pos)); gui = gui.withButton(new ChestGUIButton(entry.compile(), ()->{}, new Vector2i(x,y)));
pos.y++; LibZontreck.LOGGER.info("Add gui button : " + entry.name);
if(pos.y>=9)
y++;
if(y>=9)
{ {
pos.x++; x++;
pos.y=0; y=0;
} }
} }

View file

@ -0,0 +1,54 @@
package dev.zontreck.libzontreck.dynamicchest;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.chestgui.ChestGUI;
import dev.zontreck.libzontreck.chestgui.ChestGUIButton;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;
public class ChestGUIReadOnlyStackHandler extends ItemStackHandler
{
private ChestGUI gui;
public ChestGUIReadOnlyStackHandler(ChestGUI gui)
{
super((3*9));
this.gui = gui;
LibZontreck.LOGGER.info("Logical Side : " + LibZontreck.CURRENT_SIDE);
if(gui!=null)
{
if(gui.buttons!=null)
{
LibZontreck.LOGGER.info("Generating chest gui button items");
for(ChestGUIButton btn : gui.buttons)
{
setStackInSlot(btn.getSlotNum(), btn.buildIcon());
}
} else LibZontreck.LOGGER.error("Gui Buttons list is null");
} else LibZontreck.LOGGER.error("Gui is null!");
}
@Override
public int getSlots() {
return (3*9);
}
@Override
public @NotNull ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
return stack;
}
@Override
public @NotNull ItemStack extractItem(int slot, int amount, boolean simulate) {
ChestGUIButton btn = gui.buttons.stream().filter(x->x.getSlotNum()==slot).findFirst().orElse(null);
if(btn == null) return ItemStack.EMPTY;
btn.clicked();
return ItemStack.EMPTY;
}
}

View file

@ -2,6 +2,7 @@ package dev.zontreck.libzontreck.menus;
import dev.zontreck.libzontreck.chestgui.ChestGUI; import dev.zontreck.libzontreck.chestgui.ChestGUI;
import dev.zontreck.libzontreck.chestgui.ChestGUIButton; import dev.zontreck.libzontreck.chestgui.ChestGUIButton;
import dev.zontreck.libzontreck.dynamicchest.ChestGUIReadOnlyStackHandler;
import dev.zontreck.libzontreck.dynamicchest.ReadOnlyItemStackHandler; import dev.zontreck.libzontreck.dynamicchest.ReadOnlyItemStackHandler;
import dev.zontreck.libzontreck.types.ModMenuTypes; import dev.zontreck.libzontreck.types.ModMenuTypes;
import dev.zontreck.libzontreck.vectors.Vector2i; import dev.zontreck.libzontreck.vectors.Vector2i;
@ -20,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
public class ChestGUIMenu extends AbstractContainerMenu public class ChestGUIMenu extends AbstractContainerMenu
{ {
public final ChestGUI gui; public final ChestGUI gui;
public final ItemStackHandler slots;
public ChestGUIMenu(int id, Inventory playerInv, FriendlyByteBuf buf) public ChestGUIMenu(int id, Inventory playerInv, FriendlyByteBuf buf)
{ {
@ -32,7 +34,8 @@ public class ChestGUIMenu extends AbstractContainerMenu
this.gui = gui; this.gui = gui;
if(gui == null)return; slots = new ChestGUIReadOnlyStackHandler(gui);
int slotSize = 18; int slotSize = 18;
int startX = 15; int startX = 15;
@ -42,12 +45,7 @@ public class ChestGUIMenu extends AbstractContainerMenu
{ {
for(int column=0;column<9;column++) for(int column=0;column<9;column++)
{ {
Vector2i slot = new Vector2i(row, column); addSlot(new SlotItemHandler(slots, row*9 + column, startX + column * slotSize, startY + row * slotSize));
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));
}
} }
} }
} }

View file

@ -29,6 +29,9 @@ public class ChestGUIScreen extends AbstractContainerScreen<ChestGUIMenu> {
this.imageWidth = 191; this.imageWidth = 191;
this.imageHeight = 82; this.imageHeight = 82;
this.titleLabelX = 32;
this.titleLabelY =5;
} }
@Override @Override
@ -58,6 +61,6 @@ public class ChestGUIScreen extends AbstractContainerScreen<ChestGUIMenu> {
@Override @Override
protected void renderLabels(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY) { protected void renderLabels(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY) {
pGuiGraphics.drawString(font, title.getString(), 32, 5, 0x000000); pGuiGraphics.drawString(this.font, this.title, this.titleLabelX, this.titleLabelY, 4210752, false);
} }
} }

View file

@ -29,13 +29,14 @@ public class CreditsEntry {
ItemStack stack = player.getAsItem(); ItemStack stack = player.getAsItem();
stack.setHoverName(Component.literal(name)); stack.setHoverName(Component.literal(name));
LoreContainer contain = new LoreContainer(stack); LoreContainer contain = new LoreContainer(stack);
contain.miscData.LoreData.clear();
LoreEntry entry = new LoreEntry(); LoreEntry entry = new LoreEntry();
entry.text = ChatColor.doColors("!Dark_Purple!Role: "+role); entry.text = ChatColor.doColors("!Dark_Purple!Role: "+role + "\n");
entry.bold=true; entry.bold=true;
entry.italic=true; entry.italic=true;
contain.miscData.LoreData.add(entry); contain.miscData.LoreData.add(entry);
entry = new LoreEntry(); entry = new LoreEntry();
entry.text = ChatColor.doColors("!White!About: !Dark_Green!"+description); entry.text = ChatColor.doColors("!White!About: !Dark_Green!"+description + "\n");
entry.italic=true; entry.italic=true;
contain.miscData.LoreData.add(entry); contain.miscData.LoreData.add(entry);

View file

@ -106,7 +106,7 @@ public class HeadCache
creds.add( creds.add(
new CreditsEntry(HeadUtilities.cachedLookup("PossumTheWarrior"), "PossumTheWarrior", "Tester", "Poss has helped to test the mods from very early on")); new CreditsEntry(HeadUtilities.cachedLookup("PossumTheWarrior"), "PossumTheWarrior", "Tester", "Poss has helped to test the mods from very early on"));
creds.add( creds.add(
new CreditsEntry(HeadUtilities.cachedLookup("GemMD"), "GemMD", "Adviser", "GemMD has provided advise on marketing and development decisions for various mods")); new CreditsEntry(HeadUtilities.cachedLookup("GemMD"), "GemMD", "Adviser", "GemMD has provided advice on marketing and development decisions for various mods"));
CREDITS = creds; CREDITS = creds;