Skip to content

Commit

Permalink
NetworkHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
TonimatasDEV committed Aug 2, 2023
1 parent f29d4cc commit d2af167
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.tonimatasdev.krystalcraft;

import net.tonimatasdev.krystalcraft.networking.NetworkHandler;
import net.tonimatasdev.krystalcraft.registry.*;

public class KrystalCraft {
Expand All @@ -14,5 +15,6 @@ public static void init() {
ModRecipes.RECIPE_TYPES.init();
ModEnchants.ENCHANTMENTS.init();
ModTabs.TABS.init();
NetworkHandler.init();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<FluidHolder> fluidHolders) implements Packet<ClientboundMachineInfoPacket> {

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<ClientboundMachineInfoPacket> getHandler() {
return HANDLER;
}

private static class Handler implements PacketHandler<ClientboundMachineInfoPacket> {
@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);
}
};
}
}
}

0 comments on commit d2af167

Please sign in to comment.