tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:networking [2022/01/28 06:28] – [How do I fix the crash?] fix type solidblock | tutorial:networking [2024/05/04 19:51] (current) – Call out the 1.20.5 networking section at the top so people can know to skip to it bluemeanial | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Page Information ====== | + | **Note: **This page has replaced the old networking page. |
- | + | ||
- | This page has replaced the old networking page. | + | |
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 15: | Line 15: | ||
Say you had a wand which highlights the block you are looking at to all nearby players. | Say you had a wand which highlights the block you are looking at to all nearby players. | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | class HighlightingWandItem extends Item { | + | public |
public HighlightingWand(Item.Settings settings) { | public HighlightingWand(Item.Settings settings) { | ||
super(settings) | super(settings) | ||
Line 80: | Line 80: | ||
</ | </ | ||
- | 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 169: | Line 175: | ||
Firstly sending a packet to the server is done through '' | Firstly sending a packet to the server is done through '' | ||
- | ===== The concept of tracking and why you only see the highlighted block ===== | + | ===== The concept of tracking and why only you see the highlighted block ===== |
Now that the highlighting wand properly uses networking so the dedicated server does not crash, you invite your friend back on the server to show off the highlighting wand. You use the wand and the block is highlighted on your client and the server does not crash. However, your friend does not see the highlighted block. This is intentional with the code that you already have here. To solve this issue let us take a look at the item's use code: | Now that the highlighting wand properly uses networking so the dedicated server does not crash, you invite your friend back on the server to show off the highlighting wand. You use the wand and the block is highlighted on your client and the server does not crash. However, your friend does not see the highlighted block. This is intentional with the code that you already have here. To solve this issue let us take a look at the item's use code: | ||
Line 213: | Line 219: | ||
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, 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