From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: pkt77 Date: Fri, 10 Nov 2017 23:46:34 -0500 Subject: [PATCH] Add PlayerArmorChangeEvent diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java index a8d39b7257332ec8b3b711aab2c4b76a867e73bc..231ba23fabeb88b659a7ea9702eebe4aad72a209 100644 --- a/src/main/java/net/minecraft/world/entity/LivingEntity.java +++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java @@ -1,5 +1,6 @@ package net.minecraft.world.entity; +import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent; // Paper import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -3053,6 +3054,13 @@ public abstract class LivingEntity extends Entity implements Attackable { ItemStack itemstack1 = this.getItemBySlot(enumitemslot); if (this.equipmentHasChanged(itemstack, itemstack1)) { + // Paper start - PlayerArmorChangeEvent + if (this instanceof ServerPlayer && enumitemslot.getType() == EquipmentSlot.Type.ARMOR) { + final org.bukkit.inventory.ItemStack oldItem = CraftItemStack.asBukkitCopy(itemstack); + final org.bukkit.inventory.ItemStack newItem = CraftItemStack.asBukkitCopy(itemstack1); + new PlayerArmorChangeEvent((Player) this.getBukkitEntity(), PlayerArmorChangeEvent.SlotType.valueOf(enumitemslot.name()), oldItem, newItem).callEvent(); + } + // Paper end if (map == null) { map = Maps.newEnumMap(EquipmentSlot.class); } diff --git a/src/test/java/io/papermc/paper/inventory/item/ExtraArmorSlotTypeMaterialTest.java b/src/test/java/io/papermc/paper/inventory/item/ExtraArmorSlotTypeMaterialTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0d99bef54594f9c5eda5c19063be765783e89fdd --- /dev/null +++ b/src/test/java/io/papermc/paper/inventory/item/ExtraArmorSlotTypeMaterialTest.java @@ -0,0 +1,47 @@ +package io.papermc.paper.inventory.item; + +import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.item.Equipable; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.bukkit.Material; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.support.AbstractTestingBase; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ExtraArmorSlotTypeMaterialTest extends AbstractTestingBase { + + @MethodSource("parameters") + public static Stream parameters() { + final List parameters = new ArrayList<>(); + for (final PlayerArmorChangeEvent.SlotType slotType : PlayerArmorChangeEvent.SlotType.values()) { + for (final Material item : slotType.getTypes()) { + parameters.add(new Object[]{ slotType, item }); + } + } + return parameters.stream(); + } + + @ParameterizedTest + @MethodSource("parameters") + public void test(PlayerArmorChangeEvent.SlotType slotType, Material item) { + final Item nmsItem = CraftMagicNumbers.getItem(item); + final Equipable equipable = Equipable.get(new ItemStack(nmsItem)); + assertNotNull(equipable, item + " isn't equipable"); + final EquipmentSlot slot = switch (slotType) { + case HEAD -> EquipmentSlot.HEAD; + case CHEST -> EquipmentSlot.CHEST; + case LEGS -> EquipmentSlot.LEGS; + case FEET -> EquipmentSlot.FEET; + }; + assertEquals(equipable.getEquipmentSlot(), slot, item + " isn't set to the right slot"); + } +} diff --git a/src/test/java/io/papermc/paper/inventory/item/MissingArmorSlotTypeMaterialTest.java b/src/test/java/io/papermc/paper/inventory/item/MissingArmorSlotTypeMaterialTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f2a9515228e4e3af27fce4f30fa4f4a5f0629a1e --- /dev/null +++ b/src/test/java/io/papermc/paper/inventory/item/MissingArmorSlotTypeMaterialTest.java @@ -0,0 +1,51 @@ +package io.papermc.paper.inventory.item; + +import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.item.Equipable; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.bukkit.craftbukkit.util.CraftMagicNumbers; +import org.bukkit.support.AbstractTestingBase; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test for {@link com.destroystokyo.paper.event.player.PlayerArmorChangeEvent.SlotType} + */ +public class MissingArmorSlotTypeMaterialTest extends AbstractTestingBase { + + @MethodSource("parameters") + public static Stream parameters() { + final List parameters = new ArrayList<>(); + for (final Item item : BuiltInRegistries.ITEM) { + final Equipable equipable = Equipable.get(new ItemStack(item)); + if (equipable != null) { + parameters.add(new Object[]{equipable, item}); + } + } + return parameters.stream(); + } + + @ParameterizedTest + @MethodSource("parameters") + public void test(Equipable equipable, Item item) { + final EquipmentSlot equipmentSlot = equipable.getEquipmentSlot(); + PlayerArmorChangeEvent.SlotType slotType = switch (equipmentSlot) { + case HEAD -> PlayerArmorChangeEvent.SlotType.HEAD; + case CHEST -> PlayerArmorChangeEvent.SlotType.CHEST; + case LEGS -> PlayerArmorChangeEvent.SlotType.LEGS; + case FEET -> PlayerArmorChangeEvent.SlotType.FEET; + default -> null; + }; + if (slotType != null) { + assertTrue(slotType.getTypes().contains(CraftMagicNumbers.getMaterial(item)), "SlotType " + slotType + " doesn't include " + item); + } + } +}