Made EntityEquipEvent, replacing ArmorEquipEvent

This commit is contained in:
Arne Dalhuisen 2021-05-11 14:45:14 +02:00
parent 92d97c624f
commit 77ef5f7d42
5 changed files with 45 additions and 72 deletions

View File

@ -1,6 +1,6 @@
package net.minestom.server.entity;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.event.item.EntityEquipEvent;
import net.minestom.server.item.attribute.AttributeSlot;
import org.jetbrains.annotations.NotNull;
@ -20,21 +20,6 @@ public enum EquipmentSlot {
return !isHand();
}
@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) {

View File

@ -10,7 +10,7 @@ import net.minestom.server.entity.metadata.LivingEntityMeta;
import net.minestom.server.event.entity.EntityDamageEvent;
import net.minestom.server.event.entity.EntityDeathEvent;
import net.minestom.server.event.entity.EntityFireEvent;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.event.item.EntityEquipEvent;
import net.minestom.server.event.item.PickupItemEvent;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.block.Block;
@ -127,7 +127,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = itemStack;
this.mainHandItem = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
}
@ -139,7 +139,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = itemStack;
this.offHandItem = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
syncEquipment(EquipmentSlot.OFF_HAND);
}
@ -151,7 +151,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
this.helmet = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
syncEquipment(EquipmentSlot.HELMET);
}
@ -163,7 +163,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
this.chestplate = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
syncEquipment(EquipmentSlot.CHESTPLATE);
}
@ -175,7 +175,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
this.leggings = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
syncEquipment(EquipmentSlot.LEGGINGS);
}
@ -187,14 +187,14 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
this.boots = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
syncEquipment(EquipmentSlot.BOOTS);
}
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull ArmorEquipEvent.ArmorSlot armorSlot) {
ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot);
callEvent(ArmorEquipEvent.class, armorEquipEvent);
return armorEquipEvent.getArmorItem();
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(this, itemStack, slot);
callEvent(EntityEquipEvent.class, entityEquipEvent);
return entityEquipEvent.getArmorItem();
}
@Override

View File

@ -1,7 +1,7 @@
package net.minestom.server.entity.type.decoration;
import net.minestom.server.entity.*;
import net.minestom.server.event.item.ArmorEquipEvent;
import net.minestom.server.event.item.EntityEquipEvent;
import net.minestom.server.inventory.EquipmentHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.Position;
@ -69,7 +69,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = itemStack;
this.mainHandItem = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
syncEquipment(EquipmentSlot.MAIN_HAND);
}
@ -81,7 +81,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = itemStack;
this.offHandItem = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
syncEquipment(EquipmentSlot.OFF_HAND);
}
@ -93,7 +93,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
this.helmet = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
syncEquipment(EquipmentSlot.HELMET);
}
@ -105,7 +105,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
this.chestplate = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
syncEquipment(EquipmentSlot.CHESTPLATE);
}
@ -117,7 +117,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
this.leggings = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
syncEquipment(EquipmentSlot.LEGGINGS);
}
@ -129,7 +129,7 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
this.boots = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
syncEquipment(EquipmentSlot.BOOTS);
}
@ -235,9 +235,9 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
// Equipments
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull ArmorEquipEvent.ArmorSlot armorSlot) {
ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot);
callEvent(ArmorEquipEvent.class, armorEquipEvent);
return armorEquipEvent.getArmorItem();
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(this, itemStack, slot);
callEvent(EntityEquipEvent.class, entityEquipEvent);
return entityEquipEvent.getArmorItem();
}
}

View File

@ -1,20 +1,21 @@
package net.minestom.server.event.item;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.event.Event;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
public class ArmorEquipEvent extends Event {
public class EntityEquipEvent extends Event {
private final Entity entity;
private ItemStack armorItem;
private final ArmorSlot armorSlot;
private final EquipmentSlot slot;
public ArmorEquipEvent(@NotNull Entity entity, @NotNull ItemStack armorItem, @NotNull ArmorSlot armorSlot) {
public EntityEquipEvent(@NotNull Entity entity, @NotNull ItemStack armorItem, @NotNull EquipmentSlot slot) {
this.entity = entity;
this.armorItem = armorItem;
this.armorSlot = armorSlot;
this.slot = slot;
}
@NotNull
@ -32,14 +33,7 @@ public class ArmorEquipEvent extends Event {
}
@NotNull
public ArmorSlot getArmorSlot() {
return armorSlot;
}
public enum ArmorSlot {
HELMET,
CHESTPLATE,
LEGGINGS,
BOOTS
public EquipmentSlot getSlot() {
return slot;
}
}

View File

@ -2,7 +2,7 @@ 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.event.item.EntityEquipEvent;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.inventory.click.InventoryClickResult;
import net.minestom.server.inventory.condition.InventoryCondition;
@ -170,33 +170,27 @@ 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");
EquipmentSlot equipmentSlot;
EquipmentSlot equipmentSlot = null;
if (slot == player.getHeldSlot()) {
equipmentSlot = EquipmentSlot.MAIN_HAND;
} else if (slot == OFFHAND_SLOT) {
equipmentSlot = EquipmentSlot.OFF_HAND;
} else {
ArmorEquipEvent armorEquipEvent = null;
} else if (slot == HELMET_SLOT) {
equipmentSlot = EquipmentSlot.HELMET;
} else if (slot == CHESTPLATE_SLOT) {
equipmentSlot = EquipmentSlot.CHESTPLATE;
} else if (slot == LEGGINGS_SLOT) {
equipmentSlot = EquipmentSlot.LEGGINGS;
} else if (slot == BOOTS_SLOT) {
equipmentSlot = EquipmentSlot.BOOTS;
}
if (slot == HELMET_SLOT) {
armorEquipEvent = new ArmorEquipEvent(player, itemStack, ArmorEquipEvent.ArmorSlot.HELMET);
} else if (slot == CHESTPLATE_SLOT) {
armorEquipEvent = new ArmorEquipEvent(player, itemStack, ArmorEquipEvent.ArmorSlot.CHESTPLATE);
} else if (slot == LEGGINGS_SLOT) {
armorEquipEvent = new ArmorEquipEvent(player, itemStack, ArmorEquipEvent.ArmorSlot.LEGGINGS);
} else if (slot == BOOTS_SLOT) {
armorEquipEvent = new ArmorEquipEvent(player, itemStack, ArmorEquipEvent.ArmorSlot.BOOTS);
}
if (equipmentSlot != null) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(player, itemStack, equipmentSlot);
if (armorEquipEvent != null) {
ArmorEquipEvent.ArmorSlot armorSlot = armorEquipEvent.getArmorSlot();
equipmentSlot = EquipmentSlot.fromArmorSlot(armorSlot);
player.callEvent(ArmorEquipEvent.class, armorEquipEvent);
itemStack = armorEquipEvent.getArmorItem();
} else {
equipmentSlot = null;
}
player.callEvent(EntityEquipEvent.class, entityEquipEvent);
itemStack = entityEquipEvent.getArmorItem();
}
this.itemStacks[slot] = itemStack;