All source projects

MrUltimateLibrary Source

Standalone source project / 68 files

MrUltimateLibrary is the backbone of all my plugins; it offers features that facilitate plugin creation, maintenance, and updates.

java

MrUltimateLibrary/src/main/java/eu/mrneznamy/mrultimatelibrary/gui/MrLibGUI.java

JAVA · Read-only source

package eu.mrneznamy.mrultimatelibrary.gui;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import org.bukkit.event.inventory.InventoryType;
import eu.mrneznamy.mrultimatelibrary.utils.MrLibColors;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * Simple GUI system for plugins
 */
public class MrLibGUI {
    
    private final String title;
    private final int size;
    private final Inventory inventory;
    private final Map<Integer, Consumer<InventoryClickEvent>> clickHandlers;
    private Consumer<InventoryCloseEvent> closeHandler;
    private boolean cancelClick = true;
    
    /**
     * Create new GUI with chest layout
     * 
     * @param title GUI title (supports color codes)
     * @param rows Number of rows (1-6)
     */
    public MrLibGUI(String title, int rows) {
        this.title = MrLibColors.colorize(title);
        this.size = rows * 9;
        this.inventory = Bukkit.createInventory(null, size, this.title);
        this.clickHandlers = new HashMap<>();
    }

    /**
     * Create new GUI with specific inventory type (Anvil, Dispenser, etc.)
     * 
     * @param title GUI title (supports color codes)
     * @param type Inventory type
     */
    public MrLibGUI(String title, InventoryType type) {
        this.title = MrLibColors.colorize(title);
        this.inventory = Bukkit.createInventory(null, type, this.title);
        this.size = inventory.getSize();
        this.clickHandlers = new HashMap<>();
    }

    /**
     * Create new GUI with custom inventory
     * 
     * @param inventory Custom inventory
     */
    public MrLibGUI(Inventory inventory) {
        this.title = "";
        this.size = inventory.getSize();
        this.inventory = inventory;
        this.clickHandlers = new HashMap<>();
    }
    
    /**
     * Set item at slot
     * 
     * @param slot Slot number (0-53)
     * @param item ItemStack
     * @return this
     */
    public MrLibGUI setItem(int slot, ItemStack item) {
        inventory.setItem(slot, item);
        return this;
    }
    
    /**
     * Set item with click handler
     * 
     * @param slot Slot number
     * @param item ItemStack
     * @param onClick Click handler
     * @return this
     */
    public MrLibGUI setItem(int slot, ItemStack item, Consumer<InventoryClickEvent> onClick) {
        inventory.setItem(slot, item);
        clickHandlers.put(slot, onClick);
        return this;
    }
    
    /**
     * Fill empty slots with item
     * 
     * @param item ItemStack to fill with
     * @return this
     */
    public MrLibGUI fillEmpty(ItemStack item) {
        for (int i = 0; i < size; i++) {
            if (inventory.getItem(i) == null) {
                inventory.setItem(i, item);
            }
        }
        return this;
    }
    
    /**
     * Fill border with item
     * 
     * @param item ItemStack
     * @return this
     */
    public MrLibGUI fillBorder(ItemStack item) {
        int rows = size / 9;
        
        
        for (int i = 0; i < 9; i++) {
            inventory.setItem(i, item);
            inventory.setItem(size - 9 + i, item);
        }
        
        
        for (int i = 1; i < rows - 1; i++) {
            inventory.setItem(i * 9, item);
            inventory.setItem(i * 9 + 8, item);
        }
        
        return this;
    }
    
    /**
     * Set close handler
     * 
     * @param handler Close handler
     * @return this
     */
    public MrLibGUI onClose(Consumer<InventoryCloseEvent> handler) {
        this.closeHandler = handler;
        return this;
    }
    
    /**
     * Set if clicks should be cancelled
     * 
     * @param cancel Cancel clicks
     * @return this
     */
    public MrLibGUI setCancelClick(boolean cancel) {
        this.cancelClick = cancel;
        return this;
    }
    
    /**
     * Open GUI for player
     * 
     * @param player Player
     */
    public void open(Player player) {
        MrLibGUIManager.registerGUI(player, this);
        player.openInventory(inventory);
    }
    
    /**
     * Close GUI for player
     * 
     * @param player Player
     */
    public void close(Player player) {
        player.closeInventory();
    }
    
    /**
     * Handle click event
     */
    private final java.util.Map<java.util.UUID, Long> lastClickTimeMap = new java.util.HashMap<>();

    protected void handleClick(InventoryClickEvent event) {
        if (cancelClick) {
            if (event.getClickedInventory() != null && event.getClickedInventory().equals(event.getView().getTopInventory())) {
                event.setCancelled(true);
            } else if (event.getClick().isShiftClick()) {
                event.setCancelled(true);
            }
        }
        
        long now = System.currentTimeMillis();
        java.util.UUID uuid = event.getWhoClicked().getUniqueId();
        if (lastClickTimeMap.containsKey(uuid) && now - lastClickTimeMap.get(uuid) < 150) {
            event.setCancelled(true);
            return;
        }
        lastClickTimeMap.put(uuid, now);
        
        int slot = event.getRawSlot();
        Consumer<InventoryClickEvent> handler = clickHandlers.get(slot);
        
        if (handler != null) {
            handler.accept(event);
        }
    }
    
    /**
     * Handle close event
     */
    protected void handleClose(InventoryCloseEvent event) {
        if (closeHandler != null) {
            closeHandler.accept(event);
        }
    }
    
    /**
     * Get inventory
     */
    public Inventory getInventory() {
        return inventory;
    }
    
    /**
     * Get title
     */
    public String getTitle() {
        return title;
    }
    
    /**
     * Get size
     */
    public int getSize() {
        return size;
    }
}

233 linesGNU GENERAL PUBLIC LICENSE · Version 3, 29 June 2007 · UTF-8 · Read only