MAJOR changes to Panel API

All methods in PanelBuilder follow the Builder pattern (no more "set...")
nextSlot() now returns the first unoccupied slot
This commit is contained in:
Florian CUNY 2018-03-01 11:02:11 +01:00
parent f648c936ad
commit c60f92b7b3
3 changed files with 48 additions and 51 deletions

View File

@ -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;
}
}

View File

@ -189,7 +189,6 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
}
/**
*
* Creates a comma-separated-String with the names of the variables in this
* class
* Not used in Flat File database.

View File

@ -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);