public interface ServerConnection
Interface used to handle packet sending. All particles lists extends it to provide sending packets functionality on top of creating particle packets.
It provides a non-reflective methods that uses NMS PlayerConnection
to send them.
| Modifier and Type | Method and Description |
|---|---|
PlayerConnection |
createPlayerConnection(org.bukkit.entity.Player player)
Creates a non-reflective wrapper of
player's NMS
PlayerConnection instance. |
void |
sendPacket(Collection<org.bukkit.entity.Player> players,
Object packet)
Sends packet to each
Player. |
void |
sendPacket(org.bukkit.Location loc,
double radius,
Object packet)
Sends a packet to every player in given radius.
|
void |
sendPacket(org.bukkit.entity.Player player,
Object packet)
Sends packet to a
Player. |
void |
sendPacketIf(Collection<org.bukkit.entity.Player> players,
Object packet,
PlayerPredicate predicate)
Sends packet to each
Player that matches predicate. |
void |
sendPacketIf(org.bukkit.Location loc,
double radius,
Object packet,
PlayerPredicate predicate)
Sends a packet to every player in given radius
that matches predicate.
|
PlayerConnection createPlayerConnection(org.bukkit.entity.Player player)
Creates a non-reflective wrapper of
player's NMS PlayerConnection instance.
A generated code for this method looks roughly like this:
PlayerConnection createPlayerConnection(Player player) {
return new PlayerConnection_Impl(player);
}
If you plan to send more than 4-5 packets to one
of Player somewhere, then using this wrapper slightly faster
than using particles lists due to
caching NMS PlayerConnection directly in field.
It is better not to cache it long-term (for ex.
in HashMap/ArrayList etc.) and any complications to do it
anyways will be significantly slower than particles lists.
PlayerConnection from particles lists is fast, really.player - a player from which PlayerConnection should be obtained.PlayerConnection wrapper of
this player's NMS PlayerConnectionPlayerConnectionvoid sendPacket(org.bukkit.entity.Player player,
Object packet)
Sends packet to a Player.
A generated code for this method looks roughly like this:
void sendPacket(Player player, Object packet) {
((CraftPlayer) player).getHandle().playerConnection
.sendPacket((Packet) packet);
}
A packet parameter must be an instance of Minecraft packet interface.
Otherwise, you might get ClassCastException on packet parameter.
You can use this method to send other packet than instances created using this API. Any valid Minecraft packet can be used by this method.
player - a player to which send a packet object.packet - a valid Minecraft packet created either by this API or
via reflections.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacket(Collection<org.bukkit.entity.Player> players, Object packet)
Sends packet to each Player.
A generated code for this method looks roughly like this:
void sendPacket(Collection<Player> players, Object packet) {
Packet nmsPacket = (Packet) packet;
int length = players.size();
Iterator it = players.iterator();
while (length > 0) {
((CraftPlayer) it.next()).getHandle().playerConnection
.sendPacket(nmsPacket);
--length;
}
}
A packet parameter must be an instance of Minecraft packet interface.
Otherwise, you might get ClassCastException on packet parameter.
You can use this method to send other packet than instances created using this API. Any valid Minecraft packet can be used by this method.
players - a Collection of players to which send a packet object.packet - a valid Minecraft packet created either by this API or
via reflections.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacketIf(Collection<org.bukkit.entity.Player> players, Object packet, PlayerPredicate predicate)
Sends packet to each Player that matches predicate.
A generated code for this method looks roughly like this:
void sendPacketIf(Collection<Player> players, Object packet, PlayerPredicate predicate) {
Packet nmsPacket = (Packet) packet;
int length = players.size();
Iterator it = players.iterator();
while (length > 0) {
CraftPlayer p = (CraftPlayer) it.next();
if (predicate.shouldSend(p)) {
p.getHandle().playerConnection
.sendPacket(nmsPacket);
}
--length;
}
}
A packet parameter must be an instance of Minecraft packet interface.
Otherwise, you might get ClassCastException on packet parameter.
You can use this method to send other packet than instances created using this API. Any valid Minecraft packet can be used by this method.
players - a Collection of players to which send a packet object.packet - a valid Minecraft packet created either by this API or
via reflections.predicate - a PlayerPredicate used if packet should be send.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacket(org.bukkit.Location loc,
double radius,
Object packet)
Sends a packet to every player in given radius.
Technically speaking, gets all players from loc parameter's world
and send packet to every player in radius.
A generated code for this method looks (roughly) like this:
void sendPacket(Location loc, double radius, Object packet) {
radius *= radius;
Packet nmsPacket = (Packet) packet;
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
// this initializations are optimized
int length = loc.getWorld().getPlayers().size();
Iterator it = loc.getWorld().getPlayers().iterator();
while (length > 0) {
CraftPlayer p = (CraftPlayer) it.next();
Location pLoc = p.getLocation();
// pseudo code if statement is optimized
if (( (pLoc.getX() - x)^2
+ (pLoc.getY() - y)^2
+ (pLoc.getZ() - z)^2) <= radius) {
p.getHandle().playerConnection.sendPacket(nmsPacket);
}
--length;
}
}
This method should be a little faster than normal for-each loop with radius check due to few bytecode optimizations:
length and it variables,for loop with
cached list size instead of hasNext interface
method check on each iteration.A packet parameter must be an instance of Minecraft packet interface.
Otherwise, you might get ClassCastException on packet parameter.
You can use this method to send other packet than instances created using this API. Any valid Minecraft packet can be used by this method.
loc - a Location containing position.radius - a spherical radius around which send packet to
nearby players.packet - a valid Minecraft packet created either by this API or
via reflections.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacketIf(org.bukkit.Location loc,
double radius,
Object packet,
PlayerPredicate predicate)
Sends a packet to every player in given radius that matches predicate.
Technically speaking, gets all players from loc parameter's world
and send packet to every player in radius that matches predicate.
Predicate is executed after radius check.
A generated code for this method looks (roughly) like this:
void sendPacketIf(Location loc, double radius, Object packet, PlayerPredicate predicate) {
radius *= radius;
Packet nmsPacket = (Packet) packet;
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
// this initializations are optimized
int length = loc.getWorld().getPlayers().size();
Iterator it = loc.getWorld().getPlayers().iterator();
while (length > 0) {
CraftPlayer p = (CraftPlayer) it.next();
Location pLoc = p.getLocation();
// pseudo code if statement is optimized
if (( (pLoc.getX() - x)^2
+ (pLoc.getY() - y)^2
+ (pLoc.getZ() - z)^2) <= radius
&& predicate.shouldSend(p)) {
p.getHandle().playerConnection.sendPacket(nmsPacket);
}
--length;
}
}
This method should be a little faster than normal for-each loop with radius check due to few bytecode optimizations:
length and it variables,for loop with
cached list size instead of hasNext interface
method check on each iteration.A packet parameter must be an instance of Minecraft packet interface.
Otherwise, you might get ClassCastException on packet parameter.
You can use this method to send other packet than instances created using this API. Any valid Minecraft packet can be used by this method.
loc - a Location containing position.radius - a spherical radius around which send packet to
nearby players.packet - a valid Minecraft packet created either by this API or
via reflections.predicate - a PlayerPredicate used if packet should be send.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.Copyright © 2022. All Rights Reserved.