Moved EquipmentSlot out of equipment packet

This commit is contained in:
Arne Dalhuisen 2021-05-11 14:10:45 +02:00
parent 19e22d03ce
commit 29b0f5448d
8 changed files with 83 additions and 61 deletions

View File

@ -0,0 +1,49 @@
package net.minestom.server.entity;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.item.attribute.AttributeSlot;
import org.jetbrains.annotations.NotNull;
public enum EquipmentSlot {
MAIN_HAND,
OFF_HAND,
BOOTS,
LEGGINGS,
CHESTPLATE,
HELMET;
@NotNull
public static EquipmentSlot fromArmorSlot(ArmorEquipEvent.ArmorSlot armorSlot) {
switch (armorSlot) {
case HELMET:
return HELMET;
case CHESTPLATE:
return CHESTPLATE;
case LEGGINGS:
return LEGGINGS;
case BOOTS:
return BOOTS;
}
throw new IllegalStateException("Something weird happened");
}
@NotNull
public static EquipmentSlot fromAttributeSlot(AttributeSlot attributeSlot) {
switch (attributeSlot) {
case MAINHAND:
return MAIN_HAND;
case OFFHAND:
return OFF_HAND;
case FEET:
return BOOTS;
case LEGS:
return LEGGINGS;
case CHEST:
return CHESTPLATE;
case HEAD:
return HELMET;
}
throw new IllegalStateException("Something weird happened");
}
}

View File

@ -128,7 +128,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = itemStack;
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
}
@NotNull
@ -140,7 +140,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = itemStack;
syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND);
syncEquipment(EquipmentSlot.OFF_HAND);
}
@NotNull
@ -152,7 +152,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
syncEquipment(EntityEquipmentPacket.Slot.HELMET);
syncEquipment(EquipmentSlot.HELMET);
}
@NotNull
@ -164,7 +164,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
syncEquipment(EntityEquipmentPacket.Slot.CHESTPLATE);
syncEquipment(EquipmentSlot.CHESTPLATE);
}
@NotNull
@ -176,7 +176,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
syncEquipment(EntityEquipmentPacket.Slot.LEGGINGS);
syncEquipment(EquipmentSlot.LEGGINGS);
}
@NotNull
@ -188,7 +188,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
syncEquipment(EntityEquipmentPacket.Slot.BOOTS);
syncEquipment(EquipmentSlot.BOOTS);
}
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull ArmorEquipEvent.ArmorSlot armorSlot) {

View File

@ -2256,7 +2256,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/
public void refreshHeldSlot(byte slot) {
this.heldSlot = slot;
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
refreshEating(null);
}

View File

@ -1,13 +1,9 @@
package net.minestom.server.entity.type.decoration;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.Metadata;
import net.minestom.server.entity.ObjectEntity;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.*;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.inventory.EquipmentHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.binary.BitmaskUtil;
@ -74,7 +70,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = itemStack;
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
}
@NotNull
@ -86,7 +82,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = itemStack;
syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND);
syncEquipment(EquipmentSlot.OFF_HAND);
}
@NotNull
@ -98,7 +94,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
syncEquipment(EntityEquipmentPacket.Slot.HELMET);
syncEquipment(EquipmentSlot.HELMET);
}
@NotNull
@ -110,7 +106,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
syncEquipment(EntityEquipmentPacket.Slot.CHESTPLATE);
syncEquipment(EquipmentSlot.CHESTPLATE);
}
@NotNull
@ -122,7 +118,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
syncEquipment(EntityEquipmentPacket.Slot.LEGGINGS);
syncEquipment(EquipmentSlot.LEGGINGS);
}
@NotNull
@ -134,7 +130,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
syncEquipment(EntityEquipmentPacket.Slot.BOOTS);
syncEquipment(EquipmentSlot.BOOTS);
}
public boolean isSmall() {

View File

@ -1,6 +1,7 @@
package net.minestom.server.inventory;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.entity.Player;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
@ -139,7 +140,7 @@ public interface EquipmentHandler {
* @param slot the equipment to get the item from
* @return the equipment {@link ItemStack}
*/
default @NotNull ItemStack getEquipment(@NotNull EntityEquipmentPacket.Slot slot) {
default @NotNull ItemStack getEquipment(@NotNull EquipmentSlot slot) {
switch (slot) {
case MAIN_HAND:
return getItemInMainHand();
@ -157,7 +158,7 @@ public interface EquipmentHandler {
throw new IllegalStateException("Something weird happened");
}
default void setEquipment(@NotNull EntityEquipmentPacket.Slot slot, @NotNull ItemStack itemStack) {
default void setEquipment(@NotNull EquipmentSlot slot, @NotNull ItemStack itemStack) {
switch (slot) {
case MAIN_HAND:
setItemInMainHand(itemStack);
@ -187,7 +188,7 @@ public interface EquipmentHandler {
*
* @param slot the slot of the equipment
*/
default void syncEquipment(@NotNull EntityEquipmentPacket.Slot slot) {
default void syncEquipment(@NotNull EquipmentSlot slot) {
Check.stateCondition(!(this instanceof Entity), "Only accessible for Entity");
Entity entity = (Entity) this;
@ -196,7 +197,7 @@ public interface EquipmentHandler {
EntityEquipmentPacket entityEquipmentPacket = new EntityEquipmentPacket();
entityEquipmentPacket.entityId = entity.getEntityId();
entityEquipmentPacket.slots = new EntityEquipmentPacket.Slot[]{slot};
entityEquipmentPacket.slots = new EquipmentSlot[]{slot};
entityEquipmentPacket.itemStacks = new ItemStack[]{itemStack};
entity.sendPacketToViewers(entityEquipmentPacket);
@ -213,12 +214,12 @@ public interface EquipmentHandler {
final Entity entity = (Entity) this;
final EntityEquipmentPacket.Slot[] slots = EntityEquipmentPacket.Slot.values();
final EquipmentSlot[] slots = EquipmentSlot.values();
List<ItemStack> itemStacks = new ArrayList<>(slots.length);
// Fill items
for (EntityEquipmentPacket.Slot slot : slots) {
for (EquipmentSlot slot : slots) {
final ItemStack equipment = getEquipment(slot);
itemStacks.add(equipment);
}

View File

@ -1,12 +1,12 @@
package net.minestom.server.inventory;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.entity.Player;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.inventory.click.InventoryClickResult;
import net.minestom.server.inventory.condition.InventoryCondition;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.play.EntityEquipmentPacket;
import net.minestom.server.network.packet.server.play.SetSlotPacket;
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
import net.minestom.server.utils.MathUtils;
@ -170,12 +170,12 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
"The slot {0} does not exist for player", slot);
Check.notNull(itemStack, "The ItemStack cannot be null, you can set air instead");
EntityEquipmentPacket.Slot equipmentSlot;
EquipmentSlot equipmentSlot;
if (slot == player.getHeldSlot()) {
equipmentSlot = EntityEquipmentPacket.Slot.MAIN_HAND;
equipmentSlot = EquipmentSlot.MAIN_HAND;
} else if (slot == OFFHAND_SLOT) {
equipmentSlot = EntityEquipmentPacket.Slot.OFF_HAND;
equipmentSlot = EquipmentSlot.OFF_HAND;
} else {
ArmorEquipEvent armorEquipEvent = null;
@ -191,7 +191,7 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
if (armorEquipEvent != null) {
ArmorEquipEvent.ArmorSlot armorSlot = armorEquipEvent.getArmorSlot();
equipmentSlot = EntityEquipmentPacket.Slot.fromArmorSlot(armorSlot);
equipmentSlot = EquipmentSlot.fromArmorSlot(armorSlot);
player.callEvent(ArmorEquipEvent.class, armorEquipEvent);
itemStack = armorEquipEvent.getArmorItem();
} else {

View File

@ -1,6 +1,6 @@
package net.minestom.server.network.packet.server.play;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
@ -14,7 +14,7 @@ import java.util.List;
public class EntityEquipmentPacket implements ServerPacket {
public int entityId;
public Slot[] slots;
public EquipmentSlot[] slots;
public ItemStack[] itemStacks;
public EntityEquipmentPacket() {
@ -33,7 +33,7 @@ public class EntityEquipmentPacket implements ServerPacket {
}
for (int i = 0; i < slots.length; i++) {
final Slot slot = slots[i];
final EquipmentSlot slot = slots[i];
final ItemStack itemStack = itemStacks[i];
final boolean last = i == slots.length - 1;
@ -52,17 +52,17 @@ public class EntityEquipmentPacket implements ServerPacket {
entityId = reader.readVarInt();
boolean hasRemaining = true;
List<Slot> slots = new LinkedList<>();
List<EquipmentSlot> slots = new LinkedList<>();
List<ItemStack> stacks = new LinkedList<>();
while (hasRemaining) {
byte slotEnum = reader.readByte();
hasRemaining = (slotEnum & 0x80) == 0x80;
slots.add(Slot.values()[slotEnum & 0x7F]);
slots.add(EquipmentSlot.values()[slotEnum & 0x7F]);
stacks.add(reader.readItemStack());
}
this.slots = slots.toArray(new Slot[0]);
this.slots = slots.toArray(new EquipmentSlot[0]);
this.itemStacks = stacks.toArray(new ItemStack[0]);
}
@ -71,29 +71,4 @@ public class EntityEquipmentPacket implements ServerPacket {
return ServerPacketIdentifier.ENTITY_EQUIPMENT;
}
public enum Slot {
MAIN_HAND,
OFF_HAND,
BOOTS,
LEGGINGS,
CHESTPLATE,
HELMET;
@NotNull
public static Slot fromArmorSlot(ArmorEquipEvent.ArmorSlot armorSlot) {
switch (armorSlot) {
case HELMET:
return HELMET;
case CHESTPLATE:
return CHESTPLATE;
case LEGGINGS:
return LEGGINGS;
case BOOTS:
return BOOTS;
}
throw new IllegalStateException("Something weird happened");
}
}
}

View File

@ -2,6 +2,7 @@ package readwritepackets;
import com.google.common.reflect.ClassPath;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.item.ItemStack;
import net.minestom.server.network.packet.client.ClientPacket;
import net.minestom.server.network.packet.server.ServerPacket;
@ -67,7 +68,7 @@ public class ReadWritePackets {
// requires at least one slot and one item
EntityEquipmentPacket p = new EntityEquipmentPacket();
p.itemStacks = new ItemStack[]{ItemStack.AIR};
p.slots = new EntityEquipmentPacket.Slot[]{EntityEquipmentPacket.Slot.MAIN_HAND};
p.slots = new EquipmentSlot[]{EquipmentSlot.MAIN_HAND};
packet = (T) p;
} else {
packet = (T) constructor.newInstance();