Posted November 13, 20204 yr I'm attempting to update my packet system from 1.12.2 -> 1.16.3, and I've found that a ton appears to have changed, I've looked through the 1.13/1.14 primer and overall I'm still not sure what to do here. This was my packethandler class in 1.12 public final class PacketHandler { /** * The ID for the next packet registration. */ private byte nextPacketID = 0; /** * The internal network wrapper. */ private SimpleNetworkWrapper wrapper; /** * The channelid. */ private String channelid; /** * Instantiates a new packet handler with the given channelid and reserves * the channel. * * @param channelid the channelid. This is mostly the modid. */ public PacketHandler(String channelid) { this.wrapper = NetworkRegistry.INSTANCE.newSimpleChannel(channelid); this.channelid = channelid; } public void registerPackets() { registerPacket(PlayerSyncCapability.class, new PlayerSyncCapability.Handler(), Side.SERVER); } /** * Register an IMessage packet with it's corresponding message handler. * * @param packetClass the packet's class that should be registered. * @param messageHandler the message handler for this packet type. * @param target The side to which this packet can be sent. * @return <code>true</code>, if successful */ @SuppressWarnings({"unchecked", "rawtypes"}) public boolean registerPacket(Class<? extends IMessage> packetClass, MessageHandler messageHandler, Side target) { if (this.nextPacketID == -1) throw new IllegalStateException("Too many packets registered for channel " + this.channelid); this.wrapper.registerMessage(messageHandler, packetClass, this.nextPacketID, target); Log.debug("Registered packet class %s with handler class %s for the channel %s. Send direction: to %s. The discriminator is %s.", packetClass.getSimpleName(), messageHandler.getClass().getSimpleName(), this.channelid, target.name().toLowerCase(), this.nextPacketID); this.nextPacketID++; return true; } /** * Sends the given packet to every client. * * @param message the packet to send. */ public void sendToAll(IMessage message) { this.wrapper.sendToAll(message); } /** * Sends the given packet to the given player. * * @param message the packet to send. * @param player the player to send the packet to. */ public void sendTo(IMessage message, EntityPlayerMP player) { if (player.connection != null) this.wrapper.sendTo(message, player); } /** * Sends the given packet to all players around the given target point. * * @param message the packet to send. * @param point the target point. */ public void sendToAllAround(IMessage message, NetworkRegistry.TargetPoint point) { this.wrapper.sendToAllAround(message, point); } /** * Sends the given packet to all players within the radius around the given coordinates. * * @param message the packet to send. * @param dimension the dimension. * @param x the x coordinate. * @param y the y coordinate. * @param z the z coordinate. * @param range the radius. */ public void sendToAllAround(IMessage message, int dimension, double x, double y, double z, double range) { this.sendToAllAround(message, new NetworkRegistry.TargetPoint(dimension, x, y, z, range)); } /** * Sends the given packet to all players within the radius around the given entity. * * @param message the packet to send. * @param entity the entity. * @param range the radius. */ public void sendToAllAround(IMessage message, Entity entity, double range) { this.sendToAllAround(message, entity.dimension, entity.posX, entity.posY, entity.posZ, range); } /** * Sends the given packet to the server. * * @param message the packet to send. */ public void sendToServer(IMessage message) { this.wrapper.sendToServer(message); } public void sendToAllTracking(IMessage message, Entity entity) { this.wrapper.sendToAllTracking(message, entity); } } Couple things that threw me for a loop I'm hoping for some clarification on: 1) IMessage is no longer a thing. So can I no longer do something like my registerpacket implementation anymore? Or is it going to have to be in the form of INSTANCE.registerMessage(nextID(),SendBlockData.class,SendBlockData::encode,SendBlockData::new,SendBlockData::handle); 2) Logical sides aren't specified when registering the packets. Is there no longer any distinction between packets meant on the client vs server? 3) SimpleChannel doesn't have sendToAll alongside a few other convenient methods. Thanks!
November 14, 20204 yr 5 hours ago, Turtledove said: So can I no longer do something like my registerpacket implementation anymore? Or is it going to have to be in the form of Either through the method you mentioned or the message builder within the channel itself. 5 hours ago, Turtledove said: Logical sides aren't specified when registering the packets. Is there no longer any distinction between packets meant on the client vs server? Both the method mentioned and the message builder have the option to specify the direction of packet sending, although it is optional. 5 hours ago, Turtledove said: SimpleChannel doesn't have sendToAll alongside a few other convenient methods. Does it need to? Those are helper methods the user can construct. Specifically, this is handled via PacketDistributor::ALL.
November 14, 20204 yr Author 24 minutes ago, ChampionAsh5357 said: Either through the method you mentioned or the message builder within the channel itself. Both the method mentioned and the message builder have the option to specify the direction of packet sending, although it is optional. Does it need to? Those are helper methods the user can construct. Specifically, this is handled via PacketDistributor::ALL. I think I got it, in some ways it's cleaner than how it was done in 1.12.2 since we don't need to go through an interface. public final class PacketRegistry { /** * The ID for the next packet registration. */ private static final String PROTOCOL_VERSION = Integer.toString(1); private byte nextPacketID = 0; /** * The internal network wrapper. */ private static final SimpleChannel wrapper = NetworkRegistry.ChannelBuilder .named(new ResourceLocation(Withernauts.MODID, "main_channel")) .clientAcceptedVersions(PROTOCOL_VERSION::equals) .serverAcceptedVersions(PROTOCOL_VERSION::equals) .networkProtocolVersion(() -> PROTOCOL_VERSION) .simpleChannel(); public void registerPackets() { //register packets going to the client //register packets going to the server } @SuppressWarnings({"unchecked", "rawtypes"}) protected <MESSAGE> void registerServerToClient(Class<MESSAGE> type, BiConsumer<MESSAGE, PacketBuffer> encoder, Function<PacketBuffer, MESSAGE> decoder, BiConsumer<MESSAGE, Supplier<NetworkEvent.Context>> consumer) { this.wrapper.registerMessage(nextPacketID++, type, encoder, decoder, consumer, Optional.of(NetworkDirection.PLAY_TO_CLIENT)); } @SuppressWarnings({"unchecked", "rawtypes"}) protected <MESSAGE> void registerClientToServer(Class<MESSAGE> type, BiConsumer<MESSAGE, PacketBuffer> encoder, Function<PacketBuffer, MESSAGE> decoder, BiConsumer<MESSAGE, Supplier<NetworkEvent.Context>> consumer) { this.wrapper.registerMessage(nextPacketID++, type, encoder, decoder, consumer, Optional.of(NetworkDirection.PLAY_TO_SERVER)); } /** * Sends the given packet to every client. * * @param message the packet to send. */ public<MESSAGE> void sendToAll(MESSAGE message) { this.wrapper.send(PacketDistributor.ALL.noArg(), message); } /** * Sends the given packet to the given player. * * @param message the packet to send. * @param player the player to send the packet to. */ public<MESSAGE> void sendTo(MESSAGE message, ServerPlayerEntity player) { if (player.connection != null) this.wrapper.sendTo(message, player.connection.getNetworkManager(), NetworkDirection.PLAY_TO_CLIENT); } /** * Sends the given packet to all players around the given target point. * * @param message the packet to send. * @param point the target point. */ public<MESSAGE> void sendToAllAround(MESSAGE message, PacketDistributor.TargetPoint point) { this.wrapper.send(PacketDistributor.NEAR.with(()->point), message); } /** * Sends the given packet to the server. * * @param message the packet to send. */ public<MESSAGE> void sendToServer(MESSAGE message) { this.wrapper.sendToServer(message); } public<MESSAGE> void sendToAllTracking(MESSAGE message, Entity entity) { this.wrapper.send(PacketDistributor.TRACKING_ENTITY.with(() -> entity), message); } }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.