Search in sources :

Example 1 with StorageList

use of simple.brainsynder.storage.StorageList 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)

Example 2 with StorageList

use of simple.brainsynder.storage.StorageList in project SimplePets by brainsynder-Dev.

the class PetEventListeners method openMenuSelector.

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void openMenuSelector(PetInventoryOpenEvent event) {
    if (!PetCore.get().getConfiguration().getBoolean("UseVaultEconomy"))
        return;
    IStorage<ItemStack> items = new StorageList<>();
    IStorage<PetTypeStorage> types = event.getShownPetTypes().copy();
    PetOwner petOwner = PetOwner.getPetOwner(event.getPlayer());
    JSONArray petArray = petOwner.getOwnedPets();
    List<String> lore = economyFile.getStringList((economyFile.getBoolean("Pay-Per-Use.Enabled") ? "Pay-Per-Use.Lore-Lines" : "Lore-Lines"));
    while (types.hasNext()) {
        PetTypeStorage storage = types.next();
        PetDefault type = storage.getType();
        ItemBuilder maker = ItemBuilder.fromJSON(storage.getType().getItemBuilder().toJSON());
        String price = String.valueOf(economyFile.getPrice(type));
        if (price.equals("-1"))
            price = economyFile.getString("Price-Free");
        try {
            if (price.isEmpty()) {
                PetCore.get().debug("Price is empty for: 'Pet." + type.getConfigName() + ".Price'");
                items.add(maker.build());
                continue;
            }
            Double.parseDouble(price);
        } catch (Exception e) {
            PetCore.get().debug("Price is invalid for: 'Pet." + type.getConfigName() + ".Price'");
            items.add(maker.build());
            continue;
        }
        if (economyFile.getBoolean("Bypass.Hide-Price-If-Bypassed") && event.getPlayer().hasPermission("Pet.economy.bypass"))
            price = economyFile.getString("Bypass.Price");
        boolean contains = petArray.contains(type.getConfigName());
        for (String line : lore) maker.addLore(line.replace("%cost%", price).replace("%contains%", String.valueOf(contains)));
        items.add(maker.build());
        storage.setItem(maker);
    }
    event.setItems(items);
}
Also used : PetOwner(simplepets.brainsynder.player.PetOwner) PetDefault(simplepets.brainsynder.pet.PetDefault) JSONArray(org.json.simple.JSONArray) StorageList(simple.brainsynder.storage.StorageList) PetTypeStorage(simplepets.brainsynder.storage.PetTypeStorage) ItemBuilder(simple.brainsynder.api.ItemBuilder) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)2 StorageList (simple.brainsynder.storage.StorageList)2 PetDefault (simplepets.brainsynder.pet.PetDefault)2 PetTypeStorage (simplepets.brainsynder.storage.PetTypeStorage)2 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 Inventory (org.bukkit.inventory.Inventory)1 JSONArray (org.json.simple.JSONArray)1 ItemBuilder (simple.brainsynder.api.ItemBuilder)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 PetOwner (simplepets.brainsynder.player.PetOwner)1