Paper/patches/server/0162-Add-PlayerArmorChangeEvent.patch
Nassim Jahnke 72e87abc2d
Strip raytracing for EntityLiving#hasLineOfSight
Co-authored-by: Paul Sauve <paul@technove.co>
2023-09-10 12:28:03 +10:00

154 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 0470329b43afd4e8c5e033666d1c5f8f0a03fe6a..021f3f81746a6e986c8899aa4b28ef87a67bed44 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;
@@ -3016,6 +3017,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..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)));
+ }
+ }
+}