-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f29d4cc
commit d2af167
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
common/src/main/java/net/tonimatasdev/krystalcraft/networking/NetworkHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...et/tonimatasdev/krystalcraft/networking/packet/messages/ClientboundMachineInfoPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
} | ||
} |