From 028c9c153f6fc6dfed66aa391f442124c8037af3 Mon Sep 17 00:00:00 2001 From: Siuolplex Date: Tue, 16 Jul 2024 14:25:34 -0500 Subject: [PATCH] Net2 Work Continued I am currently hopped up on pain meds after my wisdom teeth so this is probably not my best code Signed-off-by: Siuolplex --- .../violetmoon/zeta/net2/NetworkPhase.java | 5 ++ .../zeta/net2/ZetaClientLoginNetworking.java | 36 --------- .../zeta/net2/ZetaServerLoginNetworking.java | 37 --------- .../event/load/ZRegisterPayloadHandlers.java | 13 +++ .../ZetaClientboundHandshakePacket.java | 49 ------------ .../ZetaServerboundHandshakePacket.java | 48 ----------- .../ZetaClientLoginPacketListener.java | 14 ---- .../ZetaServerLoginPacketListener.java | 15 ---- .../zeta/net2/payload/SyncPayload.java | 22 ++++++ .../zeta/net2/payload/ZetaPacketPayload.java | 7 ++ .../load/ForgeZRegisterPayloadHandlers.java | 79 +++++++++++++++++++ .../net2/handlers/ZetaPayloadHandler.java | 6 ++ 12 files changed, 132 insertions(+), 199 deletions(-) create mode 100644 src/main/java/org/violetmoon/zeta/net2/NetworkPhase.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/ZetaClientLoginNetworking.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/ZetaServerLoginNetworking.java create mode 100644 src/main/java/org/violetmoon/zeta/net2/event/load/ZRegisterPayloadHandlers.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/packet/ZetaClientboundHandshakePacket.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/packet/ZetaServerboundHandshakePacket.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaClientLoginPacketListener.java delete mode 100644 src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaServerLoginPacketListener.java create mode 100644 src/main/java/org/violetmoon/zeta/net2/payload/SyncPayload.java create mode 100644 src/main/java/org/violetmoon/zeta/net2/payload/ZetaPacketPayload.java create mode 100644 src/main/java/org/violetmoon/zetaimplforge/net2/event/load/ForgeZRegisterPayloadHandlers.java create mode 100644 src/main/java/org/violetmoon/zetaimplforge/net2/handlers/ZetaPayloadHandler.java diff --git a/src/main/java/org/violetmoon/zeta/net2/NetworkPhase.java b/src/main/java/org/violetmoon/zeta/net2/NetworkPhase.java new file mode 100644 index 0000000..b43c76d --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/net2/NetworkPhase.java @@ -0,0 +1,5 @@ +package org.violetmoon.zeta.net2; + +public enum NetworkPhase { + PLAY, LOGIN, ANY +} diff --git a/src/main/java/org/violetmoon/zeta/net2/ZetaClientLoginNetworking.java b/src/main/java/org/violetmoon/zeta/net2/ZetaClientLoginNetworking.java deleted file mode 100644 index 6aeed49..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/ZetaClientLoginNetworking.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.violetmoon.zeta.net2; - -import net.minecraft.client.Minecraft; -import net.minecraft.network.Connection; -import net.minecraft.network.DisconnectionDetails; -import org.violetmoon.zeta.config.SyncedFlagHandler; -import org.violetmoon.zeta.net2.packet.ZetaClientboundHandshakePacket; -import org.violetmoon.zeta.net2.packet.ZetaServerboundHandshakePacket; -import org.violetmoon.zeta.net2.packet.listener.ZetaClientLoginPacketListener; - -public class ZetaClientLoginNetworking implements ZetaClientLoginPacketListener { - private final Minecraft minecraft; - private final Connection connection; - - public ZetaClientLoginNetworking(Minecraft minecraft, Connection connection) { - this.minecraft = minecraft; - this.connection = connection; - } - - @Override - public void handleHandshake(ZetaClientboundHandshakePacket packet) { - if(packet.expectedLength == SyncedFlagHandler.expectedLength() && packet.expectedHash == SyncedFlagHandler.expectedHash()) - SyncedFlagHandler.receiveFlagInfoFromServer(packet.flags); - connection.send(new ZetaServerboundHandshakePacket()); - } - - @Override - public void onDisconnect(DisconnectionDetails p_350287_) { - - } - - @Override - public boolean isAcceptingMessages() { - return true; - } -} diff --git a/src/main/java/org/violetmoon/zeta/net2/ZetaServerLoginNetworking.java b/src/main/java/org/violetmoon/zeta/net2/ZetaServerLoginNetworking.java deleted file mode 100644 index 180c959..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/ZetaServerLoginNetworking.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.violetmoon.zeta.net2; - -import net.minecraft.network.Connection; -import net.minecraft.network.DisconnectionDetails; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerPlayer; -import org.violetmoon.zeta.config.SyncedFlagHandler; -import org.violetmoon.zeta.net2.packet.ZetaServerboundHandshakePacket; -import org.violetmoon.zeta.net2.packet.listener.ZetaServerLoginPacketListener; - -public class ZetaServerLoginNetworking implements ZetaServerLoginPacketListener { - private final MinecraftServer server; - private final Connection connection; - private final ServerPlayer player; - - public ZetaServerLoginNetworking(MinecraftServer server, Connection connection, ServerPlayer player) { - this.server = server; - this.connection = connection; - this.player = player; - } - - @Override - public void handleHandshake(ZetaServerboundHandshakePacket packet) { - if(packet.expectedLength == SyncedFlagHandler.expectedLength() && packet.expectedHash == SyncedFlagHandler.expectedHash()) - SyncedFlagHandler.receiveFlagInfoFromPlayer(player, packet.flags); - } - - @Override - public void onDisconnect(DisconnectionDetails p_350287_) { - - } - - @Override - public boolean isAcceptingMessages() { - return true; - } -} diff --git a/src/main/java/org/violetmoon/zeta/net2/event/load/ZRegisterPayloadHandlers.java b/src/main/java/org/violetmoon/zeta/net2/event/load/ZRegisterPayloadHandlers.java new file mode 100644 index 0000000..0b79185 --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/net2/event/load/ZRegisterPayloadHandlers.java @@ -0,0 +1,13 @@ +package org.violetmoon.zeta.net2.event.load; + +import com.mojang.serialization.Codec; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import org.violetmoon.zeta.event.bus.IZetaLoadEvent; +import org.violetmoon.zeta.net2.NetworkPhase; + +public interface ZRegisterPayloadHandlers extends IZetaLoadEvent { + void registerServerboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase); + void registerClientboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase); + void registerBidirectionalPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase); +} diff --git a/src/main/java/org/violetmoon/zeta/net2/packet/ZetaClientboundHandshakePacket.java b/src/main/java/org/violetmoon/zeta/net2/packet/ZetaClientboundHandshakePacket.java deleted file mode 100644 index dd768af..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/packet/ZetaClientboundHandshakePacket.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.violetmoon.zeta.net2.packet; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.network.protocol.Packet; -import net.minecraft.network.protocol.PacketFlow; -import net.minecraft.network.protocol.PacketType; -import net.minecraft.resources.ResourceLocation; -import org.violetmoon.zeta.config.SyncedFlagHandler; -import org.violetmoon.zeta.net2.packet.listener.ZetaClientLoginPacketListener; - -import java.util.BitSet; - -public class ZetaClientboundHandshakePacket implements Packet { - public static final StreamCodec STREAM_CODEC = Packet.codec( - ZetaClientboundHandshakePacket::write, ZetaClientboundHandshakePacket::new - ); - public BitSet flags; - public int expectedLength; - public int expectedHash; - public ZetaClientboundHandshakePacket() { - flags = SyncedFlagHandler.compileFlagInfo(); - expectedLength = SyncedFlagHandler.expectedLength(); - expectedHash = SyncedFlagHandler.expectedHash(); - } - - private ZetaClientboundHandshakePacket(FriendlyByteBuf friendlyByteBuf) { - this.flags = friendlyByteBuf.readBitSet(); - this.expectedLength = friendlyByteBuf.readInt(); - this.expectedHash = friendlyByteBuf.readInt(); - } - - private void write(FriendlyByteBuf friendlyByteBuf) { - friendlyByteBuf.writeInt(expectedHash); - friendlyByteBuf.writeInt(expectedLength); - friendlyByteBuf.writeBitSet(flags); - } - - public PacketType type() { - return new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("zeta", "login_flag")); - } - - public void handle(ZetaClientLoginPacketListener packetListener) { - if(expectedLength == SyncedFlagHandler.expectedLength() && expectedHash == SyncedFlagHandler.expectedHash()) - SyncedFlagHandler.receiveFlagInfoFromServer(flags); - - //network.send(serverboundLoginFlag); - } -} diff --git a/src/main/java/org/violetmoon/zeta/net2/packet/ZetaServerboundHandshakePacket.java b/src/main/java/org/violetmoon/zeta/net2/packet/ZetaServerboundHandshakePacket.java deleted file mode 100644 index 583b173..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/packet/ZetaServerboundHandshakePacket.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.violetmoon.zeta.net2.packet; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.network.protocol.Packet; -import net.minecraft.network.protocol.PacketFlow; -import net.minecraft.network.protocol.PacketType; -import net.minecraft.network.protocol.login.ServerLoginPacketListener; -import net.minecraft.resources.ResourceLocation; -import org.violetmoon.zeta.config.SyncedFlagHandler; -import org.violetmoon.zeta.net2.packet.listener.ZetaServerLoginPacketListener; - -import java.util.BitSet; - -public class ZetaServerboundHandshakePacket implements Packet { - public static final StreamCodec STREAM_CODEC = Packet.codec( - ZetaServerboundHandshakePacket::write, ZetaServerboundHandshakePacket::new - ); - public BitSet flags; - public int expectedLength; - public int expectedHash; - public ZetaServerboundHandshakePacket() { - flags = SyncedFlagHandler.compileFlagInfo(); - expectedLength = SyncedFlagHandler.expectedLength(); - expectedHash = SyncedFlagHandler.expectedHash(); - } - - private ZetaServerboundHandshakePacket(FriendlyByteBuf friendlyByteBuf) { - this.flags = friendlyByteBuf.readBitSet(); - this.expectedLength = friendlyByteBuf.readInt(); - this.expectedHash = friendlyByteBuf.readInt(); - } - - private void write(FriendlyByteBuf friendlyByteBuf) { - friendlyByteBuf.writeInt(expectedHash); - friendlyByteBuf.writeInt(expectedLength); - friendlyByteBuf.writeBitSet(flags); - } - - public PacketType type() { - return new PacketType<>(PacketFlow.SERVERBOUND, ResourceLocation.fromNamespaceAndPath("zeta", "login_flag")); - } - - @Override - public void handle(ZetaServerLoginPacketListener packetListener) { - packetListener.handleHandshake(this); - } -} diff --git a/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaClientLoginPacketListener.java b/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaClientLoginPacketListener.java deleted file mode 100644 index f4f7064..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaClientLoginPacketListener.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.violetmoon.zeta.net2.packet.listener; - -import net.minecraft.network.ClientboundPacketListener; -import net.minecraft.network.ConnectionProtocol; -import org.violetmoon.zeta.net2.packet.ZetaClientboundHandshakePacket; - -public interface ZetaClientLoginPacketListener extends ClientboundPacketListener { - @Override - default ConnectionProtocol protocol() { - return ConnectionProtocol.LOGIN; - } - - void handleHandshake(ZetaClientboundHandshakePacket packet); -} diff --git a/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaServerLoginPacketListener.java b/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaServerLoginPacketListener.java deleted file mode 100644 index 2a37053..0000000 --- a/src/main/java/org/violetmoon/zeta/net2/packet/listener/ZetaServerLoginPacketListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.violetmoon.zeta.net2.packet.listener; - -import net.minecraft.network.ConnectionProtocol; -import net.minecraft.network.ServerboundPacketListener; -import net.minecraft.network.protocol.game.ServerPacketListener; -import org.violetmoon.zeta.net2.packet.ZetaServerboundHandshakePacket; - -public interface ZetaServerLoginPacketListener extends ServerPacketListener { - @Override - default ConnectionProtocol protocol() { - return ConnectionProtocol.LOGIN; - } - - void handleHandshake(ZetaServerboundHandshakePacket packet); -} diff --git a/src/main/java/org/violetmoon/zeta/net2/payload/SyncPayload.java b/src/main/java/org/violetmoon/zeta/net2/payload/SyncPayload.java new file mode 100644 index 0000000..15a24b7 --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/net2/payload/SyncPayload.java @@ -0,0 +1,22 @@ +package org.violetmoon.zeta.net2.payload; + +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import org.violetmoon.zeta.Zeta; + +public record SyncPayload(byte[] flags, int expectedLength, int expectedHash) implements ZetaPacketPayload { + public static final CustomPacketPayload.Type TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(Zeta.ZETA_ID, "handshake_payload")); + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.BYTE_ARRAY, SyncPayload::flags, + ByteBufCodecs.INT, SyncPayload::expectedLength, + ByteBufCodecs.INT, SyncPayload::expectedHash, + SyncPayload::new + ); + @Override + public Type type() { + return TYPE; + } +} diff --git a/src/main/java/org/violetmoon/zeta/net2/payload/ZetaPacketPayload.java b/src/main/java/org/violetmoon/zeta/net2/payload/ZetaPacketPayload.java new file mode 100644 index 0000000..552e29f --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/net2/payload/ZetaPacketPayload.java @@ -0,0 +1,7 @@ +package org.violetmoon.zeta.net2.payload; + +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; + +public interface ZetaPacketPayload extends CustomPacketPayload { + +} diff --git a/src/main/java/org/violetmoon/zetaimplforge/net2/event/load/ForgeZRegisterPayloadHandlers.java b/src/main/java/org/violetmoon/zetaimplforge/net2/event/load/ForgeZRegisterPayloadHandlers.java new file mode 100644 index 0000000..50b62a7 --- /dev/null +++ b/src/main/java/org/violetmoon/zetaimplforge/net2/event/load/ForgeZRegisterPayloadHandlers.java @@ -0,0 +1,79 @@ +package org.violetmoon.zetaimplforge.net2.event.load; + +import com.mojang.serialization.Codec; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; +import net.neoforged.neoforge.network.registration.PayloadRegistrar; +import org.violetmoon.zeta.Zeta; +import org.violetmoon.zeta.net2.NetworkPhase; +import org.violetmoon.zeta.net2.event.load.ZRegisterPayloadHandlers; +import org.violetmoon.zetaimplforge.net2.handlers.ZetaPayloadHandler; + +public class ForgeZRegisterPayloadHandlers implements ZRegisterPayloadHandlers { + private final RegisterPayloadHandlersEvent e; + + public ForgeZRegisterPayloadHandlers(RegisterPayloadHandlersEvent event) { + this.e = event; + } + + @Override + public void registerServerboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) { + if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) { + final PayloadRegistrar registrar = e.registrar("1"); + switch (phase) { + case PLAY: { + registrar.playToServer(type, codec, zetaPayloadHandler); + } + case LOGIN: { + registrar.configurationToServer(type, codec, zetaPayloadHandler); + } + case ANY: { + registrar.commonToServer(type, codec, zetaPayloadHandler); + } + } + } else { + Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message + } + } + + @Override + public void registerClientboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) { + if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) { + final PayloadRegistrar registrar = e.registrar("1"); + switch (phase) { + case PLAY: { + registrar.playToClient(type, codec, zetaPayloadHandler); + } + case LOGIN: { + registrar.configurationToClient(type, codec, zetaPayloadHandler); + } + case ANY: { + registrar.commonToClient(type, codec, zetaPayloadHandler); + } + } + } else { + Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message + } + } + + @Override + public void registerBidirectionalPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) { + if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) { + final PayloadRegistrar registrar = e.registrar("1"); + switch (phase) { + case PLAY: { + registrar.playBidirectional(type, codec, zetaPayloadHandler); + } + case LOGIN: { + registrar.configurationBidirectional(type, codec, zetaPayloadHandler); + } + case ANY: { + registrar.commonBidirectional(type, codec, zetaPayloadHandler); + } + } + } else { + Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message + } + } +} diff --git a/src/main/java/org/violetmoon/zetaimplforge/net2/handlers/ZetaPayloadHandler.java b/src/main/java/org/violetmoon/zetaimplforge/net2/handlers/ZetaPayloadHandler.java new file mode 100644 index 0000000..a681bd8 --- /dev/null +++ b/src/main/java/org/violetmoon/zetaimplforge/net2/handlers/ZetaPayloadHandler.java @@ -0,0 +1,6 @@ +package org.violetmoon.zetaimplforge.net2.handlers; + +import net.neoforged.neoforge.network.handling.IPayloadHandler; + +//todo: Im putting this in for now, but it will probably go byebye +public interface ZetaPayloadHandler extends IPayloadHandler {}