Skip to content

Commit

Permalink
Forge networking
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticdrew committed Aug 28, 2024
1 parent b1f23d8 commit 16d3ed3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
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>Added: New registration method that takes type and stream codec, other registration is deprecated, please use the new one!</li>
<li>Fixed: Forge networking.</li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +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.server.level.ServerPlayer;
import net.minecraftforge.event.network.CustomPayloadEvent;
import net.minecraftforge.network.Channel;
import net.minecraftforge.network.ChannelBuilder;
import net.minecraftforge.network.EventNetworkChannel;

Expand All @@ -21,7 +23,8 @@

public class ForgeNetworkHandler extends PacketRegistrationHandler
{
private final Map<Class<?>, EventNetworkChannel> CHANNELS = new HashMap<>();
// private final Map<Class<?>, EventNetworkChannel> CHANNELS = new HashMap<>();
private final Map<Class<?>, Message<?>> CHANNELS = new HashMap<>();

public ForgeNetworkHandler(Side side)
{
Expand All @@ -37,20 +40,23 @@ protected <T> void registerPacket(PacketContainer<T> container)
CommonPacketWrapper<T> msg = container.getCodec().decode(event.getPayload());
buildHandler(container.handler()).accept(msg.packet(), event.getSource());
});
CHANNELS.put(container.classType(), channel);
CHANNELS.put(container.classType(), new Message<>(channel, container));
}
}

public <T> void sendToServer(T packet, boolean ignoreCheck)
{

Channel<T> channel = (Channel<T>) CHANNELS.get(packet.getClass());
if (channel != null)
var message = (Message<T>) CHANNELS.get(packet.getClass());
if (message != null)
{
var channel = message.channel();
Connection connection = Minecraft.getInstance().getConnection().getConnection();
if (ignoreCheck || channel.isRemotePresent(connection))
{
channel.send(packet, connection);
FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), Minecraft.getInstance().player.registryAccess());
message.container.codec().encode(buf, packet);
channel.send(buf, connection);
}
}
else
Expand All @@ -63,14 +69,16 @@ public <T> void sendToServer(T packet, boolean ignoreCheck)
public <T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck)
{

Channel<T> channel = (Channel<T>) CHANNELS.get(packet.getClass());

Connection connection = player.connection.getConnection();
if (channel != null)
var message = (Message<T>) CHANNELS.get(packet.getClass());
if (message != null)
{
var channel = message.channel();
Connection connection = player.connection.getConnection();
if (ignoreCheck || channel.isRemotePresent(connection))
{
channel.send(packet, connection);
FriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), player.server.registryAccess());
message.container.codec().encode(buf, packet);
channel.send(buf, connection);
}

}
Expand Down Expand Up @@ -100,7 +108,7 @@ private <T> BiConsumer<T, CustomPayloadEvent.Context> buildHandler(Consumer<Pack
};
}

// public record Message<T>(EventNetworkChannel channel, BiConsumer<T, ? extends FriendlyByteBuf> encoder)
// {
// }
public record Message<T>(EventNetworkChannel channel, PacketContainer<T> container)
{
}
}

0 comments on commit 16d3ed3

Please sign in to comment.