From 92fbb6eb8d1e163b1d83f8f9c154720346e86047 Mon Sep 17 00:00:00 2001 From: quat1024 Date: Fri, 10 Nov 2023 22:01:22 -0500 Subject: [PATCH] I survived Forge networking code and all I got was this lousy tshirt --- .../quark/base/network/QuarkNetwork.java | 48 ++-------- .../message/structural/C2SLoginFlag.java | 23 +---- .../message/structural/HandshakeMessage.java | 29 ------ .../message/structural/S2CLoginFlag.java | 25 ++---- .../tools/module/ColorRunesModule.java | 3 +- src/main/java/org/violetmoon/zeta/Zeta.java | 3 +- .../violetmoon/zeta/network/IZetaMessage.java | 4 +- .../network/IZetaNetworkEventContext.java | 3 +- .../zeta/network/ZetaHandshakeMessage.java | 22 +++++ .../zeta/network/ZetaMessageSerializer.java | 13 ++- .../zeta/network/ZetaNetworkDirection.java | 8 +- .../zeta/network/ZetaNetworkHandler.java | 17 +++- .../violetmoon/zetaimplforge/ForgeZeta.java | 4 +- .../network/ForgeNetworkEventContextImpl.java | 11 ++- .../network/ForgeZetaNetworkHandler.java | 88 +++++++++++++------ 15 files changed, 149 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/org/violetmoon/quark/base/network/message/structural/HandshakeMessage.java create mode 100644 src/main/java/org/violetmoon/zeta/network/ZetaHandshakeMessage.java diff --git a/src/main/java/org/violetmoon/quark/base/network/QuarkNetwork.java b/src/main/java/org/violetmoon/quark/base/network/QuarkNetwork.java index 166d55fc14..2b6359b1e6 100644 --- a/src/main/java/org/violetmoon/quark/base/network/QuarkNetwork.java +++ b/src/main/java/org/violetmoon/quark/base/network/QuarkNetwork.java @@ -8,15 +8,11 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; -import net.minecraftforge.network.HandshakeHandler; import net.minecraftforge.network.NetworkDirection; -import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.simple.SimpleChannel; import java.time.Instant; import java.util.BitSet; -import java.util.function.BiConsumer; -import java.util.function.Supplier; import org.violetmoon.quark.base.Quark; import org.violetmoon.quark.base.network.message.*; @@ -31,21 +27,16 @@ import org.violetmoon.zeta.network.IZetaMessage; import org.violetmoon.zeta.network.ZetaNetworkDirection; import org.violetmoon.zeta.network.ZetaNetworkHandler; -import org.violetmoon.zetaimplforge.network.ForgeZetaNetworkHandler; public final class QuarkNetwork { - private static final int PROTOCOL_VERSION = 2; + private static final int PROTOCOL_VERSION = 3; private static ZetaNetworkHandler network; - @Deprecated(forRemoval = true) - private static SimpleChannel channel; - @LoadEvent public static void setup(ZCommonSetup event) { - network = Quark.ZETA.createNetworkHandler(Quark.MOD_ID, PROTOCOL_VERSION); - channel = ((ForgeZetaNetworkHandler) network).channel; //TODO: LEAKY ABSTRACTION + network = Quark.ZETA.createNetworkHandler(PROTOCOL_VERSION); network.getSerializer().mapHandlers(Instant.class, (buf, field) -> buf.readInstant(), (buf, field, instant) -> buf.writeInstant(instant)); network.getSerializer().mapHandlers(MessageSignature.class, (buf, field) -> new MessageSignature(buf), (buf, field, signature) -> signature.write(buf)); @@ -80,34 +71,9 @@ public static void setup(ZCommonSetup event) { network.register(S2CUpdateFlag.class, ZetaNetworkDirection.PLAY_TO_CLIENT); network.register(C2SUpdateFlag.class, ZetaNetworkDirection.PLAY_TO_SERVER); - - loginIndexedBuilder(S2CLoginFlag.class, 98, NetworkDirection.LOGIN_TO_CLIENT) - .decoder(S2CLoginFlag::new) - .consumerNetworkThread(loginPacketHandler()) - .buildLoginPacketList(S2CLoginFlag::generateRegistryPackets) - .add(); - loginIndexedBuilder(C2SLoginFlag.class, 99, NetworkDirection.LOGIN_TO_SERVER) - .decoder(C2SLoginFlag::new) - .consumerNetworkThread(loginIndexFirst(loginPacketHandler())) - .noResponse() - .add(); - } - - private static SimpleChannel.MessageBuilder loginIndexedBuilder(Class clazz, int id, NetworkDirection direction) { - return channel.messageBuilder(clazz, id, direction) - .loginIndex(HandshakeMessage::getLoginIndex, HandshakeMessage::setLoginIndex) - .encoder(HandshakeMessage::encode); - } - - private static BiConsumer> loginPacketHandler() { - return (msg, contextSupplier) -> { - NetworkEvent.Context context = contextSupplier.get(); - context.setPacketHandled(msg.consume(context, channel::reply)); - }; - } - - private static BiConsumer> loginIndexFirst(BiConsumer> toWrap) { - return HandshakeHandler.indexFirst((handler, msg, context) -> toWrap.accept(msg, context)); + // Login + network.registerLogin(S2CLoginFlag.class, ZetaNetworkDirection.LOGIN_TO_CLIENT, 98, true, S2CLoginFlag::generateRegistryPackets); + network.registerLogin(C2SLoginFlag.class, ZetaNetworkDirection.LOGIN_TO_SERVER, 99, false, null); } public static void sendToPlayer(IZetaMessage msg, ServerPlayer player) { @@ -139,8 +105,8 @@ public static void sendToAllPlayers(IZetaMessage msg, MinecraftServer server) { network.sendToAllPlayers(msg, server); } - public static Packet toVanillaPacket(IZetaMessage msg, NetworkDirection direction) { - return channel.toVanillaPacket(msg, direction); + public static Packet toVanillaPacket(IZetaMessage msg, ZetaNetworkDirection dir) { + return network.wrapInVanilla(msg, dir); } } diff --git a/src/main/java/org/violetmoon/quark/base/network/message/structural/C2SLoginFlag.java b/src/main/java/org/violetmoon/quark/base/network/message/structural/C2SLoginFlag.java index 0d46271ad4..070e76fa2d 100644 --- a/src/main/java/org/violetmoon/quark/base/network/message/structural/C2SLoginFlag.java +++ b/src/main/java/org/violetmoon/quark/base/network/message/structural/C2SLoginFlag.java @@ -1,14 +1,12 @@ package org.violetmoon.quark.base.network.message.structural; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - import java.util.BitSet; -import java.util.function.BiConsumer; import org.violetmoon.quark.base.config.SyncedFlagHandler; +import org.violetmoon.zeta.network.IZetaNetworkEventContext; +import org.violetmoon.zeta.network.ZetaHandshakeMessage; -public class C2SLoginFlag extends HandshakeMessage { +public class C2SLoginFlag extends ZetaHandshakeMessage { public BitSet flags; public int expectedLength; @@ -20,21 +18,8 @@ public C2SLoginFlag() { expectedHash = SyncedFlagHandler.expectedHash(); } - public C2SLoginFlag(FriendlyByteBuf buf) { - this.flags = BitSet.valueOf(buf.readLongArray()); - this.expectedLength = buf.readInt(); - this.expectedHash = buf.readInt(); - } - - @Override - public void encode(FriendlyByteBuf buf) { - buf.writeLongArray(flags.toLongArray()); - buf.writeInt(expectedLength); - buf.writeInt(expectedHash); - } - @Override - public boolean consume(NetworkEvent.Context context, BiConsumer reply) { + public boolean receive(IZetaNetworkEventContext context) { if (expectedLength == SyncedFlagHandler.expectedLength() && expectedHash == SyncedFlagHandler.expectedHash()) SyncedFlagHandler.receiveFlagInfoFromPlayer(context.getSender(), flags); return true; diff --git a/src/main/java/org/violetmoon/quark/base/network/message/structural/HandshakeMessage.java b/src/main/java/org/violetmoon/quark/base/network/message/structural/HandshakeMessage.java deleted file mode 100644 index c6f53ad5dc..0000000000 --- a/src/main/java/org/violetmoon/quark/base/network/message/structural/HandshakeMessage.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.violetmoon.quark.base.network.message.structural; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - -import java.util.function.BiConsumer; -import java.util.function.IntSupplier; - -// Not an IMessage to avoid serializing things like loginIndex -public abstract class HandshakeMessage implements IntSupplier { - private int loginIndex; - - public void setLoginIndex(final int loginIndex) { - this.loginIndex = loginIndex; - } - - public int getLoginIndex() { - return loginIndex; - } - - @Override - public int getAsInt() { - return loginIndex; - } - - public abstract void encode(FriendlyByteBuf buf); - - public abstract boolean consume(NetworkEvent.Context context, BiConsumer reply); -} diff --git a/src/main/java/org/violetmoon/quark/base/network/message/structural/S2CLoginFlag.java b/src/main/java/org/violetmoon/quark/base/network/message/structural/S2CLoginFlag.java index 94b5034406..ee4b9fb224 100644 --- a/src/main/java/org/violetmoon/quark/base/network/message/structural/S2CLoginFlag.java +++ b/src/main/java/org/violetmoon/quark/base/network/message/structural/S2CLoginFlag.java @@ -1,16 +1,15 @@ package org.violetmoon.quark.base.network.message.structural; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; import org.apache.commons.lang3.tuple.Pair; import org.violetmoon.quark.base.config.SyncedFlagHandler; +import org.violetmoon.zeta.network.IZetaNetworkEventContext; +import org.violetmoon.zeta.network.ZetaHandshakeMessage; import java.util.BitSet; import java.util.Collections; import java.util.List; -import java.util.function.BiConsumer; -public class S2CLoginFlag extends HandshakeMessage { +public class S2CLoginFlag extends ZetaHandshakeMessage { public BitSet flags; public int expectedLength; @@ -22,19 +21,7 @@ public S2CLoginFlag() { expectedHash = SyncedFlagHandler.expectedHash(); } - public S2CLoginFlag(FriendlyByteBuf buf) { - this.flags = BitSet.valueOf(buf.readLongArray()); - this.expectedLength = buf.readInt(); - this.expectedHash = buf.readInt(); - } - - @Override - public void encode(FriendlyByteBuf buf) { - buf.writeLongArray(flags.toLongArray()); - buf.writeInt(expectedLength); - buf.writeInt(expectedHash); - } - + //what The fuck is forge doing public static List> generateRegistryPackets(boolean isLocal) { return !isLocal ? Collections.singletonList(Pair.of(S2CLoginFlag.class.getName(), new S2CLoginFlag())) : @@ -42,10 +29,10 @@ public static List> generateRegistryPackets(boolean i } @Override - public boolean consume(NetworkEvent.Context context, BiConsumer reply) { + public boolean receive(IZetaNetworkEventContext context) { if (expectedLength == SyncedFlagHandler.expectedLength() && expectedHash == SyncedFlagHandler.expectedHash()) SyncedFlagHandler.receiveFlagInfoFromServer(flags); - reply.accept(new C2SLoginFlag(), context); + context.reply(new C2SLoginFlag()); return true; } } diff --git a/src/main/java/org/violetmoon/quark/content/tools/module/ColorRunesModule.java b/src/main/java/org/violetmoon/quark/content/tools/module/ColorRunesModule.java index 76efee6c5f..3f713efccf 100644 --- a/src/main/java/org/violetmoon/quark/content/tools/module/ColorRunesModule.java +++ b/src/main/java/org/violetmoon/quark/content/tools/module/ColorRunesModule.java @@ -36,6 +36,7 @@ import org.violetmoon.zeta.event.play.loading.ZLootTableLoad; import org.violetmoon.zeta.module.ZetaLoadModule; import org.violetmoon.zeta.module.ZetaModule; +import org.violetmoon.zeta.network.ZetaNetworkDirection; import org.violetmoon.zeta.util.Hint; import org.violetmoon.zeta.util.ItemNBTHelper; @@ -118,7 +119,7 @@ public static void syncTrident(Consumer> packetConsumer, ThrownTrident ItemStack stack = trident.getPickupItem(); ItemStack prev = TRIDENT_STACK_REFERENCES.get(trident); if (force || prev == null || ItemStack.isSameItemSameTags(stack, prev)) - packetConsumer.accept(QuarkNetwork.toVanillaPacket(new UpdateTridentMessage(trident.getId(), stack), NetworkDirection.PLAY_TO_CLIENT)); + packetConsumer.accept(QuarkNetwork.toVanillaPacket(new UpdateTridentMessage(trident.getId(), stack), ZetaNetworkDirection.PLAY_TO_CLIENT)); else TRIDENT_STACK_REFERENCES.put(trident, stack); } diff --git a/src/main/java/org/violetmoon/zeta/Zeta.java b/src/main/java/org/violetmoon/zeta/Zeta.java index 1250e97d00..4ad3ca1775 100644 --- a/src/main/java/org/violetmoon/zeta/Zeta.java +++ b/src/main/java/org/violetmoon/zeta/Zeta.java @@ -7,7 +7,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; -import org.violetmoon.quark.base.handler.GeneralConfig; import org.violetmoon.zeta.util.NameChanger; import org.violetmoon.zeta.util.RaytracingUtil; import org.violetmoon.zeta.advancement.AdvancementModifierRegistry; @@ -130,7 +129,7 @@ public DyeablesRegistry createDyeablesRegistry() { public AdvancementModifierRegistry createAdvancementModifierRegistry() { return new AdvancementModifierRegistry(this); } - public abstract ZetaNetworkHandler createNetworkHandler(String modid, int protocolVersion); + public abstract ZetaNetworkHandler createNetworkHandler(int protocolVersion); public abstract ZetaCapabilityManager createCapabilityManager(); public BlockExtensionFactory createBlockExtensionFactory() { return BlockExtensionFactory.DEFAULT; diff --git a/src/main/java/org/violetmoon/zeta/network/IZetaMessage.java b/src/main/java/org/violetmoon/zeta/network/IZetaMessage.java index 873d450051..46d186c046 100644 --- a/src/main/java/org/violetmoon/zeta/network/IZetaMessage.java +++ b/src/main/java/org/violetmoon/zeta/network/IZetaMessage.java @@ -1,7 +1,5 @@ package org.violetmoon.zeta.network; -import java.io.Serializable; - -public interface IZetaMessage extends Serializable { +public interface IZetaMessage { boolean receive(IZetaNetworkEventContext context); } diff --git a/src/main/java/org/violetmoon/zeta/network/IZetaNetworkEventContext.java b/src/main/java/org/violetmoon/zeta/network/IZetaNetworkEventContext.java index 203c5a1cd4..3e30a28a45 100644 --- a/src/main/java/org/violetmoon/zeta/network/IZetaNetworkEventContext.java +++ b/src/main/java/org/violetmoon/zeta/network/IZetaNetworkEventContext.java @@ -2,12 +2,11 @@ import java.util.concurrent.CompletableFuture; -import net.minecraft.network.Connection; import net.minecraft.server.level.ServerPlayer; import org.jetbrains.annotations.Nullable; public interface IZetaNetworkEventContext { CompletableFuture enqueueWork(Runnable runnable); @Nullable ServerPlayer getSender(); - Connection getNetworkManager(); + void reply(ZetaHandshakeMessage msg); } diff --git a/src/main/java/org/violetmoon/zeta/network/ZetaHandshakeMessage.java b/src/main/java/org/violetmoon/zeta/network/ZetaHandshakeMessage.java new file mode 100644 index 0000000000..a1737c09c3 --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/network/ZetaHandshakeMessage.java @@ -0,0 +1,22 @@ +package org.violetmoon.zeta.network; + +import java.util.function.IntSupplier; + +public abstract class ZetaHandshakeMessage implements IntSupplier, IZetaMessage { + + private transient int loginIndex; + + public void setLoginIndex(final int loginIndex) { + this.loginIndex = loginIndex; + } + + public int getLoginIndex() { + return loginIndex; + } + + @Override + public int getAsInt() { + return loginIndex; + } + +} diff --git a/src/main/java/org/violetmoon/zeta/network/ZetaMessageSerializer.java b/src/main/java/org/violetmoon/zeta/network/ZetaMessageSerializer.java index 67031d60a5..b2b1ce07f3 100644 --- a/src/main/java/org/violetmoon/zeta/network/ZetaMessageSerializer.java +++ b/src/main/java/org/violetmoon/zeta/network/ZetaMessageSerializer.java @@ -46,7 +46,18 @@ public final class ZetaMessageSerializer { mapFunctions(Date.class, FriendlyByteBuf::readDate, FriendlyByteBuf::writeDate); mapFunctions(BlockHitResult.class, FriendlyByteBuf::readBlockHitResult, FriendlyByteBuf::writeBlockHitResult); } - + + public T instantiateAndReadObject(Class clazz, FriendlyByteBuf buf) { + try { + T msg = clazz.getDeclaredConstructor().newInstance(); + readObject(msg, buf); + return msg; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + // ! NO relation to java Serializable ! public void readObject(Object obj, FriendlyByteBuf buf) { try { Class clazz = obj.getClass(); diff --git a/src/main/java/org/violetmoon/zeta/network/ZetaNetworkDirection.java b/src/main/java/org/violetmoon/zeta/network/ZetaNetworkDirection.java index 2f5559b1e3..04f59bbc98 100644 --- a/src/main/java/org/violetmoon/zeta/network/ZetaNetworkDirection.java +++ b/src/main/java/org/violetmoon/zeta/network/ZetaNetworkDirection.java @@ -2,5 +2,11 @@ public enum ZetaNetworkDirection { PLAY_TO_SERVER, - PLAY_TO_CLIENT + PLAY_TO_CLIENT, + LOGIN_TO_SERVER, + LOGIN_TO_CLIENT; + + public boolean isLogin() { + return this == LOGIN_TO_SERVER || this == LOGIN_TO_CLIENT; + } } diff --git a/src/main/java/org/violetmoon/zeta/network/ZetaNetworkHandler.java b/src/main/java/org/violetmoon/zeta/network/ZetaNetworkHandler.java index 8ddd6b8af2..af57ae1c98 100644 --- a/src/main/java/org/violetmoon/zeta/network/ZetaNetworkHandler.java +++ b/src/main/java/org/violetmoon/zeta/network/ZetaNetworkHandler.java @@ -1,16 +1,23 @@ package org.violetmoon.zeta.network; +import java.util.List; +import java.util.function.Function; + +import net.minecraft.network.protocol.Packet; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; +import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.Nullable; +import org.violetmoon.zeta.Zeta; public abstract class ZetaNetworkHandler { public ZetaMessageSerializer serializer = new ZetaMessageSerializer(); - protected final String modid; + protected final Zeta zeta; protected final int protocolVersion; - public ZetaNetworkHandler(String modid, int protocolVersion) { - this.modid = modid; + public ZetaNetworkHandler(Zeta zeta, int protocolVersion) { + this.zeta = zeta; this.protocolVersion = protocolVersion; } @@ -27,7 +34,11 @@ public void sendToAllPlayers(IZetaMessage msg, MinecraftServer server) { } public abstract void register(Class clazz, ZetaNetworkDirection dir); + //TODO: BAD GARBAGE api + public abstract void registerLogin(Class clazz, ZetaNetworkDirection dir, int id, boolean hasResponse, @Nullable Function>> loginPacketGenerators); public abstract void sendToPlayer(IZetaMessage msg, ServerPlayer player); public abstract void sendToServer(IZetaMessage msg); + + public abstract Packet wrapInVanilla(IZetaMessage msg, ZetaNetworkDirection dir); } diff --git a/src/main/java/org/violetmoon/zetaimplforge/ForgeZeta.java b/src/main/java/org/violetmoon/zetaimplforge/ForgeZeta.java index e49099dc99..efec35081d 100644 --- a/src/main/java/org/violetmoon/zetaimplforge/ForgeZeta.java +++ b/src/main/java/org/violetmoon/zetaimplforge/ForgeZeta.java @@ -121,8 +121,8 @@ public BrewingRegistry createBrewingRegistry() { } @Override - public ZetaNetworkHandler createNetworkHandler(String modid, int protocolVersion) { - return new ForgeZetaNetworkHandler(modid, protocolVersion); + public ZetaNetworkHandler createNetworkHandler(int protocolVersion) { + return new ForgeZetaNetworkHandler(this, protocolVersion); } @Override diff --git a/src/main/java/org/violetmoon/zetaimplforge/network/ForgeNetworkEventContextImpl.java b/src/main/java/org/violetmoon/zetaimplforge/network/ForgeNetworkEventContextImpl.java index d830d533bc..151859a837 100644 --- a/src/main/java/org/violetmoon/zetaimplforge/network/ForgeNetworkEventContextImpl.java +++ b/src/main/java/org/violetmoon/zetaimplforge/network/ForgeNetworkEventContextImpl.java @@ -2,17 +2,20 @@ import java.util.concurrent.CompletableFuture; -import net.minecraft.network.Connection; import net.minecraft.server.level.ServerPlayer; import net.minecraftforge.network.NetworkEvent; +import net.minecraftforge.network.simple.SimpleChannel; import org.jetbrains.annotations.Nullable; import org.violetmoon.zeta.network.IZetaNetworkEventContext; +import org.violetmoon.zeta.network.ZetaHandshakeMessage; public class ForgeNetworkEventContextImpl implements IZetaNetworkEventContext { private final NetworkEvent.Context ctx; + private final SimpleChannel channel; - public ForgeNetworkEventContextImpl(NetworkEvent.Context ctx) { + public ForgeNetworkEventContextImpl(NetworkEvent.Context ctx, SimpleChannel channel) { this.ctx = ctx; + this.channel = channel; } @Override @@ -26,7 +29,7 @@ public CompletableFuture enqueueWork(Runnable runnable) { } @Override - public Connection getNetworkManager() { - return ctx.getNetworkManager(); + public void reply(ZetaHandshakeMessage msg) { + channel.reply(msg, ctx); } } diff --git a/src/main/java/org/violetmoon/zetaimplforge/network/ForgeZetaNetworkHandler.java b/src/main/java/org/violetmoon/zetaimplforge/network/ForgeZetaNetworkHandler.java index 1c148c20b5..8f024a7e88 100644 --- a/src/main/java/org/violetmoon/zetaimplforge/network/ForgeZetaNetworkHandler.java +++ b/src/main/java/org/violetmoon/zetaimplforge/network/ForgeZetaNetworkHandler.java @@ -1,64 +1,94 @@ package org.violetmoon.zetaimplforge.network; +import java.util.List; import java.util.function.BiConsumer; import java.util.function.Function; +import java.util.function.IntSupplier; import java.util.function.Supplier; +import com.google.common.base.Preconditions; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.Packet; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; +import net.minecraftforge.network.HandshakeHandler; import net.minecraftforge.network.NetworkDirection; import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; +import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.VisibleForTesting; import org.violetmoon.zeta.network.IZetaMessage; +import org.violetmoon.zeta.network.ZetaHandshakeMessage; import org.violetmoon.zeta.network.ZetaNetworkDirection; import org.violetmoon.zeta.network.ZetaNetworkHandler; +import org.violetmoon.zetaimplforge.ForgeZeta; public class ForgeZetaNetworkHandler extends ZetaNetworkHandler { - @VisibleForTesting //TODO ZETA: encapsulate, leaky abstraction in QuarkNetwork - public final SimpleChannel channel; - private int i = 0; + protected final SimpleChannel channel; + protected int i = 0; - public ForgeZetaNetworkHandler(String modid, int protocolVersion) { - super(modid, protocolVersion); + public ForgeZetaNetworkHandler(ForgeZeta zeta, int protocolVersion) { + super(zeta, protocolVersion); String protocolStr = Integer.toString(protocolVersion); channel = NetworkRegistry.ChannelBuilder - .named(new ResourceLocation(modid, "main")) + .named(new ResourceLocation(zeta.modid, "main")) .networkProtocolVersion(() -> protocolStr) .clientAcceptedVersions(protocolStr::equals) .serverAcceptedVersions(protocolStr::equals) .simpleChannel(); } + protected SimpleChannel.MessageBuilder builder(Class clazz, NetworkDirection dir) { + return builder(clazz, i++, dir); + } + + protected SimpleChannel.MessageBuilder builder(Class clazz, int id, NetworkDirection dir) { + return channel.messageBuilder(clazz, id, dir) + .encoder(serializer::writeObject) + .decoder(buf -> serializer.instantiateAndReadObject(clazz, buf)); + } + @Override public void register(Class clazz, ZetaNetworkDirection dir) { - BiConsumer encoder = serializer::writeObject; NetworkDirection forgeDir = toForge(dir); - Function decoder = (buf) -> { - try { - T msg = clazz.getDeclaredConstructor().newInstance(); - serializer.readObject(msg, buf); - return msg; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - }; + builder(clazz, forgeDir) + .consumerNetworkThread((BiConsumer>) (msg, ctxS) -> handlePacket(msg, ctxS, forgeDir)) //Yes you need the cast + .add(); + } + + //God this api suuuucks + public void registerLogin(Class clazz, ZetaNetworkDirection dir, int id, boolean hasResponse, @Nullable Function>> loginPacketGenerators) { + NetworkDirection forgeDir = toForge(dir); - BiConsumer> consumer = (msg, supp) -> { - NetworkEvent.Context context = supp.get(); - if(context.getDirection() != forgeDir) - return; + SimpleChannel.MessageBuilder builder = builder(clazz, id, forgeDir) + .loginIndex(T::getLoginIndex, T::setLoginIndex); - context.setPacketHandled(msg.receive(new ForgeNetworkEventContextImpl(context))); - }; + if(loginPacketGenerators != null) + builder.buildLoginPacketList(loginPacketGenerators); - channel.registerMessage(i, clazz, encoder, decoder, consumer); - i++; + if(dir == ZetaNetworkDirection.LOGIN_TO_SERVER) + builder = builder.consumerNetworkThread(HandshakeHandler.indexFirst((handshakeCrap, msg, ctxS) -> handlePacket(msg, ctxS, forgeDir))); + else + builder = builder.consumerNetworkThread((BiConsumer>) (msg, ctxS) -> handlePacket(msg, ctxS, forgeDir)); //Yes you need the cast + + //TODO is this strictly needed + if(!hasResponse) + builder = builder.noResponse(); + + builder.add(); + } + + private void handlePacket(T msg, Supplier contextSupplier, NetworkDirection forgeDir) { + NetworkEvent.Context context = contextSupplier.get(); + if(context.getDirection() != forgeDir) + return; + + context.setPacketHandled(msg.receive(new ForgeNetworkEventContextImpl(context, channel))); } @Override @@ -71,13 +101,19 @@ public void sendToServer(IZetaMessage msg) { channel.sendToServer(msg); } + @Override + public Packet wrapInVanilla(IZetaMessage msg, ZetaNetworkDirection dir) { + return channel.toVanillaPacket(msg, toForge(dir)); + } + /// public static ZetaNetworkDirection fromForge(NetworkDirection dir) { return switch(dir) { case PLAY_TO_SERVER -> ZetaNetworkDirection.PLAY_TO_SERVER; case PLAY_TO_CLIENT -> ZetaNetworkDirection.PLAY_TO_CLIENT; - default -> throw new UnsupportedOperationException("unknown net direction " + dir); + case LOGIN_TO_SERVER -> ZetaNetworkDirection.LOGIN_TO_SERVER; + case LOGIN_TO_CLIENT -> ZetaNetworkDirection.LOGIN_TO_CLIENT; }; } @@ -85,6 +121,8 @@ public static NetworkDirection toForge(ZetaNetworkDirection dir) { return switch(dir) { case PLAY_TO_SERVER -> NetworkDirection.PLAY_TO_SERVER; case PLAY_TO_CLIENT -> NetworkDirection.PLAY_TO_CLIENT; + case LOGIN_TO_SERVER -> NetworkDirection.LOGIN_TO_SERVER; + case LOGIN_TO_CLIENT -> NetworkDirection.LOGIN_TO_CLIENT; }; } }