From 282b8b52089b26d7d79e2313b14c67d30c66e60e Mon Sep 17 00:00:00 2001 From: Rayzr522 Date: Tue, 1 Nov 2016 15:23:42 -0400 Subject: [PATCH] v1.0 - Compatible with ChestCommand v3.1.4 --- .gitignore | 16 + lib/README.md | 2 + pom.xml | 59 ++++ .../filoghost/chestcommands/api/Icon.java | 334 ++++++++++++++++++ .../papichestcommands/PAPIChestCommands.java | 14 + src/main/resources/plugin.yml | 14 + 6 files changed, 439 insertions(+) create mode 100644 .gitignore create mode 100644 lib/README.md create mode 100644 pom.xml create mode 100644 src/main/java/com/gmail/filoghost/chestcommands/api/Icon.java create mode 100644 src/main/java/com/perceivedev/papichestcommands/PAPIChestCommands.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3fc31cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +/bin/ +/target/ +.classpath +.project +.settings +.metadata +*.jardesc +.idea +.recommenders +*.iml +*.eml +.DS_Store +META-INF +out + +lib/ChestCommands.jar diff --git a/lib/README.md b/lib/README.md new file mode 100644 index 0000000..ca7da81 --- /dev/null +++ b/lib/README.md @@ -0,0 +1,2 @@ +# Building +Place your `ChestCommands.jar` file in this folder. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..917be0d --- /dev/null +++ b/pom.xml @@ -0,0 +1,59 @@ + + 4.0.0 + com.perceivedev + papichestcommands + 1.0 + PAPI-ChestCommands + A bukkit plugin to allow ChestCommands to use PlaceholderAPI placeholders + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + placeholderapi + http://repo.extendedclip.com/content/repositories/placeholderapi/ + + + + + org.bukkit + bukkit + 1.10.2-R0.1-SNAPSHOT + provided + + + me.clip + placeholderapi + 2.0.8 + provided + + + chestCommands-local + filoghost + 3.1.4 + ${basedir}/lib/ChestCommands.jar + system + + + + + clean compile test package + ${project.name}-${project.version} + ${basedir}/src/main/java + + + ${basedir}/src/main/resources + + *.yml + + + + + diff --git a/src/main/java/com/gmail/filoghost/chestcommands/api/Icon.java b/src/main/java/com/gmail/filoghost/chestcommands/api/Icon.java new file mode 100644 index 0000000..bfa5765 --- /dev/null +++ b/src/main/java/com/gmail/filoghost/chestcommands/api/Icon.java @@ -0,0 +1,334 @@ + +package com.gmail.filoghost.chestcommands.api; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.bukkit.ChatColor; +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.LeatherArmorMeta; +import org.bukkit.inventory.meta.SkullMeta; + +import com.gmail.filoghost.chestcommands.internal.Variable; +import com.gmail.filoghost.chestcommands.util.Utils; + +import me.clip.placeholderapi.PlaceholderAPI; + +public class Icon { + + private Material material; + private int amount; + private short dataValue; + + private String name; + private List lore; + private Map enchantments; + private Color color; + private String skullOwner; + + protected boolean closeOnClick; + private ClickHandler clickHandler; + + private Set nameVariables; + private Map> loreVariables; + private ItemStack cachedItem; // When + // there + // are + // no + // variables, + // we + // don't + // recreate + // the + // item. + + public Icon() { + enchantments = new HashMap(); + closeOnClick = true; + } + + public boolean hasVariables() { + return nameVariables != null || loreVariables != null; + } + + public void setMaterial(Material material) { + if (material == Material.AIR) + material = null; + this.material = material; + } + + public Material getMaterial() { + return material; + } + + public void setAmount(int amount) { + if (amount < 1) + amount = 1; + else if (amount > 127) + amount = 127; + + this.amount = amount; + } + + public int getAmount() { + return amount; + } + + public void setDataValue(short dataValue) { + if (dataValue < 0) + dataValue = 0; + + this.dataValue = dataValue; + } + + public short getDataValue() { + return dataValue; + } + + public void setName(String name) { + this.name = name; + this.nameVariables = null; // Reset the variables + + if (name != null) { + for (Variable variable : Variable.values()) { + if (name.contains(variable.getText())) { + + if (nameVariables == null) { + nameVariables = new HashSet(); + } + + nameVariables.add(variable); + } + } + } + } + + public boolean hasName() { + return name != null; + } + + public void setLore(String... lore) { + if (lore != null) { + setLore(Arrays.asList(lore)); + } + } + + public void setLore(List lore) { + this.lore = lore; + this.loreVariables = null; // Reset the variables + + if (lore != null) { + for (int i = 0; i < lore.size(); i++) { + for (Variable variable : Variable.values()) { + if (lore.get(i).contains(variable.getText())) { + + if (loreVariables == null) { + loreVariables = new HashMap>(); + } + + Set lineVariables = loreVariables.get(i); + + if (lineVariables == null) { + lineVariables = new HashSet(); + loreVariables.put(i, lineVariables); + } + + lineVariables.add(variable); + } + } + } + } + } + + public boolean hasLore() { + return lore != null && lore.size() > 0; + } + + public List getLore() { + return lore; + } + + public void setEnchantments(Map enchantments) { + if (enchantments == null) { + this.enchantments.clear(); + return; + } + this.enchantments = enchantments; + } + + public Map getEnchantments() { + return new HashMap(enchantments); + } + + public void addEnchantment(Enchantment ench) { + addEnchantment(ench, 1); + } + + public void addEnchantment(Enchantment ench, Integer level) { + enchantments.put(ench, level); + } + + public void removeEnchantment(Enchantment ench) { + enchantments.remove(ench); + } + + public void clearEnchantments() { + enchantments.clear(); + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + public String getSkullOwner() { + return skullOwner; + } + + public void setSkullOwner(String skullOwner) { + this.skullOwner = skullOwner; + } + + public void setCloseOnClick(boolean closeOnClick) { + this.closeOnClick = closeOnClick; + } + + public void setClickHandler(ClickHandler clickHandler) { + this.clickHandler = clickHandler; + } + + public ClickHandler getClickHandler() { + return clickHandler; + } + + protected String calculateName(Player pov) { + if (hasName()) { + + String name = this.name; + + if (pov != null && nameVariables != null) { + for (Variable nameVariable : nameVariables) { + name = name.replace(nameVariable.getText(), nameVariable.getReplacement(pov)); + } + } + + if (name.isEmpty()) { + // Add a color to display the name empty. + return ChatColor.WHITE.toString(); + } else { + return name; + } + } + + return null; + } + + protected List calculateLore(Player pov) { + + List output = null; + + if (hasLore()) { + + List placeholders = PlaceholderAPI.setPlaceholders(pov, lore); + + output = Utils.newArrayList(); + + if (pov != null && loreVariables != null) { + for (int i = 0; i < placeholders.size(); i++) { + String line = placeholders.get(i); + + Set lineVariables = loreVariables.get(i); + if (lineVariables != null) { + for (Variable lineVariable : lineVariables) { + line = line.replace(lineVariable.getText(), lineVariable.getReplacement(pov)); + } + } + + output.add(line); + } + } else { + // Otherwise just copy the lines. + output.addAll(placeholders); + } + } + + if (material == null) { + + if (output == null) { + output = Utils.newArrayList(); + } + + // Add an error message. + output.add(ChatColor.RED + "(Invalid material)"); + } + + return output; + } + + public ItemStack createItemstack(Player pov) { + + if (!this.hasVariables() && cachedItem != null) { + // Performance. + return cachedItem; + } + + // If the material is not set, display BEDROCK. + ItemStack itemStack = (material != null) ? new ItemStack(material, amount, dataValue) + : new ItemStack(Material.BEDROCK, amount); + + // Apply name, lore and color. + ItemMeta itemMeta = itemStack.getItemMeta(); + + itemMeta.setDisplayName(calculateName(pov)); + itemMeta.setLore(calculateLore(pov)); + + if (color != null && itemMeta instanceof LeatherArmorMeta) { + ((LeatherArmorMeta) itemMeta).setColor(color); + } + + if (skullOwner != null && itemMeta instanceof SkullMeta) { + ((SkullMeta) itemMeta).setOwner(skullOwner); + } + + itemStack.setItemMeta(itemMeta); + + // Apply enchants. + if (enchantments.size() > 0) { + for (Entry entry : enchantments.entrySet()) { + itemStack.addUnsafeEnchantment(entry.getKey(), entry.getValue()); + } + } + + if (!this.hasVariables()) { + // If there are no variables, cache the item. + cachedItem = itemStack; + } + + return itemStack; + } + + public boolean onClick(Player whoClicked) { + if (clickHandler != null) { + return clickHandler.onClick(whoClicked); + } + + return closeOnClick; + } + + public static Class inject() { + return Icon.class; + } +} diff --git a/src/main/java/com/perceivedev/papichestcommands/PAPIChestCommands.java b/src/main/java/com/perceivedev/papichestcommands/PAPIChestCommands.java new file mode 100644 index 0000000..a822bac --- /dev/null +++ b/src/main/java/com/perceivedev/papichestcommands/PAPIChestCommands.java @@ -0,0 +1,14 @@ +package com.perceivedev.papichestcommands; + +import org.bukkit.plugin.java.JavaPlugin; + +import com.gmail.filoghost.chestcommands.api.Icon; + +public class PAPIChestCommands extends JavaPlugin { + + @Override + public void onEnable() { + getLogger().info("Injected into `" + Icon.inject().getCanonicalName() + "`"); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..430b277 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,14 @@ +main: com.perceivedev.papichestcommands.PAPIChestCommands +name: PAPI-ChestCommands +version: 1.0 +description: A bukkit plugin to allow ChestCommands to use PlaceholderAPI placeholders +author: [Rayzr] +depend: [PlaceholderAPI] +loadbefore: [ChestCommands] +load: STARTUP +#commands: +# uskyfix-chestcommands: +# description: uSkyFix-ChestCommands main command +# aliases: [alias1,alias2] +# usage: "/uskyfix-chestcommands [command]" +# permission: uSkyFix-ChestCommands.main