Panel additions or changes.

By working on the Warp addon I made changes to the Panel API.
This commit is contained in:
Tastybento 2017-12-30 21:14:58 -08:00
parent 06dec42029
commit 109c0edca3
6 changed files with 24 additions and 18 deletions

View File

@ -13,7 +13,6 @@ import org.bukkit.Server;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import us.tastybento.bskyblock.BSkyBlock;

View File

@ -19,11 +19,12 @@ public class Panel {
public Panel(String name, Map<Integer, PanelItem> items, int size, Optional<User> user, Optional<PanelListener> listener) {
this.items = items;
if (size != 0) {
// If size is undefined (0) then use the number of items
if (size == 0) {
size = items.keySet().size();
}
}
// Create panel
if (items.keySet().size() > 0) {
if (size > 0) {
// Make sure size is a multiple of 9
size = size + 8;
size -= (size % 9);
@ -43,6 +44,8 @@ public class Panel {
// If the user is defined, then open panel immediately
this.user = user;
if (user.isPresent()) {
// Register panel.
PanelListenerManager.openPanels.put(user.get().getUniqueId(), this);
user.get().getPlayer().openInventory(inventory);
}
}

View File

@ -4,11 +4,12 @@ import java.util.List;
import java.util.Optional;
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 us.tastybento.bskyblock.api.commands.User;
public class PanelItem {
private ItemStack icon;
@ -77,7 +78,7 @@ public class PanelItem {
if (glow)
meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true);
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 {
/**
* This is executed when the icon is clicked
* @param player
* @param user
* @param click
* @return false if the event should be cancelled
*/
public boolean onClick(Player player, ClickType click);
public boolean onClick(User user, ClickType click);
}
}

View File

@ -12,8 +12,8 @@ public class PanelBuilder {
private String name;
private TreeMap<Integer, PanelItem> items = new TreeMap<>();
private int size;
private Optional<User> user;
private Optional<PanelListener> listener;
private Optional<User> user = Optional.empty();
private Optional<PanelListener> listener = Optional.empty();
public PanelBuilder setName(String name) {
this.name = name;

View File

@ -6,6 +6,7 @@ import java.util.Optional;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.panels.PanelItem.ClickHandler;
public class PanelItemBuilder {
private ItemStack icon;
@ -34,7 +35,7 @@ public class PanelItemBuilder {
return this;
}
public PanelItemBuilder setClickHandler(PanelItem.ClickHandler clickHandler) {
public PanelItemBuilder setClickHandler(ClickHandler clickHandler) {
this.clickHandler = Optional.of(clickHandler);
return this;
}
@ -42,4 +43,5 @@ public class PanelItemBuilder {
public PanelItem build() {
return new PanelItem(icon, name, description, glow, clickHandler);
}
}

View File

@ -23,28 +23,28 @@ public class PanelListenerManager implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
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
//UUID playerUUID = player.getUniqueId();
Inventory inventory = event.getInventory(); // The inventory that was
// 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
if (inventory.getName().equals(openPanels.get(player.getUniqueId()).getInventory().getName())) {
if (inventory.getName().equals(openPanels.get(user.getUniqueId()).getInventory().getName())) {
// Get the panel itself
Panel panel = openPanels.get(player.getUniqueId());
Panel panel = openPanels.get(user.getUniqueId());
// Check that they clicked on a specific item
for (int slot : panel.getItems().keySet()) {
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().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);
} else {
// If there is a listener, then run it.
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 {
// 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) {
if (openPanels.containsKey(event.getPlayer().getUniqueId())) openPanels.remove(event.getPlayer().getUniqueId());
}
}