diff --git a/common/src/main/java/net/tonimatasdev/krystalcraft/KrystalCraft.java b/common/src/main/java/net/tonimatasdev/krystalcraft/KrystalCraft.java index 4d80a481..813864a5 100644 --- a/common/src/main/java/net/tonimatasdev/krystalcraft/KrystalCraft.java +++ b/common/src/main/java/net/tonimatasdev/krystalcraft/KrystalCraft.java @@ -1,5 +1,6 @@ package net.tonimatasdev.krystalcraft; +import net.tonimatasdev.krystalcraft.networking.NetworkHandler; import net.tonimatasdev.krystalcraft.registry.*; public class KrystalCraft { @@ -14,5 +15,6 @@ public static void init() { ModRecipes.RECIPE_TYPES.init(); ModEnchants.ENCHANTMENTS.init(); ModTabs.TABS.init(); + NetworkHandler.init(); } } diff --git a/common/src/main/java/net/tonimatasdev/krystalcraft/networking/NetworkHandler.java b/common/src/main/java/net/tonimatasdev/krystalcraft/networking/NetworkHandler.java new file mode 100644 index 00000000..03158206 --- /dev/null +++ b/common/src/main/java/net/tonimatasdev/krystalcraft/networking/NetworkHandler.java @@ -0,0 +1,14 @@ +package net.tonimatasdev.krystalcraft.networking; + +import com.teamresourceful.resourcefullib.common.networking.NetworkChannel; +import com.teamresourceful.resourcefullib.common.networking.base.NetworkDirection; +import net.tonimatasdev.krystalcraft.KrystalCraft; +import net.tonimatasdev.krystalcraft.networking.packet.messages.ClientboundMachineInfoPacket; + +public class NetworkHandler { + public static final NetworkChannel CHANNEL = new NetworkChannel(KrystalCraft.MOD_ID, 0, "main"); + + public static void init() { + CHANNEL.registerPacket(NetworkDirection.SERVER_TO_CLIENT, ClientboundMachineInfoPacket.ID, ClientboundMachineInfoPacket.HANDLER, ClientboundMachineInfoPacket.class); + } +} diff --git a/common/src/main/java/net/tonimatasdev/krystalcraft/networking/packet/messages/ClientboundMachineInfoPacket.java b/common/src/main/java/net/tonimatasdev/krystalcraft/networking/packet/messages/ClientboundMachineInfoPacket.java new file mode 100644 index 00000000..543ff24a --- /dev/null +++ b/common/src/main/java/net/tonimatasdev/krystalcraft/networking/packet/messages/ClientboundMachineInfoPacket.java @@ -0,0 +1,56 @@ +package net.tonimatasdev.krystalcraft.networking.packet.messages; + +import com.teamresourceful.resourcefullib.common.networking.base.Packet; +import com.teamresourceful.resourcefullib.common.networking.base.PacketContext; +import com.teamresourceful.resourcefullib.common.networking.base.PacketHandler; +import earth.terrarium.botarium.common.fluid.base.FluidHolder; +import earth.terrarium.botarium.common.fluid.utils.FluidHooks; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.resources.ResourceLocation; +import net.tonimatasdev.krystalcraft.KrystalCraft; +import net.tonimatasdev.krystalcraft.menu.AbstractMachineMenu; + +import java.util.List; + +public record ClientboundMachineInfoPacket(long energy, List fluidHolders) implements Packet { + + public static final ResourceLocation ID = new ResourceLocation(KrystalCraft.MOD_ID, "machine_info"); + public static final Handler HANDLER = new Handler(); + + @Override + public ResourceLocation getID() { + return ID; + } + + @Override + public PacketHandler getHandler() { + return HANDLER; + } + + private static class Handler implements PacketHandler { + @Override + public void encode(ClientboundMachineInfoPacket packet, FriendlyByteBuf buf) { + buf.writeLong(packet.energy()); + buf.writeCollection(packet.fluidHolders, (buf2, fluid) -> { + buf2.writeResourceLocation(BuiltInRegistries.FLUID.getKey(fluid.getFluid())); + buf2.writeLong(fluid.getFluidAmount()); + }); + } + + @Override + public ClientboundMachineInfoPacket decode(FriendlyByteBuf buf) { + return new ClientboundMachineInfoPacket(buf.readLong(), buf.readList(buf2 -> FluidHooks.newFluidHolder(BuiltInRegistries.FLUID.get(buf2.readResourceLocation()), buf2.readLong(), null))); + } + + @Override + public PacketContext handle(ClientboundMachineInfoPacket packet) { + return (player, level) -> { + if (player.containerMenu instanceof AbstractMachineMenu handler) { + handler.setEnergyAmount(packet.energy()); + handler.setFluids(packet.fluidHolders); + } + }; + } + } +}