mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-03 06:57:41 +01:00
Added more around the Panel API.
This commit is contained in:
parent
1f57e1eb0b
commit
1f92c2b60e
@ -1,6 +1,7 @@
|
|||||||
package us.tastybento.bskyblock.api.panels;
|
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.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -10,51 +11,86 @@ import org.bukkit.inventory.meta.ItemMeta;
|
|||||||
|
|
||||||
public class PanelItem {
|
public class PanelItem {
|
||||||
|
|
||||||
private ItemStack item;
|
private ItemStack icon;
|
||||||
private ClickHandler clickHandler;
|
private Optional<ClickHandler> clickHandler;
|
||||||
|
private List<String> description;
|
||||||
|
private String name;
|
||||||
|
private boolean glow;
|
||||||
|
private ItemMeta meta;
|
||||||
|
|
||||||
public PanelItem(ItemStack icon, String name, String description, boolean glow, ClickHandler clickHandler) {
|
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;
|
||||||
|
|
||||||
// Create the final item
|
// Create the final item
|
||||||
ItemMeta meta = icon.getItemMeta();
|
this.setName(name);
|
||||||
meta.setDisplayName(name);
|
this.setDescription(description);
|
||||||
meta.setLore(Arrays.asList(description.split("\\n"))); // the \n is automatically generated by the YAML when getting a multi-line value.
|
this.setGlow(glow);
|
||||||
|
|
||||||
// Set flags to neaten up the view
|
// Set flags to neaten up the view
|
||||||
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
|
||||||
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
|
meta.addItemFlags(ItemFlag.HIDE_DESTROYS);
|
||||||
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
|
meta.addItemFlags(ItemFlag.HIDE_PLACED_ON);
|
||||||
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
|
||||||
|
|
||||||
if (glow) {
|
|
||||||
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
icon.setItemMeta(meta);
|
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() {
|
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;
|
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 {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
package us.tastybento.bskyblock.api.panels.builders;
|
package us.tastybento.bskyblock.api.panels.builders;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import us.tastybento.bskyblock.api.panels.PanelItem;
|
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||||
@ -7,9 +10,9 @@ import us.tastybento.bskyblock.api.panels.PanelItem;
|
|||||||
public class PanelItemBuilder {
|
public class PanelItemBuilder {
|
||||||
private ItemStack icon;
|
private ItemStack icon;
|
||||||
private String name;
|
private String name;
|
||||||
private String description;
|
private List<String> description;
|
||||||
private boolean glow;
|
private boolean glow;
|
||||||
private PanelItem.ClickHandler clickHandler;
|
private Optional<PanelItem.ClickHandler> clickHandler = Optional.empty();
|
||||||
|
|
||||||
public PanelItemBuilder setIcon(ItemStack icon) {
|
public PanelItemBuilder setIcon(ItemStack icon) {
|
||||||
this.icon = icon;
|
this.icon = icon;
|
||||||
@ -21,8 +24,8 @@ public class PanelItemBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PanelItemBuilder setDescription(String description) {
|
public PanelItemBuilder setDescription(List<String> list) {
|
||||||
this.description = description;
|
this.description = list;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +35,7 @@ public class PanelItemBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
|
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
|
||||||
this.clickHandler = clickHandler;
|
this.clickHandler = Optional.of(clickHandler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,12 +38,14 @@ public class PanelListenerManager implements Listener {
|
|||||||
if (slot == event.getRawSlot()) {
|
if (slot == event.getRawSlot()) {
|
||||||
// Check that they left clicked on it
|
// Check that they left clicked on it
|
||||||
// TODO: in the future, we may want to support right clicking
|
// TODO: in the future, we may want to support right clicking
|
||||||
if(!panel.getItems().get(slot).getClickHandler().onClick(player.getPlayer(), ClickType.LEFT)) {
|
if (panel.getItems().get(slot).getClickHandler().isPresent()) {
|
||||||
event.setCancelled(true);
|
if(!panel.getItems().get(slot).getClickHandler().get().onClick(player.getPlayer(), ClickType.LEFT)) {
|
||||||
} else {
|
event.setCancelled(true);
|
||||||
// If there is a listener, then run it.
|
} else {
|
||||||
if (panel.getListener().isPresent()) {
|
// If there is a listener, then run it.
|
||||||
panel.getListener().get().onInventoryClick(player, inventory, event.getCurrentItem());
|
if (panel.getListener().isPresent()) {
|
||||||
|
panel.getListener().get().onInventoryClick(player, inventory, event.getCurrentItem());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user