public record CrystallineFlowerEnchantmentPacket(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
public static Gson gson = new GsonBuilder().create();
public static void sendToClient(ServerPlayer entity, int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
MessageHandler.DEFAULT_CHANNEL.send(PacketDistributor.PLAYER.with(() -> entity),
new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons));
}
/*
* How the client will read the packet.
*/
public static CrystallineFlowerEnchantmentPacket parse(final FriendlyByteBuf buf) {
int containerId = buf.readInt();
List<EnchantmentSkeleton> enchantmentSkeletons = new ArrayList<>();
int elements = buf.readInt();
for (int i = 0; i < elements; i++) {
String jsonData = buf.readUtf();
enchantmentSkeletons.add(gson.fromJson(jsonData, EnchantmentSkeleton.class));
}
return new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons);
}
/*
* creates the packet buffer and sets its values
*/
public static void compose(final CrystallineFlowerEnchantmentPacket pkt, final FriendlyByteBuf buf) {
buf.writeInt(pkt.containerId());
buf.writeInt(pkt.enchantmentSkeletons().size());
for (EnchantmentSkeleton enchantmentSkeleton : pkt.enchantmentSkeletons()) {
buf.writeUtf(gson.toJson(enchantmentSkeleton));
}
}
/*
* What the client will do with the packet
*/
public static class Handler {
//this is what gets run on the client
public static void handle(final CrystallineFlowerEnchantmentPacket pkt, final Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
if (Minecraft.getInstance().player != null && Minecraft.getInstance().player.containerMenu.containerId == pkt.containerId()) {
CrystallineFlowerScreen.enchantmentsAvailable = pkt.enchantmentSkeletons;
}
});
ctx.get().setPacketHandled(true);
}
}
}
public class MessageHandler {
//setup channel to send packages through
private static final String PROTOCOL_VERSION = "1";
public static final SimpleChannel DEFAULT_CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(Bumblezone.MODID, "networking"),
() -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals
);
/*
* Register the channel so it exists
*/
public static void init() {
int channelID = -1; // must increment channelID or issues occur when adding more packets to this setup.
DEFAULT_CHANNEL.registerMessage(++channelID, CrystallineFlowerEnchantmentPacket.class, CrystallineFlowerEnchantmentPacket::compose, CrystallineFlowerEnchantmentPacket::parse, CrystallineFlowerEnchantmentPacket.Handler::handle);
}
}
MessageHandler.init()
is called in mod init.
CrystallineFlowerEnchantmentPacket.sendToClient(serverPlayer, this.containerId, availableEnchantmentsSkeletons);
public record CrystallineFlowerEnchantmentPacket(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
public static ResourceLocation PACKET_ID = new ResourceLocation(Bumblezone.MODID, "crystalline_flower_packet");
public static Gson gson = new GsonBuilder().create();
public static void sendToClient(ServerPlayer serverPlayer, int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
FriendlyByteBuf packetData = compose(containerId, enchantmentSkeletons);
ServerPlayNetworking.send(serverPlayer, CrystallineFlowerEnchantmentPacket.PACKET_ID, packetData);
}
/*
* creates the packet buffer and sets its values
*/
private static FriendlyByteBuf compose(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
FriendlyByteBuf passedData = new FriendlyByteBuf(Unpooled.buffer());
passedData.writeInt(containerId);
passedData.writeInt(enchantmentSkeletons.size());
for (EnchantmentSkeleton skeleton : enchantmentSkeletons) {
passedData.writeUtf(CrystallineFlowerEnchantmentPacket.gson.toJson(skeleton));
}
return passedData;
}
/*
* How the client will read the packet.
*/
private static CrystallineFlowerEnchantmentPacket parse(FriendlyByteBuf passedData) {
int containerId = passedData.readInt();
List<EnchantmentSkeleton> enchantmentSkeletons = new ArrayList<>();
int elements = passedData.readInt();
for (int i = 0; i < elements; i++) {
String jsonData = passedData.readUtf();
enchantmentSkeletons.add(gson.fromJson(jsonData, EnchantmentSkeleton.class));
}
return new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons);
}
/*
* What the client will do with the packet
*/
public static void registerPacket() {
ClientPlayNetworking.registerGlobalReceiver(PACKET_ID,
(client, handler, buf, responseSender) -> {
CrystallineFlowerEnchantmentPacket packetData = parse(buf);
client.execute(() -> {
if(Minecraft.getInstance().player != null &&
Minecraft.getInstance().player.containerMenu.containerId == packetData.containerId())
{
CrystallineFlowerScreen.enchantmentsAvailable = packetData.enchantmentSkeletons();
}
});
});
}
}
CrystallineFlowerEnchantmentPacket.registerPacket()
is called in mod init.
CrystallineFlowerEnchantmentPacket.sendToClient(serverPlayer, this.containerId, availableEnchantmentsSkeletons);