Optimize getEquipmentsPacket

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-10-13 06:36:43 +02:00
parent bd4aa90753
commit c0024fe4dd

View File

@ -8,9 +8,6 @@ import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/** /**
* Represents an {@link Entity} which can have {@link ItemStack} in hands and armor slots. * Represents an {@link Entity} which can have {@link ItemStack} in hands and armor slots.
*/ */
@ -215,24 +212,17 @@ public interface EquipmentHandler {
*/ */
default @NotNull EntityEquipmentPacket getEquipmentsPacket() { default @NotNull EntityEquipmentPacket getEquipmentsPacket() {
Check.stateCondition(!(this instanceof Entity), "Only accessible for Entity"); Check.stateCondition(!(this instanceof Entity), "Only accessible for Entity");
final Entity entity = (Entity) this;
final EquipmentSlot[] slots = EquipmentSlot.values(); final EquipmentSlot[] slots = EquipmentSlot.values();
ItemStack[] equipments = new ItemStack[slots.length];
List<ItemStack> itemStacks = new ArrayList<>(slots.length); for (int i = 0; i < equipments.length; i++) {
final EquipmentSlot slot = slots[i];
// Fill items equipments[i] = getEquipment(slot);
for (EquipmentSlot slot : slots) {
final ItemStack equipment = getEquipment(slot);
itemStacks.add(equipment);
} }
// Create equipment packet // Create equipment packet
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket(); EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
equipmentPacket.entityId = entity.getEntityId(); equipmentPacket.entityId = ((Entity) this).getEntityId();
equipmentPacket.slots = slots; equipmentPacket.slots = slots;
equipmentPacket.itemStacks = itemStacks.toArray(new ItemStack[0]); equipmentPacket.itemStacks = equipments;
return equipmentPacket; return equipmentPacket;
} }