-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNetworkHandler.java
251 lines (230 loc) · 8.3 KB
/
NetworkHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package commonnetwork.api;
import net.minecraft.core.BlockPos;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.LevelChunk;
import java.util.List;
public interface NetworkHandler
{
/**
* Sends the packet to the server, if the server has the packet registered.
*
* @param packet - the packet
* @param <T> - The packet
*/
default <T> void sendToServer(T packet)
{
sendToServer(packet, false);
}
/**
* Sends the packet to the server. Can ignore the check if the server has the packet registered.
* Likely use case for this is talking to bukkit/spigot/paper servers.
*
* @param packet - the packet
* @param ignoreCheck - ignore the check if the server has the packet registered.
* @param <T> - The packet
*/
<T> void sendToServer(T packet, boolean ignoreCheck);
/**
* Sends the packet to the client player, only if the player has the packet registered.
*
* @param packet - the packet
* @param player - the player
* @param <T> - The packet
*/
default <T> void sendToClient(T packet, ServerPlayer player)
{
sendToClient(packet, player, false);
}
/**
* Sends the packet to the client player..
*
* @param packet - the packet
* @param player - the player
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
<T> void sendToClient(T packet, ServerPlayer player, boolean ignoreCheck);
/**
* Sends the packet to the client players, only if the players has the packet registered.
*
* @param packet - the packet
* @param players - the players
* @param <T> - The packet
*/
default <T> void sendToClients(T packet, List<ServerPlayer> players)
{
sendToClients(packet, players, false);
}
/**
* Sends the packet to the client players.
*
* @param packet - the packet
* @param players - the players
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClients(T packet, List<ServerPlayer> players, boolean ignoreCheck)
{
for (ServerPlayer player : players)
{
sendToClient(packet, player, ignoreCheck);
}
}
/**
* Sends the packet to all the client players in the server, only if the players has the packet registered.
*
* @param packet - the packet
* @param server - the server
* @param <T> - The packet
*/
default <T> void sendToAllClients(T packet, MinecraftServer server)
{
sendToAllClients(packet, server, false);
}
/**
* Sends the packet to all the client players in the server
*
* @param packet - the packet
* @param server - the server
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToAllClients(T packet, MinecraftServer server, boolean ignoreCheck)
{
sendToClients(packet, server.getPlayerList().getPlayers(), ignoreCheck);
}
/**
* Sends the packet to all the client players in the level, only if the players has the packet registered.
*
* @param packet - the packet
* @param level - the level
* @param <T> - The packet
*/
default <T> void sendToClientsInLevel(T packet, ServerLevel level)
{
sendToClientsInLevel(packet, level, false);
}
/**
* Sends the packet to all the client players in the level.
*
* @param packet - the packet
* @param level - the level
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClientsInLevel(T packet, ServerLevel level, boolean ignoreCheck)
{
sendToClients(packet, level.players(), ignoreCheck);
}
/**
* Sends the packet to all the client players loading a chunk, only if the players has the packet registered.
*
* @param packet - the packet
* @param chunk - the chunk
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingChunk(T packet, LevelChunk chunk)
{
sendToClientsLoadingChunk(packet, chunk, false);
}
/**
* Sends the packet to all the client players loading a chunk.
*
* @param packet - the packet
* @param chunk - the chunk
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingChunk(T packet, LevelChunk chunk, boolean ignoreCheck)
{
ServerChunkCache chunkCache = (ServerChunkCache) chunk.getLevel().getChunkSource();
sendToClients(packet, chunkCache.chunkMap.getPlayers(chunk.getPos(), false), ignoreCheck);
}
/**
* Sends the packet to all the client players loading a position, only if the players has the packet registered.
*
* @param packet - the packet
* @param level - the level
* @param pos - the chunkpos
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingPos(T packet, ServerLevel level, ChunkPos pos)
{
sendToClientsLoadingPos(packet, level, pos, false);
}
/**
* Sends the packet to all the client players loading a position.
*
* @param packet - the packet
* @param level - the level
* @param pos - the chunkpos
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingPos(T packet, ServerLevel level, ChunkPos pos, boolean ignoreCheck)
{
sendToClientsLoadingChunk(packet, level.getChunk(pos.x, pos.z), ignoreCheck);
}
/**
* Sends the packet to all the client players loading a position, only if the players has the packet registered.
*
* @param packet - the packet
* @param level - the level
* @param pos - the blockpos
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingPos(T packet, ServerLevel level, BlockPos pos)
{
sendToClientsLoadingPos(packet, level, pos, false);
}
/**
* Sends the packet to all the client players loading a position
*
* @param packet - the packet
* @param level - the level
* @param pos - the blockpos
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClientsLoadingPos(T packet, ServerLevel level, BlockPos pos, boolean ignoreCheck)
{
sendToClientsLoadingPos(packet, level, new ChunkPos(pos), ignoreCheck);
}
/**
* Sends the packet to all the client players in range of a position, only if the players has the packet registered.
*
* @param packet - the packet
* @param level - the level
* @param pos - the blockpos
* @param range - the range
* @param <T> - The packet
*/
default <T> void sendToClientsInRange(T packet, ServerLevel level, BlockPos pos, double range)
{
sendToClientsInRange(packet, level, pos, range, false);
}
/**
* Sends the packet to all the client players in range of a position.
*
* @param packet - the packet
* @param level - the level
* @param pos - the blockpos
* @param range - the range
* @param ignoreCheck - ignore the check if the client has the packet registered.
* @param <T> - The packet
*/
default <T> void sendToClientsInRange(T packet, ServerLevel level, BlockPos pos, double range, boolean ignoreCheck)
{
for (ServerPlayer player : level.players())
{
if (player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ()) <= range * range)
{
sendToClient(packet, player, ignoreCheck);
}
}
}
}