67 lines
1.8 KiB
Java
67 lines
1.8 KiB
Java
/*
|
|
* Copyright (c) 2018 univento.eu - All rights reserved
|
|
* You are not allowed to use, distribute or modify this code
|
|
*/
|
|
|
|
package eu.univento.core.api.gui.inventory;
|
|
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.gui.Action;
|
|
import eu.univento.core.api.player.CustomPlayer;
|
|
import lombok.Getter;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
public class InventoryGUI implements Listener{
|
|
|
|
private CustomPlayer player;
|
|
@Getter private Inventory inventory;
|
|
private Map<Integer, Action> actions;
|
|
|
|
public InventoryGUI(CustomPlayer player, int size, String title) {
|
|
this.player = player;
|
|
inventory = Bukkit.createInventory(player, size, title);
|
|
actions = new HashMap<>();
|
|
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
|
|
}
|
|
|
|
public void addItem(int slot, ItemStack item, Action action) {
|
|
inventory.setItem(slot, item);
|
|
actions.put(slot, action);
|
|
}
|
|
|
|
public void addItem(ItemStack item, Action action) {
|
|
int slot = inventory.firstEmpty();
|
|
inventory.setItem(slot, item);
|
|
actions.put(slot, action);
|
|
}
|
|
|
|
public void open() {
|
|
player.openInventory(inventory);
|
|
}
|
|
|
|
@EventHandler
|
|
private void onClick(InventoryClickEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer((Player) e.getWhoClicked());
|
|
if(e.getInventory().getTitle().equals(inventory.getTitle())) {
|
|
if(actions.get(e.getSlot()) != null) {
|
|
actions.get(e.getSlot()).onClick(p);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|