Search in sources :

Example 1 with InventoryOpener

use of gg.projecteden.nexus.features.menus.api.opener.InventoryOpener in project Nexus by ProjectEdenGG.

the class SmartInventory method open.

public Inventory open(Player player, int page, Map<String, Object> properties) {
    Optional<SmartInventory> oldInv = this.manager.getInventory(player);
    oldInv.ifPresent(inv -> {
        inv.getListeners().stream().filter(listener -> listener.getType() == InventoryCloseEvent.class).forEach(listener -> ((InventoryListener<InventoryCloseEvent>) listener).accept(new InventoryCloseEvent(player.getOpenInventory())));
        this.manager.setInventory(player, null);
    });
    InventoryContents contents = new InventoryContents.Impl(this, player);
    contents.pagination().page(page);
    properties.forEach(contents::setProperty);
    this.manager.setContents(player, contents);
    try {
        this.provider.setContents(contents);
        this.provider.init();
    } catch (Exception ex) {
        ex.printStackTrace();
        player.closeInventory();
        player.sendMessage(ChatColor.RED + "An unknown error occurred while trying to open the menu");
        return null;
    }
    InventoryOpener opener = this.manager.findOpener(type).orElseThrow(() -> new IllegalStateException("No opener found for the inventory type " + type.name()));
    Inventory handle = opener.open(this, player);
    this.manager.setInventory(player, this);
    this.manager.scheduleUpdateTask(player, this);
    return handle;
}
Also used : InventoryOpener(gg.projecteden.nexus.features.menus.api.opener.InventoryOpener) Setter(lombok.Setter) Getter(lombok.Getter) InventoryType(org.bukkit.event.inventory.InventoryType) JsonBuilder(gg.projecteden.nexus.utils.JsonBuilder) Event(org.bukkit.event.Event) InventoryContents(gg.projecteden.nexus.features.menus.api.content.InventoryContents) Player(org.bukkit.entity.Player) InventoryCloseEvent(org.bukkit.event.inventory.InventoryCloseEvent) ComponentLike(net.kyori.adventure.text.ComponentLike) Inventory(org.bukkit.inventory.Inventory) ArrayList(java.util.ArrayList) List(java.util.List) SlotPos(gg.projecteden.nexus.features.menus.api.content.SlotPos) Component(net.kyori.adventure.text.Component) Map(java.util.Map) InventoryProvider(gg.projecteden.nexus.features.menus.api.content.InventoryProvider) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) ChatColor(net.md_5.bungee.api.ChatColor) InventoryOpener(gg.projecteden.nexus.features.menus.api.opener.InventoryOpener) InventoryCloseEvent(org.bukkit.event.inventory.InventoryCloseEvent) InventoryContents(gg.projecteden.nexus.features.menus.api.content.InventoryContents) Inventory(org.bukkit.inventory.Inventory)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 InventoryContents (gg.projecteden.nexus.features.menus.api.content.InventoryContents)1 InventoryProvider (gg.projecteden.nexus.features.menus.api.content.InventoryProvider)1 SlotPos (gg.projecteden.nexus.features.menus.api.content.SlotPos)1 InventoryOpener (gg.projecteden.nexus.features.menus.api.opener.InventoryOpener)1 JsonBuilder (gg.projecteden.nexus.utils.JsonBuilder)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Getter (lombok.Getter)1 Setter (lombok.Setter)1 Component (net.kyori.adventure.text.Component)1 ComponentLike (net.kyori.adventure.text.ComponentLike)1 ChatColor (net.md_5.bungee.api.ChatColor)1 Player (org.bukkit.entity.Player)1 Event (org.bukkit.event.Event)1 InventoryCloseEvent (org.bukkit.event.inventory.InventoryCloseEvent)1 InventoryType (org.bukkit.event.inventory.InventoryType)1