add a user-customizable gui

This commit is contained in:
jascotty2 2019-09-06 13:37:30 -05:00
parent 2488e97ba6
commit 72187d9efe
4 changed files with 536 additions and 80 deletions

View File

@ -2,6 +2,7 @@ package com.songoda.core.configuration;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
@ -18,17 +19,16 @@ import org.jetbrains.annotations.Nullable;
/**
* Used to easily store a set of one data value
*
* @param <K> Key value that DataObject class uses to uniquely identify values
* @param <T> DataObject class that is used to store the data
* @since 2019-09-06
* @author jascotty2
*/
public class SimpleDataStore<K, T extends DataStoreObject<K>> {
public class SimpleDataStore<T extends DataStoreObject> {
protected final Plugin plugin;
protected final String filename, dirName;
private final Function<ConfigurationSection, T> getFromSection;
protected final HashMap<K, T> data = new HashMap();
protected final HashMap<Object, T> data = new HashMap();
private File file;
private final Object lock = new Object();
SaveTask saveTask;
@ -38,7 +38,7 @@ public class SimpleDataStore<K, T extends DataStoreObject<K>> {
*/
int autosaveInterval = 60;
public SimpleDataStore(@NotNull Plugin plugin, @NotNull String filename, Function<ConfigurationSection, T> loadFunction) {
public SimpleDataStore(@NotNull Plugin plugin, @NotNull String filename, @NotNull Function<ConfigurationSection, T> loadFunction) {
this.plugin = plugin;
this.filename = filename;
dirName = null;
@ -65,22 +65,23 @@ public class SimpleDataStore<K, T extends DataStoreObject<K>> {
}
/**
* @return a directly-modifiable instance of the data mapping for this storage
* @return a directly-modifiable instance of the data mapping for this
* storage
*/
public Map<K, T> getData() {
public Map<Object, T> getData() {
return data;
}
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
* Returns the value to which the specified key is mapped, or {@code null}
* if this map contains no mapping for the key.
*
* @param key key whose mapping is to be retrieved from this storage
* @return the value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
*/
@Nullable
public T get(K key) {
public T get(Object key) {
return data.get(key);
}
@ -92,7 +93,7 @@ public class SimpleDataStore<K, T extends DataStoreObject<K>> {
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
*/
@Nullable
public T remove(@NotNull K key) {
public T remove(@NotNull Object key) {
T temp;
synchronized (lock) {
temp = data.remove(key);
@ -142,6 +143,48 @@ public class SimpleDataStore<K, T extends DataStoreObject<K>> {
return temp;
}
/**
* Adds the specified value in this storage. If the map previously contained
* a mapping for the key, the old value is replaced.
*
* @param value values to be added
*/
@Nullable
public void addAll(@NotNull T[] value) {
if (value == null) {
return;
}
synchronized (lock) {
for (int i = 0; i < value.length; ++i) {
if (value[i] != null) {
data.put(value[i].getKey(), value[i]);
}
}
}
save();
}
/**
* Adds the specified value in this storage. If the map previously contained
* a mapping for the key, the old value is replaced.
*
* @param value values to be added
*/
@Nullable
public void addAll(@NotNull Collection<T> value) {
if (value == null) {
return;
}
synchronized (lock) {
for (T v : value) {
if (v != null) {
data.put(v.getKey(), v);
}
}
}
save();
}
/**
* Load data from the associated file
*/
@ -168,7 +211,8 @@ public class SimpleDataStore<K, T extends DataStoreObject<K>> {
}
/**
* Optionally save this storage's data to file if there have been changes made
* Optionally save this storage's data to file if there have been changes
* made
*/
public void saveChanges() {
if (saveTask != null || data.values().stream().anyMatch(v -> v.hasChanged())) {

View File

@ -0,0 +1,337 @@
package com.songoda.core.gui;
import com.songoda.core.compatibility.LegacyMaterials;
import com.songoda.core.configuration.DataStoreObject;
import com.songoda.core.configuration.SimpleDataStore;
import com.songoda.core.gui.methods.Clickable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Represents a GUI screen that can be user-configured
*
* @since 2019-09-06
* @author jascotty2
*/
public class CustomizableGui extends Gui {
final Map<String, CustomButton> buttons;
public CustomizableGui(SimpleDataStore<CustomButton> buttons) {
this((Map<String, CustomButton>) (Map) buttons.getData());
}
public CustomizableGui(SimpleDataStore<CustomButton> buttons, Gui parent) {
this((Map<String, CustomButton>) (Map) buttons.getData(), parent);
}
public CustomizableGui(@NotNull Map<String, CustomButton> buttons) {
this(buttons, null);
}
public CustomizableGui(@NotNull Map<String, CustomButton> buttons, @Nullable Gui parent) {
super(parent);
this.buttons = buttons;
if (buttons.containsKey("__DEFAULT__")) {
blankItem = GuiUtils.getBorderItem(buttons.get("__DEFAULT__").icon);
}
}
@NotNull
public CustomButton[] getButtons() {
return buttons.values().toArray(new CustomButton[buttons.size()]);
}
@NotNull
@Override
public CustomizableGui setDefaultItem(ItemStack item) {
if ((blankItem = item) != null) {
buttons.put("__DEFAULT__", (new CustomButton("__DEFAULT__")).setIcon(LegacyMaterials.getMaterial(item)));
}
return this;
}
@Nullable
public CustomButton getButton(@NotNull String key) {
return key == null ? null : buttons.get(key.toLowerCase());
}
@NotNull
public CustomizableGui setItem(int defaultRow, int defaultCol, @NotNull String key, @NotNull ItemStack item) {
final int cell = defaultCol + defaultRow * 9;
return setItem(cell, key, item);
}
@NotNull
public CustomizableGui setItem(int defaultCell, @NotNull String key, @NotNull ItemStack item) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn == null) {
buttons.put(key, btn = (new CustomButton(key, defaultCell)).setIcon(LegacyMaterials.getMaterial(item)));
} else {
ItemStack btnItem = btn.icon.getItem();
ItemMeta itemMeta = item.getItemMeta();
ItemMeta btnItemMeta = btnItem.getItemMeta();
if (itemMeta != null && btnItemMeta != null) {
btnItemMeta.setDisplayName(itemMeta.getDisplayName());
btnItemMeta.setLore(itemMeta.getLore());
btnItem.setItemMeta(itemMeta);
}
item = btnItem;
}
cellItems.put(btn.position, item);
if (inventory != null && btn.position >= 0 && btn.position < inventory.getSize()) {
inventory.setItem(btn.position, item);
}
return this;
}
@NotNull
public CustomizableGui setItem(int defaultRow, int defaultCol, @NotNull String key, @NotNull LegacyMaterials defaultItem, @NotNull String title, @NotNull String... lore) {
final int cell = defaultCol + defaultRow * 9;
return setItem(cell, key, defaultItem, title, lore);
}
@NotNull
public CustomizableGui setItem(int defaultCell, @NotNull String key, @NotNull LegacyMaterials defaultItem, @NotNull String title, @NotNull String... lore) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn == null) {
buttons.put(key, btn = (new CustomButton(key, defaultCell)).setIcon(defaultItem));
}
ItemStack item = GuiUtils.createButtonItem(btn.icon, title, lore);
cellItems.put(btn.position, item);
if (inventory != null && btn.position >= 0 && btn.position < inventory.getSize()) {
inventory.setItem(btn.position, item);
}
return this;
}
@NotNull
public CustomizableGui highlightItem(@NotNull String key) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.highlightItem(btn.position);
}
return this;
}
@NotNull
public CustomizableGui removeHighlight(@NotNull String key) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.removeHighlight(btn.position);
}
return this;
}
@NotNull
public CustomizableGui updateItem(@NotNull String key, @Nullable String title, @NotNull String... lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItem(btn.position, title, lore);
}
return this;
}
@NotNull
public CustomizableGui updateItem(@NotNull String key, @Nullable String title, @Nullable List<String> lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItem(btn.position, title, lore);
}
return this;
}
@NotNull
public CustomizableGui updateItem(@NotNull String key, @NotNull LegacyMaterials itemTo, @NotNull String title, @NotNull String... lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItem(btn.position, itemTo, title, lore);
}
return this;
}
@NotNull
public CustomizableGui updateItem(@NotNull String key, @NotNull LegacyMaterials itemTo, @NotNull String title, @Nullable List<String> lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItem(btn.position, itemTo, title, lore);
}
return this;
}
@NotNull
public CustomizableGui setAction(@NotNull String key, Clickable action) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn != null) {
setConditional(btn.position, null, action);
}
return this;
}
@NotNull
public CustomizableGui setAction(@NotNull String key, @Nullable ClickType type, @Nullable Clickable action) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn != null) {
setConditional(btn.position, type, action);
}
return this;
}
@NotNull
public CustomizableGui setButton(int defaultCell, @NotNull String key, ItemStack item, @Nullable Clickable action) {
setItem(defaultCell, key, item);
setAction(key, null, action);
return this;
}
@NotNull
public CustomizableGui setButton(int defaultCell, @NotNull String key, ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
setItem(defaultCell, key, item);
setAction(key, type, action);
return this;
}
@NotNull
public CustomizableGui setButton(int defaultRow, int defaultCol, @NotNull String key, ItemStack item, @Nullable Clickable action) {
final int defaultCell = defaultCol + defaultRow * 9;
setItem(defaultCell, key, item);
setAction(key, null, action);
return this;
}
@NotNull
public CustomizableGui setButton(int defaultRow, int defaultCol, @NotNull String key, ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
final int defaultCell = defaultCol + defaultRow * 9;
setItem(defaultCell, key, item);
setAction(key, type, action);
return this;
}
@NotNull
@Override
public CustomizableGui setNextPage(int row, int col, @NotNull ItemStack item) {
return this.setNextPage(col + row * 9, item);
}
@NotNull
@Override
public CustomizableGui setNextPage(int cell, @NotNull ItemStack item) {
CustomButton btn = buttons.get("__NEXT__");
if (btn == null) {
buttons.put("__NEXT__", btn = (new CustomButton("__NEXT__", cell)).setIcon(LegacyMaterials.getMaterial(item)));
} else {
ItemStack btnItem = btn.icon.getItem();
ItemMeta itemMeta = item.getItemMeta();
ItemMeta btnItemMeta = btnItem.getItemMeta();
if (itemMeta != null && btnItemMeta != null) {
btnItemMeta.setDisplayName(itemMeta.getDisplayName());
btnItemMeta.setLore(itemMeta.getLore());
btnItem.setItemMeta(itemMeta);
}
item = btnItem;
}
return (CustomizableGui) super.setNextPage(btn.position, item);
}
@NotNull
@Override
public CustomizableGui setPrevPage(int row, int col, @NotNull ItemStack item) {
return this.setPrevPage(col + row * 9, item);
}
@NotNull
@Override
public CustomizableGui setPrevPage(int cell, @NotNull ItemStack item) {
CustomButton btn = buttons.get("__PREV__");
if (btn == null) {
buttons.put("__PREV__", btn = (new CustomButton("__PREV__", cell)).setIcon(LegacyMaterials.getMaterial(item)));
} else {
ItemStack btnItem = btn.icon.getItem();
ItemMeta itemMeta = item.getItemMeta();
ItemMeta btnItemMeta = btnItem.getItemMeta();
if (itemMeta != null && btnItemMeta != null) {
btnItemMeta.setDisplayName(itemMeta.getDisplayName());
btnItemMeta.setLore(itemMeta.getLore());
btnItem.setItemMeta(itemMeta);
}
item = btnItem;
}
return (CustomizableGui) super.setPrevPage(btn.position, item);
}
public CustomizableGui clearActions(@NotNull String key) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn != null) {
this.clearActions(btn.position);
}
return this;
}
public static class CustomButton implements DataStoreObject<String> {
boolean _changed = false;
final String key;
int position = -1;
LegacyMaterials icon = LegacyMaterials.STONE;
public CustomButton(String key) {
this.key = key;
}
public CustomButton(String key, int position) {
this.key = key;
this.position = position;
}
public static CustomButton loadFromSection(ConfigurationSection sec) {
CustomButton dat = new CustomButton(sec.getName());
dat.icon = sec.contains("icon") ? LegacyMaterials.getMaterial(sec.getString("icon"), LegacyMaterials.STONE) : LegacyMaterials.STONE;
dat.position = sec.getInt("position");
return dat;
}
@Override
public void saveToSection(ConfigurationSection sec) {
sec.set("icon", icon.name());
sec.set("position", position);
}
@Override
public String getKey() {
return key;
}
@Override
public String getConfigKey() {
return key;
}
@Override
public boolean hasChanged() {
return _changed;
}
@Override
public void setChanged(boolean isChanged) {
_changed = isChanged;
}
public LegacyMaterials getIcon() {
return icon;
}
public CustomButton setIcon(LegacyMaterials icon) {
this.icon = icon != null ? icon : LegacyMaterials.STONE;
_changed = true;
return this;
}
}
}

View File

@ -26,6 +26,8 @@ import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* TODO: animated buttons
@ -62,8 +64,8 @@ public class Gui {
this.rows = 3;
}
public Gui(GuiType type) {
this.inventoryType = type;
public Gui(@NotNull GuiType type) {
this.inventoryType = type != null ? type : GuiType.STANDARD;
switch (type) {
case HOPPER:
case DISPENSER:
@ -74,7 +76,7 @@ public class Gui {
}
}
public Gui(Gui parent) {
public Gui(@Nullable Gui parent) {
this.parent = parent;
}
@ -82,11 +84,12 @@ public class Gui {
this.rows = Math.max(1, Math.min(6, rows));
}
public Gui(int rows, Gui parent) {
public Gui(int rows, @Nullable Gui parent) {
this.parent = parent;
this.rows = Math.max(1, Math.min(6, rows));
}
@NotNull
public List<Player> getPlayers() {
return inventory == null ? Collections.EMPTY_LIST
: inventory.getViewers().stream()
@ -139,7 +142,8 @@ public class Gui {
}
/**
* Close the GUI without calling onClose() and without opening any parent GUIs
* Close the GUI without calling onClose() and without opening any parent
* GUIs
*/
public void exit() {
allowClose = true;
@ -151,21 +155,25 @@ public class Gui {
.forEach(Player::closeInventory);
}
@NotNull
public GuiType getType() {
return inventoryType;
}
@NotNull
public Gui setUnlocked(int cell) {
unlockedCells.put(cell, true);
return this;
}
@NotNull
public Gui setUnlocked(int row, int col) {
final int cell = col + row * 9;
unlockedCells.put(cell, true);
return this;
}
@NotNull
public Gui setUnlockedRange(int cellFirst, int cellLast) {
for (int cell = cellFirst; cell <= cellLast; ++cell) {
unlockedCells.put(cell, true);
@ -173,6 +181,7 @@ public class Gui {
return this;
}
@NotNull
public Gui setUnlockedRange(int cellRowFirst, int cellColFirst, int cellRowLast, int cellColLast) {
final int last = cellColLast + cellRowLast * 9;
for (int cell = cellColFirst + cellRowFirst * 9; cell <= last; ++cell) {
@ -181,17 +190,20 @@ public class Gui {
return this;
}
@NotNull
public Gui setUnlocked(int cell, boolean open) {
unlockedCells.put(cell, open);
return this;
}
@NotNull
public Gui setUnlocked(int row, int col, boolean open) {
final int cell = col + row * 9;
unlockedCells.put(cell, open);
return this;
}
@NotNull
public Gui setTitle(String title) {
this.title = title;
return this;
@ -201,6 +213,7 @@ public class Gui {
return rows;
}
@NotNull
public Gui setRows(int rows) {
switch (inventoryType) {
case HOPPER:
@ -212,20 +225,24 @@ public class Gui {
return this;
}
public Gui setDefaultAction(Clickable action) {
@NotNull
public Gui setDefaultAction(@Nullable Clickable action) {
defaultClicker = action;
return this;
}
@NotNull
public Gui setDefaultItem(ItemStack item) {
blankItem = item;
return this;
}
@Nullable
public ItemStack getDefaultItem() {
return blankItem;
}
@Nullable
public ItemStack getItem(int cell) {
if (inventory != null && unlockedCells.getOrDefault(cell, false)) {
return inventory.getItem(cell);
@ -233,6 +250,7 @@ public class Gui {
return cellItems.get(cell);
}
@Nullable
public ItemStack getItem(int row, int col) {
final int cell = col + row * 9;
if (inventory != null && unlockedCells.getOrDefault(cell, false)) {
@ -241,7 +259,8 @@ public class Gui {
return cellItems.get(cell);
}
public Gui setItem(int cell, ItemStack item) {
@NotNull
public Gui setItem(int cell, @Nullable ItemStack item) {
cellItems.put(cell, item);
if (inventory != null && cell >= 0 && cell < inventory.getSize()) {
inventory.setItem(cell, item);
@ -249,7 +268,8 @@ public class Gui {
return this;
}
public Gui setItem(int row, int col, ItemStack item) {
@NotNull
public Gui setItem(int row, int col, @Nullable ItemStack item) {
final int cell = col + row * 9;
cellItems.put(cell, item);
if (inventory != null && cell >= 0 && cell < inventory.getSize()) {
@ -258,6 +278,7 @@ public class Gui {
return this;
}
@NotNull
public Gui highlightItem(int cell) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
@ -266,6 +287,7 @@ public class Gui {
return this;
}
@NotNull
public Gui highlightItem(int row, int col) {
final int cell = col + row * 9;
ItemStack item = cellItems.get(cell);
@ -275,6 +297,7 @@ public class Gui {
return this;
}
@NotNull
public Gui removeHighlight(int cell) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
@ -283,6 +306,7 @@ public class Gui {
return this;
}
@NotNull
public Gui removeHighlight(int row, int col) {
final int cell = col + row * 9;
ItemStack item = cellItems.get(cell);
@ -292,11 +316,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, String name, String... lore) {
@NotNull
public Gui updateItem(int row, int col, @Nullable String name, @NotNull String... lore) {
return updateItem(col + row * 9, name, lore);
}
public Gui updateItem(int cell, String name, String... lore) {
@NotNull
public Gui updateItem(int cell, @Nullable String name, @NotNull String... lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, name, lore));
@ -304,11 +330,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, String name, List<String> lore) {
@NotNull
public Gui updateItem(int row, int col, @Nullable String name, @Nullable List<String> lore) {
return updateItem(col + row * 9, name, lore);
}
public Gui updateItem(int cell, String name, List<String> lore) {
@NotNull
public Gui updateItem(int cell, @NotNull String name, @Nullable List<String> lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, title, lore));
@ -316,11 +344,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, ItemStack itemTo, String title, String... lore) {
@NotNull
public Gui updateItem(int row, int col, @NotNull ItemStack itemTo, @Nullable String title, @NotNull String... lore) {
return updateItem(col + row * 9, itemTo, title, lore);
}
public Gui updateItem(int cell, ItemStack itemTo, String title, String... lore) {
@NotNull
public Gui updateItem(int cell, @NotNull ItemStack itemTo, @Nullable String title, @NotNull String... lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, itemTo, title, lore));
@ -328,11 +358,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, LegacyMaterials itemTo, String title, String... lore) {
@NotNull
public Gui updateItem(int row, int col, @NotNull LegacyMaterials itemTo, @Nullable String title, @NotNull String... lore) {
return updateItem(col + row * 9, itemTo, title, lore);
}
public Gui updateItem(int cell, LegacyMaterials itemTo, String title, String... lore) {
@NotNull
public Gui updateItem(int cell, @NotNull LegacyMaterials itemTo, @Nullable String title, @Nullable String... lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, itemTo, title, lore));
@ -340,11 +372,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, ItemStack itemTo, String title, List<String> lore) {
@NotNull
public Gui updateItem(int row, int col, @NotNull ItemStack itemTo, @Nullable String title, @Nullable List<String> lore) {
return updateItem(col + row * 9, itemTo, title, lore);
}
public Gui updateItem(int cell, ItemStack itemTo, String title, List<String> lore) {
@NotNull
public Gui updateItem(int cell, @NotNull ItemStack itemTo, @Nullable String title, @Nullable List<String> lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, itemTo, title, lore));
@ -352,11 +386,13 @@ public class Gui {
return this;
}
public Gui updateItem(int row, int col, LegacyMaterials itemTo, String title, List<String> lore) {
@NotNull
public Gui updateItem(int row, int col, @NotNull LegacyMaterials itemTo, @Nullable String title, @Nullable List<String> lore) {
return updateItem(col + row * 9, itemTo, title, lore);
}
public Gui updateItem(int cell, LegacyMaterials itemTo, String title, List<String> lore) {
@NotNull
public Gui updateItem(int cell, @NotNull LegacyMaterials itemTo, @Nullable String title, @Nullable List<String> lore) {
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, GuiUtils.updateItem(item, itemTo, title, lore));
@ -364,34 +400,40 @@ public class Gui {
return this;
}
public Gui setAction(int cell, Clickable action) {
@NotNull
public Gui setAction(int cell, @Nullable Clickable action) {
setConditional(cell, null, action);
return this;
}
public Gui setAction(int row, int col, Clickable action) {
@NotNull
public Gui setAction(int row, int col, @Nullable Clickable action) {
setConditional(col + row * 9, null, action);
return this;
}
public Gui setAction(int cell, ClickType type, Clickable action) {
@NotNull
public Gui setAction(int cell, @Nullable ClickType type, @Nullable Clickable action) {
setConditional(cell, type, action);
return this;
}
public Gui setAction(int row, int col, ClickType type, Clickable action) {
@NotNull
public Gui setAction(int row, int col, @Nullable ClickType type, @Nullable Clickable action) {
setConditional(col + row * 9, type, action);
return this;
}
public Gui setActionForRange(int cellFirst, int cellLast, Clickable action) {
@NotNull
public Gui setActionForRange(int cellFirst, int cellLast, @Nullable Clickable action) {
for (int cell = cellFirst; cell <= cellLast; ++cell) {
setConditional(cell, null, action);
}
return this;
}
public Gui setActionForRange(int cellRowFirst, int cellColFirst, int cellRowLast, int cellColLast, Clickable action) {
@NotNull
public Gui setActionForRange(int cellRowFirst, int cellColFirst, int cellRowLast, int cellColLast, @Nullable Clickable action) {
final int last = cellColLast + cellRowLast * 9;
for (int cell = cellColFirst + cellRowFirst * 9; cell <= last; ++cell) {
setConditional(cell, null, action);
@ -399,14 +441,16 @@ public class Gui {
return this;
}
public Gui setActionForRange(int cellFirst, int cellLast, ClickType type, Clickable action) {
@NotNull
public Gui setActionForRange(int cellFirst, int cellLast, @Nullable ClickType type, @Nullable Clickable action) {
for (int cell = cellFirst; cell <= cellLast; ++cell) {
setConditional(cell, type, action);
}
return this;
}
public Gui setActionForRange(int cellRowFirst, int cellColFirst, int cellRowLast, int cellColLast, ClickType type, Clickable action) {
@NotNull
public Gui setActionForRange(int cellRowFirst, int cellColFirst, int cellRowLast, int cellColLast, @Nullable ClickType type, @Nullable Clickable action) {
final int last = cellColLast + cellRowLast * 9;
for (int cell = cellColFirst + cellRowFirst * 9; cell <= last; ++cell) {
setConditional(cell, type, action);
@ -414,90 +458,128 @@ public class Gui {
return this;
}
@NotNull
public Gui clearActions(int cell) {
conditionalButtons.remove(cell);
return this;
}
@NotNull
public Gui clearActions(int row, int col) {
final int cell = col + row * 9;
conditionalButtons.remove(cell);
return this;
}
public Gui setButton(int cell, ItemStack item, Clickable action) {
@NotNull
public Gui setButton(int cell, ItemStack item, @Nullable Clickable action) {
setItem(cell, item);
setConditional(cell, null, action);
return this;
}
public Gui setButton(int row, int col, ItemStack item, Clickable action) {
@NotNull
public Gui setButton(int row, int col, @Nullable ItemStack item, @Nullable Clickable action) {
final int cell = col + row * 9;
setItem(cell, item);
setConditional(cell, null, action);
return this;
}
public Gui setButton(int cell, ItemStack item, ClickType type, Clickable action) {
@NotNull
public Gui setButton(int cell, @Nullable ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
setItem(cell, item);
setConditional(cell, type, action);
return this;
}
public Gui setButton(int row, int col, ItemStack item, ClickType type, Clickable action) {
@NotNull
public Gui setButton(int row, int col, @Nullable ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
final int cell = col + row * 9;
setItem(cell, item);
setConditional(cell, type, action);
return this;
}
protected void setConditional(int cell, ClickType type, Clickable action) {
protected void setConditional(int cell, @Nullable ClickType type, @Nullable Clickable action) {
Map<ClickType, Clickable> conditionals = conditionalButtons.get(cell);
if (action != null) {
if (conditionals == null) {
conditionalButtons.put(cell, conditionals = new HashMap());
}
conditionals.put(type, action);
}
}
public Gui setOnOpen(Openable action) {
@NotNull
public Gui setOnOpen(@Nullable Openable action) {
opener = action;
return this;
}
public Gui setOnClose(Closable action) {
@NotNull
public Gui setOnClose(@Nullable Closable action) {
closer = action;
return this;
}
public Gui setOnDrop(Droppable action) {
@NotNull
public Gui setOnDrop(@Nullable Droppable action) {
dropper = action;
return this;
}
public Gui setOnPage(Pagable action) {
@NotNull
public Gui setOnPage(@Nullable Pagable action) {
pager = action;
return this;
}
public Gui setNextPage(int row, int col, ItemStack item) {
public Gui setNextPage(ItemStack item) {
nextPage = item;
return this;
}
public Gui setPrevPage(ItemStack item) {
prevPage = item;
return this;
}
@NotNull
public Gui setNextPage(int cell, @NotNull ItemStack item) {
nextPageIndex = cell;
if (page < pages) {
setButton(nextPageIndex, nextPage = item, ClickType.LEFT, (event) -> this.nextPage(event.manager));
}
return this;
}
@NotNull
public Gui setNextPage(int row, int col, @NotNull ItemStack item) {
nextPageIndex = col + row * 9;
if (page < pages) {
setButton(nextPageIndex, item, ClickType.LEFT, (event) -> this.nextPage(event.manager));
setButton(nextPageIndex, nextPage = item, ClickType.LEFT, (event) -> this.nextPage(event.manager));
}
return this;
}
public Gui setPrevPage(int row, int col, ItemStack item) {
@NotNull
public Gui setPrevPage(int cell, @NotNull ItemStack item) {
prevPageIndex = cell;
if (page > 1) {
setButton(prevPageIndex, prevPage = item, ClickType.LEFT, (event) -> this.prevPage(event.manager));
}
return this;
}
@NotNull
public Gui setPrevPage(int row, int col, @NotNull ItemStack item) {
prevPageIndex = col + row * 9;
if (page > 1) {
setButton(prevPageIndex, item, ClickType.LEFT, (event) -> this.prevPage(event.manager));
setButton(prevPageIndex, prevPage = item, ClickType.LEFT, (event) -> this.prevPage(event.manager));
}
return this;
}
public void nextPage(GuiManager manager) {
public void nextPage(@NotNull GuiManager manager) {
if (page < pages) {
int lastPage = page;
++page;
@ -515,7 +597,7 @@ public class Gui {
}
}
public void prevPage(GuiManager manager) {
public void prevPage(@NotNull GuiManager manager) {
if (page > 1) {
int lastPage = page;
--page;
@ -547,11 +629,13 @@ public class Gui {
}
}
protected Inventory getOrCreateInventory(GuiManager manager) {
@NotNull
protected Inventory getOrCreateInventory(@NotNull GuiManager manager) {
return inventory != null ? inventory : generateInventory(manager);
}
protected Inventory generateInventory(GuiManager manager) {
@NotNull
protected Inventory generateInventory(@NotNull GuiManager manager) {
final int cells = rows * 9;
InventoryType t = inventoryType == null ? InventoryType.CHEST : inventoryType.type;
switch (t) {
@ -573,6 +657,7 @@ public class Gui {
return inventory;
}
@Nullable
public Gui getParent() {
return parent;
}
@ -595,11 +680,11 @@ public class Gui {
return title;
}
protected boolean onClickOutside(GuiManager manager, Player player, InventoryClickEvent event) {
protected boolean onClickOutside(@NotNull GuiManager manager, @NotNull Player player, @NotNull InventoryClickEvent event) {
return dropper != null ? dropper.onDrop(new GuiDropItemEvent(manager, this, player, event)) : true;
}
protected boolean onClick(GuiManager manager, Player player, Inventory inventory, InventoryClickEvent event) {
protected boolean onClick(@NotNull GuiManager manager, @NotNull Player player, @NotNull Inventory inventory, @NotNull InventoryClickEvent event) {
final int cell = event.getSlot();
Map<ClickType, Clickable> conditionals = conditionalButtons.get(cell);
Clickable button;
@ -608,7 +693,7 @@ public class Gui {
button.onClick(new GuiClickEvent(manager, this, player, event, cell, true));
} else {
// no event for this button
if(defaultClicker != null) {
if (defaultClicker != null) {
// this is a default action, not a triggered action
defaultClicker.onClick(new GuiClickEvent(manager, this, player, event, cell, true));
}
@ -617,19 +702,19 @@ public class Gui {
return true;
}
protected boolean onClickPlayerInventory(GuiManager manager, Player player, Inventory openInv, InventoryClickEvent event) {
protected boolean onClickPlayerInventory(@NotNull GuiManager manager, @NotNull Player player, @NotNull Inventory openInv, @NotNull InventoryClickEvent event) {
// no events for this yet
return false;
}
public void onOpen(GuiManager manager, Player player) {
public void onOpen(@NotNull GuiManager manager, @NotNull Player player) {
open = true;
if (opener != null) {
opener.onOpen(new GuiOpenEvent(manager, this, player));
}
}
public void onClose(GuiManager manager, Player player) {
public void onClose(@NotNull GuiManager manager, @NotNull Player player) {
if (!allowClose) {
manager.showGUI(player, this);
return;

View File

@ -62,16 +62,6 @@ public class SimplePagedGui extends Gui {
return this;
}
public SimplePagedGui setNextPage(ItemStack item) {
nextPage = item;
return this;
}
public SimplePagedGui setPrevPage(ItemStack item) {
prevPage = item;
return this;
}
@Override
public SimplePagedGui setItem(int row, int col, ItemStack item) {
return setItem(col + row * 9, item);