Skip to content

Commit

Permalink
NEED 1.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Nov 17, 2024
1 parent e105589 commit 0c4e75c
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 89 deletions.
4 changes: 4 additions & 0 deletions networking/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ apply from: rootProject.file("gradle/scripts/klib-common.gradle")

base.archivesName = rootProject.name + "-package"

loom {
accessWidenerPath = file("src/main/resources/kessoku-networking.accesswidener")
}

kessoku {
modules(["base", "event-base"], "common")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"accessWidener": "kessoku-networking.accesswidener",
"injected_interfaces": {
"net/minecraft/class_8610": [ "band/kessoku/lib/api/networking/ServerConfigurationNetworkHandlerExtension" ]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessWidener v2 named
accessible field net/minecraft/server/network/ServerCommonNetworkHandler server Lnet/minecraft/server/MinecraftServer;
4 changes: 4 additions & 0 deletions networking/fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ apply from: rootProject.file("gradle/scripts/klib-fabric.gradle")

base.archivesName = rootProject.name + "-networking"

loom {
accessWidenerPath = project(":networking-common").loom.accessWidenerPath
}

kessoku {
modules(["base", "event-base"], "common")

Expand Down
8 changes: 8 additions & 0 deletions networking/neo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ apply from: rootProject.file("gradle/scripts/klib-neo.gradle")

base.archivesName = rootProject.name + "-networking"

loom {
accessWidenerPath = project(":networking-common").loom.accessWidenerPath
}

kessoku {
modules(["base", "event-base"], "common")
module("base", "neo")

common("networking", ModPlatform.NEOFORGE)
shadowBundle("networking", ModPlatform.NEOFORGE)
}

remapJar {
atAccessWideners.add("kessoku-networking.accesswidener")
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class KessokuNetworkingNeoForge {
public KessokuNetworkingNeoForge(IEventBus modEventBus) {
KessokuLib.loadModule(KessokuNetworking.class);

CommonPacketsImplNeoForge.init();
NetworkingImpl.init();

if (FMLLoader.getDist().isClient()) {
Expand All @@ -43,9 +42,9 @@ public KessokuNetworkingNeoForge(IEventBus modEventBus) {
DebugConfigCommand.register(event.getDispatcher());
});

// NeoEventUtils.registerEvent(modEventBus, RegisterConfigurationTasksEvent.class, event -> {
// ServerConfigurationNetworkHandler listener = (ServerConfigurationNetworkHandler) event.getListener();
// ServerConfigurationConnectionEvent.CONFIGURE.invoker().onSendConfiguration(listener, listener.server);
// });
NeoEventUtils.registerEvent(modEventBus, RegisterConfigurationTasksEvent.class, event -> {
ServerConfigurationNetworkHandler listener = (ServerConfigurationNetworkHandler) event.getListener();
ServerConfigurationConnectionEvent.CONFIGURE.invoker().onSendConfiguration(listener, listener.server);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package band.kessoku.lib.impl.networking.neoforge;

import band.kessoku.lib.impl.networking.PayloadTypeRegistryImpl;
import band.kessoku.lib.mixin.networking.neoforge.NetworkRegistryAccessor;
import net.minecraft.network.NetworkPhase;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import net.neoforged.neoforge.common.extensions.ICommonPacketListener;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import net.neoforged.neoforge.network.handling.IPayloadHandler;
import net.neoforged.neoforge.network.registration.ChannelAttributes;
import net.neoforged.neoforge.network.registration.NetworkPayloadSetup;
import net.neoforged.neoforge.network.registration.NetworkRegistry;
import org.apache.commons.lang3.function.TriConsumer;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class NeoNetworkRegistrar {
// Not our actual codec, see NetworkRegistryMixin
public static final PacketCodec<?, ?> DUMMY_CODEC = PacketCodec.ofStatic((a, b) -> {
throw new UnsupportedOperationException();
}, a -> {
throw new UnsupportedOperationException();
});

private final NetworkPhase protocol;

private final Map<Identifier, NeoPayloadHandler<?>> registeredPayloads = new HashMap<>();

public NeoNetworkRegistrar(NetworkPhase protocol) {
this.protocol = protocol;
}

public static boolean hasCodecFor(NetworkPhase protocol, NetworkSide flow, Identifier id) {
PayloadTypeRegistryImpl<? extends PacketByteBuf> registry = getPayloadRegistry(protocol, flow);
return registry.get(id) != null;
}

public static PayloadTypeRegistryImpl<? extends PacketByteBuf> getPayloadRegistry(NetworkPhase protocol, NetworkSide flow) {
if (protocol == NetworkPhase.PLAY) {
return flow == NetworkSide.SERVERBOUND ? PayloadTypeRegistryImpl.PLAY_C2S : PayloadTypeRegistryImpl.PLAY_S2C;
} else if (protocol == NetworkPhase.CONFIGURATION) {
return flow == NetworkSide.SERVERBOUND ? PayloadTypeRegistryImpl.CONFIG_C2S : PayloadTypeRegistryImpl.CONFIG_S2C;
} else {
throw new UnsupportedOperationException();
}
}

public <P extends CustomPayload, C, H> boolean registerGlobalReceiver(CustomPayload.Id<P> type, NetworkSide packetFlow, H handler, Function<IPayloadContext, C> ctxFactory, TriConsumer<H, P, C> consumer) {
NeoPayloadHandler<P> neoHandler = getOrRegisterNativeHandler(type);
return neoHandler.registerGlobalHandler(packetFlow, handler, ctxFactory, consumer);
}

public <H> H unregisterGlobalReceiver(Identifier id, NetworkSide flow) {
NeoPayloadHandler<?> neoHandler = registeredPayloads.get(id);
return neoHandler != null ? neoHandler.unregisterGlobalHandler(flow) : null;
}

public Set<Identifier> getGlobalReceivers(NetworkSide flow) {
return registeredPayloads.entrySet().stream()
.filter(e -> e.getValue().hasGlobalHandler(flow))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}

public <P extends CustomPayload, C, H> boolean registerLocalReceiver(CustomPayload.Id<P> type, ICommonPacketListener listener, H handler, Function<IPayloadContext, C> ctxFactory, TriConsumer<H, P, C> consumer) {
NeoPayloadHandler<P> neoHandler = getOrRegisterNativeHandler(type);
return neoHandler.registerLocalReceiver(listener, handler, ctxFactory, consumer);
}

public <H> H unregisterLocalReceiver(Identifier id, ICommonPacketListener listener) {
NeoPayloadHandler<?> neoHandler = registeredPayloads.get(id);
return neoHandler != null ? neoHandler.unregisterLocalHandler(listener) : null;
}

public Set<Identifier> getLocalReceivers(ICommonPacketListener listener) {
return registeredPayloads.entrySet().stream()
.filter(e -> e.getValue().hasLocalHandler(listener))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}

public Set<Identifier> getLocalSendable(ICommonPacketListener listener) {
NetworkPayloadSetup payloadSetup = ChannelAttributes.getPayloadSetup(listener.getConnection());
if (payloadSetup == null) {
return Set.of();
}
return payloadSetup.channels().get(this.protocol).keySet();
}

@SuppressWarnings("unchecked")
private <P extends CustomPayload> NeoPayloadHandler<P> getOrRegisterNativeHandler(CustomPayload.Id<P> type) {
return (NeoPayloadHandler<P>) registeredPayloads.computeIfAbsent(type.id(), k -> {
NeoPayloadHandler<P> handler = new NeoPayloadHandler<>();
boolean setup = NetworkRegistryAccessor.getSetup();

NetworkRegistryAccessor.setSetup(false);
NetworkRegistry.register(type, (PacketCodec<? super PacketByteBuf, P>) DUMMY_CODEC, handler, List.of(protocol), Optional.empty(), "1.0", true);
NetworkRegistryAccessor.setSetup(setup);

// TODO Send registration message when registering late
return handler;
});
}

public static class NeoPayloadHandler<P extends CustomPayload> implements IPayloadHandler<P> {
private final Map<NetworkSide, NeoSubHandler<P, ?, ?>> globalReceivers = new HashMap<>();
private final Map<ICommonPacketListener, NeoSubHandler<P, ?, ?>> localReceivers = new HashMap<>();

@Override
public void handle(P arg, IPayloadContext context) {
NeoSubHandler globalHandler = globalReceivers.get(context.flow());
if (globalHandler != null) {
context.enqueueWork(() -> globalHandler.consumer().accept(globalHandler.handler(), arg, globalHandler.ctxFactory().apply(context)));
}
NeoSubHandler localHandler = localReceivers.get(context.listener());
if (localHandler != null) {
context.enqueueWork(() -> localHandler.consumer().accept(localHandler.handler(), arg, localHandler.ctxFactory().apply(context)));
}
}

public boolean hasGlobalHandler(NetworkSide flow) {
return globalReceivers.containsKey(flow);
}

public <C, H> boolean registerGlobalHandler(NetworkSide flow, H original, Function<IPayloadContext, C> ctxFactory, TriConsumer<H, P, C> consumer) {
if (!hasGlobalHandler(flow)) {
globalReceivers.put(flow, new NeoSubHandler<>(original, ctxFactory, consumer));
return true;
}
return false;
}

public boolean hasLocalHandler(ICommonPacketListener listener) {
return localReceivers.containsKey(listener);
}

public <C, H> boolean registerLocalReceiver(ICommonPacketListener listener, H original, Function<IPayloadContext, C> ctxFactory, TriConsumer<H, P, C> consumer) {
if (!hasLocalHandler(listener)) {
localReceivers.put(listener, new NeoSubHandler<>(original, ctxFactory, consumer));
return true;
}
return false;
}

@Nullable
public <H> H unregisterGlobalHandler(NetworkSide flow) {
NeoSubHandler subHandler = globalReceivers.remove(flow);
return subHandler != null ? (H) subHandler.handler() : null;
}

@Nullable
public <H> H unregisterLocalHandler(ICommonPacketListener listener) {
NeoSubHandler subHandler = localReceivers.remove(listener);
return subHandler != null ? (H) subHandler.handler() : null;
}
}

record NeoSubHandler<P extends CustomPayload, C, H>(H handler, Function<IPayloadContext, C> ctxFactory, TriConsumer<H, P, C> consumer) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package band.kessoku.lib.mixin.networking.neoforge;

import net.minecraft.network.NetworkPhase;
import net.minecraft.util.Identifier;
import net.neoforged.neoforge.network.registration.NetworkRegistry;
import net.neoforged.neoforge.network.registration.PayloadRegistration;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Map;

@Mixin(NetworkRegistry.class)
public interface NetworkRegistryAccessor {
@Accessor("PAYLOAD_REGISTRATIONS")
static Map<NetworkPhase, Map<Identifier, PayloadRegistration<?>>> getPayloadRegistrations() {
throw new UnsupportedOperationException();
}

@Accessor
static boolean getSetup() {
throw new UnsupportedOperationException();
}

@Accessor
static void setSetup(boolean setup) {
throw new UnsupportedOperationException();
}
}

0 comments on commit 0c4e75c

Please sign in to comment.