-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDispatcher.java
149 lines (136 loc) · 4.72 KB
/
Dispatcher.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
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 class Dispatcher
{
/**
* Sends the packet to the server, if the server has the packet registered.
*
* @param packet - the packet
* @param <T> - The type
*/
public static <T> void sendToServer(T packet)
{
Network.getNetworkHandler().sendToServer(packet);
}
/**
* 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 type
*/
public static <T> void sendToServer(T packet, boolean ignoreCheck)
{
Network.getNetworkHandler().sendToServer(packet, 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 type
*/
public static <T> void sendToClient(T packet, ServerPlayer player)
{
Network.getNetworkHandler().sendToClient(packet, player);
}
/**
* 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 type
*/
public static <T> void sendToClients(T packet, List<ServerPlayer> players)
{
for (ServerPlayer player : players)
{
sendToClient(packet, player);
}
}
/**
* 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 type
*/
public static <T> void sendToAllClients(T packet, MinecraftServer server)
{
sendToClients(packet, server.getPlayerList().getPlayers());
}
/**
* 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 type
*/
public static <T> void sendToClientsInLevel(T packet, ServerLevel level)
{
sendToClients(packet, level.players());
}
/**
* 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 type
*/
public static <T> void sendToClientsLoadingChunk(T packet, LevelChunk chunk)
{
ServerChunkCache chunkCache = (ServerChunkCache) chunk.getLevel().getChunkSource();
sendToClients(packet, chunkCache.chunkMap.getPlayers(chunk.getPos(), false));
}
/**
* 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 type
*/
public static <T> void sendToClientsLoadingPos(T packet, ServerLevel level, ChunkPos pos)
{
sendToClientsLoadingChunk(packet, level.getChunk(pos.x, pos.z));
}
/**
* 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 type
*/
public static <T> void sendToClientsLoadingPos(T packet, ServerLevel level, BlockPos pos)
{
sendToClientsLoadingPos(packet, level, new ChunkPos(pos));
}
/**
* 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 type
*/
public static <T> void sendToClientsInRange(T packet, ServerLevel level, BlockPos pos, double range)
{
for (ServerPlayer player : level.players())
{
if (player.distanceToSqr(pos.getX(), pos.getY(), pos.getZ()) <= range * range)
{
sendToClient(packet, player);
}
}
}
}