Search in sources :

Example 1 with MenuItemAbstract

use of simplepets.brainsynder.menu.menuItems.base.MenuItemAbstract in project SimplePets by brainsynder-Dev.

the class DataListener method onClick.

@EventHandler
public void onClick(InventoryClickEvent e) {
    if (e.getInventory().getHolder() == null)
        return;
    if (!(e.getInventory().getHolder() instanceof PetDataHolder))
        return;
    DataMenu menu = PetCore.get().getInvLoaders().PET_DATA;
    if ((e.getWhoClicked() instanceof Player)) {
        e.setCancelled(true);
        e.setResult(Event.Result.DENY);
        final Player p = (Player) e.getWhoClicked();
        PetOwner owner = PetOwner.getPetOwner(p);
        if (e.getCurrentItem() == null)
            return;
        if (e.getClick().isShiftClick()) {
            e.setCancelled(true);
            return;
        }
        Item item = PetCore.get().getItemLoaders().getLoader(e.getCurrentItem());
        if (item != null) {
            item.onClick(owner, menu);
            return;
        }
        IStorage<MenuItem> menuItems = owner.getPet().getItems().copy();
        if (!menuItems.isEmpty()) {
            while (menuItems.hasNext()) {
                MenuItemAbstract menuItem = (MenuItemAbstract) menuItems.next();
                PetDataChangeEvent.ClickType type = PetDataChangeEvent.ClickType.LEFT_CLICK;
                if (e.getClick().toString().toLowerCase().contains("right"))
                    type = PetDataChangeEvent.ClickType.RIGHT_CLICK;
                if (menuItem.getItem().isSimilar(e.getCurrentItem())) {
                    PetDataChangeEvent event = new PetDataChangeEvent(menuItem, type);
                    Bukkit.getServer().getPluginManager().callEvent(event);
                    if (!event.isCancelled()) {
                        if (type == PetDataChangeEvent.ClickType.LEFT_CLICK) {
                            menuItem.onLeftClick();
                            e.getInventory().setItem(e.getSlot(), menuItem.getItem().build());
                        } else {
                            menuItem.onRightClick();
                            e.getInventory().setItem(e.getSlot(), menuItem.getItem().build());
                        }
                        e.getInventory().setItem(e.getSlot(), menuItem.getItem().build());
                    }
                    e.setCancelled(true);
                    return;
                }
            }
        }
    }
}
Also used : DataMenu(simplepets.brainsynder.menu.inventory.list.DataMenu) Item(simplepets.brainsynder.menu.items.Item) MenuItem(simplepets.brainsynder.menu.menuItems.base.MenuItem) Player(org.bukkit.entity.Player) PetOwner(simplepets.brainsynder.player.PetOwner) PetDataChangeEvent(simplepets.brainsynder.api.event.pet.PetDataChangeEvent) MenuItem(simplepets.brainsynder.menu.menuItems.base.MenuItem) PetDataHolder(simplepets.brainsynder.menu.holders.PetDataHolder) MenuItemAbstract(simplepets.brainsynder.menu.menuItems.base.MenuItemAbstract) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 PetDataChangeEvent (simplepets.brainsynder.api.event.pet.PetDataChangeEvent)1 PetDataHolder (simplepets.brainsynder.menu.holders.PetDataHolder)1 DataMenu (simplepets.brainsynder.menu.inventory.list.DataMenu)1 Item (simplepets.brainsynder.menu.items.Item)1 MenuItem (simplepets.brainsynder.menu.menuItems.base.MenuItem)1 MenuItemAbstract (simplepets.brainsynder.menu.menuItems.base.MenuItemAbstract)1 PetOwner (simplepets.brainsynder.player.PetOwner)1