Update equipments when clearing a player inventory

This commit is contained in:
themode 2021-01-03 21:59:58 +01:00
parent 765c5be642
commit 5ba68be4a1
3 changed files with 17 additions and 34 deletions

View File

@ -8,7 +8,6 @@ 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;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
@ -61,9 +60,8 @@ public interface EquipmentHandler {
return getItemInMainHand();
case OFF:
return getItemInOffHand();
default:
return null;
}
throw new IllegalStateException("Something weird happened");
}
/**
@ -165,9 +163,8 @@ public interface EquipmentHandler {
return getLeggings();
case BOOTS:
return getBoots();
default:
return null;
}
throw new IllegalStateException("Something weird happened");
}
/**
@ -176,26 +173,17 @@ public interface EquipmentHandler {
* @param connection the connection to send the equipments to
*/
default void syncEquipments(@NotNull PlayerConnection connection) {
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentsPacket();
if (entityEquipmentPacket == null)
return;
connection.sendPacket(entityEquipmentPacket);
connection.sendPacket(getEquipmentsPacket());
}
/**
* Sends all the equipments to all viewers.
*/
default void syncEquipments() {
if (!(this instanceof Viewable))
throw new IllegalStateException("Only accessible for Entity");
Check.stateCondition(!(this instanceof Viewable), "Only accessible for Entity");
Viewable viewable = (Viewable) this;
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentsPacket();
if (entityEquipmentPacket == null)
return;
viewable.sendPacketToViewersAndSelf(entityEquipmentPacket);
viewable.sendPacketToViewersAndSelf(getEquipmentsPacket());
}
/**
@ -204,11 +192,9 @@ public interface EquipmentHandler {
* @param slot the slot of the equipment
*/
default void syncEquipment(@NotNull EntityEquipmentPacket.Slot slot) {
if (!(this instanceof Entity))
throw new IllegalStateException("Only accessible for Entity");
Check.stateCondition(!(this instanceof Entity), "Only accessible for Entity");
Entity entity = (Entity) this;
Viewable viewable = (Viewable) this;
final ItemStack itemStack = getEquipment(slot);
@ -217,20 +203,20 @@ public interface EquipmentHandler {
entityEquipmentPacket.slots = new EntityEquipmentPacket.Slot[]{slot};
entityEquipmentPacket.itemStacks = new ItemStack[]{itemStack};
viewable.sendPacketToViewers(entityEquipmentPacket);
entity.sendPacketToViewers(entityEquipmentPacket);
}
/**
* Gets the packet with all the equipments.
*
* @return the packet with the equipments, null if all equipments are air
* @return the packet with the equipments
* @throws IllegalStateException if 'this' is not an {@link Entity}
*/
@Nullable
@NotNull
default EntityEquipmentPacket getEquipmentsPacket() {
Check.stateCondition(!(this instanceof Entity), "Only accessible for Entity");
Entity entity = (Entity) this;
final Entity entity = (Entity) this;
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
equipmentPacket.entityId = entity.getEntityId();
@ -240,14 +226,8 @@ public interface EquipmentHandler {
for (EntityEquipmentPacket.Slot slot : EntityEquipmentPacket.Slot.values()) {
final ItemStack itemStack = getEquipment(slot);
if (!itemStack.isAir()) {
slots.add(slot);
itemStacks.add(itemStack);
}
}
if (slots.isEmpty()) {
return null;
slots.add(slot);
itemStacks.add(itemStack);
}
equipmentPacket.slots = slots.toArray(new EntityEquipmentPacket.Slot[0]);

View File

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

View File

@ -53,6 +53,7 @@ public class EntityEquipmentPacket implements ServerPacket {
CHESTPLATE,
HELMET;
@NotNull
public static Slot fromArmorSlot(ArmorEquipEvent.ArmorSlot armorSlot) {
switch (armorSlot) {
case HELMET:
@ -63,9 +64,8 @@ public class EntityEquipmentPacket implements ServerPacket {
return LEGGINGS;
case BOOTS:
return BOOTS;
default:
return null;
}
throw new IllegalStateException("Something weird happened");
}
}