mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-10-04 09:37:32 +02:00
Panel additions or changes.
By working on the Warp addon I made changes to the Panel API.
This commit is contained in:
parent
06dec42029
commit
109c0edca3
@ -13,7 +13,6 @@ import org.bukkit.Server;
|
|||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
|
|
||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
|
|
||||||
|
@ -19,11 +19,12 @@ public class Panel {
|
|||||||
|
|
||||||
public Panel(String name, Map<Integer, PanelItem> items, int size, Optional<User> user, Optional<PanelListener> listener) {
|
public Panel(String name, Map<Integer, PanelItem> items, int size, Optional<User> user, Optional<PanelListener> listener) {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
if (size != 0) {
|
// If size is undefined (0) then use the number of items
|
||||||
|
if (size == 0) {
|
||||||
size = items.keySet().size();
|
size = items.keySet().size();
|
||||||
}
|
}
|
||||||
// Create panel
|
// Create panel
|
||||||
if (items.keySet().size() > 0) {
|
if (size > 0) {
|
||||||
// Make sure size is a multiple of 9
|
// Make sure size is a multiple of 9
|
||||||
size = size + 8;
|
size = size + 8;
|
||||||
size -= (size % 9);
|
size -= (size % 9);
|
||||||
@ -43,6 +44,8 @@ public class Panel {
|
|||||||
// If the user is defined, then open panel immediately
|
// If the user is defined, then open panel immediately
|
||||||
this.user = user;
|
this.user = user;
|
||||||
if (user.isPresent()) {
|
if (user.isPresent()) {
|
||||||
|
// Register panel.
|
||||||
|
PanelListenerManager.openPanels.put(user.get().getUniqueId(), this);
|
||||||
user.get().getPlayer().openInventory(inventory);
|
user.get().getPlayer().openInventory(inventory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,12 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.inventory.ItemFlag;
|
import org.bukkit.inventory.ItemFlag;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
||||||
|
import us.tastybento.bskyblock.api.commands.User;
|
||||||
|
|
||||||
public class PanelItem {
|
public class PanelItem {
|
||||||
|
|
||||||
private ItemStack icon;
|
private ItemStack icon;
|
||||||
@ -77,7 +78,7 @@ public class PanelItem {
|
|||||||
if (glow)
|
if (glow)
|
||||||
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
|
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
|
||||||
else
|
else
|
||||||
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
|
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,10 +88,10 @@ public class PanelItem {
|
|||||||
public interface ClickHandler {
|
public interface ClickHandler {
|
||||||
/**
|
/**
|
||||||
* This is executed when the icon is clicked
|
* This is executed when the icon is clicked
|
||||||
* @param player
|
* @param user
|
||||||
* @param click
|
* @param click
|
||||||
* @return false if the event should be cancelled
|
* @return false if the event should be cancelled
|
||||||
*/
|
*/
|
||||||
public boolean onClick(Player player, ClickType click);
|
public boolean onClick(User user, ClickType click);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ public class PanelBuilder {
|
|||||||
private String name;
|
private String name;
|
||||||
private TreeMap<Integer, PanelItem> items = new TreeMap<>();
|
private TreeMap<Integer, PanelItem> items = new TreeMap<>();
|
||||||
private int size;
|
private int size;
|
||||||
private Optional<User> user;
|
private Optional<User> user = Optional.empty();
|
||||||
private Optional<PanelListener> listener;
|
private Optional<PanelListener> listener = Optional.empty();
|
||||||
|
|
||||||
public PanelBuilder setName(String name) {
|
public PanelBuilder setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -6,6 +6,7 @@ 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;
|
||||||
|
import us.tastybento.bskyblock.api.panels.PanelItem.ClickHandler;
|
||||||
|
|
||||||
public class PanelItemBuilder {
|
public class PanelItemBuilder {
|
||||||
private ItemStack icon;
|
private ItemStack icon;
|
||||||
@ -34,7 +35,7 @@ public class PanelItemBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
|
public PanelItemBuilder setClickHandler(ClickHandler clickHandler) {
|
||||||
this.clickHandler = Optional.of(clickHandler);
|
this.clickHandler = Optional.of(clickHandler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -42,4 +43,5 @@ public class PanelItemBuilder {
|
|||||||
public PanelItem build() {
|
public PanelItem build() {
|
||||||
return new PanelItem(icon, name, description, glow, clickHandler);
|
return new PanelItem(icon, name, description, glow, clickHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,28 +23,28 @@ public class PanelListenerManager implements Listener {
|
|||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
User player = User.getInstance(event.getWhoClicked()); // The player that
|
User user = User.getInstance(event.getWhoClicked()); // The player that
|
||||||
// clicked the item
|
// clicked the item
|
||||||
//UUID playerUUID = player.getUniqueId();
|
//UUID playerUUID = player.getUniqueId();
|
||||||
Inventory inventory = event.getInventory(); // The inventory that was
|
Inventory inventory = event.getInventory(); // The inventory that was
|
||||||
// Open the inventory panel that this player has open (they can only ever have one)
|
// Open the inventory panel that this player has open (they can only ever have one)
|
||||||
if (openPanels.containsKey(player.getUniqueId())) {
|
if (openPanels.containsKey(user.getUniqueId())) {
|
||||||
// Check the name of the panel
|
// Check the name of the panel
|
||||||
if (inventory.getName().equals(openPanels.get(player.getUniqueId()).getInventory().getName())) {
|
if (inventory.getName().equals(openPanels.get(user.getUniqueId()).getInventory().getName())) {
|
||||||
// Get the panel itself
|
// Get the panel itself
|
||||||
Panel panel = openPanels.get(player.getUniqueId());
|
Panel panel = openPanels.get(user.getUniqueId());
|
||||||
// Check that they clicked on a specific item
|
// Check that they clicked on a specific item
|
||||||
for (int slot : panel.getItems().keySet()) {
|
for (int slot : panel.getItems().keySet()) {
|
||||||
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().isPresent()) {
|
if (panel.getItems().get(slot).getClickHandler().isPresent()) {
|
||||||
if(!panel.getItems().get(slot).getClickHandler().get().onClick(player.getPlayer(), ClickType.LEFT)) {
|
if(!panel.getItems().get(slot).getClickHandler().get().onClick(user, ClickType.LEFT)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
} else {
|
} else {
|
||||||
// If there is a listener, then run it.
|
// If there is a listener, then run it.
|
||||||
if (panel.getListener().isPresent()) {
|
if (panel.getListener().isPresent()) {
|
||||||
panel.getListener().get().onInventoryClick(player, inventory, event.getCurrentItem());
|
panel.getListener().get().onInventoryClick(user, inventory, event.getCurrentItem());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ public class PanelListenerManager implements Listener {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Wrong name - delete this panel
|
// Wrong name - delete this panel
|
||||||
openPanels.remove(player.getUniqueId());
|
openPanels.remove(user.getUniqueId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,4 +66,5 @@ public class PanelListenerManager implements Listener {
|
|||||||
public void onLogOut(PlayerQuitEvent event) {
|
public void onLogOut(PlayerQuitEvent event) {
|
||||||
if (openPanels.containsKey(event.getPlayer().getUniqueId())) openPanels.remove(event.getPlayer().getUniqueId());
|
if (openPanels.containsKey(event.getPlayer().getUniqueId())) openPanels.remove(event.getPlayer().getUniqueId());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user