Begin adding DynamicChest UI, and finish adding the HeadUtilities

This commit is contained in:
Aria 2023-03-01 02:19:47 -07:00
parent 5c70fb1291
commit 8fde794b65
23 changed files with 662 additions and 7 deletions

View file

@ -0,0 +1,54 @@
package dev.zontreck.libzontreck.networking;
import dev.zontreck.libzontreck.LibZontreck;
import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;
/**
* Networking system!
*/
public class ModMessages {
private static SimpleChannel INSTANCE;
private static int PACKET_ID=0;
private static int id()
{
return PACKET_ID++;
}
public static void register()
{
SimpleChannel net = NetworkRegistry.ChannelBuilder.named(new ResourceLocation(LibZontreck.MOD_ID, "messages"))
.networkProtocolVersion(()->"1.0")
.clientAcceptedVersions(s->true)
.serverAcceptedVersions(s->true)
.simpleChannel();
INSTANCE=net;
net.messageBuilder(ChestGUIOpenC2S.class, id(), NetworkDirection.PLAY_TO_SERVER)
.decoder(ChestGUIOpenC2S::new)
.encoder(ChestGUIOpenC2S::toBytes)
.consumer(ChestGUIOpenC2S::handle)
.add();
}
public static <MSG> void sendToServer(MSG message){
INSTANCE.sendToServer(message);
}
public static <MSG> void sendToPlayer(MSG message, ServerPlayer player)
{
INSTANCE.send(PacketDistributor.PLAYER.with(()->player), message);
}
public static <MSG> void sendToAll(MSG message)
{
INSTANCE.send(PacketDistributor.ALL.noArg(), message);
}
}

View file

@ -0,0 +1,21 @@
About Chest GUI
=====
A chest GUI is basically a dynamic menu that uses items and the standard chest layout to present a list of options in game.
These items cannot be removed from the chest and the click event is instead passed on as a ChestGUIEvent. Because the mod requesting this might not be on the client, the event is sent in both locations by utilizing a network packet.
ChestGUIEvent
====
This event is the parent of several other events.
OptionInteractEvent
----
This event gets dispatched on both the client and server when a option is interacted with.
OptionUpdateEvent
----
To be sent by the mod originating this dynamic menu. This event will instruct the ChestGUI to update a item, or multiple items after a interaction, or something else occuring. If the GUI is not open, this event gets ignored. This event should only be sent when we know the GUI is actually open!

View file

@ -0,0 +1,43 @@
package dev.zontreck.libzontreck.networking.packets;
import java.util.function.Supplier;
import dev.zontreck.libzontreck.networking.structures.OpenGUIRequest;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
/**
* To be used by first-party and third-party mods to assemble a menu
* NOTE: Without the server, only the credits menu will be able to be opened, which is the only built-in menu utilizing this system.
*/
public class ChestGUIOpenC2S {
private CompoundTag data;
public ChestGUIOpenC2S(OpenGUIRequest request)
{
data = request.serialize();
}
public ChestGUIOpenC2S(FriendlyByteBuf buf)
{
data = buf.readAnySizeNbt();
}
public void toBytes(FriendlyByteBuf buf)
{
buf.writeNbt(data);
}
public boolean handle(Supplier<NetworkEvent.Context> supplier)
{
NetworkEvent.Context ctx = supplier.get();
ctx.enqueueWork(()->{
// We are on the server!
OpenGUIRequest req = new OpenGUIRequest(data);
});
return true;
}
}

View file

@ -0,0 +1,48 @@
package dev.zontreck.libzontreck.networking.structures;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
public class OpenGUIRequest {
public List<ItemStack> options = new ArrayList<>();
public String GUITitle;
public UUID playerID;
public OpenGUIRequest(CompoundTag tag)
{
ListTag items = tag.getList("items", Tag.TAG_COMPOUND);
for(Tag tags : items)
{
ItemStack is = ItemStack.of((CompoundTag)tags);
options.add(is);
}
GUITitle = tag.getString("title");
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);
return tag;
}
public OpenGUIRequest(){}
}