From d989287fe74e3196a17b141d330cd4e7290a2d9f Mon Sep 17 00:00:00 2001 From: Thorben B <112727827+FourtySeven047@users.noreply.github.com> Date: Sun, 14 Jul 2024 21:59:58 +0200 Subject: [PATCH] feat: add events for interaction with/by the TrashCans Module (#199) Co-authored-by: Vincent B <79211348+Snabeldier@users.noreply.github.com> --- .../core/api/trashcans/TrashCanManager.java | 131 ++++++++++++++++++ .../trashcans/events/TrashCanClearEvent.java | 23 +++ .../events/UserAddItemTrashCanEvent.java | 32 +++++ .../events/UserInteractTrashCanEvent.java | 34 +++++ .../events/UserRemoveItemTrashCanEvent.java | 32 +++++ 5 files changed, 252 insertions(+) create mode 100644 src/main/java/minevalley/core/api/trashcans/TrashCanManager.java create mode 100644 src/main/java/minevalley/core/api/trashcans/events/TrashCanClearEvent.java create mode 100644 src/main/java/minevalley/core/api/trashcans/events/UserAddItemTrashCanEvent.java create mode 100644 src/main/java/minevalley/core/api/trashcans/events/UserInteractTrashCanEvent.java create mode 100644 src/main/java/minevalley/core/api/trashcans/events/UserRemoveItemTrashCanEvent.java diff --git a/src/main/java/minevalley/core/api/trashcans/TrashCanManager.java b/src/main/java/minevalley/core/api/trashcans/TrashCanManager.java new file mode 100644 index 00000000..36de2023 --- /dev/null +++ b/src/main/java/minevalley/core/api/trashcans/TrashCanManager.java @@ -0,0 +1,131 @@ +package minevalley.core.api.trashcans; + +import lombok.Setter; +import minevalley.core.api.Depends; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; + +import javax.annotation.Nullable; + +/** + * This manager is handled by another module! Do not change the manager-attribute. + *
+ * Due to the circumstance that your module might load before this manager's handler, it is necessary not to call any + * of the functions below on module start without using a scheduler! + */ +@Depends("TrashCans") +public class TrashCanManager { + + @Setter + private static Manager manager; + + /** + * Adds new ItemStack to the trash can. If the trash can is full, the operation will be cancelled. + *

+ * + * @param block the block of the trash can + * @param itemStack the item to add + *

Note: If the {@code amount} of the {@code itemStack} is greater than 1, the item will be split into multiple ItemStacks.

+ * @throws IllegalArgumentException if the block is not a trash can + */ + public static void addItem(Block block, ItemStack itemStack) throws IllegalArgumentException { + manager.addItem(block, itemStack); + } + + /** + * Removes an ItemStack from the trash can. If the trash can is empty, the operation will be cancelled. + *
+ *

+ * Note: Every replica of the ItemStack will be removed. The order or the amount of the ItemStacks in the TrashCan does not matter. + * + * @throws IllegalArgumentException if the block is not a trash can + */ + public static void removeItem(Block block, ItemStack itemStack) throws IllegalArgumentException { + manager.removeItem(block, itemStack); + } + + /** + * Clears the trash can at given block. + * + * @param block the block of the trash can + * @throws IllegalArgumentException if the block is not a trash can + */ + public static void clear(Block block) throws IllegalArgumentException { + manager.clear(block); + } + + /** + * Returns the nearest trash can to the given location. + * + * @param location the location to search from + * @return the nearest trash can + */ + public static Block getNearestTrashCan(Location location) { + return manager.getNearestTrashCan(location); + } + + /** + * Returns the peak ItemStack of the trash can. + * + * @param block the block of the trash can + * @return the peak ItemStack - if empty {@code null} will be returned. + * @throws IllegalArgumentException if the block is not a trash can + */ + @Nullable + public static ItemStack getPeak(Block block) throws IllegalArgumentException { + return manager.getPeak(block); + } + + /** + * Returns whether the given block is a trash can. + * + * @param block the block to check + * @return {@code true} if the block is a trash can, {@code false} otherwise + */ + public static boolean isTrashCan(Block block) { + return manager.isTrashCan(block); + } + + /** + * Returns whether the trash can is full. + * + * @param block the block of the trash can + * @return {@code true} if the trash can is full, {@code false} otherwise + * @throws IllegalArgumentException if the block is not a trash can + */ + public static boolean isFull(Block block) throws IllegalArgumentException { + return manager.isFull(block); + } + + /** + * Returns whether the trash can is empty. + * + * @param block the block of the trash can + * @return {@code true} if the trash can is empty, {@code false} otherwise + * @throws IllegalArgumentException if the block is not a trash can + */ + public static boolean isEmpty(Block block) throws IllegalArgumentException { + return manager.isEmpty(block); + } + + public interface Manager { + + void addItem(Block block, ItemStack itemStack); + + void removeItem(Block block, ItemStack itemStack); + + void clear(Block block); + + Block getNearestTrashCan(Location location); + + ItemStack getPeak(Block block); + + boolean isTrashCan(Block block); + + boolean isFull(Block block); + + boolean isEmpty(Block block); + + } +} diff --git a/src/main/java/minevalley/core/api/trashcans/events/TrashCanClearEvent.java b/src/main/java/minevalley/core/api/trashcans/events/TrashCanClearEvent.java new file mode 100644 index 00000000..5e02d865 --- /dev/null +++ b/src/main/java/minevalley/core/api/trashcans/events/TrashCanClearEvent.java @@ -0,0 +1,23 @@ +package minevalley.core.api.trashcans.events; + +import minevalley.core.api.users.User; +import org.bukkit.block.Block; + +/** + * The {@code TrashCanClearEvent} is triggered when a trash can is cleared by a user. + * This event is never automatically triggered by the system nor by regular players emptying the trash can. + * + * @see UserInteractTrashCanEvent + */ +public class TrashCanClearEvent extends UserInteractTrashCanEvent { + + /** + * Constructor for {@code FractionalClearTrashCanEvent.} + * + * @param user the user performing the interaction + * @param block the block of the trash can + */ + public TrashCanClearEvent(User user, Block block) { + super(user, block); + } +} diff --git a/src/main/java/minevalley/core/api/trashcans/events/UserAddItemTrashCanEvent.java b/src/main/java/minevalley/core/api/trashcans/events/UserAddItemTrashCanEvent.java new file mode 100644 index 00000000..0eee812e --- /dev/null +++ b/src/main/java/minevalley/core/api/trashcans/events/UserAddItemTrashCanEvent.java @@ -0,0 +1,32 @@ +package minevalley.core.api.trashcans.events; + +import lombok.Getter; +import lombok.Setter; +import minevalley.core.api.users.User; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; + +/** + * The {@code UserAddItemTrashCanEvent} is triggered when a user adds an item to a trash can. + * This event is only triggered when the action is allowed by the system, for example, when the trash can is not full. + * + * @see UserInteractTrashCanEvent + */ +@Setter +@Getter +public class UserAddItemTrashCanEvent extends UserInteractTrashCanEvent { + + private ItemStack itemStack; + + /** + * Constructor for {@code UserAddItemTrashCanEvent.} + * + * @param user the user performing the interaction + * @param block the block of the trash can + * @param itemStack the added item + */ + public UserAddItemTrashCanEvent(User user, Block block, ItemStack itemStack) { + super(user, block); + this.itemStack = itemStack; + } +} diff --git a/src/main/java/minevalley/core/api/trashcans/events/UserInteractTrashCanEvent.java b/src/main/java/minevalley/core/api/trashcans/events/UserInteractTrashCanEvent.java new file mode 100644 index 00000000..cdb41155 --- /dev/null +++ b/src/main/java/minevalley/core/api/trashcans/events/UserInteractTrashCanEvent.java @@ -0,0 +1,34 @@ +package minevalley.core.api.trashcans.events; + +import lombok.Getter; +import lombok.Setter; +import minevalley.core.api.users.User; +import minevalley.core.api.users.events.UserEvent; +import org.bukkit.block.Block; +import org.bukkit.event.Cancellable; + +/** + * The {@code UserInteractTrashCanEvent} is triggered on any interaction with a trash can. + * This event is the base class for all trash can interaction events. + * + * @see minevalley.core.api.trashcans.events.TrashCanClearEvent + * @see minevalley.core.api.trashcans.events.UserAddItemTrashCanEvent + * @see minevalley.core.api.trashcans.events.UserRemoveItemTrashCanEvent + */ +@Setter +@Getter +public abstract class UserInteractTrashCanEvent extends UserEvent implements Cancellable { + + private final Block block; + private boolean cancelled; + + /** + * Constructor for {@code UserInteractTrashCanEvent.} + * + * @param user The user interacting with the trash can. + */ + public UserInteractTrashCanEvent(User user, Block block) { + super(user); + this.block = block; + } +} diff --git a/src/main/java/minevalley/core/api/trashcans/events/UserRemoveItemTrashCanEvent.java b/src/main/java/minevalley/core/api/trashcans/events/UserRemoveItemTrashCanEvent.java new file mode 100644 index 00000000..1a5d99c7 --- /dev/null +++ b/src/main/java/minevalley/core/api/trashcans/events/UserRemoveItemTrashCanEvent.java @@ -0,0 +1,32 @@ +package minevalley.core.api.trashcans.events; + +import lombok.Getter; +import lombok.Setter; +import minevalley.core.api.users.User; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; + +/** + * The {@code UserRemoveItemTrashCanEvent} is triggered when a user removes an item from a trash can. + * This event is only triggered when the action is allowed by the system, for example, when the trash can is not empty. + * + * @see UserInteractTrashCanEvent + */ +@Setter +@Getter +public class UserRemoveItemTrashCanEvent extends UserInteractTrashCanEvent { + + private ItemStack itemStack; + + /** + * Constructor for {@code UserRemoveItemTrashCanEvent.} + * + * @param user the user performing the interaction + * @param block the block of the trash can + * @param itemStack the removed item + */ + public UserRemoveItemTrashCanEvent(User user, Block block, ItemStack itemStack) { + super(user, block); + this.itemStack = itemStack; + } +}