Made EntityEquipEvent cancellable

This commit is contained in:
Arne Dalhuisen 2021-05-11 17:05:16 +02:00
parent ed010ff110
commit d72fb72e74
5 changed files with 82 additions and 28 deletions

View File

@ -1,6 +1,5 @@
package net.minestom.server.entity;
import net.minestom.server.event.item.EntityEquipEvent;
import net.minestom.server.item.attribute.AttributeSlot;
import org.jetbrains.annotations.NotNull;

View File

@ -127,9 +127,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
if (itemStack != null) {
this.mainHandItem = itemStack;
syncEquipment(EquipmentSlot.MAIN_HAND);
}
}
@NotNull
@Override
@ -139,9 +142,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
if (itemStack != null) {
this.offHandItem = itemStack;
syncEquipment(EquipmentSlot.OFF_HAND);
}
}
@NotNull
@Override
@ -151,9 +157,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
if (itemStack != null) {
this.helmet = itemStack;
syncEquipment(EquipmentSlot.HELMET);
}
}
@NotNull
@Override
@ -163,9 +172,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
if (itemStack != null) {
this.chestplate = itemStack;
syncEquipment(EquipmentSlot.CHESTPLATE);
}
}
@NotNull
@Override
@ -175,9 +187,12 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
if (itemStack != null) {
this.leggings = itemStack;
syncEquipment(EquipmentSlot.LEGGINGS);
}
}
@NotNull
@Override
@ -187,14 +202,17 @@ public class LivingEntity extends Entity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
if (itemStack != null) {
this.boots = itemStack;
syncEquipment(EquipmentSlot.BOOTS);
}
}
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(this, itemStack, slot);
callEvent(EntityEquipEvent.class, entityEquipEvent);
return entityEquipEvent.getEquippedItem();
return entityEquipEvent.isCancelled() ? null : entityEquipEvent.getEquippedItem();
}
@Override

View File

@ -69,9 +69,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInMainHand(@NotNull ItemStack itemStack) {
this.mainHandItem = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.MAIN_HAND);
if (itemStack != null) {
this.mainHandItem = itemStack;
syncEquipment(EquipmentSlot.MAIN_HAND);
}
}
@NotNull
@Override
@ -81,9 +84,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setItemInOffHand(@NotNull ItemStack itemStack) {
this.offHandItem = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.OFF_HAND);
if (itemStack != null) {
this.offHandItem = itemStack;
syncEquipment(EquipmentSlot.OFF_HAND);
}
}
@NotNull
@Override
@ -93,9 +99,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setHelmet(@NotNull ItemStack itemStack) {
this.helmet = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.HELMET);
if (itemStack != null) {
this.helmet = itemStack;
syncEquipment(EquipmentSlot.HELMET);
}
}
@NotNull
@Override
@ -105,9 +114,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setChestplate(@NotNull ItemStack itemStack) {
this.chestplate = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.CHESTPLATE);
if (itemStack != null) {
this.chestplate = itemStack;
syncEquipment(EquipmentSlot.CHESTPLATE);
}
}
@NotNull
@Override
@ -117,9 +129,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setLeggings(@NotNull ItemStack itemStack) {
this.leggings = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.LEGGINGS);
if (itemStack != null) {
this.leggings = itemStack;
syncEquipment(EquipmentSlot.LEGGINGS);
}
}
@NotNull
@Override
@ -129,9 +144,12 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
@Override
public void setBoots(@NotNull ItemStack itemStack) {
this.boots = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
itemStack = getEquipmentItem(itemStack, EquipmentSlot.BOOTS);
if (itemStack != null) {
this.boots = itemStack;
syncEquipment(EquipmentSlot.BOOTS);
}
}
public boolean isSmall() {
return (getStateMeta() & 0x01) != 0;
@ -238,6 +256,6 @@ public class EntityArmorStand extends ObjectEntity implements EquipmentHandler {
private ItemStack getEquipmentItem(@NotNull ItemStack itemStack, @NotNull EquipmentSlot slot) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(this, itemStack, slot);
callEvent(EntityEquipEvent.class, entityEquipEvent);
return entityEquipEvent.getEquippedItem();
return entityEquipEvent.isCancelled() ? null : entityEquipEvent.getEquippedItem();
}
}

View File

@ -2,16 +2,19 @@ package net.minestom.server.event.item;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EquipmentSlot;
import net.minestom.server.event.CancellableEvent;
import net.minestom.server.event.Event;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.NotNull;
public class EntityEquipEvent extends Event {
public class EntityEquipEvent extends Event implements CancellableEvent {
private final Entity entity;
private ItemStack equippedItem;
private final EquipmentSlot slot;
private boolean cancelled;
public EntityEquipEvent(@NotNull Entity entity, @NotNull ItemStack equippedItem, @NotNull EquipmentSlot slot) {
this.entity = entity;
this.equippedItem = equippedItem;
@ -36,4 +39,14 @@ public class EntityEquipEvent extends Event {
public EquipmentSlot getSlot() {
return slot;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -186,11 +186,17 @@ public class PlayerInventory extends AbstractInventory implements EquipmentHandl
equipmentSlot = EquipmentSlot.BOOTS;
}
boolean cancel = false;
if (equipmentSlot != null) {
EntityEquipEvent entityEquipEvent = new EntityEquipEvent(player, itemStack, equipmentSlot);
player.callEvent(EntityEquipEvent.class, entityEquipEvent);
itemStack = entityEquipEvent.getEquippedItem();
cancel = entityEquipEvent.isCancelled();
}
if (cancel) {
return;
}
this.itemStacks[slot] = itemStack;