@Override public void onEvent(InventoryEvent event) { if (event instanceof InventoryClickEvent) { InventoryClickEvent clickEvent = ((InventoryClickEvent) event); if (isTop(event, clickEvent.getRawSlot())) { notify(event); } } else if (event instanceof InventoryDragEvent) { InventoryDragEvent dragEvent = ((InventoryDragEvent) event); for (int slot : dragEvent.getRawSlots()) { if (isTop(event, slot)) { notify(event); break; } } } }
@Override public void handle(GUIEvent<InventoryEvent> event) { InventoryEvent nativeEvent = event.getEvent(); if (nativeEvent instanceof InventoryClickEvent) { InventoryClickEvent clickEvent = (InventoryClickEvent) nativeEvent; if (clickEvent.getRawSlot() < clickEvent.getInventory().getSize()) receive(event); } else if (nativeEvent instanceof InventoryDragEvent) { InventoryDragEvent dragEvent = (InventoryDragEvent) nativeEvent; for (int slot : dragEvent.getRawSlots()) { if (slot < dragEvent.getInventory().getSize()) { receive(event); break; } } } }
@Override protected void onInventoryEvent(InventoryEvent event) { if (event instanceof EnchantItemEvent) { callEvent(((EnchantItemEvent) event).getEnchanter(), event); } else if (event instanceof PrepareItemEnchantEvent) { callEvent(((PrepareItemEnchantEvent) event).getEnchanter(), event); } else { InventoryHolder holder = event.getInventory().getHolder(); if (holder instanceof Player) { callEvent((Player)holder, event); } } }
@SuppressWarnings("unchecked") public <T extends InventoryEvent> Optional<T> getEvent(Class<T> eventClass) { E event = getEvent(); return eventClass.isInstance(event) ? Optional.ofNullable((T) event) : Optional.empty(); }
@Override public void receive(GUIEvent<InventoryEvent> guiEvent) { guiEvent.getEvent(GUIClickEvent.class).ifPresent(click -> { if (click.getRawSlot() >= size) click.setCancelled(true); }); }
@Override public void handle(GUIEvent<InventoryEvent> event) { event.getEvent(GUIClickEvent.class).ifPresent(e -> { int slot = e.getRawSlot(); if (slot >= 0 && slot < size) { e.setRawSlot(e.getRawSlot() + (currentPage - 1) * size); } if (slot >= size) event.setConsume(true); }); }
public void notify(InventoryEvent event) { GUIEvent<InventoryEvent> guiEvent = new GUIEvent<>(gui, convert(event)); for (GUIHandler handler : handlers) { handler.handle(guiEvent); if (guiEvent.isConsume()) break; } }
public void on(Object e) { if (!(e instanceof Event)) return; Event event = (Event) e; onEvent(event); if (event instanceof PlayerEvent) { onPlayerEvent((PlayerEvent)event); } else if (event instanceof BlockEvent) { onBlockEvent((BlockEvent)event); } else if (event instanceof HangingEvent) { onHangingEvent((HangingEvent)event); } else if (event instanceof InventoryEvent) { onInventoryEvent((InventoryEvent) event); } else if (event instanceof VehicleEvent) { onVehicleEvent((VehicleEvent) event); } else if (event instanceof EntityEvent) { onEntityEvent((EntityEvent) event); } }
private boolean isKitGuiInvolved(InventoryEvent evt) { Inventory inventory = evt.getInventory(); InventoryHolder holder = inventory.getHolder(); if (holder instanceof Player) { if (((Player) holder).getOpenInventory().getTitle().equals(plugin.getKitGui().getKitGuiTitle())) { // This ensure that a player is unable to manipulate their own items when a kit GUI is open. return true; } } return inventory.getTitle().equals(plugin.getKitGui().getKitGuiTitle()); }
public static boolean isTop(InventoryEvent event, int slot) { Inventory topInv = event.getInventory(); return topInv != null && slot >= 0 && slot <= topInv.getSize(); }
@SafeVarargs public final TopHandler addHandler(GUIHandler<InventoryEvent>... handlers) { this.handlers.addAll(Arrays.asList(handlers)); return this; }
private void notify(InventoryEvent event) { handlers.forEach(handler -> handler.onEvent(event)); }
@Override public void onEvent(InventoryEvent event) { if (event instanceof Cancellable) { ((Cancellable) event).setCancelled(true); } }
@SafeVarargs public final GUIHandlers add(GUIHandler<InventoryEvent>... handlers) { this.handlers.addAll(Arrays.asList(handlers)); return this; }
public void notify(InventoryEvent event) { handlers.forEach(handler -> handler.onEvent(event)); }
private InventoryEvent convert(InventoryEvent event) { if (event instanceof InventoryClickEvent) { event = new GUIClickEvent((InventoryClickEvent) event, gui); } return event; }
protected void callHandlers(String eventName, InventoryEvent event) { callHandlers(eventName, event, event.getInventory()); }
public void onInventory(InventoryEvent event) { check("onInventory", event); }
@EventHandler public void InventoryEvent(InventoryEvent event){ library.onInventory(event); }
@EventHandler public void onOtherInteraction(InventoryEvent event) { if (event.getInventory().getTitle().contains("MainChest") || event.getInventory().getTitle().contains("RelatedChest")) { SyncManager.getManager().updateHoppers(); } }
@EventHandler(priority = EventPriority.MONITOR) public void onInventoryEvent(InventoryEvent evt) { checkLocation(evt.getView().getPlayer().getLocation(), evt); }
/** * Invoked whenever a registered inventory event is called. * * <p>Intended for optional override.</p> * * @param event The called event. */ protected void onInventoryEvent(InventoryEvent event) {}
void handle(GUIEvent<InventoryEvent> event);
public abstract void receive(GUIEvent<InventoryEvent> event);