2020-01-22 03:02:07 +01:00
|
|
|
From a75e25aa2f87e441bce055f45716c5367b0e33f3 Mon Sep 17 00:00:00 2001
|
2018-07-21 06:37:40 +02:00
|
|
|
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
|
2020-01-22 03:02:07 +01:00
|
|
|
index 00000000..a83c286c
|
2018-07-21 06:37:40 +02:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java
|
2019-03-20 01:28:15 +01:00
|
|
|
@@ -0,0 +1,148 @@
|
2018-07-21 06:37:40 +02:00
|
|
|
+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;
|
2019-03-20 01:28:15 +01:00
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
+import org.jetbrains.annotations.Nullable;
|
2018-07-21 06:37:40 +02:00
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Called when an anvil is damaged from being used
|
|
|
|
+ */
|
|
|
|
+public class AnvilDamagedEvent extends InventoryEvent implements Cancellable {
|
|
|
|
+ private static final HandlerList handlers = new HandlerList();
|
|
|
|
+ private boolean cancel;
|
|
|
|
+ private DamageState damageState;
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ public AnvilDamagedEvent(@NotNull InventoryView inventory, @NotNull BlockData blockData) {
|
2018-07-21 06:37:40 +02:00
|
|
|
+ super(inventory);
|
|
|
|
+ this.damageState = DamageState.getState(blockData);
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-07-21 06:37:40 +02:00
|
|
|
+ @Override
|
|
|
|
+ public AnvilInventory getInventory() {
|
|
|
|
+ return (AnvilInventory) super.getInventory();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets the new state of damage on the anvil
|
|
|
|
+ *
|
|
|
|
+ * @return Damage state
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-07-21 06:37:40 +02:00
|
|
|
+ public DamageState getDamageState() {
|
|
|
|
+ return damageState;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets the new state of damage on the anvil
|
|
|
|
+ *
|
|
|
|
+ * @param damageState Damage state
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ public void setDamageState(@NotNull DamageState damageState) {
|
2018-07-21 06:37:40 +02:00
|
|
|
+ this.damageState = damageState;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Gets if anvil is breaking on this use
|
|
|
|
+ *
|
|
|
|
+ * @return True if breaking
|
|
|
|
+ */
|
|
|
|
+ public boolean isBreaking() {
|
|
|
|
+ return damageState == DamageState.BROKEN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets if anvil is breaking on this use
|
|
|
|
+ *
|
|
|
|
+ * @param breaking True if breaking
|
|
|
|
+ */
|
|
|
|
+ public void setBreaking(boolean breaking) {
|
|
|
|
+ if (breaking) {
|
|
|
|
+ damageState = DamageState.BROKEN;
|
|
|
|
+ } else if (damageState == DamageState.BROKEN) {
|
|
|
|
+ damageState = DamageState.DAMAGED;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean isCancelled() {
|
|
|
|
+ return cancel;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setCancelled(boolean cancel) {
|
|
|
|
+ this.cancel = cancel;
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-07-21 06:37:40 +02:00
|
|
|
+ public HandlerList getHandlers() {
|
|
|
|
+ return handlers;
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-07-21 06:37:40 +02:00
|
|
|
+ public static HandlerList getHandlerList() {
|
|
|
|
+ return handlers;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 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 Material material;
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ DamageState(@NotNull Material material) {
|
2018-07-21 06:37:40 +02:00
|
|
|
+ this.material = material;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get block material of this state
|
|
|
|
+ *
|
|
|
|
+ * @return Material
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-07-21 06:37:40 +02:00
|
|
|
+ public Material getMaterial() {
|
|
|
|
+ return material;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get damaged state by block data
|
|
|
|
+ *
|
|
|
|
+ * @param blockData Block data
|
|
|
|
+ * @return DamageState
|
|
|
|
+ * @throws IllegalArgumentException If non anvil block data is given
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
|
|
|
+ public static DamageState getState(@Nullable BlockData blockData) {
|
2018-07-21 06:37:40 +02:00
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
|
|
|
+ public static DamageState getState(@Nullable Material material) {
|
2018-07-21 06:37:40 +02:00
|
|
|
+ if (material == null) {
|
|
|
|
+ return BROKEN;
|
|
|
|
+ }
|
|
|
|
+ for (DamageState state : values()) {
|
|
|
|
+ if (state.material == material) {
|
|
|
|
+ return state;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ throw new IllegalArgumentException("Material not an anvil");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
--
|
2020-01-22 03:02:07 +01:00
|
|
|
2.25.0.windows.1
|
2018-07-21 06:37:40 +02:00
|
|
|
|