Skip to content

Commit

Permalink
add configuration packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticdrew committed Oct 18, 2024
1 parent d9dccdf commit 3733c07
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 37 deletions.
12 changes: 12 additions & 0 deletions common/src/main/java/commonnetwork/CommonNetworkMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public static <T> PacketRegistrar registerPacket(CustomPacketPayload.Type<? exte
}
}

public static <T> PacketRegistrar registerConfigurationPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
if (INSTANCE != null)
{
return INSTANCE.packetRegistration.registerConfigurationPacket(type, packetClass, codec, handler);
}
else
{
return getDelayedHandler().registerConfigurationPacket(type, packetClass, codec, handler);
}
}

public PacketRegistrationHandler getPacketRegistration()
{
return packetRegistration;
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/commonnetwork/api/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public static <T> PacketRegistrar registerPacket(CustomPacketPayload.Type<? exte
return CommonNetworkMod.registerPacket(type, packetClass, codec, handler);
}

/**
* Packet Registration
*
* @param type - The packet type.
* @param packetClass - The class of the packet.
* @param codec - The StreamCodec.
* @param handler - The handler method.
* @param <T> - The class type
* @return The registrar for chaining registrations.
*/
public static <T> PacketRegistrar registerConfigurationPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
return CommonNetworkMod.registerConfigurationPacket(type, packetClass, codec, handler);
}

/**
* Gets the Network handler for use to send packets.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ public <T> PacketRegistrar registerPacket(ResourceLocation id, Class<T> packetCl
@Override
public <T> PacketRegistrar registerPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler);
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler, PacketContainer.PacketType.PLAY);
QUEUED_PACKET_MAP.put(packetClass, container);
return this;
}

@Override
public <T> PacketRegistrar registerConfigurationPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler, PacketContainer.PacketType.CONFIGURATION);
QUEUED_PACKET_MAP.put(packetClass, container);
return this;
}
Expand Down
15 changes: 13 additions & 2 deletions common/src/main/java/commonnetwork/networking/PacketRegistrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface PacketRegistrar
Side getSide();

/**
* Packet Registration
* Packet Registration registers a PLAY packet
*
* @param packetIdentifier - The unique {@link ResourceLocation} packet id.
* @param packetClass - The class of the packet.
Expand All @@ -34,7 +34,7 @@ public interface PacketRegistrar
<T> PacketRegistrar registerPacket(ResourceLocation packetIdentifier, Class<T> packetClass, BiConsumer<T, FriendlyByteBuf> encoder, Function<FriendlyByteBuf, T> decoder, Consumer<PacketContext<T>> handler);

/**
* Packet Registration
* Packet Registration, registers a PLAY packet
*
* @param type - The packet type.
* @param packetClass - The class of the packet.
Expand All @@ -45,4 +45,15 @@ public interface PacketRegistrar
*/
<T> PacketRegistrar registerPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler);

/**
* Packet Registration, registers a CONFIGURATION packet
*
* @param type - The packet type.
* @param packetClass - The class of the packet.
* @param codec - The StreamCodec.
* @param handler - The handler method.
* @param <T> - The class type
* @return The registrar for chaining registrations.
*/
<T> PacketRegistrar registerConfigurationPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public PacketRegistrationHandler(Side side)
this.side = side;
}

@Override
public <T> PacketRegistrar registerPacket(ResourceLocation packetIdentifier, Class<T> packetClass, BiConsumer<T, FriendlyByteBuf> encoder, Function<FriendlyByteBuf, T> decoder, Consumer<PacketContext<T>> handler)
{
PacketContainer<T> container = new PacketContainer<>(packetIdentifier, packetClass, encoder, decoder, handler);
Expand All @@ -40,10 +41,19 @@ public <T> PacketRegistrar registerPacket(ResourceLocation packetIdentifier, Cla
return this;
}

@Override
public <T> PacketRegistrar registerConfigurationPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler, PacketContainer.PacketType.CONFIGURATION);
PACKET_MAP.put(packetClass, container);
registerPacket(container);
return this;
}

@Override
public <T> PacketRegistrar registerPacket(CustomPacketPayload.Type<? extends CustomPacketPayload> type, Class<T> packetClass, StreamCodec<? extends FriendlyByteBuf, T> codec, Consumer<PacketContext<T>> handler)
{
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler);
PacketContainer<T> container = new PacketContainer<>(type, packetClass, codec, handler, PacketContainer.PacketType.PLAY);
PACKET_MAP.put(packetClass, container);
registerPacket(container);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public record PacketContainer<T>(
BiConsumer<T, FriendlyByteBuf> encoder,
Function<FriendlyByteBuf, T> decoder,
StreamCodec<? super FriendlyByteBuf, T> codec,
Consumer<PacketContext<T>> handler)
Consumer<PacketContext<T>> handler,
PacketType packetType)
{
//TODO: Removing for mc 1.21.2 or 1.22
@Deprecated(forRemoval = true)
Expand All @@ -25,16 +26,17 @@ public PacketContainer(ResourceLocation id,
Function<FriendlyByteBuf, T> decoder,
Consumer<PacketContext<T>> handle)
{
this(new CustomPacketPayload.Type<>(id), classType, encoder, decoder, null, handle);
this(new CustomPacketPayload.Type<>(id), classType, encoder, decoder, null, handle, PacketType.PLAY);
}

@SuppressWarnings("unchecked")
public <B extends FriendlyByteBuf> PacketContainer(CustomPacketPayload.Type<? extends CustomPacketPayload> type,
Class<T> classType,
StreamCodec<? super B, T> codec,
Consumer<PacketContext<T>> handle)
Consumer<PacketContext<T>> handle,
PacketType packetType)
{
this(type, classType, null, null, (StreamCodec<? super FriendlyByteBuf, T>) codec, handle);
this(type, classType, null, null, (StreamCodec<? super FriendlyByteBuf, T>) codec, handle, packetType );
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -62,4 +64,10 @@ public <K extends FriendlyByteBuf> StreamCodec<K, CommonPacketWrapper> getCodec(
(buf) -> new CommonPacketWrapper<>(this, this.codec().decode(buf)));
}
}

public enum PacketType
{
PLAY,
CONFIGURATION
}
}
2 changes: 1 addition & 1 deletion doc/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ <h1>Common Networking ${version} for Minecraft ${mcversion}</h1>

<p>New in ${version}</p>
<ul>
<li>Fixed: Forge networking.</li>
<li>Added: Configuration packets this is a beta version, please understand there may be issues.</li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import commonnetwork.networking.data.PacketContext;
import commonnetwork.networking.data.Side;
import commonnetwork.networking.exceptions.RegistrationException;
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.server.level.ServerPlayer;

Expand All @@ -24,8 +26,16 @@ protected <T> void registerPacket(PacketContainer<T> container)
{
try
{
PayloadTypeRegistry.playC2S().register(container.getType(), container.getCodec());
PayloadTypeRegistry.playS2C().register(container.getType(), container.getCodec());
if (container.packetType() == PacketContainer.PacketType.PLAY)
{
PayloadTypeRegistry.playC2S().register(container.getType(), container.getCodec());
PayloadTypeRegistry.playS2C().register(container.getType(), container.getCodec());
}
else
{
PayloadTypeRegistry.configurationC2S().register(container.getType(), container.getCodec());
PayloadTypeRegistry.configurationS2C().register(container.getType(), container.getCodec());
}
}
catch (IllegalArgumentException e)
{
Expand All @@ -36,17 +46,41 @@ protected <T> void registerPacket(PacketContainer<T> container)
{
Constants.LOG.debug("Registering packet {} : {} on the: {}", container.type().id(), container.classType(), Side.CLIENT);

ClientPlayNetworking.registerGlobalReceiver(container.getType(),
(ClientPlayNetworking.PlayPayloadHandler<CommonPacketWrapper<T>>) (payload, context) -> context.client().execute(() ->
container.handler().accept(
new PacketContext<>(payload.packet(), Side.CLIENT))));
if (container.packetType() == PacketContainer.PacketType.PLAY)
{
// play packets
ClientPlayNetworking.registerGlobalReceiver(container.getType(),
(ClientPlayNetworking.PlayPayloadHandler<CommonPacketWrapper<T>>) (payload, context) -> context.client().execute(() ->
container.handler().accept(
new PacketContext<>(payload.packet(), Side.CLIENT))));
}
else
{
// configuration packets
ClientConfigurationNetworking.registerGlobalReceiver(container.getType(),
(ClientConfigurationNetworking.ConfigurationPayloadHandler<CommonPacketWrapper<T>>) (payload, context) -> context.client().execute(() ->
container.handler().accept(
new PacketContext<>(payload.packet(), Side.CLIENT))));
}
}

Constants.LOG.debug("Registering packet {} : {} on the: {}", container.type().id(), container.classType(), Side.SERVER);
ServerPlayNetworking.registerGlobalReceiver(container.getType(),
(ServerPlayNetworking.PlayPayloadHandler<CommonPacketWrapper<T>>) (payload, context) -> context.player().server.execute(() ->
container.handler().accept(
new PacketContext<>(context.player(), payload.packet(), Side.SERVER))));
if (container.packetType() == PacketContainer.PacketType.PLAY)
{
// play packets
ServerPlayNetworking.registerGlobalReceiver(container.getType(),
(ServerPlayNetworking.PlayPayloadHandler<CommonPacketWrapper<T>>) (payload, context) -> context.player().server.execute(() ->
container.handler().accept(
new PacketContext<>(context.player(), payload.packet(), Side.SERVER))));
}
else
{
// configuration packets
ServerConfigurationNetworking.registerGlobalReceiver(container.getType(),
(ServerConfigurationNetworking.ConfigurationPacketHandler<CommonPacketWrapper<T>>) (payload, context) -> context.server().execute(() ->
container.handler().accept(
new PacketContext<>(null, payload.packet(), Side.SERVER))));
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import commonnetwork.networking.data.PacketContext;
import commonnetwork.networking.data.Side;
import commonnetwork.networking.exceptions.RegistrationException;
import io.netty.buffer.Unpooled;
import net.minecraft.client.Minecraft;
import net.minecraft.network.Connection;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.network.CustomPayloadEvent;
import net.minecraftforge.network.Channel;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.EventNetworkChannel;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,11 +33,32 @@ protected <T> void registerPacket(PacketContainer<T> container)
{
if (CHANNELS.get(container.classType()) == null)
{
var channel = ChannelBuilder.named(container.type().id()).optional().eventNetworkChannel()
.addListener(event -> {
CommonPacketWrapper<T> msg = container.getCodec().decode(event.getPayload());
buildHandler(container.handler()).accept(msg.packet(), event.getSource());
});
var channelBuilder = ChannelBuilder.named(container.type().id()).optional().payloadChannel();
Channel<CustomPacketPayload> channel;
if (container.packetType() == PacketContainer.PacketType.PLAY)
{
channel = channelBuilder
.play()
.bidirectional()
.addMain(container.getType(), container.getCodec(), (msg, ctx) -> buildHandler(container.handler())
.accept((T) msg.packet(), ctx))
.build();
}
else
{
channel = channelBuilder
.configuration()
.bidirectional()
.addMain(container.getType(), container.getCodec(), (msg, ctx) -> buildHandler(container.handler())
.accept((T) msg.packet(), ctx)
).build();
}

// var channel = ChannelBuilder.named(container.type().id()).optional().eventNetworkChannel()
// .addListener(event -> {
// CommonPacketWrapper<T> msg = container.getCodec().decode(event.getPayload());
// buildHandler(container.handler()).accept(msg.packet(), event.getSource());
// });
CHANNELS.put(container.classType(), new Message<>(channel, container));
}
}
Expand All @@ -54,9 +73,9 @@ public <T> void sendToServer(T packet, boolean ignoreCheck)
Connection connection = Minecraft.getInstance().getConnection().getConnection();
if (ignoreCheck || channel.isRemotePresent(connection))
{
FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), Minecraft.getInstance().player.registryAccess());
message.container.codec().encode(buf, packet);
channel.send(buf, connection);
// FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), Minecraft.getInstance().player.registryAccess());
// message.container.codec().encode(buf, packet);
channel.send(new CommonPacketWrapper(message.container, packet), connection);
}
}
else
Expand All @@ -76,9 +95,9 @@ public <T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck)
Connection connection = player.connection.getConnection();
if (ignoreCheck || channel.isRemotePresent(connection))
{
FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), player.server.registryAccess());
message.container.codec().encode(buf, packet);
channel.send(buf, connection);
// FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), player.server.registryAccess());
// message.container.codec().encode(buf, packet);
channel.send(new CommonPacketWrapper(message.container, packet), connection);
}

}
Expand All @@ -88,6 +107,9 @@ public <T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck)
}
}

private static void handle(CustomPacketPayload customPacketPayload, CustomPayloadEvent.Context ctx)
{
}

private <T> BiConsumer<T, CustomPayloadEvent.Context> buildHandler(Consumer<PacketContext<T>> handler)
{
Expand All @@ -108,7 +130,7 @@ private <T> BiConsumer<T, CustomPayloadEvent.Context> buildHandler(Consumer<Pack
};
}

public record Message<T>(EventNetworkChannel channel, PacketContainer<T> container)
public record Message<T>(Channel<CustomPacketPayload> channel, PacketContainer<T> container)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ public void register(final RegisterPayloadHandlersEvent event)
{
if (!PACKET_MAP.isEmpty())
{
PACKET_MAP.forEach((type, container) -> event.registrar(container.getType().id().getNamespace())
.optional().commonBidirectional(container.getType(), container.getCodec(), buildHandler(container.handler())));
PACKET_MAP.forEach((type, container) -> {
var registrar = event.registrar(container.getType().id().getNamespace()).optional();
if (container.packetType() == PacketContainer.PacketType.PLAY)
{
registrar.playBidirectional(container.getType(), container.getCodec(), buildHandler(container.handler()));
}
else
{
registrar.configurationBidirectional(container.getType(), container.getCodec(), buildHandler(container.handler()));
}
});
}
}

Expand Down

0 comments on commit 3733c07

Please sign in to comment.