|
| 1 | +package minevalley.core.api.trashcans; |
| 2 | + |
| 3 | +import lombok.Setter; |
| 4 | +import minevalley.core.api.Depends; |
| 5 | +import org.bukkit.Location; |
| 6 | +import org.bukkit.block.Block; |
| 7 | +import org.bukkit.inventory.ItemStack; |
| 8 | + |
| 9 | +import javax.annotation.Nullable; |
| 10 | + |
| 11 | +/** |
| 12 | + * This manager is handled by another module! Do not change the manager-attribute. |
| 13 | + * <br> |
| 14 | + * Due to the circumstance that your module might load before this manager's handler, it is necessary not to call any |
| 15 | + * of the functions below on module start without using a scheduler! |
| 16 | + */ |
| 17 | +@Depends("TrashCans") |
| 18 | +public class TrashCanManager { |
| 19 | + |
| 20 | + @Setter |
| 21 | + private static Manager manager; |
| 22 | + |
| 23 | + /** |
| 24 | + * Adds new ItemStack to the trash can. If the trash can is full, the operation will be cancelled. |
| 25 | + * <p> |
| 26 | + * |
| 27 | + * @param block the block of the trash can |
| 28 | + * @param itemStack the item to add |
| 29 | + * <p><b>Note:</b> If the {@code amount} of the {@code itemStack} is greater than 1, the item will be split into multiple ItemStacks.</p> |
| 30 | + * @throws IllegalArgumentException if the block is not a trash can |
| 31 | + */ |
| 32 | + public static void addItem(Block block, ItemStack itemStack) throws IllegalArgumentException { |
| 33 | + manager.addItem(block, itemStack); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Removes an ItemStack from the trash can. If the trash can is empty, the operation will be cancelled. |
| 38 | + * <br> |
| 39 | + * <p> |
| 40 | + * <b>Note:</b> Every replica of the ItemStack will be removed. The order or the amount of the ItemStacks in the TrashCan does not matter. |
| 41 | + * |
| 42 | + * @throws IllegalArgumentException if the block is not a trash can |
| 43 | + */ |
| 44 | + public static void removeItem(Block block, ItemStack itemStack) throws IllegalArgumentException { |
| 45 | + manager.removeItem(block, itemStack); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Clears the trash can at given block. |
| 50 | + * |
| 51 | + * @param block the block of the trash can |
| 52 | + * @throws IllegalArgumentException if the block is not a trash can |
| 53 | + */ |
| 54 | + public static void clear(Block block) throws IllegalArgumentException { |
| 55 | + manager.clear(block); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the nearest trash can to the given location. |
| 60 | + * |
| 61 | + * @param location the location to search from |
| 62 | + * @return the nearest trash can |
| 63 | + */ |
| 64 | + public static Block getNearestTrashCan(Location location) { |
| 65 | + return manager.getNearestTrashCan(location); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the peak ItemStack of the trash can. |
| 70 | + * |
| 71 | + * @param block the block of the trash can |
| 72 | + * @return the peak ItemStack - if empty {@code null} will be returned. |
| 73 | + * @throws IllegalArgumentException if the block is not a trash can |
| 74 | + */ |
| 75 | + @Nullable |
| 76 | + public static ItemStack getPeak(Block block) throws IllegalArgumentException { |
| 77 | + return manager.getPeak(block); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns whether the given block is a trash can. |
| 82 | + * |
| 83 | + * @param block the block to check |
| 84 | + * @return {@code true} if the block is a trash can, {@code false} otherwise |
| 85 | + */ |
| 86 | + public static boolean isTrashCan(Block block) { |
| 87 | + return manager.isTrashCan(block); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns whether the trash can is full. |
| 92 | + * |
| 93 | + * @param block the block of the trash can |
| 94 | + * @return {@code true} if the trash can is full, {@code false} otherwise |
| 95 | + * @throws IllegalArgumentException if the block is not a trash can |
| 96 | + */ |
| 97 | + public static boolean isFull(Block block) throws IllegalArgumentException { |
| 98 | + return manager.isFull(block); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns whether the trash can is empty. |
| 103 | + * |
| 104 | + * @param block the block of the trash can |
| 105 | + * @return {@code true} if the trash can is empty, {@code false} otherwise |
| 106 | + * @throws IllegalArgumentException if the block is not a trash can |
| 107 | + */ |
| 108 | + public static boolean isEmpty(Block block) throws IllegalArgumentException { |
| 109 | + return manager.isEmpty(block); |
| 110 | + } |
| 111 | + |
| 112 | + public interface Manager { |
| 113 | + |
| 114 | + void addItem(Block block, ItemStack itemStack); |
| 115 | + |
| 116 | + void removeItem(Block block, ItemStack itemStack); |
| 117 | + |
| 118 | + void clear(Block block); |
| 119 | + |
| 120 | + Block getNearestTrashCan(Location location); |
| 121 | + |
| 122 | + ItemStack getPeak(Block block); |
| 123 | + |
| 124 | + boolean isTrashCan(Block block); |
| 125 | + |
| 126 | + boolean isFull(Block block); |
| 127 | + |
| 128 | + boolean isEmpty(Block block); |
| 129 | + |
| 130 | + } |
| 131 | +} |
0 commit comments