|
| 1 | +package me.flame.menus.events; |
| 2 | + |
| 3 | +import me.flame.menus.items.MenuItem; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.bukkit.event.HandlerList; |
| 6 | +import org.bukkit.event.inventory.InventoryInteractEvent; |
| 7 | +import org.bukkit.event.inventory.InventoryClickEvent; |
| 8 | +import org.bukkit.event.inventory.InventoryAction; |
| 9 | +import org.bukkit.event.inventory.InventoryType; |
| 10 | +import org.bukkit.event.inventory.ClickType; |
| 11 | +import org.bukkit.inventory.Inventory; |
| 12 | +import org.bukkit.inventory.InventoryView; |
| 13 | +import org.bukkit.inventory.ItemStack; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import org.jetbrains.annotations.Nullable; |
| 16 | + |
| 17 | +@SuppressWarnings("unused") |
| 18 | +public class ClickActionEvent extends InventoryInteractEvent { |
| 19 | + private static final HandlerList handlers = new HandlerList(); |
| 20 | + private final ClickType click; |
| 21 | + private final InventoryAction action; |
| 22 | + private final InventoryView view; |
| 23 | + private final InventoryType.SlotType slot_type; |
| 24 | + private final int whichSlot; |
| 25 | + private final int rawSlot; |
| 26 | + private ItemStack current; |
| 27 | + private MenuItem currentItem; |
| 28 | + |
| 29 | + public ClickActionEvent(@NotNull InventoryClickEvent e) { |
| 30 | + super(e.getView()); |
| 31 | + this.view = e.getView(); |
| 32 | + this.slot_type = e.getSlotType(); |
| 33 | + this.rawSlot = e.getRawSlot(); |
| 34 | + this.whichSlot = e.getSlot(); |
| 35 | + this.click = e.getClick(); |
| 36 | + this.action = e.getAction(); |
| 37 | + } |
| 38 | + |
| 39 | + public ClickActionEvent(@NotNull InventoryView view, @NotNull InventoryType.SlotType type, int slot, @NotNull ClickType click, @NotNull InventoryAction action) { |
| 40 | + super(view); |
| 41 | + this.view = view; |
| 42 | + this.slot_type = type; |
| 43 | + this.rawSlot = slot; |
| 44 | + this.whichSlot = view.convertSlot(slot); |
| 45 | + this.click = click; |
| 46 | + this.action = action; |
| 47 | + } |
| 48 | + |
| 49 | + public ClickActionEvent(@NotNull InventoryView view, @NotNull InventoryType.SlotType type, int rawSlot, int slot, @NotNull ClickType click, ItemStack current, @NotNull InventoryAction action) { |
| 50 | + super(view); |
| 51 | + this.view = view; |
| 52 | + this.slot_type = type; |
| 53 | + this.rawSlot = rawSlot; |
| 54 | + this.whichSlot = slot; |
| 55 | + this.click = click; |
| 56 | + this.action = action; |
| 57 | + this.current = current; |
| 58 | + } |
| 59 | + |
| 60 | + public Player getPlayer() { |
| 61 | + return (Player) view.getPlayer(); |
| 62 | + } |
| 63 | + |
| 64 | + public MenuItem getCursorItem() { |
| 65 | + return MenuItem.of(view.getCursor()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets the type of slot that was clicked. |
| 70 | + * |
| 71 | + * @return the slot type |
| 72 | + */ |
| 73 | + @NotNull |
| 74 | + public InventoryType.SlotType getSlotType() { |
| 75 | + return slot_type; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Gets the current ItemStack on the cursor. |
| 80 | + * |
| 81 | + * @return the cursor ItemStack |
| 82 | + */ |
| 83 | + @Nullable |
| 84 | + public ItemStack getCursor() { |
| 85 | + return view.getCursor(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Gets the ItemStack currently in the clicked slot. |
| 90 | + * |
| 91 | + * @return the item in the clicked |
| 92 | + */ |
| 93 | + @Nullable |
| 94 | + public ItemStack getCurrentItem() { |
| 95 | + if (slot_type == InventoryType.SlotType.OUTSIDE) { |
| 96 | + return current; |
| 97 | + } |
| 98 | + return view.getItem(rawSlot); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets the ItemStack currently in the clicked slot. |
| 103 | + * |
| 104 | + * @return the item in the clicked |
| 105 | + */ |
| 106 | + @Nullable |
| 107 | + public MenuItem getCurrentMenuItem() { |
| 108 | + if (slot_type == InventoryType.SlotType.OUTSIDE) { |
| 109 | + return currentItem; |
| 110 | + } |
| 111 | + return MenuItem.of(view.getItem(rawSlot)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Gets whether the ClickType for this event represents a right |
| 116 | + * click. |
| 117 | + * |
| 118 | + * @return true if the ClickType uses the right mouse button. |
| 119 | + * @see ClickType#isRightClick() |
| 120 | + */ |
| 121 | + public boolean isRightClick() { |
| 122 | + return click.isRightClick(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets whether the ClickType for this event represents a left |
| 127 | + * click. |
| 128 | + * |
| 129 | + * @return true if the ClickType uses the left mouse button. |
| 130 | + * @see ClickType#isLeftClick() |
| 131 | + */ |
| 132 | + public boolean isLeftClick() { |
| 133 | + return click.isLeftClick(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Gets whether the ClickType for this event indicates that the key was |
| 138 | + * pressed down when the click was made. |
| 139 | + * |
| 140 | + * @return true if the ClickType uses Shift or Ctrl. |
| 141 | + * @see ClickType#isShiftClick() |
| 142 | + */ |
| 143 | + public boolean isShiftClick() { |
| 144 | + return click.isShiftClick(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the ItemStack currently in the clicked slot. |
| 149 | + * |
| 150 | + * @param stack the item to be placed in the current slot |
| 151 | + */ |
| 152 | + public void setCurrentItem(@Nullable ItemStack stack) { |
| 153 | + if (slot_type == InventoryType.SlotType.OUTSIDE) { |
| 154 | + current = stack; |
| 155 | + } else { |
| 156 | + view.setItem(rawSlot, stack); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the ItemStack currently in the clicked slot. |
| 162 | + * |
| 163 | + * @param stack the item to be placed in the current slot |
| 164 | + */ |
| 165 | + public void setCurrentItem(@Nullable MenuItem stack) { |
| 166 | + if (stack == null) return; |
| 167 | + |
| 168 | + ItemStack item = stack.getItemStack(); |
| 169 | + if (slot_type == InventoryType.SlotType.OUTSIDE) { |
| 170 | + currentItem = stack; |
| 171 | + return; |
| 172 | + } |
| 173 | + view.setItem(rawSlot, item); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Gets the inventory corresponding to the clicked slot. |
| 178 | + * |
| 179 | + * @see InventoryView#getInventory(int) |
| 180 | + * @return inventory, or null if clicked outside |
| 181 | + */ |
| 182 | + @Nullable |
| 183 | + public Inventory getClickedInventory() { |
| 184 | + return view.getInventory(rawSlot); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * The slot number that was clicked, ready for passing to |
| 189 | + * {@link Inventory#getItem(int)}. Note that there may be two slots with |
| 190 | + * the same slot number, since a view links two different inventories. |
| 191 | + * |
| 192 | + * @return The slot number. |
| 193 | + */ |
| 194 | + public int getSlot() { |
| 195 | + return whichSlot; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * The raw slot number clicked, ready for passing to {@link InventoryView |
| 200 | + * #getItem(int)} This slot number is unique for the view. |
| 201 | + * |
| 202 | + * @return the slot number |
| 203 | + */ |
| 204 | + public int getRawSlot() { |
| 205 | + return rawSlot; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Gets the InventoryAction that triggered this event. |
| 210 | + * <p> |
| 211 | + * This action cannot be changed, and represents what the normal outcome |
| 212 | + * of the event will be. To change the behavior of this |
| 213 | + * InventoryClickEvent, changes must be manually applied. |
| 214 | + * |
| 215 | + * @return the InventoryAction that triggered this event. |
| 216 | + */ |
| 217 | + @NotNull |
| 218 | + public InventoryAction getAction() { |
| 219 | + return action; |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Gets the ClickType for this event. |
| 224 | + * <p> |
| 225 | + * This is insulated against changes to the inventory by other plugins. |
| 226 | + * |
| 227 | + * @return the type of inventory click |
| 228 | + */ |
| 229 | + @NotNull |
| 230 | + public ClickType getClick() { |
| 231 | + return click; |
| 232 | + } |
| 233 | + |
| 234 | + @NotNull |
| 235 | + @Override |
| 236 | + public HandlerList getHandlers() { |
| 237 | + return handlers; |
| 238 | + } |
| 239 | + |
| 240 | + @NotNull |
| 241 | + public static HandlerList getHandlerList() { |
| 242 | + return handlers; |
| 243 | + } |
| 244 | +} |
0 commit comments