All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tchristofferson.pagedinventories.InventoryRegistrar Maven / Gradle / Ivy

Go to download

API for easily creating multi paged inventory GUIs in Spigot/Bukkit (Minecraft)

The newest version!
package com.tchristofferson.pagedinventories;

import com.tchristofferson.pagedinventories.handlers.*;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import java.util.*;

/**
 * This is a registrar for open PagedInventories
 */
public class InventoryRegistrar {

    private final Map registrar;//Player's with an IPagedInventory open and the specified page
    private final Map pagedInventoryRegistrar;//Player's with an IPagedInventory open
    private final List switchingPages;//Players that are currently switching pages

    private final List globalClickHandlers;
    private final List globalCloseHandlers;
    private final List globalSwitchHandlers;

    InventoryRegistrar() {
        registrar = new HashMap<>();
        pagedInventoryRegistrar = new HashMap<>();
        switchingPages = new ArrayList<>();

        globalClickHandlers = new ArrayList<>(3);
        globalCloseHandlers = new ArrayList<>(3);
        globalSwitchHandlers = new ArrayList<>(3);
    }

    /**'
     * Registers that a player is switching between pages in a {@link IPagedInventory}
     * @param player The player switching between pages
     */
    public void registerSwitch(Player player) {
        switchingPages.add(player.getUniqueId());
    }

    boolean unregisterSwitch(Player player) {
        return switchingPages.remove(player.getUniqueId());
    }

    /**
     * Register that a player has an {@link IPagedInventory} open
     * Automatically called when using {@link IPagedInventory#open(Player)} or {@link IPagedInventory#open(Player, int)}
     * Will unregister when the player closes the IPagedInventory
     * @param player The player that has opened the {@link IPagedInventory}
     * @param pagedInventory The {@link IPagedInventory} the player is opening / has opened
     * @param inventory The page opened by the player
     */
    public void register(Player player, IPagedInventory pagedInventory, Inventory inventory) {
        UUID uuid = player.getUniqueId();
        registrar.put(uuid, inventory);
        pagedInventoryRegistrar.putIfAbsent(uuid, pagedInventory);
    }

    void unregister(Player player) {
        registrar.remove(player.getUniqueId());
        pagedInventoryRegistrar.remove(player.getUniqueId());
    }

    /**
     * This will get all the inventories that are open AND are a part of a {@link IPagedInventory}
     * @return a {@link Map} with the player's uuid as the key and the open inventory as the value
     */
    public Map getOpenInventories() {
        return new HashMap<>(registrar);
    }

    /**
     * This will get all the paged inventories that are open
     * @return a {@link Map} with the player's uuid as the key and the open {@link IPagedInventory} as the value
     */
    public Map getOpenPagedInventories() {
        return new HashMap<>(pagedInventoryRegistrar);
    }

    /**
     * Add a global click handler
     * Whenever any paged inventory gets clicked the specified {@link PagedInventoryGlobalClickHandler#handle(PagedInventoryClickHandler.Handler)} will be called
     * The global handler is called before the paged inventory specific handler(s)
     * @param handler The handler
     */
    public void addGlobalHandler(PagedInventoryGlobalClickHandler handler) {
        globalClickHandlers.add(handler);
    }

    /**
     * Add a global close handler
     * Whenever any paged inventory gets closed the specified {@link PagedInventoryGlobalCloseHandler#handle(PagedInventoryCloseHandler.Handler)} will be called
     * The global handler is called before the paged inventory specific handler(s)
     * @param handler The handler
     */
    public void addGlobalHandler(PagedInventoryGlobalCloseHandler handler) {
        globalCloseHandlers.add(handler);
    }

    /**
     * Add a global switch page handler
     * Whenever any player switches pages the specified {@link PagedInventoryGlobalSwitchPageHandler#handle(PagedInventorySwitchPageHandler.Handler)} will be called
     * The global handler is called before the paged inventory specific handler(s)
     * @param handler The handler
     */
    public void addGlobalHandler(PagedInventoryGlobalSwitchPageHandler handler) {
        globalSwitchHandlers.add(handler);
    }

    void callGlobalClickHandlers(PagedInventoryGlobalClickHandler.Handler handler) {
        globalClickHandlers.forEach(pagedInventoryGlobalClickHandler -> pagedInventoryGlobalClickHandler.handle(handler));
    }

    /**
     * Clear global click handlers
     * To clear all global handlers see {@link InventoryRegistrar#clearAllGlobalHandlers()}
     */
    public void clearGlobalClickHandlers() {
        globalClickHandlers.clear();
    }

    void callGlobalCloseHandlers(PagedInventoryGlobalCloseHandler.Handler handler) {
        globalCloseHandlers.forEach(pagedInventoryGlobalHandler -> pagedInventoryGlobalHandler.handle(handler));
    }

    /**
     * Clear global close handlers
     * To clear all global handlers see {@link InventoryRegistrar#clearAllGlobalHandlers()}
     */
    public void clearGlobalCloseHandlers() {
        globalCloseHandlers.clear();
    }

    void callGlobalSwitchHandlers(PagedInventoryGlobalSwitchPageHandler.Handler handler) {
        globalSwitchHandlers.forEach(pagedInventoryGlobalSwitchHandler -> pagedInventoryGlobalSwitchHandler.handle(handler));
    }

    /**
     * Clear global switch page handlers
     * To clear all global handlers see {@link InventoryRegistrar#clearAllGlobalHandlers()}
     */
    public void clearGlobalSwitchHandlers() {
        globalSwitchHandlers.clear();
    }

    public void clearAllGlobalHandlers() {
        clearGlobalClickHandlers();
        clearGlobalCloseHandlers();
        clearGlobalSwitchHandlers();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!obj.getClass().equals(InventoryRegistrar.class))
            return false;

        InventoryRegistrar inventoryRegistrar = (InventoryRegistrar) obj;
        return registrar.equals(inventoryRegistrar.registrar)
                && pagedInventoryRegistrar.equals(inventoryRegistrar.pagedInventoryRegistrar)
                && switchingPages.equals(inventoryRegistrar.switchingPages);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy