From 852f20a50ef622154821046e65b93093cbd5989e Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Thu, 29 Dec 2022 15:25:26 +0100 Subject: [PATCH] Add ItemBridge support (Resolves #361) This also slightly changes how the max width is applied for generating sign item IDs --- pom.xml | 6 +++ .../com/Acrobot/ChestShop/Dependencies.java | 5 ++ .../Events/ItemStringQueryEvent.java | 19 ++++++++ .../Listeners/Item/ItemStringListener.java | 2 +- .../Listeners/Modules/ItemAliasModule.java | 9 ++++ .../Acrobot/ChestShop/Plugins/ItemBridge.java | 46 +++++++++++++++++++ .../com/Acrobot/ChestShop/Utils/ItemUtil.java | 2 +- src/main/resources/plugin.yml | 2 +- 8 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java diff --git a/pom.xml b/pom.xml index 0a47db6..050c999 100644 --- a/pom.xml +++ b/pom.xml @@ -244,6 +244,12 @@ + + com.github.jojodmo + ItemBridge + b0054538c1 + + br.net.fabiozumbi12.RedProtect RedProtect-Spigot diff --git a/src/main/java/com/Acrobot/ChestShop/Dependencies.java b/src/main/java/com/Acrobot/ChestShop/Dependencies.java index ad8b76e..3231afa 100644 --- a/src/main/java/com/Acrobot/ChestShop/Dependencies.java +++ b/src/main/java/com/Acrobot/ChestShop/Dependencies.java @@ -174,6 +174,9 @@ public class Dependencies implements Listener { listener = heroes; break; + case ItemBridge: + listener = new ItemBridge(); + break; case ShowItem: MaterialUtil.Show.initialize(plugin); break; @@ -202,6 +205,8 @@ public class Dependencies implements Listener { Heroes, + ItemBridge, + ShowItem } diff --git a/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java index d099e4d..7b714f9 100644 --- a/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java +++ b/src/main/java/com/Acrobot/ChestShop/Events/ItemStringQueryEvent.java @@ -9,13 +9,24 @@ public class ItemStringQueryEvent extends Event { private String itemString = null; private final ItemStack item; + private final int maxWidth; /** * Query the item string representation of a certain item with a certain length * @param item The item to query the string for */ public ItemStringQueryEvent(ItemStack item) { + this(item, 0); + } + + /** + * Query the item string representation of a certain item with a certain length + * @param item The item to query the string for + * @param maxWidth The max width of the item string + */ + public ItemStringQueryEvent(ItemStack item, int maxWidth) { this.item = item; + this.maxWidth = maxWidth; } @Override @@ -50,4 +61,12 @@ public class ItemStringQueryEvent extends Event { public void setItemString(String itemString) { this.itemString = itemString; } + + /** + * Get the max width that the result item string should have + * @return The max width of the result item string + */ + public int getMaxWidth() { + return maxWidth; + } } 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 08febb4..d3b3afc 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Item/ItemStringListener.java @@ -11,7 +11,7 @@ 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(), 0)); + event.setItemString(MaterialUtil.getName(event.getItem(), event.getMaxWidth())); } } 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 223493a..ef894b4 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/ItemAliasModule.java @@ -17,6 +17,8 @@ import java.io.IOException; import java.util.Locale; import java.util.Map; +import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth; + /** * @author Acrobot */ @@ -118,6 +120,13 @@ public class ItemAliasModule implements Listener { } if (newCode != null) { + if (event.getMaxWidth() > 0) { + int width = getMinecraftStringWidth(newCode); + if (width > event.getMaxWidth()) { + ChestShop.getBukkitLogger().warning("Can't use configured alias " + newCode + " as it's width (" + width + ") was wider than the allowed max width of " + event.getMaxWidth()); + return; + } + } event.setItemString(newCode); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java b/src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java new file mode 100644 index 0000000..de757cc --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Plugins/ItemBridge.java @@ -0,0 +1,46 @@ +package com.Acrobot.ChestShop.Plugins; + +import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Events.ItemStringQueryEvent; +import com.jojodmo.itembridge.ItemBridgeKey; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; + +import static com.Acrobot.Breeze.Utils.StringUtil.getMinecraftStringWidth; + +/** + * @author Acrobot + */ +public class ItemBridge implements Listener { + + @EventHandler(priority = EventPriority.LOW) + public void onItemParse(ItemParseEvent event) { + if (event.getItem() == null) { + ItemStack item = com.jojodmo.itembridge.ItemBridge.getItemStack(event.getItemString()); + if (item != null) { + event.setItem(item); + } + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onItemStringQuery(ItemStringQueryEvent event) { + ItemBridgeKey key = com.jojodmo.itembridge.ItemBridge.getItemKey(event.getItem()); + // If namespace is "minecraft" then we ignore it and use our own logic + if (key != null && !"minecraft".equalsIgnoreCase(key.getNamespace())) { + String code = key.toString(); + // Make sure the ItemBridge string is not too long as we can't parse shortened ones + if (event.getMaxWidth() > 0) { + int width = getMinecraftStringWidth(code); + if (width > event.getMaxWidth()) { + ChestShop.logDebug("Can't use ItemBridge alias " + code + " as it's width (" + width + ") was wider than the allowed max width of " + event.getMaxWidth()); + return; + } + } + event.setItemString(key.toString()); + } + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java index 50c9376..e139490 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java @@ -53,7 +53,7 @@ public class ItemUtil { * @return ItemStack's name */ public static String getName(ItemStack itemStack, int maxWidth) { - String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack)).getItemString(); + String code = ChestShop.callEvent(new ItemStringQueryEvent(itemStack, maxWidth)).getItemString(); if (code != null) { if (maxWidth > 0) { int codeWidth = getMinecraftStringWidth(code); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 909f9d9..3e4d6bd 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,7 +4,7 @@ version: '${bukkit.plugin.version}' author: Acrobot authors: ['https://github.com/ChestShop-authors/ChestShop-3/contributors'] description: A chest shop for economy plugins. -softdepend: [Vault, Reserve, LWC, Lockette, LockettePro, Deadbolt, BlockLocker, OddItem, WorldGuard, GriefPrevention, RedProtect, Heroes, SimpleChestLock, Residence, ShowItem] +softdepend: [Vault, Reserve, LWC, Lockette, LockettePro, Deadbolt, BlockLocker, OddItem, WorldGuard, GriefPrevention, RedProtect, Heroes, SimpleChestLock, Residence, ShowItem, ItemBridge] api-version: '1.13' commands: