1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: pkt77 <parkerkt77@gmail.com>
Date: Fri, 10 Nov 2017 23:45:59 -0500
Subject: [PATCH] Add PlayerArmorChangeEvent
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerArmorChangeEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerArmorChangeEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..b1985f57d0e52df49304964797dfbbe9a7578ddd
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerArmorChangeEvent.java
@@ -0,0 +1,189 @@
+package com.destroystokyo.paper.event.player;
+
+import org.bukkit.Bukkit;
+import org.bukkit.Material;
+import org.bukkit.entity.Player;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.bukkit.inventory.ItemStack;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import org.bukkit.inventory.ItemType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.Unmodifiable;
+
+import static org.bukkit.inventory.ItemType.*;
+
+/**
+ * Called when the player themselves change their armor items
+ * <p>
+ * Not currently called for environmental factors though it <strong>MAY BE IN THE FUTURE</strong>
+ */
+public class PlayerArmorChangeEvent extends PlayerEvent {
+ private static final HandlerList HANDLERS = new HandlerList();
+
+ @NotNull private final SlotType slotType;
+ @Nullable private final ItemStack oldItem;
+ @Nullable private final ItemStack newItem;
+
+ public PlayerArmorChangeEvent(@NotNull Player player, @NotNull SlotType slotType, @Nullable ItemStack oldItem, @Nullable ItemStack newItem) {
+ super(player);
+ this.slotType = slotType;
+ this.oldItem = oldItem;
+ this.newItem = newItem;
+ }
+
+ /**
+ * Gets the type of slot being altered.
+ *
+ * @return type of slot being altered
+ */
+ @NotNull
+ public SlotType getSlotType() {
+ return this.slotType;
+ }
+
+ /**
+ * Gets the existing item that's being replaced
+ *
+ * @return old item
+ */
+ @Nullable
+ public ItemStack getOldItem() {
+ return this.oldItem;
+ }
+
+ /**
+ * Gets the new item that's replacing the old
+ *
+ * @return new item
+ */
+ @Nullable
+ public ItemStack getNewItem() {
+ return this.newItem;
+ }
+
+ @Override
+ public String toString() {
+ return "ArmorChangeEvent{" + "player=" + player + ", slotType=" + slotType + ", oldItem=" + oldItem + ", newItem=" + newItem + '}';
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLERS;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return HANDLERS;
+ }
+
+ public enum SlotType {
+ HEAD(NETHERITE_HELMET, DIAMOND_HELMET, GOLDEN_HELMET, IRON_HELMET, CHAINMAIL_HELMET, LEATHER_HELMET, CARVED_PUMPKIN, PLAYER_HEAD, SKELETON_SKULL, ZOMBIE_HEAD, CREEPER_HEAD, WITHER_SKELETON_SKULL, TURTLE_HELMET),
+ CHEST(NETHERITE_CHESTPLATE, DIAMOND_CHESTPLATE, GOLDEN_CHESTPLATE, IRON_CHESTPLATE, CHAINMAIL_CHESTPLATE, LEATHER_CHESTPLATE, ELYTRA),
+ LEGS(NETHERITE_LEGGINGS, DIAMOND_LEGGINGS, GOLDEN_LEGGINGS, IRON_LEGGINGS, CHAINMAIL_LEGGINGS, LEATHER_LEGGINGS),
+ FEET(NETHERITE_BOOTS, DIAMOND_BOOTS, GOLDEN_BOOTS, IRON_BOOTS, CHAINMAIL_BOOTS, LEATHER_BOOTS);
+
+ private final Set<ItemType> mutableTypes = new HashSet<>();
+ private Set<ItemType> immutableTypes;
+ @Deprecated
+ private Set<Material> legacyTypes;
+
+ SlotType(ItemType... types) {
+ this.mutableTypes.addAll(Arrays.asList(types));
+ }
+
+ /**
+ * Gets an immutable set of all allowed material types that can be placed in an
+ * armor slot.
+ *
+ * @return immutable set of material types
+ * @deprecated use {@link #getItemTypes()}
+ */
+ @Deprecated
+ public @NotNull @Unmodifiable Set<Material> getTypes() {
+ if (this.legacyTypes == null) {
+ this.legacyTypes = new HashSet<>();
+ for (final ItemType itemType : this.getItemTypes()) {
+ this.legacyTypes.add(Bukkit.getUnsafe().toMaterial(itemType));
+ }
+ }
+ return Collections.unmodifiableSet(this.legacyTypes);
+ }
+
+ /**
+ * Gets an immutable set of all allowed item types that can be placed in an
+ * armor slot.
+ *
+ * @return immutable set of item types
+ */
+ @NotNull
+ public @Unmodifiable Set<ItemType> getItemTypes() {
+ if (this.immutableTypes == null) {
+ this.immutableTypes = Collections.unmodifiableSet(this.mutableTypes);
+ }
+
+ return this.immutableTypes;
+ }
+
+ /**
+ * Gets the type of slot via the specified material
+ *
+ * @param material material to get slot by
+ * @return slot type the material will go in, or null if it won't
+ * @deprecated use {@link #getByItemType(ItemType)}
+ */
+ @Nullable
+ @Deprecated
+ public static SlotType getByMaterial(@NotNull Material material) {
+ for (SlotType slotType : values()) {
+ if (slotType.getTypes().contains(material)) {
+ return slotType;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Gets the type of slot via the specified item type
+ *
+ * @param itemType item type to get slot by
+ * @return slot type the item type will go in, or null if it won't
+ */
+ @Nullable
+ public static SlotType getByItemType(@NotNull ItemType itemType) {
+ for (SlotType slotType : values()) {
+ if (slotType.getItemTypes().contains(itemType)) {
+ return slotType;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Gets whether this material can be equipped to a slot
+ *
+ * @param material material to check
+ * @return whether this material can be equipped
+ */
+ @Deprecated
+ public static boolean isEquipable(@NotNull Material material) {
+ return getByMaterial(material) != null;
+ }
+
+ /**
+ * Gets whether this item type can be equipped to a slot
+ *
+ * @param itemType item type to check
+ * @return whether this item type can be equipped
+ */
+ public static boolean isEquipable(@NotNull ItemType itemType) {
+ return getByItemType(itemType) != null;
+ }
+ }
+}
|