Skip to content

Commit

Permalink
[1.20.2+] Update Forge network handling
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Jul 19, 2024
1 parent a258db5 commit 806b91f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import dev.the_fireplace.lib.api.network.interfaces.ClientboundPacketReceiver;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;

import java.util.function.Supplier;

public interface ReceiveClientPacket
{
void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, NetworkEvent.Context context, FriendlyByteBuf packetContents);
void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, CustomPayloadEvent.Context context, FriendlyByteBuf packetContents);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import dev.the_fireplace.lib.api.network.interfaces.PacketSpecification;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.network.SimpleChannel;

public interface SimpleChannelManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraftforge.fml.IExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.NetworkConstants;

@Mod("fireplacelib")
public final class Forge
Expand All @@ -24,6 +23,6 @@ public Forge() {
});

// Register as optional on both sides
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (a, b) -> true));
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> IExtensionPoint.DisplayTest.IGNORESERVERONLY, (a, b) -> true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.event.network.CustomPayloadEvent;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.SimpleChannel;

import javax.inject.Singleton;
import java.util.Map;
Expand All @@ -24,12 +24,12 @@
@Implementation(allInterfaces = true)
public final class ForgePacketChannel implements SimpleChannelManager, PacketSpecificationRegistry
{
private final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(FireplaceLibConstants.MODID, "packets"),
() -> "",
version -> true,
version -> true
);
private final SimpleChannel CHANNEL = ChannelBuilder
.named(new ResourceLocation(FireplaceLibConstants.MODID, "packets"))
.clientAcceptedVersions((a, b) -> true)
.serverAcceptedVersions((a, b) -> true)
.networkProtocolVersion(1)
.simpleChannel();
private final Map<ResourceLocation, Supplier<ClientboundPacketReceiver>> clientReceivers = new ConcurrentHashMap<>();
private final Map<ResourceLocation, Supplier<ServerboundPacketReceiver>> serverReceivers = new ConcurrentHashMap<>();
private final ReceiveClientPacket receiveClientPacket;
Expand All @@ -46,13 +46,11 @@ public SimpleChannel getChannel() {

@Override
public void register() {
CHANNEL.registerMessage(
1,
ReceiverWrapper.class,
(receiverWrapper, outputByteBuf) -> outputByteBuf.writeBytes(receiverWrapper.buffer),
ReceiverWrapper::new,
ReceiverWrapper::read
);
CHANNEL.messageBuilder(ReceiverWrapper.class)
.decoder(ReceiverWrapper::new)
.encoder((receiverWrapper, outputByteBuf) -> outputByteBuf.writeBytes(receiverWrapper.buffer))
.consumerMainThread(ReceiverWrapper::read)
.add();
}

@Override
Expand Down Expand Up @@ -80,8 +78,7 @@ private ReceiverWrapper(FriendlyByteBuf buffer) {
this.buffer = buffer;
}

public void read(Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
public void read(CustomPayloadEvent.Context context) {
ResourceLocation packetId = this.buffer.readResourceLocation();
FriendlyByteBuf packetContents = new FriendlyByteBuf(this.buffer.copy());
Supplier<ClientboundPacketReceiver> clientReceiver = clientReceivers.get(packetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import dev.the_fireplace.lib.api.network.interfaces.PacketSpecification;
import dev.the_fireplace.lib.domain.network.ServerboundSender;
import dev.the_fireplace.lib.domain.network.SimpleChannelManager;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.network.SimpleChannel;

import javax.inject.Inject;

Expand All @@ -33,8 +32,8 @@ public void sendToServer(PacketSpecification specification, FriendlyByteBuf pack
@Override
public void sendToClient(ServerGamePacketListenerImpl connection, PacketSpecification specification, FriendlyByteBuf packetContents) {
SimpleChannel channel = simpleChannelManager.getChannel();
if (channel.isRemotePresent(connection.connection)) {
channel.send(PacketDistributor.PLAYER.with(() -> connection.player), simpleChannelManager.wrap(specification, packetContents));
if (channel.isRemotePresent(connection.getConnection())) {
channel.send(simpleChannelManager.wrap(specification, packetContents), PacketDistributor.PLAYER.with(connection.player));
} else if (!specification.shouldSilentlyFailOnMissingReceiver()) {
throw new IllegalStateException(String.format(
"Player %s is missing a receiver for packet %s.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import dev.the_fireplace.lib.api.network.interfaces.PacketSpecification;
import dev.the_fireplace.lib.domain.network.ServerboundSender;
import dev.the_fireplace.lib.domain.network.SimpleChannelManager;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.network.simple.SimpleChannel;
import net.minecraftforge.network.SimpleChannel;

import javax.inject.Inject;

Expand All @@ -28,7 +27,7 @@ public void sendToServer(PacketSpecification specification, FriendlyByteBuf pack
SimpleChannel channel = simpleChannelManager.getChannel();
ClientPacketListener connection = Minecraft.getInstance().getConnection();
if (connection != null && channel.isRemotePresent(connection.getConnection())) {
channel.send(PacketDistributor.SERVER.with(() -> null), simpleChannelManager.wrap(specification, packetContents));
channel.send(simpleChannelManager.wrap(specification, packetContents), PacketDistributor.SERVER.with(null));
} else if (connection == null) {
throw new IllegalStateException(String.format(
"Not connected to a server, cannot send packet %s.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.network.CustomPayloadEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

@Implementation(environment = "CLIENT")
public final class ReceiveClientPacketClient implements ReceiveClientPacket
{
@Override
public void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, NetworkEvent.Context context, FriendlyByteBuf packetContents) {
public void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, CustomPayloadEvent.Context context, FriendlyByteBuf packetContents) {
if (clientReceiver != null) {
context.enqueueWork(() ->
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import dev.the_fireplace.lib.api.network.interfaces.ClientboundPacketReceiver;
import dev.the_fireplace.lib.domain.network.ReceiveClientPacket;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.event.network.CustomPayloadEvent;

import java.util.function.Supplier;

@Implementation(environment = "SERVER")
public final class ReceiveClientPacketServer implements ReceiveClientPacket
{
@Override
public void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, NetworkEvent.Context context, FriendlyByteBuf packetContents) {
public void receiveClientPacket(Supplier<ClientboundPacketReceiver> clientReceiver, CustomPayloadEvent.Context context, FriendlyByteBuf packetContents) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraftforge.fml.IExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.NetworkConstants;

@Mod(PlaytestConstants.MODID)
public final class Forge
Expand All @@ -14,7 +13,7 @@ public Forge() {
PlaytestConstants.getInjector().getInstance(IPlaytestInitializer.class).init();

// Register as optional on both sides
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> NetworkConstants.IGNORESERVERONLY, (a, b) -> true));
ModLoadingContext.get().registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> new IExtensionPoint.DisplayTest(() -> IExtensionPoint.DisplayTest.IGNORESERVERONLY, (a, b) -> true));
}
}

0 comments on commit 806b91f

Please sign in to comment.