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

@ -2,6 +2,7 @@ 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;
@ -51,6 +52,11 @@ 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();
}

View file

@ -0,0 +1,55 @@
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<NetworkEvent.Context> 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;
}
}

View file

@ -2,9 +2,11 @@ package dev.zontreck.libzontreck.networking.packets;
import java.util.function.Supplier;
import dev.zontreck.libzontreck.events.OpenGUIEvent;
import dev.zontreck.libzontreck.networking.structures.OpenGUIRequest;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.network.NetworkEvent;
/**
@ -36,6 +38,8 @@ public class ChestGUIOpenC2S {
ctx.enqueueWork(()->{
// We are on the server!
OpenGUIRequest req = new OpenGUIRequest(data);
MinecraftForge.EVENT_BUS.post(new OpenGUIEvent(req.ID, req.playerID));
});
return true;

View file

@ -7,37 +7,30 @@ import java.util.UUID;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
public class OpenGUIRequest {
public List<ItemStack> options = new ArrayList<>();
public String GUITitle;
public ResourceLocation ID;
public UUID playerID;
public OpenGUIRequest(CompoundTag tag)
{
ListTag items = tag.getList("items", Tag.TAG_COMPOUND);
CompoundTag tags = tag.getCompound("id");
for(Tag tags : items)
{
ItemStack is = ItemStack.of((CompoundTag)tags);
options.add(is);
}
GUITitle = tag.getString("title");
ID = new ResourceLocation(tags.getString("mod"), tags.getString("id"));
playerID = tag.getUUID("player");
}
public CompoundTag serialize()
{
CompoundTag tag = new CompoundTag();
tag.putString("title", GUITitle);
tag.putUUID("player", playerID);
ListTag lst = new ListTag();
for (ItemStack itemStack : options) {
lst.add(itemStack.serializeNBT());
}
tag.put("items", lst);
CompoundTag tags = new CompoundTag();
tags.putString("mod", ID.getNamespace());
tags.putString("id", ID.getPath());
tag.put("id", tags);
return tag;
}