From c60f92b7b33a1568062e76f6e1da94e335daf6db Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Thu, 1 Mar 2018 11:02:11 +0100 Subject: [PATCH] MAJOR changes to Panel API All methods in PanelBuilder follow the Builder pattern (no more "set...") nextSlot() now returns the first unoccupied slot --- .../api/panels/builders/PanelBuilder.java | 92 +++++++++---------- .../database/mysql/MySQLDatabaseHandler.java | 1 - .../bskyblock/panels/SettingsPanel.java | 6 +- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/panels/builders/PanelBuilder.java b/src/main/java/us/tastybento/bskyblock/api/panels/builders/PanelBuilder.java index 6c500ea88..249e2dfb7 100644 --- a/src/main/java/us/tastybento/bskyblock/api/panels/builders/PanelBuilder.java +++ b/src/main/java/us/tastybento/bskyblock/api/panels/builders/PanelBuilder.java @@ -14,28 +14,70 @@ public class PanelBuilder { private User user; private PanelListener listener; - public PanelBuilder setName(String name) { + public PanelBuilder name(String name) { this.name = name; return this; } + /** + * Add item to the panel in the last slot. + * @param item - Panel item + * @return PanelBuilder + */ + public PanelBuilder item(PanelItem item) { + return item(nextSlot(), item); + } + /** * Add item into a specific slot. If it is already occupied, it will be replaced. * @param slot - slot * @param item - Panel item * @return PanelBuilder */ - public PanelBuilder addItem(int slot, PanelItem item) { + public PanelBuilder item(int slot, PanelItem item) { items.put(slot, item); return this; } + /** + * Forces panel to be a specific number of slots. + * @param size + * @return PanelBuilder + */ + public PanelBuilder size(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 - the User + * @return PanelBuilder + */ + public PanelBuilder user(User user) { + this.user = user; + return this; + } + + /** + * Sets which PanelListener will listen for clicks + * @param listener + * @return PanelBuilder + */ + public PanelBuilder listener(PanelListener listener) { + this.listener = listener; + return this; + } + public int nextSlot() { if (items.isEmpty()) { return 0; } else { - return items.lastEntry().getKey() + 1; + for (int i = 0; i < 54; i++) { + if (!slotOccupied(i)) return i; + } } + return 0; } /** @@ -54,48 +96,4 @@ public class PanelBuilder { public Panel build() { return new Panel(name, items, size, user, listener); } - - /** - * Add item to the panel in the last slot. - * @param item - Panel item - * @return PanelBuilder - */ - public PanelBuilder addItem(PanelItem item) { - if (items.isEmpty()) { - items.put(0, item); - } else { - items.put(items.lastEntry().getKey() + 1, item); - } - 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 - the User - * @return PanelBuilder - */ - public PanelBuilder setUser(User user) { - this.user = user; - return this; - } - - /** - * Sets which PanelListener will listen for clicks - * @param listener - * @return PanelBuilder - */ - public PanelBuilder setListener(PanelListener listener) { - this.listener = listener; - return this; - } } diff --git a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java index 47f1e08cf..0b54e8e4a 100644 --- a/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/us/tastybento/bskyblock/database/mysql/MySQLDatabaseHandler.java @@ -189,7 +189,6 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { } /** - * * Creates a comma-separated-String with the names of the variables in this * class * Not used in Flat File database. diff --git a/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java b/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java index 66dc704f8..bfaed4b99 100644 --- a/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java +++ b/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java @@ -18,18 +18,18 @@ public class SettingsPanel { */ public static void openPanel(User user) { PanelBuilder panelBuilder = new PanelBuilder() - .setName(user.getTranslation("protection.panel.title")); + .name(user.getTranslation("protection.panel.title")); PanelItem help = new PanelItemBuilder() .name(user.getTranslation("protection.panel.help-item.name")) .icon("crashdummie99") // Question marks .build(); - panelBuilder.addItem(8, help); + panelBuilder.item(8, help); for (Flag flag : BSkyBlock.getInstance().getFlagsManager().getFlags()) { PanelItem flagIcon = flag.toPanelItem(user); - panelBuilder.addItem(flagIcon); + panelBuilder.item(flagIcon); } panelBuilder.build().open(user);