Finish implementing initial chest-gui interface API
This commit is contained in:
parent
b034b2b388
commit
848b6f495b
9 changed files with 108 additions and 110 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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<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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue