More inline packets

This commit is contained in:
TheMode 2021-07-22 12:50:38 +02:00
parent ff9ca60a58
commit 081266775c
8 changed files with 57 additions and 67 deletions

View File

@ -271,19 +271,8 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
*/ */
public void setView(float yaw, float pitch) { public void setView(float yaw, float pitch) {
this.position = position.withView(yaw, pitch); this.position = position.withView(yaw, pitch);
sendPacketToViewersAndSelf(new EntityHeadLookPacket(getEntityId(), yaw));
EntityRotationPacket entityRotationPacket = new EntityRotationPacket(); sendPacketToViewersAndSelf(new EntityRotationPacket(getEntityId(), yaw, pitch, onGround));
entityRotationPacket.entityId = getEntityId();
entityRotationPacket.yaw = yaw;
entityRotationPacket.pitch = pitch;
entityRotationPacket.onGround = onGround;
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = yaw;
sendPacketToViewersAndSelf(entityHeadLookPacket);
sendPacketToViewersAndSelf(entityRotationPacket);
} }
/** /**
@ -330,20 +319,12 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
playerConnection.sendPacket(getVelocityPacket()); playerConnection.sendPacket(getVelocityPacket());
} }
playerConnection.sendPacket(getMetadataPacket()); playerConnection.sendPacket(getMetadataPacket());
// Passenger // Passenger
if (hasPassenger()) { if (hasPassenger()) {
playerConnection.sendPacket(getPassengersPacket()); playerConnection.sendPacket(getPassengersPacket());
} }
// Head position // Head position
{ playerConnection.sendPacket(new EntityHeadLookPacket(getEntityId(), position.yaw()));
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket();
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = position.yaw();
playerConnection.sendPacket(entityHeadLookPacket);
}
return true; return true;
} }
@ -948,10 +929,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
* @param status the status to trigger * @param status the status to trigger
*/ */
public void triggerStatus(byte status) { public void triggerStatus(byte status) {
EntityStatusPacket statusPacket = new EntityStatusPacket(); sendPacketToViewersAndSelf(new EntityStatusPacket(getEntityId(), status));
statusPacket.entityId = getEntityId();
statusPacket.status = status;
sendPacketToViewersAndSelf(statusPacket);
} }
/** /**
@ -1381,13 +1359,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
} }
protected @NotNull EntityVelocityPacket getVelocityPacket() { protected @NotNull EntityVelocityPacket getVelocityPacket() {
EntityVelocityPacket velocityPacket = new EntityVelocityPacket(); return new EntityVelocityPacket(getEntityId(), getVelocityForPacket());
velocityPacket.entityId = getEntityId();
Vec velocity = getVelocityForPacket();
velocityPacket.velocityX = (short) velocity.x();
velocityPacket.velocityY = (short) velocity.y();
velocityPacket.velocityZ = (short) velocity.z();
return velocityPacket;
} }
/** /**

View File

@ -48,11 +48,7 @@ public enum EntitySpawnType {
PLAYER { PLAYER {
@Override @Override
public ServerPacket getSpawnPacket(Entity entity) { public ServerPacket getSpawnPacket(Entity entity) {
SpawnPlayerPacket packet = new SpawnPlayerPacket(); return new SpawnPlayerPacket(entity.getEntityId(), entity.getUuid(), entity.getPosition());
packet.entityId = entity.getEntityId();
packet.playerUuid = entity.getUuid();
packet.position = entity.getPosition();
return packet;
} }
}, },
EXPERIENCE_ORB { EXPERIENCE_ORB {

View File

@ -206,8 +206,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
final Set<Entity> entities = instance.getChunkEntities(chunk); final Set<Entity> entities = instance.getChunkEntities(chunk);
for (Entity entity : entities) { for (Entity entity : entities) {
if (entity instanceof ItemEntity) { if (entity instanceof ItemEntity) {
// Do not pick up if not visible
// Do not pickup if not visible
if (this instanceof Player && !entity.isViewer((Player) this)) if (this instanceof Player && !entity.isViewer((Player) this))
continue; continue;
@ -222,12 +221,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
PickupItemEvent pickupItemEvent = new PickupItemEvent(this, itemEntity); PickupItemEvent pickupItemEvent = new PickupItemEvent(this, itemEntity);
EventDispatcher.callCancellable(pickupItemEvent, () -> { EventDispatcher.callCancellable(pickupItemEvent, () -> {
final ItemStack item = itemEntity.getItemStack(); final ItemStack item = itemEntity.getItemStack();
sendPacketToViewersAndSelf(new CollectItemPacket(itemEntity.getEntityId(), getEntityId(), item.getAmount()));
CollectItemPacket collectItemPacket = new CollectItemPacket();
collectItemPacket.collectedEntityId = itemEntity.getEntityId();
collectItemPacket.collectorEntityId = getEntityId();
collectItemPacket.pickupItemCount = item.getAmount();
sendPacketToViewersAndSelf(collectItemPacket);
entity.remove(); entity.remove();
}); });
} }
@ -358,10 +352,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
float remainingDamage = entityDamageEvent.getDamage(); float remainingDamage = entityDamageEvent.getDamage();
EntityAnimationPacket entityAnimationPacket = new EntityAnimationPacket(); sendPacketToViewersAndSelf(new EntityAnimationPacket(getEntityId(), EntityAnimationPacket.Animation.TAKE_DAMAGE));
entityAnimationPacket.entityId = getEntityId();
entityAnimationPacket.animation = EntityAnimationPacket.Animation.TAKE_DAMAGE;
sendPacketToViewersAndSelf(entityAnimationPacket);
// Additional hearts support // Additional hearts support
if (this instanceof Player) { if (this instanceof Player) {
@ -546,11 +537,9 @@ public class LivingEntity extends Entity implements EquipmentHandler {
final PlayerConnection playerConnection = player.getPlayerConnection(); final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(getEquipmentsPacket()); playerConnection.sendPacket(getEquipmentsPacket());
playerConnection.sendPacket(getPropertiesPacket()); playerConnection.sendPacket(getPropertiesPacket());
if (getTeam() != null) { if (getTeam() != null) {
playerConnection.sendPacket(getTeam().createTeamsCreationPacket()); playerConnection.sendPacket(getTeam().createTeamsCreationPacket());
} }
return true; return true;
} }
@ -565,10 +554,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
* (can be used for attack animation). * (can be used for attack animation).
*/ */
public void swingMainHand() { public void swingMainHand() {
EntityAnimationPacket animationPacket = new EntityAnimationPacket(); sendPacketToViewers(new EntityAnimationPacket(getEntityId(), EntityAnimationPacket.Animation.SWING_MAIN_ARM));
animationPacket.entityId = getEntityId();
animationPacket.animation = EntityAnimationPacket.Animation.SWING_MAIN_ARM;
sendPacketToViewers(animationPacket);
} }
/** /**
@ -576,10 +562,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
* (can be used for attack animation). * (can be used for attack animation).
*/ */
public void swingOffHand() { public void swingOffHand() {
EntityAnimationPacket animationPacket = new EntityAnimationPacket(); sendPacketToViewers(new EntityAnimationPacket(getEntityId(), EntityAnimationPacket.Animation.SWING_OFF_HAND));
animationPacket.entityId = getEntityId();
animationPacket.animation = EntityAnimationPacket.Animation.SWING_OFF_HAND;
sendPacketToViewers(animationPacket);
} }
public void refreshActiveHand(boolean isHandActive, boolean offHand, boolean riptideSpinAttack) { public void refreshActiveHand(boolean isHandActive, boolean offHand, boolean riptideSpinAttack) {

View File

@ -12,7 +12,15 @@ public class CollectItemPacket implements ServerPacket {
public int collectorEntityId; public int collectorEntityId;
public int pickupItemCount; public int pickupItemCount;
public CollectItemPacket() {} public CollectItemPacket(int collectedEntityId, int collectorEntityId, int pickupItemCount) {
this.collectedEntityId = collectedEntityId;
this.collectorEntityId = collectorEntityId;
this.pickupItemCount = pickupItemCount;
}
public CollectItemPacket() {
this(0, 0, 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -11,8 +11,13 @@ public class EntityAnimationPacket implements ServerPacket {
public int entityId; public int entityId;
public Animation animation; public Animation animation;
public EntityAnimationPacket(int entityId, Animation animation) {
this.entityId = entityId;
this.animation = animation;
}
public EntityAnimationPacket() { public EntityAnimationPacket() {
animation = Animation.SWING_MAIN_ARM; this(0, Animation.SWING_MAIN_ARM);
} }
@Override @Override

View File

@ -11,7 +11,14 @@ public class EntityStatusPacket implements ServerPacket {
public int entityId; public int entityId;
public byte status; public byte status;
public EntityStatusPacket() {} public EntityStatusPacket(int entityId, byte status) {
this.entityId = entityId;
this.status = status;
}
public EntityStatusPacket() {
this(0, (byte) 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -1,5 +1,6 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.minestom.server.coordinate.Point;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryReader;
@ -11,7 +12,20 @@ public class EntityVelocityPacket implements ServerPacket {
public int entityId; public int entityId;
public short velocityX, velocityY, velocityZ; public short velocityX, velocityY, velocityZ;
public EntityVelocityPacket() {} public EntityVelocityPacket(int entityId, short velocityX, short velocityY, short velocityZ) {
this.entityId = entityId;
this.velocityX = velocityX;
this.velocityY = velocityY;
this.velocityZ = velocityZ;
}
public EntityVelocityPacket(int entityId, Point velocity) {
this(entityId, (short) velocity.x(), (short) velocity.y(), (short) velocity.z());
}
public EntityVelocityPacket() {
this(0, (short) 0, (short) 0, (short) 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -1,10 +1,10 @@
package net.minestom.server.network.packet.server.play; package net.minestom.server.network.packet.server.play;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.coordinate.Pos;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.UUID; import java.util.UUID;
@ -15,9 +15,14 @@ public class SpawnPlayerPacket implements ServerPacket {
public UUID playerUuid; public UUID playerUuid;
public Pos position; public Pos position;
public SpawnPlayerPacket(int entityId, UUID playerUuid, Pos position) {
this.entityId = entityId;
this.playerUuid = playerUuid;
this.position = position;
}
public SpawnPlayerPacket() { public SpawnPlayerPacket() {
playerUuid = new UUID(0, 0); this(0, new UUID(0, 0), Pos.ZERO);
position = Pos.ZERO;
} }
@Override @Override