From ec1fe83f186a339c5fa550b01647e9a4f1346d56 Mon Sep 17 00:00:00 2001 From: Kai Date: Fri, 27 May 2016 11:03:35 +0200 Subject: [PATCH] Added Custom Items Name (supports 1.9.4 potions!) --- .../org/maxgamer/quickshop/QuickShop.java | 58 +++++++++++++- .../quickshop/Util/CustomItemName.java | 75 +++++++++++++++++++ .../org/maxgamer/quickshop/Util/Util.java | 14 ++++ src/main/resources/config.yml | 9 ++- 4 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/maxgamer/quickshop/Util/CustomItemName.java diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index d73fbf26a..aeb2ee270 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -10,12 +10,14 @@ import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Map.Entry; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; @@ -23,6 +25,8 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionType; import org.bukkit.scheduler.BukkitTask; import org.maxgamer.quickshop.Command.QS; import org.maxgamer.quickshop.Database.Database; @@ -45,12 +49,14 @@ import org.maxgamer.quickshop.Shop.ContainerShop; import org.maxgamer.quickshop.Shop.Shop; import org.maxgamer.quickshop.Shop.ShopManager; import org.maxgamer.quickshop.Shop.ShopType; +import org.maxgamer.quickshop.Util.CustomItemName; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; import org.maxgamer.quickshop.Watcher.ItemWatcher; import org.maxgamer.quickshop.Watcher.LogWatcher; +@SuppressWarnings("deprecation") public class QuickShop extends JavaPlugin { /** The active instance of QuickShop */ public static QuickShop instance; @@ -96,6 +102,8 @@ public class QuickShop extends JavaPlugin { // private Metrics metrics; /** Whether debug info should be shown in the console */ public static boolean debug = false; + + private Map customItemNames = new HashMap(); /** The plugin metrics from Hidendra */ // public Metrics getMetrics(){ return metrics; } @@ -140,6 +148,51 @@ public class QuickShop extends JavaPlugin { LockListener ll = new LockListener(this); getServer().getPluginManager().registerEvents(ll, this); } + + if (this.getConfig().isSet("custom-names")) { + for (String s : this.getConfig().getStringList("custom-names")) { + try { + String[] mainVal = s.split("[;]"); + if (mainVal.length!=3) { + throw new Exception("Invalid format (main args length must be 3)"); + } + + String[] matVal = mainVal[0].split("[:]"); + Material material = Material.matchMaterial(matVal[0]); + if (material==null) { + throw new Exception("Invalid material: "+matVal[0]); + } + + ItemStack is; + if (matVal.length>1) { + if (material==Material.POTION) { + PotionType potionType = PotionType.valueOf(matVal[1].toUpperCase()); + if (potionType==null) { + throw new Exception("Invalid PotionType: "+matVal[1]); + } + is = new Potion(potionType).toItemStack(1); + } else { + try { + is = new ItemStack(material, 1, Short.valueOf(matVal[1])); + } catch (Exception e) { + throw new Exception("Invalid data value: "+matVal[1]); + } + } + } else { + is = new ItemStack(material, 1, (short) 0); + } + + if (mainVal[1].length()>16) { + Bukkit.getLogger().warning("Custom item name definition {"+s+"} sign name is longer than 16 characters. Only the first 16 characters will be shown."); + } + + customItemNames.put(is, new CustomItemName(is, mainVal[1], mainVal[2])); + } catch (Exception e) { + Bukkit.getLogger().warning("Invalid custom item name definition {"+s+"} Error: "+e.getMessage()); + } + } + } + ConfigurationSection limitCfg = this.getConfig().getConfigurationSection("limits"); if (limitCfg != null) { getLogger().info("Limit cfg found..."); @@ -215,7 +268,6 @@ public class QuickShop extends JavaPlugin { ownerUUID = UUID.fromString(owner); } catch (IllegalArgumentException e) { // This could be old data to be converted... check if it's a player - @SuppressWarnings("deprecation") OfflinePlayer player = Bukkit.getOfflinePlayer(owner); if (player.hasPlayedBefore()) { ownerUUID = player.getUniqueId(); @@ -414,4 +466,8 @@ public class QuickShop extends JavaPlugin { public ShopManager getShopManager() { return this.shopManager; } + + public Map getCustomItemNames() { + return customItemNames; + } } \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/Util/CustomItemName.java b/src/main/java/org/maxgamer/quickshop/Util/CustomItemName.java new file mode 100644 index 000000000..bcf4d2a08 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Util/CustomItemName.java @@ -0,0 +1,75 @@ +package org.maxgamer.quickshop.Util; + +import org.bukkit.inventory.ItemStack; + +public class CustomItemName { + private ItemStack itemStack; + private String signName, fullName; + + public CustomItemName(ItemStack itemStack, String signName, String fullName) { + this.itemStack = itemStack; + this.signName = signName; + this.fullName = fullName; + } + + public ItemStack getItemStack() { + return itemStack; + } + public String getSignName() { + return signName; + } + public String getFullName() { + return fullName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((fullName == null) ? 0 : fullName.hashCode()); + result = prime * result + ((itemStack == null) ? 0 : itemStack.getType().hashCode()); + result = prime * result + ((signName == null) ? 0 : signName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof CustomItemName)) { + return false; + } + CustomItemName other = (CustomItemName) obj; + if (fullName == null) { + if (other.fullName != null) { + return false; + } + } else if (!fullName.equals(other.fullName)) { + return false; + } + if (itemStack == null) { + if (other.itemStack != null) { + return false; + } + } else if (!itemStack.isSimilar(other.itemStack)) { + return false; + } + if (signName == null) { + if (other.signName != null) { + return false; + } + } else if (!signName.equals(other.signName)) { + return false; + } + return true; + } + + @Override + public String toString() { + return "CustomItemName [itemStack=" + itemStack + ", signName=" + signName + ", fullName=" + fullName + "]"; + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index 838fe0de6..ecf7777b5 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -368,6 +368,13 @@ public class Util { * @return The human readable item name. */ public static String getName(ItemStack i) { + ItemStack is = i.clone(); + is.setAmount(1); + CustomItemName customItemName = QuickShop.instance.getCustomItemNames().get(is); + if (customItemName!=null) { + return customItemName.getFullName(); + } + String vanillaName = getDataName(i.getType(), i.getDurability()); return prettifyText(vanillaName); } @@ -396,6 +403,13 @@ public class Util { // Let's make very long names shorter for our sign public static String getNameForSign(ItemStack itemStack) { + ItemStack is = itemStack.clone(); + is.setAmount(1); + CustomItemName customItemName = QuickShop.instance.getCustomItemNames().get(is); + if (customItemName!=null) { + return customItemName.getSignName(); + } + String name = getDataName(itemStack.getType(), itemStack.getDurability()); String[] nameParts = name.split("_"); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index d2cc3baf5..12b929020 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -87,9 +87,16 @@ shop: #List of items to disallow selling of. Anyone with quickshop.bypass. can bypass it blacklist: - - 7 #Bedrock +- 7 #Bedrock #List of items with restricted prices. Format: ;; price-restriction: - Bedrock;100;200 + +#Custom names for items. Format: ;; +# PotionType: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/potion/PotionType.html +custom-names: +- NETHER_STALK;Nether Wart;Nether Wart +- POTION:INVISIBILITY;Invis.Potion;Invisibility Potion +