Implemented Panels API - still need some work, but it is functionnal

This commit is contained in:
Florian CUNY 2017-11-29 16:54:24 +01:00
parent 01b7b04dfc
commit dd40faa3b6
7 changed files with 245 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.generators.IslandWorld; import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener; import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.NetherPortals; import us.tastybento.bskyblock.listeners.NetherPortals;
import us.tastybento.bskyblock.listeners.PanelListener;
import us.tastybento.bskyblock.listeners.protection.IslandGuard; import us.tastybento.bskyblock.listeners.protection.IslandGuard;
import us.tastybento.bskyblock.listeners.protection.IslandGuard1_8; import us.tastybento.bskyblock.listeners.protection.IslandGuard1_8;
import us.tastybento.bskyblock.listeners.protection.IslandGuard1_9; import us.tastybento.bskyblock.listeners.protection.IslandGuard1_9;
@ -34,7 +35,7 @@ import us.tastybento.bskyblock.util.VaultHelper;
* @author Tastybento * @author Tastybento
* @author Poslovitch * @author Poslovitch
*/ */
public class BSkyBlock extends JavaPlugin{ public class BSkyBlock extends JavaPlugin {
private static BSkyBlock plugin; private static BSkyBlock plugin;
@ -168,6 +169,7 @@ public class BSkyBlock extends JavaPlugin{
manager.registerEvents(new IslandGuard(this), this); manager.registerEvents(new IslandGuard(this), this);
manager.registerEvents(new IslandGuard1_8(this), this); manager.registerEvents(new IslandGuard1_8(this), this);
manager.registerEvents(new IslandGuard1_9(this), this); manager.registerEvents(new IslandGuard1_9(this), this);
manager.registerEvents(new PanelListener(this), this);
} }
@Override @Override

View File

@ -0,0 +1,6 @@
package us.tastybento.bskyblock.api.panels;
public enum ClickType {
RIGHT,
LEFT
}

View File

@ -0,0 +1,47 @@
package us.tastybento.bskyblock.api.panels;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import us.tastybento.bskyblock.listeners.PanelListener;
import java.util.Map;
public class Panel {
private Inventory inventory;
private Map<Integer, PanelItem> items;
public Panel(String name, Map<Integer, PanelItem> items) {
this.items = items;
// Create panel
if (items.keySet().size() > 0) {
// Make sure size is a multiple of 9
int size = items.keySet().size() + 8;
size -= (size % 9);
inventory = Bukkit.createInventory(null, size, name);
// Fill the inventory and return
for (Map.Entry<Integer, PanelItem> en: items.entrySet()) {
inventory.setItem(en.getKey(), en.getValue().getItem());
}
} else {
inventory = Bukkit.createInventory(null, 9, name);
}
}
public Inventory getInventory() {
return inventory;
}
public Map<Integer, PanelItem> getItems() {
return items;
}
public void open(Player... players) {
for (Player player : players) {
player.openInventory(inventory);
PanelListener.openPanels.put(player.getUniqueId(), this);
}
}
}

View File

@ -0,0 +1,60 @@
package us.tastybento.bskyblock.api.panels;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
public class PanelItem {
private ItemStack item;
private ClickHandler clickHandler;
public PanelItem(ItemStack icon, String name, String description, boolean glow, ClickHandler clickHandler) {
// Create the final item
ItemMeta meta = icon.getItemMeta();
meta.setDisplayName(name);
meta.setLore(Arrays.asList(description.split("\\n"))); // the \n is automatically generated by the YAML when getting a multi-line value.
// Set flags to neaten up the view
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
if (glow) {
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
}
icon.setItemMeta(meta);
// Assign the values
this.item = icon;
this.clickHandler = clickHandler;
// if clickHandler is null, set a default one which will prevent picking up the item from the Panel
if (this.clickHandler == null) {
this.clickHandler = new ClickHandler() {
@Override
public boolean onClick(Player player, ClickType click) {
return false;
}
};
}
}
public ItemStack getItem() {
return item;
}
public ClickHandler getClickHandler() {
return clickHandler;
}
public interface ClickHandler {
boolean onClick(Player player, ClickType click);
}
}

View File

@ -0,0 +1,26 @@
package us.tastybento.bskyblock.api.panels.builders;
import us.tastybento.bskyblock.api.panels.Panel;
import us.tastybento.bskyblock.api.panels.PanelItem;
import java.util.HashMap;
import java.util.Map;
public class PanelBuilder {
private String name;
private Map<Integer, PanelItem> items = new HashMap<>();
public PanelBuilder setName(String name) {
this.name = name;
return this;
}
public PanelBuilder addItem(int slot, PanelItem item) {
this.items.put(slot, item);
return this;
}
public Panel build() {
return new Panel(name, items);
}
}

View File

@ -0,0 +1,41 @@
package us.tastybento.bskyblock.api.panels.builders;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.api.panels.PanelItem;
public class PanelItemBuilder {
private ItemStack icon;
private String name;
private String description;
private boolean glow;
private PanelItem.ClickHandler clickHandler;
public PanelItemBuilder setIcon(ItemStack icon) {
this.icon = icon;
return this;
}
public PanelItemBuilder setName(String name) {
this.name = name;
return this;
}
public PanelItemBuilder setDescription(String description) {
this.description = description;
return this;
}
public PanelItemBuilder setGlow(boolean glow) {
this.glow = glow;
return this;
}
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
this.clickHandler = clickHandler;
return this;
}
public PanelItem build() {
return new PanelItem(icon, name, description, glow, clickHandler);
}
}

View File

@ -0,0 +1,62 @@
package us.tastybento.bskyblock.listeners;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.panels.ClickType;
import us.tastybento.bskyblock.api.panels.Panel;
import us.tastybento.bskyblock.api.panels.PanelItem;
import java.util.HashMap;
import java.util.UUID;
public class PanelListener implements Listener {
private static final boolean DEBUG = false;
private BSkyBlock plugin;
public static HashMap<UUID, Panel> openPanels = new HashMap<>();
public PanelListener(BSkyBlock plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked(); // The player that
// clicked the item
//UUID playerUUID = player.getUniqueId();
Inventory inventory = event.getInventory(); // The inventory that was
if (openPanels.containsKey(player.getUniqueId())) {
if (inventory.getName().equals(openPanels.get(player.getUniqueId()).getInventory().getName())) {
Panel panel = openPanels.get(player.getUniqueId());
for (int slot : panel.getItems().keySet()) {
if (slot == event.getRawSlot()) {
if(!panel.getItems().get(slot).getClickHandler().onClick(player, ClickType.LEFT)) {
event.setCancelled(true);
}
}
}
}
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInventoryClose(InventoryCloseEvent event) {
if (openPanels.containsKey(event.getPlayer().getUniqueId())) openPanels.remove(event.getPlayer().getUniqueId());
}
@EventHandler(priority = EventPriority.NORMAL)
public void onLogOut(PlayerQuitEvent event) {
if (openPanels.containsKey(event.getPlayer().getUniqueId())) openPanels.remove(event.getPlayer().getUniqueId());
}
}