tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:networking [2023/04/16 01:01] – Make sure to show how to make the ID fx | tutorial:networking [2024/04/15 02:17] – [Advanced Networking topics] solidblock | ||
---|---|---|---|
Line 79: | Line 79: | ||
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 90: | Line 96: | ||
Since we are not writing any data to the packet, for now, we will send the packet with an empty payload. A buf with an empty payload may be created using '' | Since we are not writing any data to the packet, for now, we will send the packet with an empty payload. A buf with an empty payload may be created using '' | ||
- | |||
- | (Note that the identifier we will put in a class accessible from both sides) | ||
- | |||
- | <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(" | ||
- | } | ||
- | </ | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 220: | Line 217: | ||
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 is totally rewrited. In 1.20.5, you have to define a custom '' | ||
- | ^ Networking Topic ^ Description ^ | + | <code java> |
- | | [[tutorial:networking: | + | 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 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 | | + | |
- | | [[tutorial:networking: | + | // or you can also write like this: |
+ | // public static final PacketCodec< | ||
+ | @Override | ||
+ | public Id<? extends CustomPayload> | ||
+ | return ID; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And then, register the receiver like this: | ||
+ | <code java> | ||
+ | PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, | ||
+ | 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/05/04 19:51 by bluemeanial