tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:networking [2023/01/11 19:25] – [Example: Why is networking important?] netuserget | tutorial:networking [2024/06/10 11:45] (current) – 1.20.5: general improvements l1ttle | ||
---|---|---|---|
Line 2: | Line 2: | ||
It is recommended to use the new networking API described on this page. | It is recommended to use the new networking API described on this page. | ||
The old page can be [[tutorial: | The old page can be [[tutorial: | ||
+ | |||
+ | For the even newer networking API introduced at 1.20.5, read the [[# | ||
====== Networking ====== | ====== Networking ====== | ||
Line 79: | Line 81: | ||
Next, we need to send the packet to the game client. First, you need to define an '' | Next, we need to send the packet to the game client. First, you need to define an '' | ||
+ | <code java> | ||
+ | public class TutorialNetworkingConstants { | ||
+ | // Save the id of the packet so we can reference it later | ||
+ | public static final Identifier HIGHLIGHT_PACKET_ID = new Identifier(" | ||
+ | } | ||
+ | </ | ||
To send the packet to the player, we will use some of the methods inside of '' | To send the packet to the player, we will use some of the methods inside of '' | ||
Line 123: | Line 131: | ||
client.execute(() -> { | client.execute(() -> { | ||
// Everything in this lambda is run on the render thread | // Everything in this lambda is run on the render thread | ||
- | ClientBlockHighlighting.highlightBlock(client, | ||
}); | }); | ||
}); | }); | ||
Line 211: | Line 218: | ||
After this change, when you use the wand, your friend should also see the highlighted block on their own client. | After this change, when you use the wand, your friend should also see the highlighted block on their own client. | ||
- | ====== Advanced Networking topics ====== | ||
- | The Networking | + | ===== Networking |
+ | Since 1.20.5, the logic of networking has been totally rewritten. In 1.20.5, RegistryByteBuf is now used in PLAY-phase networking and you have to define a custom '' | ||
- | ^ Networking Topic ^ Description ^ | + | <code java> |
- | | [[tutorial:networking:connection_events|Connection Network connection events]] | Events related | + | public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload { |
- | | [[tutorial:networking:channel_events|Channel registration events]] | Events related to a server of client declaring the ability to receive a packet on a channel of a specific name | | + | public static final CustomPayload.Id< |
- | | [[tutorial:networking:login|Login phase networking]]| Sending requests to a client during login; and allowing delay of login for a short amount of time | | + | public static final PacketCodec< |
- | | [[tutorial:networking:dynamic_handlers|Dynamic registration of channel handlers]]| Allowing for a connection to receive a packet with a special handler | | + | // should you need to send more data, add the appropriate record parameters and change your codec: |
+ | // public static final PacketCodec< | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // ); | ||
+ | @Override | ||
+ | public CustomPayload.Id<? | ||
+ | return ID; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And then, register the receiver like this: | ||
+ | <code java> | ||
+ | // In your common initializer method | ||
+ | PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, | ||
+ | |||
+ | // In your client-only initializer method | ||
+ | ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, | ||
+ | context.client().execute(() -> { | ||
+ | ClientBlockHighlighting.highlightBlock(client, | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | Now, on the server side, you can send the packet to players like this: | ||
+ | <code java> | ||
+ | public TypedActionResult< | ||
+ | if (world.isClient()) return super.use(world, | ||
+ | |||
+ | // ... | ||
+ | |||
+ | for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) { | ||
+ | ServerPlayNetworking.send(player, | ||
+ | } | ||
+ | |||
+ | return TypedActionResult.success(user.getHandStack(hand)); | ||
+ | } | ||
+ | </ |
tutorial/networking.txt · Last modified: 2024/06/10 11:45 by l1ttle