Improved the panel API.

I'm working on addons and realized it would be a better approach to be
able to register the listener for a panel explicitly for each panel.
This is optional. Also, added the ability to open the panel immediately
for a player.
This commit is contained in:
Tastybento 2017-12-29 09:11:14 -08:00
parent a6f112a93f
commit 4289a24c56
6 changed files with 145 additions and 23 deletions

View File

@ -12,7 +12,6 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<server.jars>${project.basedir}/lib</server.jars>
</properties>
<build>
<defaultGoal>clean package install</defaultGoal>

View File

@ -17,7 +17,7 @@ import us.tastybento.bskyblock.database.managers.PlayersManager;
import us.tastybento.bskyblock.database.managers.island.IslandsManager;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.PanelListener;
import us.tastybento.bskyblock.listeners.PanelListenerManager;
import us.tastybento.bskyblock.managers.AddonsManager;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
@ -137,7 +137,7 @@ public class BSkyBlock extends JavaPlugin implements BSBModule {
PluginManager manager = getServer().getPluginManager();
// Player join events
manager.registerEvents(new JoinLeaveListener(this), this);
manager.registerEvents(new PanelListener(this), this);
manager.registerEvents(new PanelListenerManager(), this);
}
@Override

View File

@ -1,26 +1,31 @@
package us.tastybento.bskyblock.api.panels;
import java.util.Map;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.listeners.PanelListener;
import us.tastybento.bskyblock.listeners.PanelListenerManager;
public class Panel {
private Inventory inventory;
private Map<Integer, PanelItem> items;
private Optional<PanelListener> listener;
private Optional<User> user;
public Panel(String name, Map<Integer, PanelItem> items) {
public Panel(String name, Map<Integer, PanelItem> items, int size, Optional<User> user, Optional<PanelListener> listener) {
this.items = items;
if (size != 0) {
size = items.keySet().size();
}
// Create panel
if (items.keySet().size() > 0) {
// Make sure size is a multiple of 9
int size = items.keySet().size() + 8;
size = size + 8;
size -= (size % 9);
inventory = Bukkit.createInventory(null, size, name);
// Fill the inventory and return
@ -30,6 +35,16 @@ public class Panel {
} else {
inventory = Bukkit.createInventory(null, 9, name);
}
this.listener = listener;
// If the listener is defined, then run setup
if (listener.isPresent()) {
listener.get().setup();
}
// If the user is defined, then open panel immediately
this.user = user;
if (user.isPresent()) {
user.get().getPlayer().openInventory(inventory);
}
}
public Inventory getInventory() {
@ -40,10 +55,21 @@ public class Panel {
return items;
}
/**
* @return the listener
*/
public Optional<PanelListener> getListener() {
return listener;
}
public Optional<User> getUser() {
return user;
}
public void open(Player... players) {
for (Player player : players) {
player.openInventory(inventory);
PanelListener.openPanels.put(player.getUniqueId(), this);
PanelListenerManager.openPanels.put(player.getUniqueId(), this);
}
}
@ -54,7 +80,35 @@ public class Panel {
public void open(User... users) {
for (User user : users) {
user.getPlayer().openInventory(inventory);
PanelListener.openPanels.put(user.getUniqueId(), this);
PanelListenerManager.openPanels.put(user.getUniqueId(), this);
}
}
/**
* @param inventory the inventory to set
*/
public void setInventory(Inventory inventory) {
this.inventory = inventory;
}
/**
* @param items the items to set
*/
public void setItems(Map<Integer, PanelItem> items) {
this.items = items;
}
/**
* @param listener the listener to set
*/
public void setListener(Optional<PanelListener> listener) {
this.listener = listener;
}
/**
* @param user the user to set
*/
public void setUser(Optional<User> user) {
this.user = user;
}
}

View File

@ -0,0 +1,22 @@
package us.tastybento.bskyblock.api.panels;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import us.tastybento.bskyblock.api.commands.User;
public interface PanelListener {
/**
* This is called when the panel is first setup
*/
public void setup();
/**
* Called when the panel is clicked
* @param user
* @param inventory
* @param clicked
*/
public void onInventoryClick(User user, Inventory inventory, ItemStack clicked);
}

View File

@ -1,13 +1,19 @@
package us.tastybento.bskyblock.api.panels.builders;
import java.util.Optional;
import java.util.TreeMap;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.Panel;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.panels.PanelListener;
public class PanelBuilder {
private String name;
private TreeMap<Integer, PanelItem> items = new TreeMap<>();
private int size;
private Optional<User> user;
private Optional<PanelListener> listener;
public PanelBuilder setName(String name) {
this.name = name;
@ -41,8 +47,13 @@ public class PanelBuilder {
public boolean slotOccupied(int slot) {
return this.items.containsKey(slot);
}
/**
* Build the panel
* @return Panel
*/
public Panel build() {
return new Panel(name, items);
return new Panel(name, items, size, user, listener);
}
/**
@ -58,4 +69,34 @@ public class PanelBuilder {
}
return this;
}
/**
* Forces panel to be a specific number of slots.
* @param size
* @return PanelBuilder
*/
public PanelBuilder setSize(int size) {
this.size = size;
return this;
}
/**
* Sets the user who will get this panel. This will open it immediately when it is built
* @param user
* @return PanelBuilder
*/
public PanelBuilder setUser(User user) {
this.user = Optional.of(user);
return this;
}
/**
* Sets which PanelListener will listen for clicks
* @param listener
* @return PanelBuilder
*/
public PanelBuilder setListener(PanelListener listener) {
this.listener = Optional.of(listener);
return this;
}
}

View File

@ -3,7 +3,6 @@ package us.tastybento.bskyblock.listeners;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -12,40 +11,47 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.commands.User;
import us.tastybento.bskyblock.api.panels.ClickType;
import us.tastybento.bskyblock.api.panels.Panel;
public class PanelListener implements Listener {
public class PanelListenerManager implements Listener {
private static final boolean DEBUG = false;
private BSkyBlock plugin;
//private static final boolean DEBUG = false;
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
User player = 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())) {
// Check the name of the panel
if (inventory.getName().equals(openPanels.get(player.getUniqueId()).getInventory().getName())) {
// Get the panel itself
Panel panel = openPanels.get(player.getUniqueId());
// Check that they clicked on a specific item
for (int slot : panel.getItems().keySet()) {
if (slot == event.getRawSlot()) {
if(!panel.getItems().get(slot).getClickHandler().onClick(player, ClickType.LEFT)) {
// 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());
}
}
}
}
} else {
// Wrong name - delete this panel
openPanels.remove(player.getUniqueId());
}
}
}