aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/api/0127-AnvilDamageEvent.patch
blob: ed69e49e575a3fe35ca088375625e2fb4fee4c76 (plain)
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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: BillyGalbreath <Blake.Galbreath@GMail.com>
Date: Fri, 20 Jul 2018 23:36:55 -0500
Subject: [PATCH] AnvilDamageEvent


diff --git a/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..4f88c101d81e5c3a8a065260304d5816337666d7
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java
@@ -0,0 +1,154 @@
+package com.destroystokyo.paper.event.block;
+
+import org.bukkit.Material;
+import org.bukkit.block.data.BlockData;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.inventory.InventoryEvent;
+import org.bukkit.inventory.AnvilInventory;
+import org.bukkit.inventory.InventoryView;
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called when an anvil is damaged from being used
+ */
+public class AnvilDamagedEvent extends InventoryEvent implements Cancellable {
+
+    private static final HandlerList HANDLER_LIST = new HandlerList();
+
+    private DamageState damageState;
+    private boolean cancelled;
+
+    @ApiStatus.Internal
+    public AnvilDamagedEvent(@NotNull InventoryView inventory, @Nullable BlockData blockData) {
+        super(inventory);
+        this.damageState = DamageState.getState(blockData);
+    }
+
+    @NotNull
+    @Override
+    public AnvilInventory getInventory() {
+        return (AnvilInventory) super.getInventory();
+    }
+
+    /**
+     * Gets the new state of damage on the anvil
+     *
+     * @return Damage state
+     */
+    @NotNull
+    public DamageState getDamageState() {
+        return this.damageState;
+    }
+
+    /**
+     * Sets the new state of damage on the anvil
+     *
+     * @param damageState Damage state
+     */
+    public void setDamageState(@NotNull DamageState damageState) {
+        this.damageState = damageState;
+    }
+
+    /**
+     * Gets if anvil is breaking on this use
+     *
+     * @return {@code true} if breaking
+     */
+    public boolean isBreaking() {
+        return this.damageState == DamageState.BROKEN;
+    }
+
+    /**
+     * Sets if anvil is breaking on this use
+     *
+     * @param breaking {@code true} if breaking
+     */
+    public void setBreaking(boolean breaking) {
+        if (breaking) {
+            this.damageState = DamageState.BROKEN;
+        } else if (this.damageState == DamageState.BROKEN) {
+            this.damageState = DamageState.DAMAGED;
+        }
+    }
+
+    @Override
+    public boolean isCancelled() {
+        return this.cancelled;
+    }
+
+    @Override
+    public void setCancelled(boolean cancel) {
+        this.cancelled = cancel;
+    }
+
+    @NotNull
+    public HandlerList getHandlers() {
+        return HANDLER_LIST;
+    }
+
+    @NotNull
+    public static HandlerList getHandlerList() {
+        return HANDLER_LIST;
+    }
+
+    /**
+     * Represents the amount of damage on an anvil block
+     */
+    public enum DamageState {
+        FULL(Material.ANVIL),
+        CHIPPED(Material.CHIPPED_ANVIL),
+        DAMAGED(Material.DAMAGED_ANVIL),
+        BROKEN(Material.AIR);
+
+        private final Material material;
+
+        DamageState(@NotNull Material material) {
+            this.material = material;
+        }
+
+        /**
+         * Get block material of this state
+         *
+         * @return Material
+         */
+        @NotNull
+        public Material getMaterial() {
+            return this.material;
+        }
+
+        /**
+         * Get damaged state by block data
+         *
+         * @param blockData Block data
+         * @return DamageState
+         * @throws IllegalArgumentException If non anvil block data is given
+         */
+        @NotNull
+        public static DamageState getState(@Nullable BlockData blockData) {
+            return blockData == null ? BROKEN : getState(blockData.getMaterial());
+        }
+
+        /**
+         * Get damaged state by block material
+         *
+         * @param material Block material
+         * @return DamageState
+         * @throws IllegalArgumentException If non anvil material is given
+         */
+        @NotNull
+        public static DamageState getState(@Nullable Material material) {
+            if (material == null) {
+                return BROKEN;
+            }
+            for (DamageState state : values()) {
+                if (state.getMaterial() == material) {
+                    return state;
+                }
+            }
+            throw new IllegalArgumentException("Material is not an anvil state");
+        }
+    }
+}