Paper/Spigot-API-Patches/0130-AnvilDamageEvent.patch
Aikar 7438edc9a1
Rework Async Chunks API in prep for merge, add utility
This adds a new Future based, Consumer<Chunk> based, and ability
to control whether or not to generate to the Async Chunk API.

Until Async Chunks merges, these API's are still synchronous, but
this commit will allow plugins to start using the API's in use
with the Async Chunks beta.
2018-09-21 16:56:08 -04:00

155 lines
4.3 KiB
Diff

From b08e64c433d4014812d1c11d8ef43268f6804dcb 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 000000000..fd3c5c02e
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java
@@ -0,0 +1,139 @@
+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;
+
+/**
+ * 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;
+
+ public AnvilDamagedEvent(InventoryView inventory, BlockData blockData) {
+ super(inventory);
+ this.damageState = DamageState.getState(blockData);
+ }
+
+ @Override
+ public AnvilInventory getInventory() {
+ return (AnvilInventory) super.getInventory();
+ }
+
+ /**
+ * Gets the new state of damage on the anvil
+ *
+ * @return Damage state
+ */
+ public DamageState getDamageState() {
+ return damageState;
+ }
+
+ /**
+ * Sets the new state of damage on the anvil
+ *
+ * @param damageState Damage state
+ */
+ public void setDamageState(DamageState damageState) {
+ 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;
+ }
+
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ 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;
+
+ DamageState(Material material) {
+ this.material = material;
+ }
+
+ /**
+ * Get block material of this state
+ *
+ * @return Material
+ */
+ 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
+ */
+ public static DamageState getState(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
+ */
+ public static DamageState getState(Material material) {
+ if (material == null) {
+ return BROKEN;
+ }
+ for (DamageState state : values()) {
+ if (state.material == material) {
+ return state;
+ }
+ }
+ throw new IllegalArgumentException("Material not an anvil");
+ }
+ }
+}
--
2.19.0