tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:networking [2020/12/14 00:56] – Update to API call getStackInHand zulrang | tutorial:networking [2024/04/15 02:17] (current) – [Advanced Networking topics] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Page Information ====== | + | **Note: **This page has replaced the old networking page. |
- | + | It is recommended to use the new networking | |
- | This page has replaced the old networking page. | + | |
- | It is recommended to use the new networking | + | |
The old page can be [[tutorial: | The old page can be [[tutorial: | ||
Line 15: | Line 13: | ||
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 51: | Line 49: | ||
[[Minecraft' | [[Minecraft' | ||
- | The diagram above shows that the game client and dedicated server | + | The diagram above shows that the game client and dedicated server |
^ Connection Type ^ Access to game client | ^ Connection Type ^ Access to game client | ||
Line 58: | Line 56: | ||
| Singleplayer (or LAN host) | Yes -> Full access | | Singleplayer (or LAN host) | Yes -> Full access | ||
- | It may seem complicated to have to communication with the server in three different ways. However you don't need to communicate in three different ways with the game client. Since all three connection types communicate with the game client using packets, you only need to communicate with the game client like you are always running on a dedicated server. Connection to server over LAN or Singleplayer can be also be treated like the server is a remote dedicated server; so your game client cannot directly access the server instance. | + | It may seem complicated to have communication with the server in three different ways. However, you don't need to communicate in three different ways with the game client. Since all three connection types communicate with the game client using packets, you only need to communicate with the game client like you are always running on a dedicated server. Connection to a server over LAN or Singleplayer can be also be treated like the server is a remote dedicated server; so your game client cannot directly access the server instance. |
===== An introduction to networking ===== | ===== An introduction to networking ===== | ||
Line 80: | Line 78: | ||
</ | </ | ||
- | 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 91: | Line 95: | ||
The player in this method is the player the packet will be sent to. The channel name is the '' | The player in this method is the player the packet will be sent to. The channel name is the '' | ||
- | 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 '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
.... | .... | ||
- | ServerPlayNetworking.send((ServerPlayerEntity) user, ModNetworkingConstants.HIGHLIGHT_PACKET_ID, | + | ServerPlayNetworking.send((ServerPlayerEntity) user, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, |
return TypedActionResult.success(user.getHandStack(hand)); | return TypedActionResult.success(user.getHandStack(hand)); | ||
} | } | ||
Line 105: | Line 109: | ||
To receive a packet from a server on the game client, your mod needs to specify how it will handle the incoming packet. | To receive a packet from a server on the game client, your mod needs to specify how it will handle the incoming packet. | ||
- | In your client entrypoint, you will register the receiver for your packet using '' | + | In your client entrypoint, you will register the receiver for your packet using '' |
- | The '' | + | The '' |
The example below implements the play channel handler as a lambda: | The example below implements the play channel handler as a lambda: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, | + | ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, |
... | ... | ||
}); | }); | ||
</ | </ | ||
- | However you cannot draw the highlight box immediately. This is because the receiver is called on the netty event loop. The event loop runs on another thread, and you must draw the highlight box on the render thread. | + | However, you cannot draw the highlight box immediately. This is because the receiver is called on the netty event loop. The event loop runs on another thread, and you must draw the highlight box on the render thread. |
In order to draw the highlight box, you need to schedule the task on the game client. This may be done with the '' | In order to draw the highlight box, you need to schedule the task on the game client. This may be done with the '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, | + | ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, |
client.execute(() -> { | client.execute(() -> { | ||
// Everything in this lambda is run on the render thread | // Everything in this lambda is run on the render thread | ||
Line 130: | Line 134: | ||
</ | </ | ||
- | You may have noticed you are not told where the block to highlight is. You can write this data to the packet byte buf. Instead of sending '' | + | You may have noticed you are not told where the block to highlight is. You can write this data to the packet byte buf. Instead of sending '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 136: | Line 140: | ||
</ | </ | ||
- | Next you need to write the data to the packet byte buf. It should be noted that you must read data in the same order you write it. | + | Next, you need to write the data to the packet byte buf. It should be noted that you must read data in the same order you write it. |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 144: | Line 148: | ||
</ | </ | ||
- | Afterwards you will send the '' | + | Afterwards, you will send the '' |
To read this block position on the game client, you can use '' | To read this block position on the game client, you can use '' | ||
Line 152: | Line 156: | ||
In the end, the client' | In the end, the client' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, | + | ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, |
// Read packet data on the event loop | // Read packet data on the event loop | ||
BlockPos target = buf.readBlockPos(); | BlockPos target = buf.readBlockPos(); | ||
Line 165: | Line 169: | ||
==== Sending packets to the server and receiving packets on the server ==== | ==== Sending packets to the server and receiving packets on the server ==== | ||
- | Sending packets to a server and receiving a packet on the server | + | Sending packets to a server and receiving a packet on the server |
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: |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 183: | Line 187: | ||
buf.writeBlockPos(target); | buf.writeBlockPos(target); | ||
- | ServerPlayNetworking.send((ServerPlayerEntity) user, ModNetworkingConstants.HIGHLIGHT_PACKET_ID, | + | ServerPlayNetworking.send((ServerPlayerEntity) user, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, |
return TypedActionResult.success(user.getHandStack(hand)); | return TypedActionResult.success(user.getHandStack(hand)); | ||
} | } | ||
Line 192: | Line 196: | ||
Since we know where the highlight will occur, we can use '' | Since we know where the highlight will occur, we can use '' | ||
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
public TypedActionResult< | public TypedActionResult< | ||
// Verify we are processing the use action on the logical server | // Verify we are processing the use action on the logical server | ||
Line 204: | Line 208: | ||
// Iterate over all players tracking a position in the world and send the packet to each player | // Iterate over all players tracking a position in the world and send the packet to each player | ||
for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) { | for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) { | ||
- | ServerPlayNetworking.send(player, | + | ServerPlayNetworking.send(player, |
} | } | ||
Line 211: | Line 215: | ||
</ | </ | ||
- | After this change, when you use the wand, you 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.1607907372.txt.gz · Last modified: 2020/12/14 00:56 by zulrang