Fix properties & equipments changes not being properly sent to viewers & self

This commit is contained in:
themode 2021-03-12 01:38:52 +01:00
parent be6fddfa31
commit d91fcde798
6 changed files with 32 additions and 45 deletions

View File

@ -57,10 +57,6 @@ public class AttributeInstance {
if (this.baseValue != baseValue) {
this.baseValue = baseValue;
refreshCachedValue();
if (propertyChangeListener != null) {
propertyChangeListener.accept(this);
}
}
}
@ -126,5 +122,10 @@ public class AttributeInstance {
}
this.cachedValue = Math.min(result, getAttribute().getMaxValue());
// Signal entity
if (propertyChangeListener != null) {
propertyChangeListener.accept(this);
}
}
}

View File

@ -15,7 +15,9 @@ import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.block.Block;
import net.minestom.server.inventory.EquipmentHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.server.play.*;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.scoreboard.Team;
import net.minestom.server.sound.Sound;
import net.minestom.server.sound.SoundCategory;
@ -484,9 +486,23 @@ public class LivingEntity extends Entity implements EquipmentHandler {
/**
* Callback used when an attribute instance has been modified.
*
* @param instance the modified attribute instance
* @param attributeInstance the modified attribute instance
*/
protected void onAttributeChanged(@NotNull AttributeInstance instance) {
protected void onAttributeChanged(@NotNull AttributeInstance attributeInstance) {
if (attributeInstance.getAttribute().isShared()) {
boolean self = false;
if (this instanceof Player) {
Player player = (Player) this;
PlayerConnection playerConnection = player.playerConnection;
// connection null during Player initialization (due to #super call)
self = playerConnection != null && playerConnection.getConnectionState() == ConnectionState.PLAY;
}
if (self) {
sendPacketToViewersAndSelf(getPropertiesPacket());
} else {
sendPacketToViewers(getPropertiesPacket());
}
}
}
/**
@ -532,7 +548,9 @@ public class LivingEntity extends Entity implements EquipmentHandler {
if (!super.addViewer0(player)) {
return false;
}
syncEquipments(player.getPlayerConnection());
final PlayerConnection playerConnection = player.getPlayerConnection();
playerConnection.sendPacket(getEquipmentsPacket());
playerConnection.sendPacket(getPropertiesPacket());
return true;
}

View File

@ -1060,15 +1060,6 @@ public class Player extends LivingEntity implements CommandSender {
return super.isImmune(type);
}
@Override
protected void onAttributeChanged(@NotNull final AttributeInstance attributeInstance) {
if (attributeInstance.getAttribute().isShared() &&
playerConnection != null &&
playerConnection.getConnectionState() == ConnectionState.PLAY) {
playerConnection.sendPacket(getPropertiesPacket());
}
}
@Override
public void setHealth(float health) {
super.setHealth(health);
@ -1403,8 +1394,8 @@ public class Player extends LivingEntity implements CommandSender {
// Update for viewers
sendPacketToViewersAndSelf(getVelocityPacket());
sendPacketToViewersAndSelf(getMetadataPacket());
playerConnection.sendPacket(getPropertiesPacket());
syncEquipments();
sendPacketToViewersAndSelf(getPropertiesPacket());
sendPacketToViewersAndSelf(getEquipmentsPacket());
{
// Send new chunks
@ -2405,9 +2396,7 @@ public class Player extends LivingEntity implements CommandSender {
connection.sendPacket(getEntityType().getSpawnType().getSpawnPacket(this));
connection.sendPacket(getVelocityPacket());
connection.sendPacket(getMetadataPacket());
// Equipments synchronization
syncEquipments(connection);
connection.sendPacket(getEquipmentsPacket());
if (hasPassenger()) {
connection.sendPacket(getPassengersPacket());

View File

@ -56,7 +56,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
if (!super.addViewer0(player)) {
return false;
}
syncEquipments(player.getPlayerConnection());
player.getPlayerConnection().sendPacket(getEquipmentsPacket());
return true;
}

View File

@ -1,11 +1,9 @@
package net.minestom.server.inventory;
import net.minestom.server.Viewable;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -167,25 +165,6 @@ public interface EquipmentHandler {
throw new IllegalStateException("Something weird happened");
}
/**
* Sends all the equipments to a {@link PlayerConnection}.
*
* @param connection the connection to send the equipments to
*/
default void syncEquipments(@NotNull PlayerConnection connection) {
connection.sendPacket(getEquipmentsPacket());
}
/**
* Sends all the equipments to all viewers.
*/
default void syncEquipments() {
Check.stateCondition(!(this instanceof Viewable), "Only accessible for Entity");
Viewable viewable = (Viewable) this;
viewable.sendPacketToViewersAndSelf(getEquipmentsPacket());
}
/**
* Sends a specific equipment to viewers.
*

View File

@ -135,8 +135,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
// Send the cleared inventory to the inventory's owner
update();
// Update equipments for viewers
this.player.syncEquipments();
// Update equipments
this.player.sendPacketToViewersAndSelf(player.getEquipmentsPacket());
}
@Override