Added more around the Panel API.

This commit is contained in:
Tastybento 2017-12-30 17:13:20 -08:00
parent 1f57e1eb0b
commit 1f92c2b60e
3 changed files with 81 additions and 40 deletions

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.api.panels;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
@ -10,51 +11,86 @@ import org.bukkit.inventory.meta.ItemMeta;
public class PanelItem {
private ItemStack item;
private ClickHandler clickHandler;
private ItemStack icon;
private Optional<ClickHandler> clickHandler;
private List<String> description;
private String name;
private boolean glow;
private ItemMeta meta;
public PanelItem(ItemStack icon, String name, List<String> description, boolean glow, Optional<ClickHandler> clickHandler) {
this.icon = icon;
// Get the meta
meta = icon.getItemMeta();
this.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.
this.setName(name);
this.setDescription(description);
this.setGlow(glow);
// 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;
return icon;
}
public ClickHandler getClickHandler() {
public List<String> getDescription() {
return description;
}
public void setDescription(List<String> description) {
this.description = description;
ItemMeta meta = icon.getItemMeta();
meta.setLore(description);
icon.setItemMeta(meta);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
meta.setDisplayName(name);
icon.setItemMeta(meta);
}
public Optional<ClickHandler> getClickHandler() {
return clickHandler;
}
public boolean isGlow() {
return glow;
}
public void setGlow(boolean glow) {
this.glow = glow;
if (glow)
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
else
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
}
/**
* Click handler interface
*
*/
public interface ClickHandler {
boolean onClick(Player player, ClickType click);
/**
* This is executed when the icon is clicked
* @param player
* @param click
* @return false if the event should be cancelled
*/
public boolean onClick(Player player, ClickType click);
}
}

View File

@ -1,5 +1,8 @@
package us.tastybento.bskyblock.api.panels.builders;
import java.util.List;
import java.util.Optional;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.api.panels.PanelItem;
@ -7,9 +10,9 @@ import us.tastybento.bskyblock.api.panels.PanelItem;
public class PanelItemBuilder {
private ItemStack icon;
private String name;
private String description;
private List<String> description;
private boolean glow;
private PanelItem.ClickHandler clickHandler;
private Optional<PanelItem.ClickHandler> clickHandler = Optional.empty();
public PanelItemBuilder setIcon(ItemStack icon) {
this.icon = icon;
@ -21,8 +24,8 @@ public class PanelItemBuilder {
return this;
}
public PanelItemBuilder setDescription(String description) {
this.description = description;
public PanelItemBuilder setDescription(List<String> list) {
this.description = list;
return this;
}
@ -32,7 +35,7 @@ public class PanelItemBuilder {
}
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
this.clickHandler = clickHandler;
this.clickHandler = Optional.of(clickHandler);
return this;
}

View File

@ -38,12 +38,14 @@ public class PanelListenerManager implements Listener {
if (slot == event.getRawSlot()) {
// Check that they left clicked on it
// TODO: in the future, we may want to support right clicking
if(!panel.getItems().get(slot).getClickHandler().onClick(player.getPlayer(), ClickType.LEFT)) {
event.setCancelled(true);
} else {
// If there is a listener, then run it.
if (panel.getListener().isPresent()) {
panel.getListener().get().onInventoryClick(player, inventory, event.getCurrentItem());
if (panel.getItems().get(slot).getClickHandler().isPresent()) {
if(!panel.getItems().get(slot).getClickHandler().get().onClick(player.getPlayer(), ClickType.LEFT)) {
event.setCancelled(true);
} else {
// If there is a listener, then run it.
if (panel.getListener().isPresent()) {
panel.getListener().get().onInventoryClick(player, inventory, event.getCurrentItem());
}
}
}
}