I’ve managed to setup a custom inventory and have actions executed when an item is clicked successfully but I have a few issues:
- The item that is clicked (sometimes) ends up in the users inventory or is dropped.
- If the action includes clearing the custom inventory, the clicked item is still there.
I’m presuming most of this is due to my Listener so here it is, if you need any more code let me know.
public class InventoryEventListener extends OCListener {
public InventoryEventListener(OblivionCore plugin) {
super(plugin);
}
@Listener
@Exclude({ClickInventoryEvent.Open.class, ClickInventoryEvent.Close.class, ClickInventoryEvent.Primary.class})
public void inventoryEventListener(ClickInventoryEvent event) {
if (!(event instanceof Cancellable)) return;
Cancellable cancellableEvent = event;
boolean cancelled = cancellableEvent.isCancelled();
for (Inventory slot : event.getTargetInventory().slots()) {
if (!slot.peek().isPresent()) return;
if (!slot.peek().get().get(MenuButtonIDData.class).isPresent()) return;
if (!event.getTargetInventory().getClass().getSimpleName().equals("CustomInventory")) {
slot.peek().get().setQuantity(0);
}
cancelled = true;
break;
}
cancellableEvent.setCancelled(cancelled);
}
@Listener
public void inventoryClickListener(ClickInventoryEvent.Primary event) {
ItemStackSnapshot clickedItem = event.getCursorTransaction().getFinal();
if (!(event.getCause().containsType(Player.class) && clickedItem.get(ImmutableMenuButtonIDData.class).isPresent())) {
return;
}
Player player = event.getCause().first(Player.class).get();
UUID buttonID = clickedItem.get(ImmutableMenuButtonIDData.class).get().getButtonID();
Optional<MenuItem> optionalMenuItem = plugin.getCompositionRoot().getMenuService().getMenuItem(buttonID);
if (!optionalMenuItem.isPresent()) {
event.getTransactions().forEach(slotTransaction -> slotTransaction.setValid(false));
event.getCursorTransaction().setValid(false);
event.setCancelled(true);
return;
}
Optional<MenuSession> oMenuSession = plugin.getCompositionRoot().getMenuService().getMenuSession(player.getUniqueId());
if (!oMenuSession.isPresent()) {
if (!player.getOpenInventory().isPresent()) return;
//TODO Handle non-existant menu sessions
event.getTransactions().forEach(slotTransaction -> slotTransaction.setValid(false));
event.getCursorTransaction().setValid(false);
event.setCancelled(true);
return;
}
Optional<Menu> oMenu = plugin.getCompositionRoot().getMenuService().getMenu(oMenuSession.get().getActiveMenuClass());
if (oMenu.isPresent()) {
optionalMenuItem.get().handle(new MenuItemClickEventArgs(optionalMenuItem.get(), oMenu.get(), player));
event.getTransactions().forEach(slotTransaction -> slotTransaction.setValid(false));
event.getCursorTransaction().setValid(false);
event.setCancelled(true);
return;
}
}
@Listener
public void inventoryCloseListener(InteractInventoryEvent.Close event) {
if (!event.getCause().first(Player.class).isPresent()) {
return;
}
Player player = event.getCause().first(Player.class).get();
if (plugin.getCompositionRoot().getMenuService().getMenuSession(player.getUniqueId()).isPresent()) {
plugin.getCompositionRoot().getMenuService().removeMenuSession(player.getUniqueId());
}
}
@Listener
public void clientDisconnectListener(ClientConnectionEvent.Disconnect event) {
Optional<Player> opPlayer = event.getCause().first(Player.class);
if (!opPlayer.isPresent()) {
return;
}
UUID uuid = opPlayer.get().getUniqueId();
plugin.getCompositionRoot().getMenuService().removeMenuSession(uuid);
}