Skip to content

Commit

Permalink
I survived Forge networking code and all I got was this lousy tshirt
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Nov 11, 2023
1 parent 3d6fdc3 commit 92fbb6e
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 152 deletions.
48 changes: 7 additions & 41 deletions src/main/java/org/violetmoon/quark/base/network/QuarkNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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));
Expand Down Expand Up @@ -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 <MSG extends HandshakeMessage> SimpleChannel.MessageBuilder<MSG> loginIndexedBuilder(Class<MSG> clazz, int id, NetworkDirection direction) {
return channel.messageBuilder(clazz, id, direction)
.loginIndex(HandshakeMessage::getLoginIndex, HandshakeMessage::setLoginIndex)
.encoder(HandshakeMessage::encode);
}

private static <MSG extends HandshakeMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> loginPacketHandler() {
return (msg, contextSupplier) -> {
NetworkEvent.Context context = contextSupplier.get();
context.setPacketHandled(msg.consume(context, channel::reply));
};
}

private static <MSG extends HandshakeMessage> BiConsumer<MSG, Supplier<NetworkEvent.Context>> loginIndexFirst(BiConsumer<MSG, Supplier<NetworkEvent.Context>> 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) {
Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<HandshakeMessage, NetworkEvent.Context> reply) {
public boolean receive(IZetaNetworkEventContext context) {
if (expectedLength == SyncedFlagHandler.expectedLength() && expectedHash == SyncedFlagHandler.expectedHash())
SyncedFlagHandler.receiveFlagInfoFromPlayer(context.getSender(), flags);
return true;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,30 +21,18 @@ 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<Pair<String, S2CLoginFlag>> generateRegistryPackets(boolean isLocal) {
return !isLocal ?
Collections.singletonList(Pair.of(S2CLoginFlag.class.getName(), new S2CLoginFlag())) :
Collections.emptyList();
}

@Override
public boolean consume(NetworkEvent.Context context, BiConsumer<HandshakeMessage, NetworkEvent.Context> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -118,7 +119,7 @@ public static void syncTrident(Consumer<Packet<?>> 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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/violetmoon/zeta/Zeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/violetmoon/zeta/network/IZetaMessage.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> enqueueWork(Runnable runnable);
@Nullable ServerPlayer getSender();
Connection getNetworkManager();
void reply(ZetaHandshakeMessage msg);
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,18 @@ public final class ZetaMessageSerializer {
mapFunctions(Date.class, FriendlyByteBuf::readDate, FriendlyByteBuf::writeDate);
mapFunctions(BlockHitResult.class, FriendlyByteBuf::readBlockHitResult, FriendlyByteBuf::writeBlockHitResult);
}


public <T> T instantiateAndReadObject(Class<T> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
17 changes: 14 additions & 3 deletions src/main/java/org/violetmoon/zeta/network/ZetaNetworkHandler.java
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -27,7 +34,11 @@ public void sendToAllPlayers(IZetaMessage msg, MinecraftServer server) {
}

public abstract <T extends IZetaMessage> void register(Class<T> clazz, ZetaNetworkDirection dir);
//TODO: BAD GARBAGE api
public abstract <T extends ZetaHandshakeMessage> void registerLogin(Class<T> clazz, ZetaNetworkDirection dir, int id, boolean hasResponse, @Nullable Function<Boolean, List<Pair<String,T>>> loginPacketGenerators);

public abstract void sendToPlayer(IZetaMessage msg, ServerPlayer player);
public abstract void sendToServer(IZetaMessage msg);

public abstract Packet<?> wrapInVanilla(IZetaMessage msg, ZetaNetworkDirection dir);
}
4 changes: 2 additions & 2 deletions src/main/java/org/violetmoon/zetaimplforge/ForgeZeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 92fbb6e

Please sign in to comment.