Optimize EntityEquipmentPacket

This commit is contained in:
Felix Cravic 2020-08-10 13:55:06 +02:00
parent c2165abe1f
commit dad8503ee5
7 changed files with 121 additions and 80 deletions

View File

@ -292,8 +292,6 @@ public class PlayerInit {
player.setHelmet(new ItemStack(Material.DIAMOND_HELMET, (byte) 1));
player.getInventory().setItemStack(41, ItemStack.getAirItem());
inventory.addItemStack(item.clone());
//player.openInventory(inventory);

View File

@ -11,8 +11,10 @@ import net.minestom.server.instance.Chunk;
import net.minestom.server.inventory.EquipmentHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.play.*;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.network.packet.server.play.CollectItemPacket;
import net.minestom.server.network.packet.server.play.EntityAnimationPacket;
import net.minestom.server.network.packet.server.play.EntityPropertiesPacket;
import net.minestom.server.network.packet.server.play.SoundEffectPacket;
import net.minestom.server.scoreboard.Team;
import net.minestom.server.sound.Sound;
import net.minestom.server.sound.SoundCategory;
@ -290,7 +292,7 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
* Is this entity immune to the given type of damage?
*
* @param type the type of damage
* @return true iff this entity is immune to the given type of damage
* @return true if this entity is immune to the given type of damage
*/
public boolean isImmune(DamageType type) {
return false;
@ -366,40 +368,6 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
return this.attributeValues[attribute.ordinal()];
}
// Equipments
protected void syncEquipments(PlayerConnection connection) {
for (EntityEquipmentPacket.Slot slot : EntityEquipmentPacket.Slot.values()) {
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentPacket(slot);
if (entityEquipmentPacket == null)
return;
connection.sendPacket(entityEquipmentPacket);
}
}
protected void syncEquipments() {
for (EntityEquipmentPacket.Slot slot : EntityEquipmentPacket.Slot.values()) {
syncEquipment(slot);
}
}
public void syncEquipment(EntityEquipmentPacket.Slot slot) {
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentPacket(slot);
if (entityEquipmentPacket == null)
return;
sendPacketToViewers(entityEquipmentPacket);
}
protected EntityEquipmentPacket getEquipmentPacket(EntityEquipmentPacket.Slot slot) {
final ItemStack itemStack = getEquipment(slot);
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
equipmentPacket.entityId = getEntityId();
equipmentPacket.slot = slot;
equipmentPacket.itemStack = itemStack;
return equipmentPacket;
}
/**
* Get if the entity is dead or not
*

View File

@ -1370,7 +1370,7 @@ public class Player extends LivingEntity implements CommandSender {
public void setTeam(Team team) {
super.setTeam(team);
if(team != null)
if (team != null)
getPlayerConnection().sendPacket(team.getTeamsCreationPacket());
}

View File

@ -13,7 +13,7 @@ import java.util.List;
import java.util.Set;
/**
* Target the closest entity
* Target the closest targetable entity (based on the class array)
*/
public class ClosestEntityTarget extends TargetSelector {
@ -33,7 +33,6 @@ public class ClosestEntityTarget extends TargetSelector {
final Chunk currentChunk = instance.getChunkAt(entityCreature.getPosition());
final List<Chunk> chunks = getNeighbours(instance, currentChunk.getChunkX(), currentChunk.getChunkZ());
Entity entity = null;
float distance = Float.MAX_VALUE;

View File

@ -8,7 +8,6 @@ import net.minestom.server.inventory.EquipmentHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.item.ItemStackUtils;
@ -317,28 +316,6 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
}
// Equipments
public void syncEquipments(PlayerConnection connection) {
for (EntityEquipmentPacket.Slot slot : EntityEquipmentPacket.Slot.values()) {
EntityEquipmentPacket entityEquipmentPacket = getEquipmentPacket(slot);
if (entityEquipmentPacket == null)
return;
connection.sendPacket(entityEquipmentPacket);
}
}
public void syncEquipments() {
for (EntityEquipmentPacket.Slot slot : EntityEquipmentPacket.Slot.values()) {
syncEquipment(slot);
}
}
protected void syncEquipment(EntityEquipmentPacket.Slot slot) {
EntityEquipmentPacket entityEquipmentPacket = getEquipmentPacket(slot);
if (entityEquipmentPacket == null)
return;
sendPacketToViewers(entityEquipmentPacket);
}
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {
itemStack = ItemStackUtils.notNull(itemStack);
@ -347,14 +324,4 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
callEvent(ArmorEquipEvent.class, armorEquipEvent);
return armorEquipEvent.getArmorItem();
}
protected EntityEquipmentPacket getEquipmentPacket(EntityEquipmentPacket.Slot slot) {
ItemStack itemStack = getEquipment(slot);
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
equipmentPacket.entityId = getEntityId();
equipmentPacket.slot = slot;
equipmentPacket.itemStack = itemStack;
return equipmentPacket;
}
}

View File

@ -1,8 +1,14 @@
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 java.util.ArrayList;
import java.util.List;
/**
* Represent an entity which can have item in hand and armor slots
@ -155,4 +161,88 @@ public interface EquipmentHandler {
}
}
/**
* Send all the equipments to a {@link PlayerConnection}
*
* @param connection the connection to send the equipments to
*/
default void syncEquipments(PlayerConnection connection) {
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentsPacket();
if (entityEquipmentPacket == null)
return;
connection.sendPacket(entityEquipmentPacket);
}
/**
* Send all the equipments to all viewers
*/
default void syncEquipments() {
if (!(this instanceof Viewable))
throw new IllegalStateException("Only accessible for Entity");
Viewable viewable = (Viewable) this;
final EntityEquipmentPacket entityEquipmentPacket = getEquipmentsPacket();
if (entityEquipmentPacket == null)
return;
viewable.sendPacketToViewersAndSelf(entityEquipmentPacket);
}
/**
* Send a specific equipment to viewers
*
* @param slot the slot of the equipment
*/
default void syncEquipment(EntityEquipmentPacket.Slot slot) {
if (!(this instanceof Entity))
throw new IllegalStateException("Only accessible for Entity");
Entity entity = (Entity) this;
Viewable viewable = (Viewable) this;
final ItemStack itemStack = getEquipment(slot);
EntityEquipmentPacket entityEquipmentPacket = new EntityEquipmentPacket();
entityEquipmentPacket.entityId = entity.getEntityId();
entityEquipmentPacket.slots = new EntityEquipmentPacket.Slot[]{slot};
entityEquipmentPacket.itemStacks = new ItemStack[]{itemStack};
viewable.sendPacketToViewersAndSelf(entityEquipmentPacket);
}
/**
* Get the packet with all the equipments
*
* @return the packet with the equipments
*/
default EntityEquipmentPacket getEquipmentsPacket() {
if (!(this instanceof Entity))
throw new IllegalStateException("Only accessible for Entity");
Entity entity = (Entity) this;
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
equipmentPacket.entityId = entity.getEntityId();
List<EntityEquipmentPacket.Slot> slots = new ArrayList<>();
List<ItemStack> itemStacks = new ArrayList<>();
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;
}
equipmentPacket.slots = slots.toArray(new EntityEquipmentPacket.Slot[0]);
equipmentPacket.itemStacks = itemStacks.toArray(new ItemStack[0]);
return equipmentPacket;
}
}

View File

@ -9,15 +9,34 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier;
public class EntityEquipmentPacket implements ServerPacket {
public int entityId;
public Slot slot;
public ItemStack itemStack;
public Slot[] slots;
public ItemStack[] itemStacks;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(entityId);
// TODO multiple equipments
writer.writeByte((byte) slot.ordinal());
writer.writeItemStack(itemStack);
if (slots == null || itemStacks == null) {
throw new IllegalArgumentException("You need to specify at least one slot and one item");
}
if (slots.length != itemStacks.length) {
throw new IllegalArgumentException("You need the same amount of slots and items");
}
for (int i = 0; i < slots.length; i++) {
final Slot slot = slots[i];
final ItemStack itemStack = itemStacks[i];
final boolean last = i == slots.length - 1;
byte slotEnum = (byte) slot.ordinal();
if (!last) {
slotEnum |= 0x80;
}
writer.writeByte(slotEnum);
writer.writeItemStack(itemStack);
}
}
@Override