More packet constructors

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-07-23 08:15:25 +02:00
parent 991de2d0cc
commit 8734478126
4 changed files with 34 additions and 49 deletions

View File

@ -937,7 +937,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
@Override @Override
public void setHealth(float health) { public void setHealth(float health) {
super.setHealth(health); super.setHealth(health);
sendUpdateHealthPacket(); this.playerConnection.sendPacket(new UpdateHealthPacket(health, food, foodSaturation));
} }
@Override @Override
@ -982,7 +982,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
Check.argCondition(!MathUtils.isBetween(food, 0, 20), Check.argCondition(!MathUtils.isBetween(food, 0, 20),
"Food has to be between 0 and 20"); "Food has to be between 0 and 20");
this.food = food; this.food = food;
sendUpdateHealthPacket(); this.playerConnection.sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
} }
public float getFoodSaturation() { public float getFoodSaturation() {
@ -999,7 +999,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
Check.argCondition(!MathUtils.isBetween(foodSaturation, 0, 20), Check.argCondition(!MathUtils.isBetween(foodSaturation, 0, 20),
"Food saturation has to be between 0 and 20"); "Food saturation has to be between 0 and 20");
this.foodSaturation = foodSaturation; this.foodSaturation = foodSaturation;
sendUpdateHealthPacket(); this.playerConnection.sendPacket(new UpdateHealthPacket(getHealth(), food, foodSaturation));
} }
/** /**
@ -1335,17 +1335,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
heal(); heal();
} }
/**
* Sends an {@link UpdateHealthPacket} to refresh client-side information about health and food.
*/
protected void sendUpdateHealthPacket() {
UpdateHealthPacket updateHealthPacket = new UpdateHealthPacket();
updateHealthPacket.health = getHealth();
updateHealthPacket.food = food;
updateHealthPacket.foodSaturation = foodSaturation;
playerConnection.sendPacket(updateHealthPacket);
}
/** /**
* Gets the percentage displayed in the experience bar. * Gets the percentage displayed in the experience bar.
* *
@ -1364,9 +1353,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
public void setExp(float exp) { public void setExp(float exp) {
Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1"); Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1");
this.exp = exp; this.exp = exp;
sendExperienceUpdatePacket(); this.playerConnection.sendPacket(new SetExperiencePacket(exp, level, 0));
} }
/** /**
@ -1386,17 +1374,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
public void setLevel(int level) { public void setLevel(int level) {
this.level = level; this.level = level;
sendExperienceUpdatePacket(); this.playerConnection.sendPacket(new SetExperiencePacket(exp, level, 0));
}
/**
* Sends a {@link SetExperiencePacket} to refresh client-side information about the experience bar.
*/
protected void sendExperienceUpdatePacket() {
SetExperiencePacket setExperiencePacket = new SetExperiencePacket();
setExperiencePacket.percentage = exp;
setExperiencePacket.level = level;
playerConnection.sendPacket(setExperiencePacket);
} }
/** /**
@ -1556,7 +1534,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
public void setGameMode(@NotNull GameMode gameMode) { public void setGameMode(@NotNull GameMode gameMode) {
this.gameMode = gameMode; this.gameMode = gameMode;
// Condition to prevent sending the packets before spawning the player // Condition to prevent sending the packets before spawning the player
if (isActive()) { if (isActive()) {
sendChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.getId()); sendChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.getId());
@ -1585,7 +1562,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
protected void sendDimension(@NotNull DimensionType dimensionType) { protected void sendDimension(@NotNull DimensionType dimensionType) {
Check.argCondition(dimensionType.equals(getDimensionType()), Check.argCondition(dimensionType.equals(getDimensionType()),
"The dimension needs to be different than the current one!"); "The dimension needs to be different than the current one!");
this.dimensionType = dimensionType; this.dimensionType = dimensionType;
RespawnPacket respawnPacket = new RespawnPacket(); RespawnPacket respawnPacket = new RespawnPacket();
respawnPacket.dimensionType = dimensionType; respawnPacket.dimensionType = dimensionType;
@ -1649,11 +1625,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
public void setHeldItemSlot(byte slot) { public void setHeldItemSlot(byte slot) {
Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8"); Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8");
HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket();
heldItemChangePacket.slot = slot;
playerConnection.sendPacket(heldItemChangePacket);
refreshHeldSlot(slot); refreshHeldSlot(slot);
this.playerConnection.sendPacket(new HeldItemChangePacket(slot));
} }
/** /**
@ -2106,7 +2079,6 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
public void refreshHeldSlot(byte slot) { public void refreshHeldSlot(byte slot) {
this.heldSlot = slot; this.heldSlot = slot;
syncEquipment(EquipmentSlot.MAIN_HAND); syncEquipment(EquipmentSlot.MAIN_HAND);
refreshEating(null); refreshEating(null);
} }
@ -2241,24 +2213,18 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
protected void showPlayer(@NotNull PlayerConnection connection) { protected void showPlayer(@NotNull PlayerConnection connection) {
connection.sendPacket(getAddPlayerToList()); connection.sendPacket(getAddPlayerToList());
connection.sendPacket(getEntityType().getSpawnType().getSpawnPacket(this)); connection.sendPacket(getEntityType().getSpawnType().getSpawnPacket(this));
connection.sendPacket(getVelocityPacket()); connection.sendPacket(getVelocityPacket());
connection.sendPacket(getMetadataPacket()); connection.sendPacket(getMetadataPacket());
connection.sendPacket(getEquipmentsPacket()); connection.sendPacket(getEquipmentsPacket());
if (hasPassenger()) { if (hasPassenger()) {
connection.sendPacket(getPassengersPacket()); connection.sendPacket(getPassengersPacket());
} }
// Team // Team
if (this.getTeam() != null) if (this.getTeam() != null) {
connection.sendPacket(this.getTeam().createTeamsCreationPacket()); connection.sendPacket(this.getTeam().createTeamsCreationPacket());
}
EntityHeadLookPacket entityHeadLookPacket = new EntityHeadLookPacket(); connection.sendPacket(new EntityHeadLookPacket(getEntityId(), position.yaw()));
entityHeadLookPacket.entityId = getEntityId();
entityHeadLookPacket.yaw = position.yaw();
connection.sendPacket(entityHeadLookPacket);
} }
@NotNull @NotNull

View File

@ -10,7 +10,13 @@ public class HeldItemChangePacket implements ServerPacket {
public byte slot; public byte slot;
public HeldItemChangePacket() {} public HeldItemChangePacket(byte slot) {
this.slot = slot;
}
public HeldItemChangePacket() {
this((byte) 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -12,7 +12,15 @@ public class SetExperiencePacket implements ServerPacket {
public int level; public int level;
public int totalExperience; public int totalExperience;
public SetExperiencePacket() {} public SetExperiencePacket(float percentage, int level, int totalExperience) {
this.percentage = percentage;
this.level = level;
this.totalExperience = totalExperience;
}
public SetExperiencePacket() {
this(0, 0, 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {

View File

@ -12,10 +12,15 @@ public class UpdateHealthPacket implements ServerPacket {
public int food; public int food;
public float foodSaturation; public float foodSaturation;
/** public UpdateHealthPacket(float health, int food, float foodSaturation) {
* Default constructor, required for reflection operations. this.health = health;
*/ this.food = food;
public UpdateHealthPacket() {} this.foodSaturation = foodSaturation;
}
public UpdateHealthPacket() {
this(0, 0, 0);
}
@Override @Override
public void write(@NotNull BinaryWriter writer) { public void write(@NotNull BinaryWriter writer) {