From f12e6ed3b68407b2fc55fc8d9f6472d82f444981 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sun, 3 Jan 2021 20:47:07 +0100 Subject: [PATCH] Use full item name instead of shortened for aliases This fixes an issue where aliases would not show up in transaction messages when the aliases item's string was shortened. Also remove an unnecessary string length calculation from the MaterialUtil getName method if there was no max width set. --- .../Acrobot/Breeze/Utils/MaterialUtil.java | 10 +++-- .../Events/ItemStringQueryEvent.java | 13 +------ .../Listeners/Item/ItemStringListener.java | 17 +-------- .../Listeners/Modules/ItemAliasModule.java | 2 +- .../com/Acrobot/ChestShop/Utils/ItemUtil.java | 37 ++++++++++++++++++- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java index 67be41f..dac7b97 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java @@ -228,10 +228,12 @@ public class MaterialUtil { } String code = StringUtil.capitalizeFirstLetter(itemName, '_'); - int codeWidth = getMinecraftStringWidth(code + durability + metaData); - if (maxWidth > 0 && codeWidth > maxWidth) { - int exceeding = codeWidth - maxWidth; - code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding); + if (maxWidth > 0) { + int codeWidth = getMinecraftStringWidth(code + durability + metaData); + if (codeWidth > maxWidth) { + int exceeding = codeWidth - maxWidth; + code = getShortenedName(code, getMinecraftStringWidth(code) - exceeding); + } } return code + durability + metaData; diff --git a/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java index 98f3835..d099e4d 100644 --- a/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java +++ b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java @@ -9,16 +9,13 @@ public class ItemStringQueryEvent extends Event { 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) { + public ItemStringQueryEvent(ItemStack item) { this.item = item; - this.maxLength = maxLength; } @Override @@ -38,14 +35,6 @@ public class ItemStringQueryEvent extends Event { 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 diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java index 6e6af0f..08febb4 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java @@ -1,32 +1,17 @@ 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 + ")"); + event.setItemString(MaterialUtil.getName(event.getItem(), 0)); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java index 69bd1d3..6acd7ca 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java @@ -36,7 +36,7 @@ public class ItemAliasModule implements Listener { configuration = YamlConfiguration.loadConfiguration(file); configuration.options().header( - "This file specified optional aliases for certain item codes. (Use the sign name from /iteminfo)" + "This file specified optional aliases for certain item codes. (Use the full name from /iteminfo)" + "\nPlease note that these aliases should fit on a sign for it to work properly!" ); diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java index b9f55a2..75ac9a1 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java @@ -1,14 +1,18 @@ package com.Acrobot.ChestShop.Utils; import com.Acrobot.Breeze.Utils.InventoryUtil; +import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.ItemStringQueryEvent; +import org.bukkit.Bukkit; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; import static com.Acrobot.Breeze.Utils.MaterialUtil.MAXIMUM_SIGN_WIDTH; +import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth; public class ItemUtil { @@ -49,7 +53,38 @@ public class ItemUtil { * @return ItemStack's name */ public static String getName(ItemStack itemStack, int maxWidth) { - return ChestShop.callEvent(new ItemStringQueryEvent(itemStack, maxWidth)).getItemString(); + String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack)).getItemString(); + if (code != null) { + if (maxWidth > 0) { + int codeWidth = getMinecraftStringWidth(code); + if (codeWidth > maxWidth) { + int exceeding = codeWidth - maxWidth; + + int poundIndex = code.indexOf('#'); + int colonIndex = code.indexOf(':'); + String material = code; + String rest = ""; + if (poundIndex > 0 && poundIndex < colonIndex) { + material = code.substring(0, poundIndex); + rest = code.substring(poundIndex); + } else if (colonIndex > 0 && colonIndex < poundIndex) { + material = code.substring(0, colonIndex); + rest = code.substring(colonIndex); + } + code = MaterialUtil.getShortenedName(material, getMinecraftStringWidth(material) - exceeding) + rest; + } + } + + ItemParseEvent parseEvent = new ItemParseEvent(code); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack codeItem = parseEvent.getItem(); + if (!MaterialUtil.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; } /**