Search in sources :

Example 1 with PetInventoryOpenEvent

use of simplepets.brainsynder.api.event.inventory.PetInventoryOpenEvent in project SimplePets by brainsynder-Dev.

the class SelectionMenu method open.

@Override
public void open(PetOwner owner, int page) {
    if (!isEnabled())
        return;
    Player player = Bukkit.getPlayer(owner.getUuid());
    pageSave.put(player.getName(), page);
    Inventory inv = Bukkit.createInventory(new SelectionHolder(), getSize(), getTitle());
    int placeHolder = inv.getSize();
    int maxPets = 0;
    while (placeHolder > 0) {
        int slot = (placeHolder - 1);
        if (getSlots().containsKey(slot)) {
            Item item = getSlots().get(slot);
            if (item instanceof Air) {
                maxPets++;
            } else {
                inv.setItem(placeHolder - 1, PetCore.get().getItemLoaders().PLACEHOLDER.getItemBuilder().build());
            }
        } else {
            inv.setItem(placeHolder - 1, PetCore.get().getItemLoaders().PLACEHOLDER.getItemBuilder().build());
        }
        placeHolder--;
    }
    boolean removeNoPerms = PetCore.get().getConfiguration().getBoolean("Remove-Item-If-No-Permission");
    IStorage<PetTypeStorage> petTypes = new StorageList<>();
    for (PetDefault type : availableTypes) {
        if (type.hasPermission(player)) {
            petTypes.add(new PetTypeStorage(type));
        } else {
            if (!removeNoPerms)
                petTypes.add(new PetTypeStorage(type));
        }
    }
    if ((petTypes.getSize() == 0) && (PetCore.get().getConfiguration().getBoolean("Needs-Pet-Permission-To-Open-GUI"))) {
        player.sendMessage(PetCore.get().getMessages().getString("No-Permission"));
        return;
    }
    ObjectPager<PetTypeStorage> pages = new ObjectPager<>(maxPets, petTypes.toArrayList());
    pagerMap.put(player.getName(), pages);
    getSlots().forEach((slot, item) -> {
        if (item.isEnabled() && item.addItemToInv(owner, this))
            inv.setItem(slot, item.getItemBuilder().build());
    });
    PetInventoryOpenEvent event = new PetInventoryOpenEvent(pages.getPage(page), player);
    Bukkit.getServer().getPluginManager().callEvent(event);
    if (event.isCancelled())
        return;
    IStorage<ItemStack> types = event.getItems().copy();
    while (types.hasNext()) {
        inv.addItem(types.next());
    }
    petMap.put(player.getName(), event.getShownPetTypes());
    player.openInventory(inv);
}
Also used : Player(org.bukkit.entity.Player) PetDefault(simplepets.brainsynder.pet.PetDefault) ObjectPager(simple.brainsynder.utils.ObjectPager) Air(simplepets.brainsynder.menu.items.list.Air) StorageList(simple.brainsynder.storage.StorageList) Item(simplepets.brainsynder.menu.items.Item) PetTypeStorage(simplepets.brainsynder.storage.PetTypeStorage) ItemStack(org.bukkit.inventory.ItemStack) PetInventoryOpenEvent(simplepets.brainsynder.api.event.inventory.PetInventoryOpenEvent) CustomInventory(simplepets.brainsynder.menu.inventory.CustomInventory) Inventory(org.bukkit.inventory.Inventory) SelectionHolder(simplepets.brainsynder.menu.holders.SelectionHolder)

Aggregations

Player (org.bukkit.entity.Player)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 StorageList (simple.brainsynder.storage.StorageList)1 ObjectPager (simple.brainsynder.utils.ObjectPager)1 PetInventoryOpenEvent (simplepets.brainsynder.api.event.inventory.PetInventoryOpenEvent)1 SelectionHolder (simplepets.brainsynder.menu.holders.SelectionHolder)1 CustomInventory (simplepets.brainsynder.menu.inventory.CustomInventory)1 Item (simplepets.brainsynder.menu.items.Item)1 Air (simplepets.brainsynder.menu.items.list.Air)1 PetDefault (simplepets.brainsynder.pet.PetDefault)1 PetTypeStorage (simplepets.brainsynder.storage.PetTypeStorage)1