Paper/patches/server/0162-Add-PlayerArmorChangeEvent.patch

154 Zeilen
6.6 KiB
Diff

2021-06-11 14:02:28 +02:00
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
2023-09-21 23:04:51 +02:00
index a8d39b7257332ec8b3b711aab2c4b76a867e73bc..231ba23fabeb88b659a7ea9702eebe4aad72a209 100644
2021-06-11 14:02:28 +02:00
--- 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;
2023-09-21 23:04:51 +02:00
@@ -3053,6 +3054,13 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-06-11 14:02:28 +02:00
ItemStack itemstack1 = this.getItemBySlot(enumitemslot);
2022-12-07 19:52:24 +01:00
if (this.equipmentHasChanged(itemstack, itemstack1)) {
2021-06-11 14:02:28 +02:00
+ // 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..36cde1a68b0fb388cca40399fcb4297b4d799262
--- /dev/null
+++ b/src/test/java/io/papermc/paper/inventory/item/ExtraArmorSlotTypeMaterialTest.java
@@ -0,0 +1,53 @@
+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..f05cfe2e4a9f876a7e6425c612a56f86d5ac3915
--- /dev/null
+++ b/src/test/java/io/papermc/paper/inventory/item/MissingArmorSlotTypeMaterialTest.java
@@ -0,0 +1,57 @@
+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)));
+ }
+ }
+}