public interface ServerConnection
Interface used to handle packet sending.
It provides a non-reflective methods that uses NMS PlayerConnection
to send Minecraft packets.
It is dynamically generated by ParticleNativeAPI instance and should
only be obtained from there.
ParticleNativeAPI| Modifier and Type | Method and Description |
|---|---|
PlayerConnection |
createPlayerConnection(org.bukkit.entity.Player player)
Creates a non-reflective wrapper of
parameter player's NMS
PlayerConnection instance. |
void |
sendPacket(java.util.Collection<org.bukkit.entity.Player> players,
java.lang.Object packet)
Sends packet to each Player using their NMS
PlayerConnection. |
void |
sendPacket(org.bukkit.Location loc,
double radius,
java.lang.Object packet)
Send a packet to every player in given radius.
|
void |
sendPacket(org.bukkit.entity.Player player,
java.lang.Object packet)
Sends packet to a Player using its NMS
PlayerConnection. |
PlayerConnection createPlayerConnection(org.bukkit.entity.Player player)
Creates a non-reflective wrapper of
parameter 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 Players somewhere, then using this wrapper will be
more beneficial (faster) than using ServerConnection 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 ServerConnection.
PlayerConnection from ServerConnection is fast, really.player - a player from which PlayerConnection should be obtained.PlayerConnection wrapper of
this player's NMS PlayerConnection.PlayerConnectionvoid sendPacket(org.bukkit.entity.Player player,
java.lang.Object packet)
Sends packet to a Player using its NMS PlayerConnection.
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.java.lang.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacket(java.util.Collection<org.bukkit.entity.Player> players,
java.lang.Object packet)
Sends packet to each Player using their NMS PlayerConnection.
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.java.lang.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.void sendPacket(org.bukkit.Location loc,
double radius,
java.lang.Object packet)
Send a packet to every player in given radius.
Technically speaking, gets all players around loc parameter
in given radius and send packet to every player in radius
using its NMS PlayerConnection.
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.java.lang.ClassCastException - when provided packet object is not
an instance of Minecraft packet interface.Copyright © 2020. All Rights Reserved.