Chest GUI API now completed

This commit is contained in:
zontreck 2024-01-02 17:59:10 -07:00
parent 01a6f1ddc7
commit 0fb37b1633
19 changed files with 436 additions and 239 deletions

View file

@ -1,8 +1,11 @@
package dev.zontreck.libzontreck.chestgui;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.events.CloseGUIEvent;
import dev.zontreck.libzontreck.events.OpenGUIEvent;
import dev.zontreck.libzontreck.menus.ChestGUIMenu;
import dev.zontreck.libzontreck.networking.ModMessages;
import dev.zontreck.libzontreck.networking.packets.S2CCloseChestGUI;
import dev.zontreck.libzontreck.util.ServerUtilities;
import dev.zontreck.libzontreck.vectors.Vector2;
import dev.zontreck.libzontreck.vectors.Vector2i;
@ -59,11 +62,24 @@ public class ChestGUI
{
if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER)
{
MinecraftForge.EVENT_BUS.post(new OpenGUIEvent(id, player));
MinecraftForge.EVENT_BUS.post(new OpenGUIEvent(id, player, this));
NetworkHooks.openScreen(ServerUtilities.getPlayerByID(player.toString()), new SimpleMenuProvider(ChestGUIMenu.getServerMenu(this), Component.literal((MenuTitle != "") ? MenuTitle : "No Title")));
}
}
/**
* Send a signal to trigger the GUI to close on the server, then send a signal to the client to also close the interface
*/
public void close()
{
if(LibZontreck.CURRENT_SIDE == LogicalSide.SERVER)
{
MinecraftForge.EVENT_BUS.post(new CloseGUIEvent(this, ServerUtilities.getPlayerByID(player.toString())));
ModMessages.sendToPlayer(new S2CCloseChestGUI(), ServerUtilities.getPlayerByID(player.toString()));
}
}
public boolean isPlayer(UUID ID)
{
return player.equals(ID);

View file

@ -18,7 +18,7 @@ public class ChestGUIButton
{
private Item icon;
private String name;
private List<LoreEntry> tooltipInfo;
private List<LoreEntry> tooltipInfo = new ArrayList<>();
private Runnable callback;
private CompoundTag NBT = new CompoundTag();
@ -43,7 +43,7 @@ public class ChestGUIButton
this.position = position;
LoreContainer container = new LoreContainer(existing);
tooltipInfo = container.miscData.LoreData;
tooltipInfo = container.miscData.loreData;
name = existing.getHoverName().getString();
icon = existing.getItem();
NBT = existing.getTag();
@ -61,19 +61,17 @@ public class ChestGUIButton
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);
LoreContainer cont = new LoreContainer(ret);
cont.clear();
cont.miscData.loreData.addAll(tooltipInfo);
cont.commitLore();
return ret;
}

View file

@ -0,0 +1,33 @@
package dev.zontreck.libzontreck.chestgui;
import dev.zontreck.libzontreck.events.CloseGUIEvent;
import dev.zontreck.libzontreck.events.OpenGUIEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class ChestGUIRegistry
{
private static Map<UUID, ChestGUI> GUIS = new HashMap<>();
@SubscribeEvent
public static void onGuiOpen(final OpenGUIEvent event)
{
GUIS.put(event.getPlayer().getUUID(), event.getGui());
}
@SubscribeEvent
public static void onGuiClose(final CloseGUIEvent event)
{
GUIS.remove(event.player.getUUID());
}
public static ChestGUI get(UUID ID)
{
if(GUIS.containsKey(ID)) return GUIS.get(ID);
return null;
}
}