diff --git a/pom.xml b/pom.xml index 20cb5d4..60924c4 100644 --- a/pom.xml +++ b/pom.xml @@ -140,23 +140,6 @@ - - info.somethingodd - odditem - 0.9.5 - provided - - - org.bukkit - bukkit - - - org.bukkit - craftbukkit - - - - com.herocraftonline.heroes diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar deleted file mode 100644 index b8ecdcb..0000000 Binary files a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar and /dev/null differ diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.md5 b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.md5 deleted file mode 100644 index 4308693..0000000 --- a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.md5 +++ /dev/null @@ -1 +0,0 @@ -c7b3b9b7cd7cd27a50c079ce4349f628 \ No newline at end of file diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.sha1 b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.sha1 deleted file mode 100644 index 9da414f..0000000 --- a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.jar.sha1 +++ /dev/null @@ -1 +0,0 @@ -697ff692f06dc9064badb2610fd52399651b8274 \ No newline at end of file diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom deleted file mode 100644 index 0b1fc3f..0000000 --- a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom +++ /dev/null @@ -1,9 +0,0 @@ - - - 4.0.0 - info.somethingodd - odditem - 0.9.5 - POM was created from install:install-file - diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.md5 b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.md5 deleted file mode 100644 index 0d62156..0000000 --- a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.md5 +++ /dev/null @@ -1 +0,0 @@ -9fa8325983cb90581156734d0500aacb \ No newline at end of file diff --git a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.sha1 b/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.sha1 deleted file mode 100644 index 912fe62..0000000 --- a/repo/info/somethingodd/odditem/0.9.5/odditem-0.9.5.pom.sha1 +++ /dev/null @@ -1 +0,0 @@ -288ed3c67d66d532eac2128c120a6d74986823d6 \ No newline at end of file diff --git a/repo/info/somethingodd/odditem/maven-metadata-local.xml b/repo/info/somethingodd/odditem/maven-metadata-local.xml deleted file mode 100644 index fb133c4..0000000 --- a/repo/info/somethingodd/odditem/maven-metadata-local.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - info.somethingodd - odditem - - 0.9.5 - - 0.9.5 - - 20130826223956 - - diff --git a/repo/info/somethingodd/odditem/maven-metadata-local.xml.md5 b/repo/info/somethingodd/odditem/maven-metadata-local.xml.md5 deleted file mode 100644 index 8ad04cd..0000000 --- a/repo/info/somethingodd/odditem/maven-metadata-local.xml.md5 +++ /dev/null @@ -1 +0,0 @@ -7240080156e6cd8df6f550049950a99c \ No newline at end of file diff --git a/repo/info/somethingodd/odditem/maven-metadata-local.xml.sha1 b/repo/info/somethingodd/odditem/maven-metadata-local.xml.sha1 deleted file mode 100644 index 04f7cdd..0000000 --- a/repo/info/somethingodd/odditem/maven-metadata-local.xml.sha1 +++ /dev/null @@ -1 +0,0 @@ -23208c6e99b6df3664c26f771fa941a37387f428 \ No newline at end of file diff --git a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java index 68fe312..67be41f 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java @@ -4,13 +4,11 @@ import com.Acrobot.Breeze.Collection.SimpleCache; import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; -import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.MaterialParseEvent; +import com.Acrobot.ChestShop.Utils.ItemUtil; import de.themoep.ShowItem.api.ShowItem; import de.themoep.minedown.adventure.Replacer; -import info.somethingodd.OddItem.OddItem; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.ComponentBuilder; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.bukkit.Bukkit; @@ -158,7 +156,9 @@ public class MaterialUtil { * * @param items The items to get the information from * @return The list, including the amount and names of the items + * @deprecated Use {@link ItemUtil#getItemList(ItemStack[])} instead! */ + @Deprecated public static String getItemList(ItemStack[] items) { ItemStack[] mergedItems = InventoryUtil.mergeSimilarStacks(items); @@ -173,6 +173,7 @@ public class MaterialUtil { /** * Returns item's name + * Use {@link ItemUtil#getName(ItemStack, int)} if you want to get name aliases too! * * @param itemStack ItemStack to name * @return ItemStack's name @@ -196,6 +197,7 @@ public class MaterialUtil { /** * Returns item's name, just like on the sign + * Use {@link ItemUtil#getSignName(ItemStack)} if you want to get name aliases too! * * @param itemStack ItemStack to name * @return ItemStack's name @@ -205,15 +207,15 @@ public class MaterialUtil { } /** - * Returns item's name, with a maximum width + * Returns item's name, with a maximum width. + * Use {@link ItemUtil#getName(ItemStack, int)} if you want to get name aliases too! * * @param itemStack ItemStack to name * @param maxWidth The max width that the name should have; 0 or below if it should be unlimited * @return ItemStack's name */ public static String getName(ItemStack itemStack, int maxWidth) { - String alias = Odd.getAlias(itemStack); - String itemName = alias != null ? alias : itemStack.getType().toString(); + String itemName = itemStack.getType().toString(); String durability = ""; if (itemStack.getDurability() != 0) { @@ -232,17 +234,7 @@ public class MaterialUtil { code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding); } - code += durability + metaData; - - ItemParseEvent parseEvent = new ItemParseEvent(code); - Bukkit.getPluginManager().callEvent(parseEvent); - ItemStack codeItem = parseEvent.getItem(); - if (!equals(itemStack, codeItem)) { - throw new IllegalArgumentException("Cannot generate code for item " + itemStack + " with maximum length of " + maxWidth - + " (code " + code + " results in item " + codeItem + ")"); - } - - return code; + return code + durability + metaData; } /** @@ -321,12 +313,6 @@ public class MaterialUtil { * @return ItemStack */ public static ItemStack getItem(String itemName) { - ItemStack itemStack = Odd.getFromString(itemName); - - if (itemStack != null) { - return itemStack; - } - String[] split = itemName.split("[:\\-#]"); for (int i = 0; i < split.length; i++) { split[i] = split[i].trim(); @@ -340,7 +326,7 @@ public class MaterialUtil { return null; } - itemStack = new ItemStack(material); + ItemStack itemStack = new ItemStack(material); ItemMeta meta = getMetadata(itemName); @@ -458,52 +444,6 @@ public class MaterialUtil { } } - public static class Odd { - private static boolean isInitialized = false; - - /** - * Returns the item stack from OddItem plugin - * - * @param itemName Item name to parse - * @return itemStack that was parsed - */ - public static ItemStack getFromString(String itemName) { - if (!isInitialized) { - return null; - } - - String name = itemName.replace(':', ';'); - - try { - return OddItem.getItemStack(name); - } catch (Exception ex) { - return null; - } - } - - public static String getAlias(ItemStack itemStack) { - if (!isInitialized) { - return null; - } - - try { - Collection aliases = OddItem.getAliases(itemStack); - if (!aliases.isEmpty()) { - return aliases.iterator().next(); - } - } catch (Exception ignored) { - } - return null; - } - - /** - * Lets the class know that it's safe to use the OddItem methods now - */ - public static void initialize() { - isInitialized = true; - } - } - public static class Show { private static ShowItem showItem = null; diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 13e1c59..0dbf97e 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -18,7 +18,9 @@ import com.Acrobot.ChestShop.Listeners.Economy.TaxModule; import com.Acrobot.ChestShop.Listeners.AuthMeChestShopListener; import com.Acrobot.ChestShop.Listeners.GarbageTextListener; import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener; +import com.Acrobot.ChestShop.Listeners.Item.ItemStringListener; import com.Acrobot.ChestShop.Listeners.ItemInfoListener; +import com.Acrobot.ChestShop.Listeners.Modules.ItemAliasModule; import com.Acrobot.ChestShop.Listeners.Modules.MetricsModule; import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule; import com.Acrobot.ChestShop.Listeners.SignParseListener; @@ -329,6 +331,7 @@ public class ChestShop extends JavaPlugin { registerEvent(new PlayerTeleport()); registerEvent(new SignParseListener()); + registerEvent(new ItemStringListener()); registerEvent(new ItemInfoListener()); registerEvent(new GarbageTextListener()); @@ -398,6 +401,7 @@ public class ChestShop extends JavaPlugin { } private void registerModules() { + registerEvent(new ItemAliasModule()); registerEvent(new DiscountModule()); registerEvent(new MetricsModule()); registerEvent(new PriceRestrictionModule()); @@ -532,8 +536,9 @@ public class ChestShop extends JavaPlugin { plugin.registerEvent(listener); } - public static void callEvent(Event event) { + public static E callEvent(E event) { Bukkit.getPluginManager().callEvent(event); + return event; } public static void sendBungeeMessage(String playerName, Messages.Message message, Map replacementMap, String... replacements) { diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/Give.java b/src/main/java/com/Acrobot/ChestShop/Commands/Give.java index d33f31f..b5db5af 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/Give.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/Give.java @@ -5,6 +5,7 @@ import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.NumberUtil; import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Utils.ItemUtil; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -72,7 +73,7 @@ public class Give implements CommandExecutor { item.setAmount(quantity); InventoryUtil.add(item, receiver.getInventory()); - Messages.ITEM_GIVEN.send(sender, "item", MaterialUtil.getName(item), "player", receiver.getName()); + Messages.ITEM_GIVEN.send(sender, "item", ItemUtil.getName(item), "player", receiver.getName()); return true; } diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java index 1c9c66d..a8d5b74 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java @@ -3,9 +3,9 @@ package com.Acrobot.ChestShop.Commands; import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.StringUtil; import com.Acrobot.ChestShop.ChestShop; -import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Events.ItemInfoEvent; import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Utils.ItemUtil; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -19,7 +19,6 @@ import java.util.logging.Level; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_fullname; import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo_shopname; -import static com.Acrobot.ChestShop.Configuration.Messages.replace; /** * @author Acrobot @@ -46,7 +45,7 @@ public class ItemInfo implements CommandExecutor { iteminfo.send(sender); try { - iteminfo_fullname.send(sender, "item", MaterialUtil.getName(item)); + iteminfo_fullname.send(sender, "item", ItemUtil.getName(item)); } catch (IllegalArgumentException e) { sender.sendMessage(ChatColor.RED + "Error while generating full name. Please contact an admin or take a look at the console/log!"); ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating full item name", e); @@ -54,7 +53,7 @@ public class ItemInfo implements CommandExecutor { } try { - iteminfo_shopname.send(sender, "item", MaterialUtil.getSignName(item)); + iteminfo_shopname.send(sender, "item", ItemUtil.getSignName(item)); } catch (IllegalArgumentException e) { sender.sendMessage(ChatColor.RED + "Error while generating shop sign name. Please contact an admin or take a look at the console/log!"); ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e); diff --git a/src/main/java/com/Acrobot/ChestShop/Dependencies.java b/src/main/java/com/Acrobot/ChestShop/Dependencies.java index 2e1b17b..9e715d8 100644 --- a/src/main/java/com/Acrobot/ChestShop/Dependencies.java +++ b/src/main/java/com/Acrobot/ChestShop/Dependencies.java @@ -171,9 +171,6 @@ public class Dependencies implements Listener { listener = heroes; break; - case OddItem: - MaterialUtil.Odd.initialize(); - break; case ShowItem: MaterialUtil.Show.initialize(plugin); break; @@ -195,8 +192,6 @@ public class Dependencies implements Listener { SimpleChestLock, Residence, - OddItem, - WorldGuard, GriefPrevention, RedProtect, diff --git a/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java new file mode 100644 index 0000000..98f3835 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java @@ -0,0 +1,64 @@ +package com.Acrobot.ChestShop.Events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +public class ItemStringQueryEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private String itemString = null; + private final ItemStack item; + private final int maxLength; + + /** + * Query the item string representation of a certain item with a certain length + * @param item The item to query the string for + * @param maxLength The maximum length the string should have. <= 0 means unlimited + */ + public ItemStringQueryEvent(ItemStack item, int maxLength) { + this.item = item; + this.maxLength = maxLength; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * The item for which the string is queried + * @return The item + */ + public ItemStack getItem() { + return item; + } + + /** + * Get the maximum length that the item string should have. <= 0 means unlimited + * @return The maximum string length + */ + public int getMaxLength() { + return maxLength; + } + + /** + * Get the item string that represents the item + * @return The item string that represents the item + */ + public String getItemString() { + return itemString; + } + + /** + * Set the item string that represents the item + * @param itemString The item string that represents the item + */ + public void setItemString(String itemString) { + this.itemString = itemString; + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java new file mode 100644 index 0000000..6e6af0f --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java @@ -0,0 +1,33 @@ +package com.Acrobot.ChestShop.Listeners.Item; + +import com.Acrobot.Breeze.Utils.MaterialUtil; +import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Events.ItemStringQueryEvent; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; + +public class ItemStringListener implements Listener { + + @EventHandler(priority = EventPriority.NORMAL) + public static void calculateItemString(ItemStringQueryEvent event) { + if (event.getItemString() == null) { + event.setItemString(MaterialUtil.getName(event.getItem(), event.getMaxLength())); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public static void checkValidity(ItemStringQueryEvent event) { + ItemParseEvent parseEvent = new ItemParseEvent(event.getItemString()); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack codeItem = parseEvent.getItem(); + if (!MaterialUtil.equals(event.getItem(), codeItem)) { + throw new IllegalArgumentException("Cannot generate code for item " + event.getItem() + + " with maximum length of " + event.getMaxLength() + + " (code " + event.getItemString() + " results in item " + codeItem + ")"); + } + } + +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java new file mode 100644 index 0000000..3b37ade --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java @@ -0,0 +1,99 @@ +package com.Acrobot.ChestShop.Listeners.Modules; + +import com.Acrobot.Breeze.Utils.MaterialUtil; +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.ChestShopReloadEvent; +import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Events.ItemStringQueryEvent; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; + +import java.io.File; +import java.io.IOException; +import java.util.Locale; + +/** + * @author Acrobot + */ +public class ItemAliasModule implements Listener { + private YamlConfiguration configuration; + /** + * Map ChestShop item code -> alias + */ + private BiMap aliases; + + public ItemAliasModule() { + load(); + } + + private void load() { + File file = new File(ChestShop.getFolder(), "itemAliases.yml"); + + configuration = YamlConfiguration.loadConfiguration(file); + + configuration.options().header( + "This file specified optional aliases for certain item codes. (Use the sign name from /iteminfo)" + + "\nPlease note that these aliases should fit on a sign for it to work properly!" + ); + + if (!file.exists()) { + configuration.addDefault("Item String#3d", "My Cool Item"); + configuration.addDefault("Other Material#Eg", "Some other Item"); + + try { + configuration.options().copyDefaults(true); + configuration.save(ChestShop.loadFile("itemAliases.yml")); + } catch (IOException e) { + e.printStackTrace(); + } + } + + aliases = HashBiMap.create(configuration.getKeys(false).size()); + + for (String key : configuration.getKeys(false)) { + if (configuration.isString(key)) { + aliases.put(key, configuration.getString(key)); + } + } + } + + @EventHandler + public void onReload(ChestShopReloadEvent event) { + load(); + } + + @EventHandler(priority = EventPriority.LOW) + public void onItemParse(ItemParseEvent event) { + String code = aliases.inverse().get(event.getItemString()); + if (code != null) { + event.setItem(MaterialUtil.getItem(code)); + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onItemStringQuery(ItemStringQueryEvent event) { + if (event.getItemString() != null) { + String newCode = null; + + if (aliases.containsKey(event.getItemString())) { + newCode = aliases.get(event.getItemString()); + } else if (!event.getItemString().contains("#") && aliases.containsKey(event.getItemString().toLowerCase(Locale.ROOT))) { + newCode = aliases.get(event.getItemString().toLowerCase(Locale.ROOT)); + } else { + String[] parts = event.getItemString().split("#", 2); + String lowercaseCode = parts[0].toLowerCase(Locale.ROOT) + "#" + parts[1]; + if (aliases.containsKey(lowercaseCode)) { + newCode = aliases.get(lowercaseCode); + } + } + + if (newCode != null) { + event.setItemString(newCode); + } + } + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index 022e06b..840e207 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -14,6 +14,7 @@ import com.Acrobot.ChestShop.Events.TransactionEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Security; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.Acrobot.ChestShop.Utils.ItemUtil; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -88,7 +89,7 @@ public class PlayerInteract implements Listener { if (!MaterialUtil.isEmpty(item)) { String itemCode; try { - itemCode = MaterialUtil.getSignName(item); + itemCode = ItemUtil.getSignName(item); } catch (IllegalArgumentException e) { player.sendMessage(ChatColor.RED + "Error while generating shop sign item name. Please contact an admin or take a look at the console/log!"); com.Acrobot.ChestShop.ChestShop.getPlugin().getLogger().log(Level.SEVERE, "Error while generating shop sign item name", e); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionLogger.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionLogger.java index 8ce22b3..6ee86fd 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionLogger.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionLogger.java @@ -9,7 +9,7 @@ import org.bukkit.event.Listener; import org.bukkit.inventory.ItemStack; import static com.Acrobot.Breeze.Utils.InventoryUtil.mergeSimilarStacks; -import static com.Acrobot.Breeze.Utils.MaterialUtil.getName; +import static com.Acrobot.ChestShop.Utils.ItemUtil.getName; import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY; /** diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java index 98b085a..609f462 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PostTransaction/TransactionMessageSender.java @@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Events.TransactionEvent; +import com.Acrobot.ChestShop.Utils.ItemUtil; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -75,10 +76,10 @@ public class TransactionMessageSender implements Listener { } if (player != null) { - replacementMap.put("item", MaterialUtil.getItemList(event.getStock())); + replacementMap.put("item", ItemUtil.getItemList(event.getStock())); rawMessage.sendWithPrefix(player, replacementMap); } else if (playerName != null) { - replacementMap.put("item", MaterialUtil.getItemList(event.getStock())); + replacementMap.put("item", ItemUtil.getItemList(event.getStock())); ChestShop.sendBungeeMessage(playerName, rawMessage, replacementMap); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java index 8e51f27..0281451 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java @@ -2,11 +2,11 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation; import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.StringUtil; -import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; +import com.Acrobot.ChestShop.Utils.ItemUtil; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -60,7 +60,7 @@ public class ItemChecker implements Listener { } } - itemCode = MaterialUtil.getSignName(item); + itemCode = ItemUtil.getSignName(item); if (StringUtil.getMinecraftStringWidth(itemCode) > MAXIMUM_SIGN_WIDTH) { event.setOutcome(INVALID_ITEM); diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java index bae41b1..ede1644 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/ErrorMessageSender.java @@ -8,6 +8,7 @@ import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Events.PreTransactionEvent; +import com.Acrobot.ChestShop.Utils.ItemUtil; import com.google.common.collect.HashBasedTable; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Table; @@ -129,7 +130,7 @@ public class ErrorMessageSender implements Listener { if (player != null || Properties.BUNGEECORD_MESSAGES) { if (Properties.NOTIFICATION_MESSAGE_COOLDOWN > 0) { - String cacheKey = message.getKey() + "|" + String.join(",", replacements) + "|" + MaterialUtil.getItemList(stock); + String cacheKey = message.getKey() + "|" + String.join(",", replacements) + "|" + ItemUtil.getItemList(stock); Long last = notificationCooldowns.get(ownerAccount.getUuid(), cacheKey); if (last != null && last + Properties.NOTIFICATION_MESSAGE_COOLDOWN * 1000 > System.currentTimeMillis()) { return; @@ -141,11 +142,11 @@ public class ErrorMessageSender implements Listener { if (Properties.SHOWITEM_MESSAGE && MaterialUtil.Show.sendMessage(player, message, stock, Collections.emptyMap(), replacements)) { return; } - String items = MaterialUtil.getItemList(stock); + String items = ItemUtil.getItemList(stock); message.sendWithPrefix(player, ImmutableMap.of("material", items, "item", items), replacements); } else { - String items = MaterialUtil.getItemList(stock); + String items = ItemUtil.getItemList(stock); ChestShop.sendBungeeMessage(ownerAccount.getName(), message, ImmutableMap.of("material", items, "item", items), replacements); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java index b75f316..d370502 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java @@ -4,17 +4,22 @@ import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.MaterialParseEvent; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; public class SignParseListener implements Listener { - @EventHandler + @EventHandler(priority = EventPriority.NORMAL) public static void onItemParse(ItemParseEvent event) { - event.setItem(MaterialUtil.getItem(event.getItemString())); + if (event.getItem() == null) { + event.setItem(MaterialUtil.getItem(event.getItemString())); + } } - @EventHandler + @EventHandler(priority = EventPriority.NORMAL) public static void onMaterialParse(MaterialParseEvent event) { - event.setMaterial(MaterialUtil.getMaterial(event.getMaterialString())); + if (event.getMaterial() == null) { + event.setMaterial(MaterialUtil.getMaterial(event.getMaterialString())); + } } } diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java new file mode 100644 index 0000000..b9f55a2 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java @@ -0,0 +1,64 @@ +package com.Acrobot.ChestShop.Utils; + +import com.Acrobot.Breeze.Utils.InventoryUtil; +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.ItemStringQueryEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.ArrayList; +import java.util.List; + +import static com.Acrobot.Breeze.Utils.MaterialUtil.MAXIMUM_SIGN_WIDTH; + +public class ItemUtil { + + /** + * Get a list with item information + * + * @param items The items to get the information from + * @return The list, including the amount and names of the items + */ + public static String getItemList(ItemStack[] items) { + ItemStack[] mergedItems = InventoryUtil.mergeSimilarStacks(items); + + List itemText = new ArrayList<>(); + + for (ItemStack item : mergedItems) { + itemText.add(item.getAmount() + " " + getName(item)); + } + + return String.join(", ", itemText); + } + + /** + * Returns item's name + * + * @param itemStack ItemStack to name + * @return ItemStack's name + */ + public static String getName(ItemStack itemStack) { + return getName(itemStack, 0); + } + + + /** + * Returns item's name, with a maximum width + * + * @param itemStack ItemStack to name + * @param maxWidth The max width that the name should have; 0 or below if it should be unlimited + * @return ItemStack's name + */ + public static String getName(ItemStack itemStack, int maxWidth) { + return ChestShop.callEvent(new ItemStringQueryEvent(itemStack, maxWidth)).getItemString(); + } + + /** + * Returns item's name, just like on the sign + * + * @param itemStack ItemStack to name + * @return ItemStack's name + */ + public static String getSignName(ItemStack itemStack) { + return getName(itemStack, MAXIMUM_SIGN_WIDTH); + } +}