3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-14 20:10:05 +01:00
Paper/patches/api/0127-AnvilDamageEvent.patch

162 Zeilen
4.7 KiB
Diff

2021-06-11 14:02:28 +02:00
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..1472ecef11627d82f72911d86d579ad3bb365dca
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/block/AnvilDamagedEvent.java
@@ -0,0 +1,149 @@
2021-06-11 14:02:28 +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;
2023-09-14 12:44:12 +02:00
+import org.jetbrains.annotations.ApiStatus;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2021-06-11 14:02:28 +02:00
+
+/**
+ * Called when an anvil is damaged from being used
+ */
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class AnvilDamagedEvent extends InventoryEvent implements Cancellable {
2024-02-01 10:15:57 +01:00
+
2023-09-14 12:44:12 +02:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ private DamageState damageState;
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
2021-06-11 14:02:28 +02:00
+
2023-09-14 12:44:12 +02:00
+ @ApiStatus.Internal
+ public AnvilDamagedEvent(final InventoryView inventory, final @Nullable BlockData blockData) {
2021-06-11 14:02:28 +02:00
+ 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() {
2024-02-01 10:15:57 +01:00
+ return this.damageState;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the new state of damage on the anvil
+ *
+ * @param damageState Damage state
+ */
+ public void setDamageState(final DamageState damageState) {
2021-06-11 14:02:28 +02:00
+ this.damageState = damageState;
+ }
+
+ /**
+ * Gets if anvil is breaking on this use
+ *
2024-02-01 10:15:57 +01:00
+ * @return {@code true} if breaking
2021-06-11 14:02:28 +02:00
+ */
+ public boolean isBreaking() {
2024-02-01 10:15:57 +01:00
+ return this.damageState == DamageState.BROKEN;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets if anvil is breaking on this use
+ *
2024-02-01 10:15:57 +01:00
+ * @param breaking {@code true} if breaking
2021-06-11 14:02:28 +02:00
+ */
+ public void setBreaking(final boolean breaking) {
2021-06-11 14:02:28 +02:00
+ if (breaking) {
2024-02-01 10:15:57 +01:00
+ this.damageState = DamageState.BROKEN;
+ } else if (this.damageState == DamageState.BROKEN) {
+ this.damageState = DamageState.DAMAGED;
2021-06-11 14:02:28 +02:00
+ }
+ }
+
2024-02-01 10:15:57 +01:00
+ @Override
2021-06-11 14:02:28 +02:00
+ public boolean isCancelled() {
2024-02-01 10:15:57 +01:00
+ return this.cancelled;
2021-06-11 14:02:28 +02:00
+ }
+
2024-02-01 10:15:57 +01:00
+ @Override
+ public void setCancelled(final boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
2021-06-11 14:02:28 +02:00
+ public HandlerList getHandlers() {
2023-09-14 12:44:12 +02:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2023-09-14 12:44:12 +02:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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);
+
2023-09-14 12:44:12 +02:00
+ private final Material material;
2021-06-11 14:02:28 +02:00
+
+ DamageState(final Material material) {
2021-06-11 14:02:28 +02:00
+ this.material = material;
+ }
+
+ /**
+ * Get block material of this state
+ *
+ * @return Material
+ */
+ public Material getMaterial() {
2024-02-01 10:15:57 +01:00
+ return this.material;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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(final @Nullable BlockData blockData) {
2021-06-11 14:02:28 +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
+ */
+ public static DamageState getState(final @Nullable Material material) {
2021-06-11 14:02:28 +02:00
+ if (material == null) {
+ return BROKEN;
+ }
+ for (final DamageState state : values()) {
2024-02-01 10:15:57 +01:00
+ if (state.getMaterial() == material) {
2021-06-11 14:02:28 +02:00
+ return state;
+ }
+ }
2023-09-14 12:44:12 +02:00
+ throw new IllegalArgumentException("Material is not an anvil state");
2021-06-11 14:02:28 +02:00
+ }
+ }
+}