mirror of
https://github.com/PaperMC/Paper.git
synced 2024-12-30 16:19:03 +01:00
7afebb7fb6
Also added 2 parameterized tests to make sure this doesn't happen again.
153 lines
6.6 KiB
Diff
153 lines
6.6 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: pkt77 <parkerkt77@gmail.com>
|
|
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 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
|
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
|
|
@@ -0,0 +0,0 @@
|
|
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;
|
|
@@ -0,0 +0,0 @@ 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..0000000000000000000000000000000000000000
|
|
--- /dev/null
|
|
+++ b/src/test/java/io/papermc/paper/inventory/item/ExtraArmorSlotTypeMaterialTest.java
|
|
@@ -0,0 +0,0 @@
|
|
+package io.papermc.paper.inventory.item;
|
|
+
|
|
+import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+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.Test;
|
|
+import org.junit.runner.RunWith;
|
|
+import org.junit.runners.Parameterized;
|
|
+
|
|
+import static org.junit.Assert.assertEquals;
|
|
+import static org.junit.Assert.assertNotNull;
|
|
+
|
|
+@RunWith(Parameterized.class)
|
|
+public class ExtraArmorSlotTypeMaterialTest extends AbstractTestingBase {
|
|
+
|
|
+ @Parameterized.Parameter(0)
|
|
+ public PlayerArmorChangeEvent.SlotType slotType;
|
|
+
|
|
+ @Parameterized.Parameter(1)
|
|
+ public Material item;
|
|
+
|
|
+ @Parameterized.Parameters(name = "{0}: {1}")
|
|
+ public static Iterable<Object[]> parameters() {
|
|
+ final List<Object[]> 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;
|
|
+ }
|
|
+
|
|
+ @Test
|
|
+ public void test() {
|
|
+ final Item nmsItem = CraftMagicNumbers.getItem(this.item);
|
|
+ final Equipable equipable = Equipable.get(new ItemStack(nmsItem));
|
|
+ assertNotNull(this.item + " isn't equipable", equipable);
|
|
+ final EquipmentSlot slot = switch (this.slotType) {
|
|
+ case HEAD -> EquipmentSlot.HEAD;
|
|
+ case CHEST -> EquipmentSlot.CHEST;
|
|
+ case LEGS -> EquipmentSlot.LEGS;
|
|
+ case FEET -> EquipmentSlot.FEET;
|
|
+ };
|
|
+ assertEquals(this.item + " isn't set to the right slot", equipable.getEquipmentSlot(), 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..0000000000000000000000000000000000000000
|
|
--- /dev/null
|
|
+++ b/src/test/java/io/papermc/paper/inventory/item/MissingArmorSlotTypeMaterialTest.java
|
|
@@ -0,0 +0,0 @@
|
|
+package io.papermc.paper.inventory.item;
|
|
+
|
|
+import com.destroystokyo.paper.event.player.PlayerArmorChangeEvent;
|
|
+import java.util.ArrayList;
|
|
+import java.util.List;
|
|
+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.Test;
|
|
+import org.junit.runner.RunWith;
|
|
+import org.junit.runners.Parameterized;
|
|
+
|
|
+import static org.junit.Assert.assertTrue;
|
|
+
|
|
+/**
|
|
+ * Test for {@link com.destroystokyo.paper.event.player.PlayerArmorChangeEvent.SlotType}
|
|
+ */
|
|
+@RunWith(Parameterized.class)
|
|
+public class MissingArmorSlotTypeMaterialTest extends AbstractTestingBase {
|
|
+
|
|
+ @Parameterized.Parameter(0)
|
|
+ public Equipable equipable;
|
|
+
|
|
+ @Parameterized.Parameter(1)
|
|
+ public Item item;
|
|
+
|
|
+ @Parameterized.Parameters(name = "{1}")
|
|
+ public static Iterable<Object[]> parameters() {
|
|
+ final List<Object[]> 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;
|
|
+ }
|
|
+
|
|
+ @Test
|
|
+ public void test() {
|
|
+ final EquipmentSlot equipmentSlot = this.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 " + slotType + " doesn't include " + this.item, slotType.getTypes().contains(CraftMagicNumbers.getMaterial(this.item)));
|
|
+ }
|
|
+ }
|
|
+}
|