Start adding a chest gui system

This commit is contained in:
zontreck 2023-12-31 05:24:21 -07:00
parent d237912942
commit a8048391a0
10 changed files with 357 additions and 16 deletions

View file

@ -0,0 +1,90 @@
package dev.zontreck.libzontreck.chestgui;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S;
import dev.zontreck.libzontreck.vectors.Vector2;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.items.ItemStackHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ChestGUI
{
private ItemStackHandler container = new ItemStackHandler((9*3));
private String MenuTitle;
private UUID player;
private List<ChestGUIButton> buttons = new ArrayList<>();
private ResourceLocation id;
public ChestGUI withButton(ChestGUIButton button)
{
buttons.add(button);
container.setStackInSlot(button.getSlotNum(), button.buildIcon());
return this;
}
public ChestGUI withTitle(String title)
{
MenuTitle = title;
return this;
}
public static ChestGUI builder()
{
return new ChestGUI();
}
public ChestGUI withPlayer(UUID id)
{
player=id;
return this;
}
/**
* Open the GUI on the client
*/
public void open()
{
if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER)
{
}
}
public boolean isPlayer(UUID ID)
{
return player.equals(ID);
}
public ChestGUI withGUIId(ResourceLocation id)
{
this.id = id;
return this;
}
public boolean matches(ResourceLocation id)
{
return this.id.equals(id);
}
public void handleButtonClicked(int slot, Vector2 pos, Item item) {
for(ChestGUIButton button : buttons)
{
if(button.getSlotNum() == slot)
{
if(button.buildIcon().getItem() == item)
{
button.clicked();
return;
}
}
}
}
}

View file

@ -0,0 +1,104 @@
package dev.zontreck.libzontreck.chestgui;
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 net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import java.util.ArrayList;
import java.util.List;
public class ChestGUIButton
{
private Item icon;
private String name;
private List<LoreEntry> tooltipInfo;
private Runnable callback;
private CompoundTag NBT = new CompoundTag();
/**
* Position is Row (X), Column (Y)
*/
private Vector2 position;
public ChestGUIButton(Item icon, String name, Runnable callback, Vector2 position)
{
this.icon = icon;
this.name = name;
this.callback = callback;
this.position = position;
tooltipInfo = new ArrayList<>();
}
public ChestGUIButton(ItemStack existing, Runnable callback, Vector2 position)
{
this.callback = callback;
this.position = position;
LoreContainer container = new LoreContainer(existing);
tooltipInfo = container.miscData.LoreData;
name = existing.getHoverName().getString();
icon = existing.getItem();
NBT = existing.getTag();
}
public ChestGUIButton withNBT(CompoundTag tag)
{
this.NBT = tag;
return this;
}
public ItemStack buildIcon()
{
ItemStack ret = new ItemStack(icon,1);
ret = ret.setHoverName(ChatHelpers.macro(name));
LoreContainer cont = new LoreContainer(ret);
for (LoreEntry str : tooltipInfo)
{
cont.miscData.LoreData.add(str);
}
cont.commitLore();
NBT.putInt("slot", getSlotNum());
NBT.put("pos", position.serialize());
ret.setTag(NBT);
return ret;
}
/**
* Adds a line to the Lore (Tooltip) of the button
* @param line The line to add
* @return ChestGUIButton instance
*/
public ChestGUIButton withInfo(LoreEntry line)
{
tooltipInfo.add(line);
return this;
}
/**
* Returns the slot number in the 32 item grid
* @return Slot number from vector (row, column)
*/
public int getSlotNum()
{
return (int) Math.floor((position.x * 9) + position.y);
}
public void clicked()
{
callback.run();
}
}

View file

@ -0,0 +1,34 @@
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<ChestGUI> 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());
}
}
}
}
}