Experimental CustomizableGUI.

This commit is contained in:
Brianna 2020-12-01 12:50:27 -06:00
parent 0168bcec94
commit dc4be7e93c
5 changed files with 687 additions and 357 deletions

View File

@ -8,6 +8,7 @@ import com.songoda.core.core.PluginInfo;
import com.songoda.core.core.PluginInfoModule;
import com.songoda.core.core.SongodaCoreCommand;
import com.songoda.core.core.SongodaCoreDiagCommand;
import com.songoda.core.core.SongodaCoreShowGuiKeysCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -163,10 +164,11 @@ public class SongodaCore {
private void init() {
shadingListener = new ShadedEventListener();
commandManager.registerCommandDynamically(new SongodaCoreCommand())
.addSubCommand(new SongodaCoreDiagCommand());
.addSubCommand(new SongodaCoreDiagCommand())
.addSubCommand(new SongodaCoreShowGuiKeysCommand());
Bukkit.getPluginManager().registerEvents(loginListener, piggybackedPlugin);
Bukkit.getPluginManager().registerEvents(shadingListener, piggybackedPlugin);
// we aggressevely want to own this command
// we aggressively want to own this command
tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(piggybackedPlugin, () -> {
CommandManager.registerCommandDynamically(piggybackedPlugin, "songoda", commandManager, commandManager);
}, 10 * 60 * 1));

View File

@ -0,0 +1,42 @@
package com.songoda.core.core;
import com.songoda.core.commands.AbstractCommand;
import com.songoda.core.gui.CustomizableGui;
import org.bukkit.command.CommandSender;
import java.util.List;
public class SongodaCoreShowGuiKeysCommand extends AbstractCommand {
public SongodaCoreShowGuiKeysCommand() {
super(false, "showguikeys");
}
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
sender.sendMessage(CustomizableGui.toggleShowGuiKeys() ? "Now showing keys." : "No longer showing keys.");
return ReturnType.SUCCESS;
}
@Override
public String getPermissionNode() {
return "songoda.admin";
}
@Override
public String getSyntax() {
return "/songoda showguikeys";
}
@Override
public String getDescription() {
return "Show the keys for all items in every gui.";
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
return null;
}
}

View File

@ -1,363 +1,678 @@
package com.songoda.core.gui;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.configuration.DataStoreObject;
import com.songoda.core.configuration.SimpleDataStore;
import com.songoda.core.configuration.Config;
import com.songoda.core.configuration.ConfigSection;
import com.songoda.core.gui.methods.Clickable;
import org.bukkit.configuration.ConfigurationSection;
import com.songoda.core.utils.TextUtils;
import org.bukkit.Bukkit;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Represents a GUI screen that can be user-configured
*
* @author jascotty2
* @since 2019-09-06
*/
public class CustomizableGui extends Gui {
final Map<String, CustomButton> buttons;
private static boolean showGuiKeys = false;
public CustomizableGui(SimpleDataStore<CustomButton> buttons) {
this((Map<String, CustomButton>) (Map) buttons.getData());
private static final Map<String, CustomContent> loadedGuis = new HashMap<>();
private final CustomContent customContent;
public CustomizableGui(Plugin plugin, String guiKey) {
this(plugin, guiKey, null);
}
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) {
public CustomizableGui(@NotNull Plugin plugin, @NotNull String guiKey, @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()]);
}
if (!loadedGuis.containsKey(guiKey) || showGuiKeys) {
File localeFolder = new File(plugin.getDataFolder(), "gui/");
if (!localeFolder.exists()) localeFolder.mkdir();
@NotNull
@Override
public CustomizableGui setDefaultItem(ItemStack item) {
if ((blankItem = item) != null) {
buttons.put("__DEFAULT__", (new CustomButton("__DEFAULT__")).setIcon(CompatibleMaterial.getMaterial(item)));
}
return this;
}
Config config = new Config(plugin, "gui/" + guiKey + ".yml");
config.load();
@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(CompatibleMaterial.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);
if (!config.isConfigurationSection("overrides")) {
config.setDefault("overrides.example.item", CompatibleMaterial.STONE.name(),
"This is the icon material you would like to replace",
"the current material with.")
.setDefault("overrides.example.position", 5,
"This is the current position of the icon you would like to move.",
"The number represents the cell the icon currently resides in.")
.setDefaultComment("overrides.example",
"This is just an example and does not override to any items",
"in this GUI.")
.setDefaultComment("overrides",
"For information on how to apply overrides please visit",
"https://wiki.songoda.com/Gui");
config.saveChanges();
}
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 CompatibleMaterial 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 CompatibleMaterial 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 Gui updateItemLore(@NotNull String key, @NotNull String... lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItemLore(btn.position, lore);
}
return this;
}
@NotNull
public Gui updateItemLore(@NotNull String key, @Nullable List<String> lore) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItemLore(btn.position, lore);
}
return this;
}
@NotNull
public Gui updateItemName(@NotNull String key, @Nullable String name) {
CustomButton btn = key == null ? null : buttons.get(key.toLowerCase());
if (btn != null) {
this.updateItemName(btn.position, title);
}
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 CompatibleMaterial 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 CompatibleMaterial 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(CompatibleMaterial.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);
if (!config.isConfigurationSection("disabled")) {
config.setDefault("disabled", Arrays.asList("example3", "example4", "example5"),
"All keys on this list will be disabled. You can add any items key here",
"if you no longer want that item in the GUI.");
config.saveChanges();
}
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);
}
CustomContent customContent = loadedGuis.computeIfAbsent(guiKey, g -> new CustomContent(guiKey));
loadedGuis.put(guiKey, customContent);
this.customContent = customContent;
@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(CompatibleMaterial.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);
for (ConfigSection section : config.getSections("overrides")) {
if (section.contains("row") || section.contains("col")
|| section.contains("mirrorrow") || section.contains("mirrorcol")) {
if (section.contains("mirrorrow") || section.contains("mirrorcol"))
customContent.addButton(section.getNodeKey(), section.getInt("row", -1),
section.getInt("col", -1),
section.getBoolean("mirrorrow", false),
section.getBoolean("mirrorcol", false),
section.isSet("item") ? CompatibleMaterial.getMaterial(section.getString("item")) : null);
else
customContent.addButton(section.getNodeKey(), section.getInt("row", -1),
section.getInt("col", -1),
section.getString("title", null),
section.isSet("lore") ? section.getStringList("lore") : null,
section.isSet("item") ? CompatibleMaterial.getMaterial(section.getString("item")) : null);
} else {
customContent.addButton(section.getNodeKey(), section.getInt("position", -1),
section.getString("title", null),
section.isSet("lore") ? section.getStringList("lore") : null,
section.isSet("item") ? CompatibleMaterial.getMaterial(section.getString("item")) : null);
}
}
item = btnItem;
for (String disabled : config.getStringList("disabled"))
customContent.disableButton(disabled);
} else {
customContent = loadedGuis.get(guiKey);
}
return (CustomizableGui) super.setPrevPage(btn.position, item);
if (customContent.isButtonCustomized("__DEFAULT__"))
blankItem = GuiUtils.getBorderItem(customContent.getCustomizedButton("__DEFAULT__").item);
}
public CustomizableGui clearActions(@NotNull String key) {
CustomButton btn = key == null ? null : buttons.get(key = key.toLowerCase());
if (btn != null) {
this.clearActions(btn.position);
@NotNull
protected Inventory generateInventory(@NotNull GuiManager manager) {
applyCustomItems();
return super.generateInventory(manager);
}
public void update() {
applyCustomItems();
super.update();
}
private void applyCustomItems() {
for (CustomButton customButton : customContent.getCustomButtons().values())
if (customButton instanceof MirrorFill)
applyCustomItem(customButton);
for (CustomButton customButton : customContent.getCustomButtons().values())
if (!(customButton instanceof MirrorFill))
applyCustomItem(customButton);
}
private void applyCustomItem(CustomButton customButton) {
if (customButton.row != -1 && customButton.col != -1)
if (customButton instanceof MirrorFill)
mirrorFill(customButton.key, customButton.row, customButton.col,
((MirrorFill) customButton).mirrorRow, ((MirrorFill) customButton).mirrorCol,
customButton.createItem());
else
setItem(customButton.key, customButton.row, customButton.col, customButton.createItem());
else
setItem(customButton.key, customButton.position, customButton.createItem());
}
@NotNull
public Gui setDefaultItem(@Nullable ItemStack item) {
if (item == null) return this;
applyShowGuiKeys("__DEFAULT__", item);
if (customContent.isButtonCustomized("__DEFAULT__"))
return this;
return super.setDefaultItem(item);
}
@NotNull
public Gui setItem(@NotNull String key, int cell, @Nullable ItemStack item) {
if (customContent.isButtonDisabled(key)) return this;
applyShowGuiKeys(key, item);
if (customContent.isButtonCustomized(key)) {
CustomButton btn = customContent.getCustomizedButton(key);
cell = btn.applyPosition(cell);
btn.applyItem(item);
}
return setItem(cell, item);
}
@NotNull
public Gui setItem(@NotNull String key, int row, int col, @Nullable ItemStack item) {
final int cell = col + row * inventoryType.columns;
return setItem(key, cell, item);
}
public Gui mirrorFill(@NotNull String key, int row, int col, boolean mirrorRow, boolean mirrorCol, @NotNull ItemStack item) {
if (customContent.isButtonDisabled(key)) return this;
ItemStack newItem = item.clone();
boolean isShow = applyShowGuiKeys(key, newItem);
if (customContent.isButtonCustomized(key)) {
CustomButton btn = customContent.getCustomizedButton(key);
row = btn.applyPositionRow(row);
col = btn.applyPositionCol(col);
if (btn.applyItem(newItem))
isShow = true;
if (btn instanceof MirrorFill) {
MirrorFill mf = (MirrorFill) btn;
mirrorRow = mf.mirrorRow;
mirrorCol = mf.mirrorCol;
}
}
return mirrorFill(row, col, mirrorRow, mirrorCol, isShow ? newItem : item);
}
@NotNull
public Gui highlightItem(@NotNull String key, int cell) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return highlightItem(cell);
}
@NotNull
public Gui highlightItem(@NotNull String key, int row, int col) {
if (customContent.isButtonDisabled(key)) return this;
final int cell = col + row * inventoryType.columns;
return highlightItem(key, cell);
}
@NotNull
public Gui removeHighlight(@NotNull String key, int cell) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return removeHighlight(cell);
}
@NotNull
public Gui removeHighlight(@NotNull String key, int row, int col) {
if (customContent.isButtonDisabled(key)) return this;
final int cell = col + row * inventoryType.columns;
return removeHighlight(key, cell);
}
@NotNull
public Gui updateItemLore(@NotNull String key, int row, int col, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItemLore(key, col + row * inventoryType.columns, lore);
}
@NotNull
public Gui updateItemLore(@NotNull String key, int cell, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItemLore(cell, lore);
}
@NotNull
public Gui updateItemLore(@NotNull String key, int row, int col, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItemLore(key, col + row * inventoryType.columns, lore);
}
@NotNull
public Gui updateItemLore(@NotNull String key, int cell, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItemLore(cell, lore);
}
@NotNull
public Gui updateItemName(@NotNull String key, int row, int col, @Nullable String name) {
if (customContent.isButtonDisabled(key)) return this;
return updateItemName(key, col + row * inventoryType.columns, name);
}
@NotNull
public Gui updateItemName(@NotNull String key, int cell, @Nullable String name) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItemName(cell, name);
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @Nullable String name, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, name, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @Nullable String name, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, cell, name, Arrays.asList(lore));
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @Nullable String name, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, name, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @NotNull String name, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
lore = applyShowGuiKeys(key, lore);
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItem(cell, name, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @NotNull ItemStack itemTo, @Nullable String title, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @NotNull ItemStack itemTo, @Nullable String title, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItem(cell, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @NotNull CompatibleMaterial itemTo, @Nullable String title, @NotNull String... lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @NotNull CompatibleMaterial itemTo, @Nullable String title, @Nullable String... lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItem(key, cell, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @NotNull ItemStack itemTo, @Nullable String title, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @NotNull ItemStack itemTo, @Nullable String title, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItem(key, cell, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int row, int col, @NotNull CompatibleMaterial itemTo, @Nullable String title, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
return updateItem(key, col + row * inventoryType.columns, itemTo, title, lore);
}
@NotNull
public Gui updateItem(@NotNull String key, int cell, @NotNull CompatibleMaterial itemTo, @Nullable String title, @Nullable List<String> lore) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return updateItem(key, cell, itemTo, title, lore);
}
@NotNull
public Gui setAction(@NotNull String key, int cell, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
setConditional(key, cell, null, action);
return this;
}
public static class CustomButton implements DataStoreObject<String> {
@NotNull
public Gui setAction(@NotNull String key, int row, int col, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
setConditional(key, col + row * inventoryType.columns, null, action);
return this;
}
boolean _changed = false;
final String key;
int position = -1;
CompatibleMaterial icon = CompatibleMaterial.STONE;
@NotNull
public Gui setAction(@NotNull String key, int cell, @Nullable ClickType type, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
setConditional(key, cell, type, action);
return this;
}
public CustomButton(String key) {
this.key = key;
@NotNull
public Gui setAction(@NotNull String key, int row, int col, @Nullable ClickType type, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
setConditional(key, col + row * inventoryType.columns, type, action);
return this;
}
@NotNull
public Gui clearActions(@NotNull String key, int cell) {
if (customContent.isButtonDisabled(key)) return this;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
return clearActions(cell);
}
@NotNull
public Gui clearActions(@NotNull String key, int row, int col) {
if (customContent.isButtonDisabled(key)) return this;
return clearActions(key, col + row * inventoryType.columns);
}
@NotNull
public Gui setButton(@NotNull String key, int cell, ItemStack item, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
applyShowGuiKeys(key, item);
if (customContent.isButtonCustomized(key)) {
CustomButton btn = customContent.getCustomizedButton(key);
cell = btn.applyPosition(cell);
btn.applyItem(item);
}
public CustomButton(String key, int position) {
setItem(cell, item);
setConditional(cell, null, action);
return this;
}
@NotNull
public Gui setButton(@NotNull String key, int row, int col, @Nullable ItemStack item, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
return setButton(key, col + row * inventoryType.columns, item, action);
}
@NotNull
public Gui setButton(@NotNull String key, int cell, @Nullable ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
applyShowGuiKeys(key, item);
if (customContent.isButtonCustomized(key)) {
CustomButton btn = customContent.getCustomizedButton(key);
cell = btn.applyPosition(cell);
btn.applyItem(item);
}
return setButton(cell, item, type, action);
}
@NotNull
public Gui setButton(@NotNull String key, int row, int col, @Nullable ItemStack item, @Nullable ClickType type, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return this;
return setButton(key, col + row + inventoryType.columns, item, type, action);
}
protected void setConditional(@NotNull String key, int cell, @Nullable ClickType type, @Nullable Clickable action) {
if (customContent.isButtonDisabled(key)) return;
if (customContent.isButtonCustomized(key))
cell = customContent.getCustomizedButton(key).applyPosition(cell);
setConditional(cell, type, action);
}
public Gui setNextPage(ItemStack item) {
applyShowGuiKeys("__NEXT__", item);
if (customContent.isButtonCustomized("__NEXT__"))
customContent.getCustomizedButton("__NEXT__").applyItem(item);
return super.setNextPage(item);
}
public Gui setPrevPage(ItemStack item) {
applyShowGuiKeys("__PREV__", item);
if (customContent.isButtonCustomized("__PREV__"))
customContent.getCustomizedButton("__PREV__").applyItem(item);
return super.setPrevPage(item);
}
@NotNull
public Gui setNextPage(int cell, @NotNull ItemStack item) {
applyShowGuiKeys("__NEXT__", item);
if (customContent.isButtonCustomized("__NEXT__")) {
CustomButton btn = customContent.getCustomizedButton("__NEXT__");
cell = btn.applyPosition(cell);
btn.applyItem(item);
}
return super.setNextPage(cell, item);
}
@NotNull
public Gui setNextPage(int row, int col, @NotNull ItemStack item) {
applyShowGuiKeys("__NEXT__", item);
return setNextPage(col + row * inventoryType.columns, item);
}
@NotNull
public Gui setPrevPage(int cell, @NotNull ItemStack item) {
applyShowGuiKeys("__PREV__", item);
if (customContent.isButtonCustomized("__PREV__")) {
CustomButton btn = customContent.getCustomizedButton("__PREV__");
cell = btn.applyPosition(cell);
btn.applyItem(item);
}
return super.setPrevPage(cell, item);
}
@NotNull
public Gui setPrevPage(int row, int col, @NotNull ItemStack item) {
applyShowGuiKeys("__PREV__", item);
return setPrevPage(col + row * inventoryType.columns, item);
}
private boolean applyShowGuiKeys(String key, ItemStack item) {
if (!showGuiKeys) return false;
ItemMeta meta = item.getItemMeta();
if (meta == null) meta = Bukkit.getItemFactory().getItemMeta(item.getType());
List<String> lore = new ArrayList<>(Collections.singletonList("Key: " + key));
if (meta.hasLore())
lore.addAll(meta.getLore());
meta.setLore(lore);
item.setItemMeta(meta);
return true;
}
private List<String> applyShowGuiKeys(String key, List<String> lore) {
if (!showGuiKeys) return lore;
List<String> newLore = new ArrayList<>(Collections.singletonList("Key: " + key));
newLore.addAll(lore);
return newLore;
}
public static boolean toggleShowGuiKeys() {
showGuiKeys = !showGuiKeys;
return showGuiKeys;
}
private class CustomButton {
private final String key;
private final int position;
private final int row;
private final int col;
private final String title;
private final List<String> lore;
private final CompatibleMaterial item;
public CustomButton(String key, int position, String title, List<String> lore, CompatibleMaterial item) {
this.key = key;
this.position = position;
this.row = -1;
this.col = -1;
this.item = item;
this.title = title;
this.lore = lore;
}
public static CustomButton loadFromSection(ConfigurationSection sec) {
CustomButton dat = new CustomButton(sec.getName());
dat.icon = sec.contains("icon") ? CompatibleMaterial.getMaterial(sec.getString("icon"), CompatibleMaterial.STONE) : CompatibleMaterial.STONE;
dat.position = sec.getInt("position");
return dat;
public CustomButton(String key, int row, int col, String title, List<String> lore, CompatibleMaterial item) {
this.key = key;
this.position = -1;
this.row = row;
this.col = col;
this.item = item;
this.title = title;
this.lore = lore;
}
@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;
public boolean applyItem(ItemStack item) {
if (item == null) return false;
item.setType(this.item.getMaterial());
item.setDurability(this.item.getData());
applyMeta(item);
return true;
}
@Override
public boolean hasChanged() {
return _changed;
public ItemStack createItem() {
ItemStack item = this.item.getItem();
applyMeta(item);
return item;
}
@Override
public void setChanged(boolean isChanged) {
_changed = isChanged;
private void applyMeta(ItemStack item) {
ItemMeta meta = item.getItemMeta();
if (title != null)
meta.setDisplayName(TextUtils.formatText(title));
if (lore != null)
meta.setLore(TextUtils.formatText(lore));
item.setItemMeta(meta);
}
public CompatibleMaterial getIcon() {
return icon;
public int applyPosition(int cell) {
if (row != -1 && col != -1)
return col + row * inventoryType.columns;
return position == -1 ? cell : position;
}
public CustomButton setIcon(CompatibleMaterial icon) {
this.icon = icon != null ? icon : CompatibleMaterial.STONE;
_changed = true;
return this;
public int applyPositionRow(int row) {
return row == -1 ? row : row;
}
public int applyPositionCol(int col) {
return col == -1 ? col : col;
}
}
private class MirrorFill extends CustomButton {
private final boolean mirrorRow;
private final boolean mirrorCol;
public MirrorFill(String key, int row, int col, boolean mirrorRow, boolean mirrorCol, CompatibleMaterial item) {
super(key, row, col, null, null, item);
this.mirrorRow = mirrorRow;
this.mirrorCol = mirrorCol;
}
public boolean isMirrorRow() {
return mirrorRow;
}
public boolean isMirrorCol() {
return mirrorCol;
}
}
private class CustomContent {
private final String guiKey;
private final Map<String, CustomButton> customizedButtons = new HashMap<>();
private final Map<String, CustomButton> customButtons = new HashMap<>();
private final List<String> disabledButtons = new ArrayList<>();
public CustomContent(String guiKey) {
this.guiKey = guiKey;
}
public String getGuiKey() {
return guiKey;
}
public CustomButton getCustomizedButton(String key) {
return customizedButtons.get(key);
}
public CustomButton getCustomButton(String key) {
return customizedButtons.get(key);
}
public Map<String, CustomButton> getCustomButtons() {
return Collections.unmodifiableMap(customButtons);
}
public void addButton(String key, int position, String title, List<String> lore, CompatibleMaterial item) {
CustomButton customButton = new CustomButton(key, position, title, lore, item);
if (key.startsWith("custom_"))
customButtons.put(key, customButton);
else
customizedButtons.put(key, customButton);
}
public void addButton(String key, int row, int col, String title, List<String> lore, CompatibleMaterial item) {
CustomButton customButton = new CustomButton(key, row, col, title, lore, item);
if (key.startsWith("custom_"))
customButtons.put(key, customButton);
else
customizedButtons.put(key, customButton);
}
public void addButton(String key, int row, int col, boolean mirrorRow, boolean mirrorCol, CompatibleMaterial item) {
MirrorFill mirrorFill = new MirrorFill(key, row, col, mirrorRow, mirrorCol, item);
if (key.startsWith("custom_"))
customButtons.put(key, mirrorFill);
else
customizedButtons.put(key, mirrorFill);
}
public boolean isButtonCustomized(String key) {
return customizedButtons.containsKey(key);
}
public void disableButton(String button) {
disabledButtons.add(button);
}
public boolean isButtonDisabled(String button) {
return disabledButtons.contains(button);
}
}
}

View File

@ -24,6 +24,8 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -278,7 +280,7 @@ public class Gui {
}
@NotNull
public Gui setDefaultItem(ItemStack item) {
public Gui setDefaultItem(@Nullable ItemStack item) {
blankItem = item;
return this;
}
@ -317,10 +319,18 @@ public class Gui {
@NotNull
public Gui setItem(int row, int col, @Nullable ItemStack item) {
final int cell = col + row * inventoryType.columns;
cellItems.put(cell, item);
if (inventory != null && cell >= 0 && cell < inventory.getSize()) {
inventory.setItem(cell, item);
}
return setItem(cell, item);
}
@NotNull
public Gui mirrorFill(int row, int col, boolean mirrorRow, boolean mirrorCol, ItemStack item) {
setItem(row, col, item);
if (mirrorRow)
setItem(rows - row - 1, col, item);
if (mirrorCol)
setItem(row, 8 - col, item);
if (mirrorRow && mirrorCol)
setItem(rows - row - 1, 8 - col, item);
return this;
}
@ -336,11 +346,7 @@ public class Gui {
@NotNull
public Gui highlightItem(int row, int col) {
final int cell = col + row * inventoryType.columns;
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, ItemUtils.addGlow(item));
}
return this;
return highlightItem(cell);
}
@NotNull
@ -355,11 +361,7 @@ public class Gui {
@NotNull
public Gui removeHighlight(int row, int col) {
final int cell = col + row * inventoryType.columns;
ItemStack item = cellItems.get(cell);
if (item != null && item.getType() != Material.AIR) {
setItem(cell, ItemUtils.removeGlow(item));
}
return this;
return removeHighlight(cell);
}
@NotNull
@ -411,11 +413,7 @@ public class Gui {
@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));
}
return this;
return updateItem(cell, name, Arrays.asList(lore));
}
@NotNull
@ -427,7 +425,7 @@ public class Gui {
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));
setItem(cell, GuiUtils.updateItem(item, name, lore));
}
return this;
}
@ -554,9 +552,7 @@ public class Gui {
@NotNull
public Gui clearActions(int row, int col) {
final int cell = col + row * inventoryType.columns;
conditionalButtons.remove(cell);
return this;
return clearActions(col + row * inventoryType.columns);
}
@NotNull
@ -590,10 +586,7 @@ public class Gui {
}
protected void setConditional(int cell, @Nullable ClickType type, @Nullable Clickable action) {
Map<ClickType, Clickable> conditionals = conditionalButtons.get(cell);
if (conditionals == null) {
conditionalButtons.put(cell, conditionals = new HashMap());
}
Map<ClickType, Clickable> conditionals = conditionalButtons.computeIfAbsent(cell, k -> new HashMap());
conditionals.put(type, action);
}
@ -643,12 +636,7 @@ public class Gui {
@NotNull
public Gui setNextPage(int row, int col, @NotNull ItemStack item) {
nextPageIndex = col + row * inventoryType.columns;
nextPage = item;
if (page < pages) {
setButton(nextPageIndex, nextPage, ClickType.LEFT, (event) -> this.nextPage());
}
return this;
return setNextPage(col + row * inventoryType.columns, item);
}
@NotNull
@ -663,12 +651,7 @@ public class Gui {
@NotNull
public Gui setPrevPage(int row, int col, @NotNull ItemStack item) {
prevPageIndex = col + row * inventoryType.columns;
prevPage = item;
if (page > 1) {
setButton(prevPageIndex, prevPage, ClickType.LEFT, (event) -> this.prevPage());
}
return this;
return setPrevPage(col + row * inventoryType.columns, item);
}
public void setPages(int pages) {
@ -768,6 +751,7 @@ public class Gui {
inventory.setItem(i, item != null ? item : (unlockedCells.getOrDefault(i, false) ? AIR : blankItem));
}
return inventory;
}

View File

@ -409,17 +409,4 @@ public class GuiUtils {
}
return item;
}
public static void mirrorFill(Gui gui, int row, int col, boolean mirrorRow, boolean mirrorCol, ItemStack item) {
gui.setItem(row, col, item);
if (mirrorRow) {
gui.setItem(gui.rows - row - 1, col, item);
}
if (mirrorCol) {
gui.setItem(row, 8 - col, item);
}
if (mirrorRow && mirrorCol) {
gui.setItem(gui.rows - row - 1, 8 - col, item);
}
}
}