diff --git a/pom.xml b/pom.xml index dc5a33c..66d0090 100644 --- a/pom.xml +++ b/pom.xml @@ -121,10 +121,9 @@ provided - fr.minuskube.inv - smart-invs - 1.2.7 - compile + com.github.MinusKube + SmartInvs + 9c9dbbee16 com.github.cryptomorin diff --git a/src/main/java/nl/sbdeveloper/showcontrol/ShowControlPlugin.java b/src/main/java/nl/sbdeveloper/showcontrol/ShowControlPlugin.java index eeb4b8e..b4879a7 100644 --- a/src/main/java/nl/sbdeveloper/showcontrol/ShowControlPlugin.java +++ b/src/main/java/nl/sbdeveloper/showcontrol/ShowControlPlugin.java @@ -5,7 +5,7 @@ import nl.sbdeveloper.showcontrol.api.ShowAPI; import nl.sbdeveloper.showcontrol.commands.ShowCMD; import nl.sbdeveloper.showcontrol.data.DataStorage; import nl.sbdeveloper.showcontrol.data.Shows; -import nl.sbdeveloper.showcontrol.utils.Inventory; +import nl.sbdeveloper.showcontrol.utils.inventories.Inventory; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -29,7 +29,7 @@ public final class ShowControlPlugin extends JavaPlugin { commandManager.getCommandCompletions().registerCompletion("showtype", c -> ShowAPI.getTriggers().keySet()); getLogger().info("Loading GUI manageer..."); - Inventory.init(); + Inventory.init(this); getLogger().info("Loading default triggers..."); ShowAPI.index(ShowControlPlugin.class, "nl.sbdeveloper.showcontrol.api.triggers.impl"); diff --git a/src/main/java/nl/sbdeveloper/showcontrol/gui/ShowCueGUI.java b/src/main/java/nl/sbdeveloper/showcontrol/gui/ShowCueGUI.java index 5d3569a..c4caac3 100644 --- a/src/main/java/nl/sbdeveloper/showcontrol/gui/ShowCueGUI.java +++ b/src/main/java/nl/sbdeveloper/showcontrol/gui/ShowCueGUI.java @@ -6,9 +6,8 @@ import fr.minuskube.inv.content.Pagination; import fr.minuskube.inv.content.SlotIterator; import nl.sbdeveloper.showcontrol.api.ShowCuePoint; import nl.sbdeveloper.showcontrol.data.Shows; -import nl.sbdeveloper.showcontrol.utils.Inventory; -import nl.sbdeveloper.showcontrol.utils.ItemBuilder; import nl.sbdeveloper.showcontrol.utils.MainUtil; +import nl.sbdeveloper.showcontrol.utils.inventories.PaginationInventory; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -19,48 +18,16 @@ import java.util.List; import static nl.sbdeveloper.showcontrol.utils.MainUtil.__; -public class ShowCueGUI extends Inventory { - private final String showName; - +public class ShowCueGUI extends PaginationInventory { public ShowCueGUI(Player p, String name) { super(6, ChatColor.DARK_AQUA + "Show Cue Manager:"); - this.showName = name; - open(p); - } - @Override - public void init(Player player, InventoryContents contents) { - Pagination pagination = contents.pagination(); - - List items = new ArrayList<>(); - Shows.getPoints(showName).stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) - .forEach(cue -> items.add(ClickableItem.of(MainUtil.pointToItem(cue), e -> { - Shows.removePoint(showName, cue); - open(player, pagination.getPage()); + Shows.getPoints(name).stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) + .forEach(cue -> addItem(ClickableItem.of(MainUtil.pointToItem(cue), e -> { + Shows.removePoint(name, cue); + refresh(p); }))); - ClickableItem[] itemsArray = new ClickableItem[items.size()]; - itemsArray = items.toArray(itemsArray); - pagination.setItems(itemsArray); - pagination.setItemsPerPage(45); - - pagination.addToIterator(contents.newIterator(SlotIterator.Type.HORIZONTAL, 0, 0)); - - contents.set(5, 0, ClickableItem.of(new ItemBuilder(Material.PLAYER_HEAD, 1) - .setName(__("&7Vorige pagina")) - .setLore(__("&eGa naar de vorige pagina.")) - .setSkullTexture("http://textures.minecraft.net/texture/6e8c3ce2aee6cf2faade7db37bbae73a36627ac1473fef75b410a0af97659f") - .toItemStack(), e -> open(player, pagination.previous().getPage()))); - - contents.set(5, 4, ClickableItem.of(new ItemBuilder(Material.BARRIER, 1) - .setName(__("&7Sluiten")) - .setLore(__("&eSluit dit menu.")) - .toItemStack(), e -> player.closeInventory())); - - contents.set(5, 8, ClickableItem.of(new ItemBuilder(Material.PLAYER_HEAD, 1) - .setName(__("&7Volgende pagina")) - .setLore(__("&eGa naar de volgende pagina.")) - .setSkullTexture("http://textures.minecraft.net/texture/6e8cd53664d9307b6869b9abbae2b7737ab762bb18bb34f31c5ca8f3edb63b6") - .toItemStack(), e -> open(player, pagination.next().getPage()))); + open(p); } } diff --git a/src/main/java/nl/sbdeveloper/showcontrol/utils/Inventory.java b/src/main/java/nl/sbdeveloper/showcontrol/utils/Inventory.java deleted file mode 100644 index 3b5cf53..0000000 --- a/src/main/java/nl/sbdeveloper/showcontrol/utils/Inventory.java +++ /dev/null @@ -1,53 +0,0 @@ -package nl.sbdeveloper.showcontrol.utils; - -import fr.minuskube.inv.InventoryManager; -import fr.minuskube.inv.SmartInventory; -import fr.minuskube.inv.content.InventoryContents; -import fr.minuskube.inv.content.InventoryProvider; -import nl.sbdeveloper.showcontrol.ShowControlPlugin; -import org.bukkit.entity.Player; - -import static nl.sbdeveloper.showcontrol.utils.MainUtil.__; - -public abstract class Inventory implements InventoryProvider { - /* - This file is part of FrogRacing. - Copyright (c) 2018-2021 FrogNetwork - All Rights Reserved - Unauthorized copying of this file, via any medium is strictly prohibited - Proprietary and confidential - Written by Stijn Bannink , March 2020 - */ - - private static InventoryManager manager; - protected SmartInventory inventory; - - public Inventory(int rows, String title) { - this(rows, title, true); //Standaard sluitbaar! - } - - public Inventory(int rows, String title, boolean closeable) { - this.inventory = SmartInventory.builder().id(title).provider(this).manager(manager).size(rows, 9).closeable(closeable).title(__("&8" + title)).build(); - } - - @Override - public void update(Player player, InventoryContents contents) { - // Niet altijd nodig, daarom staat hij hier alvast. - } - - protected void open(Player player) { - this.inventory.open(player); - } - - protected void open(Player player, int page) { - this.inventory.open(player, page); - } - - public void close(Player player) { - this.inventory.close(player); - } - - public static void init() { - manager = new InventoryManager(ShowControlPlugin.getInstance()); - manager.init(); - } -} diff --git a/src/main/java/nl/sbdeveloper/showcontrol/utils/ItemBuilder.java b/src/main/java/nl/sbdeveloper/showcontrol/utils/ItemBuilder.java index 37311ed..4130f54 100644 --- a/src/main/java/nl/sbdeveloper/showcontrol/utils/ItemBuilder.java +++ b/src/main/java/nl/sbdeveloper/showcontrol/utils/ItemBuilder.java @@ -1,601 +1,167 @@ package nl.sbdeveloper.showcontrol.utils; import com.cryptomorin.xseries.SkullUtils; -import org.bukkit.Bukkit; +import com.cryptomorin.xseries.XMaterial; +import lombok.Getter; import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.inventory.meta.SkullMeta; -import org.bukkit.material.MaterialData; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionType; -import java.util.*; -import java.util.function.Function; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.UnaryOperator; -/** - * @author Blutkrone - */ +@Getter public class ItemBuilder { - /* - This file is part of FrogRacing. - Copyright (c) 2018-2021 FrogNetwork - All Rights Reserved - Unauthorized copying of this file, via any medium is strictly prohibited - Proprietary and confidential - Written by Stijn Bannink , March 2020 - */ + private final ItemStack itemStack; - private final ItemStack is; - private static boolean loaded = false; - private static boolean usePotionSetColor = false; + public ItemBuilder(Material material) { + this.itemStack = new ItemStack(material); + } - /** */ - private static void init() { - try { - Class.forName("org.bukkit.Color"); - usePotionSetColor = true; - } catch (Exception e) { - usePotionSetColor = false; + public ItemBuilder(ItemStack stack) { + this.itemStack = stack.clone(); + } + + public ItemBuilder(XMaterial material) { + ItemStack item = material.parseItem(); + if (item == null) { + throw new IllegalArgumentException("Received invalid / unsupported XMaterial: " + material.name()); } - - loaded = true; + this.itemStack = item; } - /** - * Create a new ItemBuilder from scratch. - * - * @param m The material to create the ItemBuilder with. - */ - public ItemBuilder(Material m) { - this(m, 1); + private void applyToMeta(UnaryOperator callback) { + this.itemStack.setItemMeta(callback.apply(this.itemStack.getItemMeta())); } - /** - * Create a new ItemBuilder over an existing itemstack. - * - * @param is The itemstack to create the ItemBuilder over. - */ - public ItemBuilder(ItemStack is) { - this.is = is; + public ItemBuilder amount(int amount) { + this.itemStack.setAmount(amount); + return this; } - /** - * Create a new ItemBuilder from scratch. - * - * @param m The material of the item. - * @param amount The evaluate of the item. - */ - public ItemBuilder(Material m, int amount) { - this(m, amount, (short) 0); + public ItemBuilder displayname(String name) { + applyToMeta(meta -> { + meta.setDisplayName(name); + return meta; + }); + return this; } - /** - * Create a new ItemBuilder from scratch. - * - * @param m The material of the item. - * @param amount The evaluate of the item. - * @param durability The durability of the item. - */ - public ItemBuilder(Material m, int amount, short durability) { - this(m, amount, durability, null); + public ItemBuilder lore(String... lore) { + applyToMeta(meta -> { + List lores = meta.getLore(); + if (lores == null) lores = new ArrayList<>(); + for (String loreString : lore) { + String[] loreParts = loreString.split("[\\r\\n]+"); + Collections.addAll(lores, loreParts); + } + meta.setLore(lores); + return meta; + }); + return this; } - /** - * Create a new ItemBuilder from scratch. - * - * @param m The material of the item. - * @param amount The evaluate of the item. - * @param durability The durability of the item. - */ - public ItemBuilder(Material m, int amount, short durability, Byte data) { - if (!loaded) init(); + public ItemBuilder lore(List lore) { + lore(lore.toArray(String[]::new)); + return this; + } - if (data != null && data > 0 && data <= 15) { - is = new ItemStack(m, amount); - setDurability(durability); + public ItemBuilder flag(ItemFlag... flags) { + applyToMeta(meta -> { + meta.addItemFlags(flags); + return meta; + }); + return this; + } - // Ensure that we only allow applying data on valid objects. - if (is.getType().name().contains("STAINED_GLASS")) { - setData(GlassColor.values()[data]); - } else if (is.getType().name().contains("WOOL")) { - setData(WoolColor.values()[data]); - } else if (is.getType().name().contains("DYE") || DyeColor.is(is.getType())) { - setData(DyeColor.values()[data]); - } else - Bukkit.getLogger().warning("Unable to assign " + m + " a data value!"); + public ItemBuilder hideAllFlags() { + return flag(ItemFlag.values()); + } + + public ItemBuilder customModelData(int customModelData) { + if (XMaterial.supports(13)) { + applyToMeta(meta -> { + meta.setCustomModelData(customModelData); + return meta; + }); + } + return this; + } + + public ItemBuilder enchant(Map enchantments) { + itemStack.addEnchantments(enchantments); + return this; + } + + public ItemBuilder enchant(Enchantment enchantment, int level) { + itemStack.addEnchantment(enchantment, level); + return this; + } + + public ItemBuilder durability(int damage) { + if (XMaterial.supports(13)) { + applyToMeta(meta -> { + if (!(meta instanceof org.bukkit.inventory.meta.Damageable)) return meta; + ((org.bukkit.inventory.meta.Damageable) meta).setDamage(damage); + return meta; + }); } else { - is = new ItemStack(m, amount); - setDurability(durability); - } - } - - public static ItemBuilder create(ItemStack from) { - return new ItemBuilder(from); - } - - public ItemBuilder setData(GlassColor color) { - if (is.getType().name().contains("STAINED_GLASS")) { - color.apply(this); - } else - throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); - return this; - } - - public ItemBuilder setData(WoolColor color) { - if (is.getType().name().contains("WOOL")) { - color.apply(this); - } else - throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); - return this; - } - - public ItemBuilder setData(DyeColor color) { - if (is.getType().name().contains("DYE") || DyeColor.is(is.getType())) { - color.apply(this); - } else - throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); - return this; - } - - public ItemBuilder setData(PotionColor color) { - if (is.getType().name().contains("POTION")) { - color.apply(this); - } else - throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); - return this; - } - - /** - * Clone the ItemBuilder into a new one. - * - * @return The cloned instance. - */ - public ItemBuilder clone() { - return new ItemBuilder(is.clone()); - } - - /** - * Change the durability of the item. - * - * @param dur The durability to set it to. - */ - public ItemBuilder setDurability(short dur) { - if (is.getType() == Material.AIR) return this; - ItemMeta meta = is.getItemMeta(); - if (meta instanceof org.bukkit.inventory.meta.Damageable) { - ((org.bukkit.inventory.meta.Damageable) meta).setDamage(dur); - is.setItemMeta(meta); + itemStack.setDurability((short) damage); } return this; } - /** - * Change the durability of the item. - * - * @param data The durability to set it to. - */ - @Deprecated - public ItemBuilder setData(byte data) { - if (is.getType() == Material.AIR) return this; - is.setData(new MaterialData(is.getType(), data)); - return this; + public ItemBuilder unbreakable() { + return unbreakable(true); } - /** - * Change the unbreakable of the item. - * - * @param unbreakable The unbreakable to set it to. - */ - public ItemBuilder setUnbreakable(boolean unbreakable) { - if (is.getType() == Material.AIR) return this; - ItemMeta meta = is.getItemMeta(); - meta.setUnbreakable(unbreakable); - is.setItemMeta(meta); - return this; - } - - /** - * Change the evaluate of the item. - * - * @param amount The evaluate to set it to. - */ - public ItemBuilder setAmount(int amount) { - if (is.getType() == Material.AIR) return this; - is.setAmount(amount); - return this; - } - - /** - * Add an item flag - * - * @param flags item flagS - */ - public ItemBuilder addFlags(ItemFlag... flags) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - im.addItemFlags(flags); - is.setItemMeta(im); - return this; - } - - /** - * Remove an item flag - * - * @param flags item flagS - */ - public ItemBuilder removeFlags(ItemFlag... flags) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - im.removeItemFlags(flags); - is.setItemMeta(im); - return this; - } - - /** - * Set the displayname of the item. - * - * @param name The name to change it to. - */ - public ItemBuilder setName(String name) { - if (is.getType() == Material.AIR || name == null || name.isEmpty()) return this; - ItemMeta im = is.getItemMeta(); - im.setDisplayName(name); - is.setItemMeta(im); - return this; - } - - /** - * Add an unsafe enchantment. - * - * @param ench The enchantment to add. - * @param level The level to put the enchant on. - */ - public ItemBuilder addUnsafeEnchantment(Enchantment ench, int level) { - if (is.getType() == Material.AIR) return this; - is.addUnsafeEnchantment(ench, level); - return this; - } - - /** - * Remove a certain enchant from the item. - * - * @param ench The enchantment to remove - */ - public ItemBuilder removeEnchantment(Enchantment ench) { - if (is.getType() == Material.AIR) return this; - is.removeEnchantment(ench); + public ItemBuilder unbreakable(boolean unbreakable) { + applyToMeta(meta -> { + meta.setUnbreakable(unbreakable); + return meta; + }); return this; } public ItemBuilder glow() { - if (is.getType() == Material.AIR) return this; - is.addUnsafeEnchantment(Enchantment.LURE, 0); - ItemMeta im = is.getItemMeta(); - im.addItemFlags(ItemFlag.HIDE_ENCHANTS); - is.setItemMeta(im); + this.itemStack.addUnsafeEnchantment(Enchantment.LURE, 0); + flag(ItemFlag.HIDE_ENCHANTS); return this; } - /** - * Set the skull getOwner for the item. Works on skulls only. - * - * @param owner The name of the skull's getOwner. - */ - public ItemBuilder setSkullOwner(String owner) { - if (is.getType() == Material.AIR) return this; - try { - SkullMeta im = (SkullMeta) is.getItemMeta(); - im.setOwner(owner); - is.setItemMeta(im); - } catch (ClassCastException expected) { - } + public ItemBuilder skullTexture(String identifier) { + applyToMeta(meta -> { + SkullUtils.applySkin(meta, identifier); + return meta; + }); return this; } - public ItemBuilder setSkullTexture(String url) { - if (is.getType() == Material.AIR) return this; - is.setItemMeta(SkullUtils.applySkin(is.getItemMeta(), url)); + public ItemBuilder armorColor(Color color) { + applyToMeta(meta -> { + if (!(meta instanceof LeatherArmorMeta)) return meta; + ((LeatherArmorMeta) meta).setColor(color); + return meta; + }); return this; } - /** - * Add an enchant to the item. - * - * @param ench The enchant to add - * @param level The level - */ - public ItemBuilder addEnchant(Enchantment ench, int level) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - im.addEnchant(ench, level, true); - is.setItemMeta(im); + public ItemBuilder potionEffect(PotionType type) { + if (!itemStack.getType().name().contains("POTION")) throw new UnsupportedOperationException("ItemStack is not a potion! (Type: " + itemStack.getType().name() + ")"); + applyToMeta(meta -> { + if (!(meta instanceof org.bukkit.inventory.meta.PotionMeta)) return meta; + ((org.bukkit.inventory.meta.PotionMeta) meta).setBasePotionData(new PotionData(type)); + return meta; + }); return this; } - - /** - * Add multiple enchants at once. - * - * @param enchantments The enchants to add. - */ - public ItemBuilder addEnchantments(Map enchantments) { - if (is.getType() == Material.AIR) return this; - is.addEnchantments(enchantments); - return this; - } - - /** - * Sets infinity durability on the item by setting the durability to Short.MAX_VALUE. - */ - public ItemBuilder setInfinityDurability() { - if (is.getType() == Material.AIR) return this; - is.setDurability(Short.MAX_VALUE); - return this; - } - - /** - * Re-sets the lore. - * - * @param lore The lore to set it to. - */ - public ItemBuilder setLore(String... lore) { - if (is.getType() == Material.AIR || lore == null) return this; - ItemMeta im = is.getItemMeta(); - im.setLore(Arrays.asList(lore)); - is.setItemMeta(im); - return this; - } - - /** - * Re-sets the lore. - * - * @param lore The lore to set it to. - */ - public ItemBuilder setLore(List lore) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - im.setLore(lore); - is.setItemMeta(im); - return this; - } - - /** - * Remove a lore line. - * - * @param line The lore to remove. - */ - public ItemBuilder removeLoreLine(String line) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - List lore = new ArrayList<>(im.getLore()); - if (!lore.contains(line)) return this; - lore.remove(line); - im.setLore(lore); - is.setItemMeta(im); - return this; - } - - /** - * Remove a lore line. - * - * @param index The index of the lore line to remove. - */ - public ItemBuilder removeLoreLine(int index) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - List lore = new ArrayList<>(im.getLore()); - if (index < 0 || index > lore.size()) return this; - lore.remove(index); - im.setLore(lore); - is.setItemMeta(im); - return this; - } - - /** - * Add a lore line. - * - * @param line The lore line to add. - */ - public ItemBuilder addLoreLine(String line) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - List lore = new ArrayList<>(); - if (im.hasLore()) lore = new ArrayList<>(im.getLore()); - lore.add(line); - im.setLore(lore); - is.setItemMeta(im); - return this; - } - - public ItemBuilder addLoreLine(String... lines) { - if (is.getType() == Material.AIR) return this; - for (String line : lines) { - addLoreLine(line); - } - return this; - } - - public ItemBuilder addLoreLine(Collection lines) { - if (is.getType() == Material.AIR) return this; - for (String line : lines) { - addLoreLine(line); - } - return this; - } - - /** - * Add a lore line. - * - * @param line The lore line to add. - * @param pos The index of where to put it. - */ - public ItemBuilder addLoreLine(String line, int pos) { - if (is.getType() == Material.AIR) return this; - ItemMeta im = is.getItemMeta(); - List lore = new ArrayList<>(im.getLore()); - lore.set(pos, line); - im.setLore(lore); - is.setItemMeta(im); - return this; - } - - /** - * Direct access to the item meta - * - * @param modifier function to modify the meta. - */ - public ItemBuilder writeMeta( - Function modifier - ) { - if (is.getType() == Material.AIR) return this; - is.setItemMeta(modifier.apply(is.getItemMeta())); - return this; - } - - /** - * Retrieves the itemstack from the ItemBuilder. - * - * @return The itemstack created/modified by the ItemBuilder instance. - */ - public ItemStack toItemStack() { - return is; - } - - public enum GlassColor { - WHITE, - ORANGE, - MAGENTA, - LIGHT_BLUE, - YELLOW, - LIME, - PINK, - GRAY, - LIGHT_GRAY, - CYAN, - PURPLE, - BLUE, - BROWN, - GREEN, - RED, - BLACK; - - private void apply(ItemBuilder builder) { - boolean panel = builder.is.getType().name().contains("STAINED_GLASS_PANE"); - builder.is.setType(Material.valueOf(name() + (panel ? "_STAINED_GLASS_PANE" : "STAINED_GLASS"))); - } - } - - public enum WoolColor { - WHITE, - ORANGE, - MAGENTA, - LIGHT_BLUE, - YELLOW, - LIME, - PINK, - GRAY, - LIGHT_GRAY, - CYAN, - PURPLE, - BLUE, - BROWN, - GREEN, - RED, - BLACK; - - private void apply(ItemBuilder builder) { - builder.is.setType(Material.valueOf(name() + "_WOOL")); - } - } - - public enum DyeColor { - BLACK(0, "INK_SACK"), - RED(1, "ROSE_RED"), - GREEN(2, "CACTUS_GREEN"), - BROWN(3, "COCOA_BEANS"), - BLUE(4, "LAPIS_LAZULI"), - PURPLE(5, "PURPLE_DYE"), - CYAN(6, "CYAN_DYE"), - LIGHT_GRAY(7, "LIGHT_GRAY_DYE"), - GRAY(8, "GRAY_DYE"), - PINK(9, "PINK_DYE"), - LIME(10, "LIME_DYE"), - YELLOW(11, "DANDELION_YELLOW"), - LIGHT_BLUE(12, "LIGHT_BLUE_DYE"), - MAGENTA(13, "MAGENTA_DYE"), - ORANGE(14, "ORANGE_DYE"), - WHITE(15, "BONE_MEAL"); - - private final String coded; - - DyeColor(int data, String coded) { - this.coded = coded; - } - - static boolean is(Material mat) { - if (mat.name().contains("DYE")) return true; - if (mat.name().contains("INK_SAC")) return true; - - for (DyeColor dyeColor : values()) { - if (dyeColor.name().equalsIgnoreCase(mat.name())) - return true; - } - - return false; - } - - private void apply(ItemBuilder builder) { - builder.is.setType(Material.valueOf(coded)); - } - } - - public enum PotionColor { - PINK("REGEN", 0xF442E2), - CYAN("SPEED", 0x42f1f4), - GOLD("FIRE_RESISTANCE", 0xf4b942), - DARK_GREEN("POISON", 0x365b0e), - RED("INSTANT_HEAL", 0xe5251b), - DARK_BLUE("NIGHT_VISION", 0x092366), - DARK_GRAY("WEAKNESS", 0x2e2f33), - DARK_RED("STRENGTH", 0x4f0a01), - GRAY("SLOWNESS", 0x939393), - LIGHT_GREEN("JUMP", 0x42f4b0), - BROWN("INSTANT_DAMAGE", 0xa54126), - BLUE("WATER_BREATHING", 0x0e59ef), // TEAL - LIGHT_GRAY("INVISIBILITY", 0xc4c4c4), - GREEN("LUCK", 0x1f890f), - BLACK(null, 0x161616), - LIGHT_BROWN("TURTLE_MASTER", 0xad581b), - SILVER("SLOW_FALLING", 0xd8ccc3); - - private final String potionType; - private final int rgb; - - PotionColor(String potionType, int rgb) { - this.potionType = potionType; - this.rgb = rgb; - } - - private void apply(ItemBuilder builder) { - PotionMeta meta = (PotionMeta) builder.is.getItemMeta(); - if (usePotionSetColor) { - meta.setColor(Color.fromRGB(rgb)); - } else if (potionType != null) { - try { - meta.setBasePotionData(new org.bukkit.potion.PotionData(PotionType.valueOf(potionType))); - } catch (Exception e) { - Bukkit.getLogger().severe("Unknown Potion Color: " + this + ", " + - "the underlying " + potionType + " handle isn't available in this version!"); - } - } - builder.is.setItemMeta(meta); - } - } - - public enum FireworkColor { - - } -} - +} \ No newline at end of file diff --git a/src/main/java/nl/sbdeveloper/showcontrol/utils/MainUtil.java b/src/main/java/nl/sbdeveloper/showcontrol/utils/MainUtil.java index b768ac4..3cfbd30 100644 --- a/src/main/java/nl/sbdeveloper/showcontrol/utils/MainUtil.java +++ b/src/main/java/nl/sbdeveloper/showcontrol/utils/MainUtil.java @@ -17,9 +17,6 @@ public class MainUtil { public static ItemStack pointToItem(ShowCuePoint point) { TriggerIdentifier identifier = point.getTask().getClass().getAnnotation(TriggerIdentifier.class); - ItemBuilder builder = new ItemBuilder(identifier.item()); - builder.setName(ChatColor.ITALIC + "TimeCode: " + TimeUtil.makeReadable(point.getTime())); - List lores = new ArrayList<>(); lores.add(ChatColor.GREEN + "Type: " + ChatColor.AQUA + capitalize(point.getTask().getTriggerId())); lores.add(ChatColor.GREEN + "Data:"); @@ -29,8 +26,9 @@ public class MainUtil { lores.add(""); lores.add(ChatColor.RED + ChatColor.BOLD.toString() + "Click to remove!"); - builder.setLore(lores); - return builder.toItemStack(); + return new ItemBuilder(identifier.item()) + .displayname(ChatColor.ITALIC + "TimeCode: " + TimeUtil.makeReadable(point.getTime())) + .lore(lores).getItemStack(); } public static String capitalize(String str) { diff --git a/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/Inventory.java b/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/Inventory.java new file mode 100644 index 0000000..a8550c9 --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/Inventory.java @@ -0,0 +1,59 @@ +package nl.sbdeveloper.showcontrol.utils.inventories; + +import com.cryptomorin.xseries.XMaterial; +import fr.minuskube.inv.ClickableItem; +import fr.minuskube.inv.InventoryManager; +import fr.minuskube.inv.SmartInventory; +import fr.minuskube.inv.content.InventoryContents; +import fr.minuskube.inv.content.InventoryProvider; +import nl.sbdeveloper.showcontrol.utils.ItemBuilder; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import static nl.sbdeveloper.showcontrol.utils.MainUtil.__; + +public abstract class Inventory implements InventoryProvider { + private static InventoryManager manager; + protected SmartInventory inventory; + private final boolean filler; + + public Inventory(int rows, String title, boolean filler) { + this(rows, title, filler, true); + } + + public Inventory(int rows, String title, boolean filler, boolean closeable) { + if (rows < 1 || rows > 6) { + throw new IllegalArgumentException("Amount of rows must be between 1 and 6"); + } + + this.inventory = SmartInventory.builder().id(title).provider(this).manager(manager).size(rows, 9).closeable(closeable).title(__("&8" + title)).build(); + this.filler = filler; + } + + protected void open(Player player) { + this.inventory.open(player); + } + + protected void open(Player player, int page) { + this.inventory.open(player, page); + } + + public void close(Player player) { + this.inventory.close(player); + } + + public static void init(JavaPlugin plugin) { + manager = new InventoryManager(plugin); + manager.init(); + } + + @Override + public void init(Player player, InventoryContents inventoryContents) { + if (filler) { + inventoryContents.fill(ClickableItem.empty(new ItemBuilder(XMaterial.GRAY_STAINED_GLASS_PANE.parseItem()).displayname(__("&r")).getItemStack())); + } + addItems(player, inventoryContents); + } + + public abstract void addItems(Player player, InventoryContents contents); +} diff --git a/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/PaginationInventory.java b/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/PaginationInventory.java new file mode 100644 index 0000000..19295ca --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showcontrol/utils/inventories/PaginationInventory.java @@ -0,0 +1,85 @@ +package nl.sbdeveloper.showcontrol.utils.inventories; + +import com.cryptomorin.xseries.XMaterial; +import fr.minuskube.inv.ClickableItem; +import fr.minuskube.inv.content.InventoryContents; +import fr.minuskube.inv.content.Pagination; +import fr.minuskube.inv.content.SlotIterator; +import nl.sbdeveloper.showcontrol.utils.ItemBuilder; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +import static nl.sbdeveloper.showcontrol.utils.MainUtil.__; + +public abstract class PaginationInventory extends Inventory { + private final int paginationRows; //Amount of rows for pagination (excluding button row) + private final int paginationRow; //The start row of the pagination (starts from 0) + private final List items = new ArrayList<>(); + private Pagination pagination; //Available after init() is called, can only be used in the event of a ClickableItem!!! + + private final int staticRows; //Amount of rows that are static + private final boolean filler; + + protected PaginationInventory(int paginationRows, String title) { + //Default full-inventory pagination + this(paginationRows, 0, 0, title, false); + } + + protected PaginationInventory(int paginationRows, int staticRows, String title, boolean filler) { + this(paginationRows, 0, staticRows, title, filler); + } + + protected PaginationInventory(int paginationRows, int paginationRow, int staticRows, String title, boolean filler) { + //Custom pagination with static rows + super(paginationRows + 1 + staticRows, title, false); + this.paginationRows = paginationRows; + this.paginationRow = paginationRow; + this.staticRows = staticRows; + this.filler = filler; + } + + public void addItem(ClickableItem item) { + this.items.add(item); + } + + public void refresh(Player player) { + open(player, pagination.getPage()); + } + + @Override + public void addItems(Player player, InventoryContents contents) { + //Set up static items + if (filler) { + //Fill all static rows + for (int i = paginationRows + 1; i <= paginationRows + staticRows; i++) { + contents.fillRow(i, ClickableItem.empty(new ItemBuilder(XMaterial.GRAY_STAINED_GLASS_PANE.parseItem()).displayname(__("&r")).getItemStack())); + } + } + addStaticItems(player, contents); + + //Set up pagination + pagination = contents.pagination(); + pagination.setItems(items.toArray(ClickableItem[]::new)); + pagination.setItemsPerPage(9 * paginationRows); + pagination.addToIterator(contents.newIterator(SlotIterator.Type.HORIZONTAL, paginationRow, 0)); + + contents.set(paginationRows, 0, ClickableItem.of(new ItemBuilder(XMaterial.ARROW.parseItem()).displayname(__("&6First page")) + .lore(__("&fGo to the first page (" + pagination.first().getPage() + ")")).getItemStack(), + e -> open(player, pagination.first().getPage()))); + contents.set(paginationRows, 3, ClickableItem.of(new ItemBuilder(XMaterial.OAK_SIGN.parseItem()).displayname(__("&6Previous")) + .lore(__("&fGo to the previous page (" + pagination.previous().getPage() + ")")).getItemStack(), + e -> open(player, pagination.previous().getPage()))); + contents.set(paginationRows, 5, ClickableItem.of(new ItemBuilder(XMaterial.OAK_SIGN.parseItem()).displayname(__("&6Next")) + .lore(__("&fGo to the next page (" + pagination.next().getPage() + ")")).getItemStack(), + e -> open(player, pagination.next().getPage()))); + contents.set(paginationRows, 8, ClickableItem.of(new ItemBuilder(XMaterial.ARROW.parseItem()).displayname(__("&6Last page")) + .lore(__("&fGo to the last page (" + pagination.last().getPage() + ")")).getItemStack(), + e -> open(player, pagination.last().getPage()))); + } + + public void addStaticItems(Player player, InventoryContents contents) { + //Override this method to add static items + } +} \ No newline at end of file