-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
9 changed files
with
217 additions
and
89 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
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
2 changes: 2 additions & 0 deletions
2
networking/common/src/main/resources/kessoku-networking.accesswidener
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,2 @@ | ||
accessWidener v2 named | ||
accessible field net/minecraft/server/network/ServerCommonNetworkHandler server Lnet/minecraft/server/MinecraftServer; |
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
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
84 changes: 0 additions & 84 deletions
84
...eo/src/main/java/band/kessoku/lib/impl/networking/neoforge/CommonPacketsImplNeoForge.java
This file was deleted.
Oops, something went wrong.
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
166 changes: 166 additions & 0 deletions
166
...king/neo/src/main/java/band/kessoku/lib/impl/networking/neoforge/NeoNetworkRegistrar.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,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) { } | ||
} |
28 changes: 28 additions & 0 deletions
28
...neo/src/main/java/band/kessoku/lib/mixin/networking/neoforge/NetworkRegistryAccessor.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,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(); | ||
} | ||
} |