From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: BillyGalbreath 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..4b90dc67f75b05cb8a766025781b9eaa2fef2606 --- /dev/null +++ b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java @@ -0,0 +1,171 @@ +package com.destroystokyo.paper.event.block; + +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.BlockType; +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.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 handlers = new HandlerList(); + private boolean cancel; + private DamageState damageState; + + public AnvilDamagedEvent(@NotNull InventoryView inventory, @NotNull 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 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 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; + } + + @NotNull + public HandlerList getHandlers() { + return handlers; + } + + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Represents the amount of damage on an anvil block + */ + public enum DamageState { + FULL(BlockType.ANVIL), + CHIPPED(BlockType.CHIPPED_ANVIL), + DAMAGED(BlockType.DAMAGED_ANVIL), + BROKEN(BlockType.AIR); + + private BlockType blockType; + + DamageState(@NotNull BlockType blockType) { + this.blockType = blockType; + } + + /** + * Get block material of this state + * + * @return Material + */ + @Deprecated + public @NotNull Material getMaterial() { + return Bukkit.getUnsafe().toMaterial(this.blockType); + } + + /** + * Get the block type for this damage state. + * + * @return the block type + */ + public @NotNull BlockType getBlockType() { + return this.blockType; + } + + /** + * 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.getBlockType()); + } + + /** + * Get damaged state by block material + * + * @param material Block material + * @return DamageState + * @throws IllegalArgumentException If non anvil material is given + * @deprecated use {@link #getState(BlockType)} + */ + @Deprecated + public static @NotNull DamageState getState(@Nullable Material material) { + return material == null ? BROKEN : getState(material.asBlockType()); + } + + /** + * Get damaged state by block type + * + * @param blockType Block type + * @return DamageState + * @throws IllegalArgumentException If non anvil block type is given + */ + public static @NotNull DamageState getState(@Nullable BlockType blockType) { + if (blockType == null) { + return BROKEN; + } + for (DamageState state : values()) { + if (state.blockType == blockType) { + return state; + } + } + throw new IllegalArgumentException("Material not an anvil"); + } + } +}