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);
}
Aggregations