diff --git a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java index ddef41f..df86cc4 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java @@ -3,9 +3,12 @@ package com.Acrobot.Breeze.Utils; import com.Acrobot.Breeze.Collection.SimpleCache; import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Configuration.Properties; +import com.Acrobot.ChestShop.Events.ItemParseEvent; +import com.Acrobot.ChestShop.Events.MaterialParseEvent; import com.google.common.collect.ImmutableMap; import de.themoep.ShowItem.api.ShowItem; import info.somethingodd.OddItem.OddItem; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -17,7 +20,6 @@ import org.bukkit.plugin.Plugin; import org.json.simple.JSONObject; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.logging.Level; @@ -191,7 +193,9 @@ public class MaterialUtil { code += durability + metaData; - ItemStack codeItem = getItem(code); + 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 + ")"); @@ -287,9 +291,10 @@ public class MaterialUtil { split[i] = split[i].trim(); } - Material material = getMaterial(split[0]); short durability = getDurability(itemName); - + MaterialParseEvent parseEvent = new MaterialParseEvent(split[0], durability); + Bukkit.getPluginManager().callEvent(parseEvent); + Material material = parseEvent.getMaterial(); if (material == null) { return null; } diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index 8dcf52f..d7248b4 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -18,6 +18,7 @@ import com.Acrobot.ChestShop.Listeners.AuthMeChestShopListener; import com.Acrobot.ChestShop.Listeners.GarbageTextListener; import com.Acrobot.ChestShop.Listeners.Item.ItemMoveListener; import com.Acrobot.ChestShop.Listeners.ItemInfoListener; +import com.Acrobot.ChestShop.Listeners.SignParseListener; import com.Acrobot.ChestShop.Listeners.Modules.DiscountModule; import com.Acrobot.ChestShop.Listeners.Modules.PriceRestrictionModule; import com.Acrobot.ChestShop.Listeners.Player.*; @@ -294,6 +295,7 @@ public class ChestShop extends JavaPlugin { registerEvent(new PlayerLeave()); registerEvent(new PlayerTeleport()); + registerEvent(new SignParseListener()); registerEvent(new ItemInfoListener()); registerEvent(new GarbageTextListener()); diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/Give.java b/src/main/java/com/Acrobot/ChestShop/Commands/Give.java index 89a6739..6a7941a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/Give.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/Give.java @@ -4,6 +4,7 @@ import com.Acrobot.Breeze.Utils.InventoryUtil; 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.Permission; import org.bukkit.Bukkit; import org.bukkit.command.Command; @@ -89,6 +90,8 @@ public class Give implements CommandExecutor { builder.append(arguments[index]).append(' '); } - return MaterialUtil.getItem(builder.toString()); + ItemParseEvent parseEvent = new ItemParseEvent(builder.toString()); + Bukkit.getPluginManager().callEvent(parseEvent); + return parseEvent.getItem(); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java index 365bc9a..ba1d428 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java @@ -5,6 +5,8 @@ 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 org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -28,7 +30,9 @@ public class ItemInfo implements CommandExecutor { item = ((HumanEntity) sender).getItemInHand(); } else { - item = MaterialUtil.getItem(StringUtil.joinArray(args)); + ItemParseEvent parseEvent = new ItemParseEvent(StringUtil.joinArray(args)); + Bukkit.getPluginManager().callEvent(parseEvent); + item = parseEvent.getItem(); } if (MaterialUtil.isEmpty(item)) { diff --git a/src/main/java/com/Acrobot/ChestShop/Events/ItemParseEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/ItemParseEvent.java new file mode 100644 index 0000000..32e279f --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Events/ItemParseEvent.java @@ -0,0 +1,56 @@ +package com.Acrobot.ChestShop.Events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +public class ItemParseEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private final String itemString; + private ItemStack item = null; + + public ItemParseEvent(String itemString) { + this.itemString = itemString; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the item string that should be parsed + * @return The item string to parse + */ + public String getItemString() { + return itemString; + } + + /** + * Set the item that the string represents + * @param item The item for the string + */ + public void setItem(ItemStack item) { + this.item = item; + } + + /** + * The item that was parsed + * @return The parsed item or null if none was found + */ + public ItemStack getItem() { + return item; + } + + /** + * Whether or not the item string of this event has a parsed item + * @return True if an item was successfully parsed; false if not + */ + public boolean hasItem() { + return item != null; + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Events/MaterialParseEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/MaterialParseEvent.java new file mode 100644 index 0000000..f80d80a --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Events/MaterialParseEvent.java @@ -0,0 +1,67 @@ +package com.Acrobot.ChestShop.Events; + +import org.bukkit.Material; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class MaterialParseEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private final String materialString; + private final short data; + private Material material = null; + + public MaterialParseEvent(String materialString, short data) { + this.materialString = materialString; + this.data = data; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Get the material string that should be parsed + * @return The material string to parse + */ + public String getMaterialString() { + return materialString; + } + + /** + * Get the data of legacy materials that might result in different flattening materials + * @return The data + */ + public short getData() { + return data; + } + + /** + * Set the material that the string represents + * @param material The material for the string + */ + public void setMaterial(Material material) { + this.material = material; + } + + /** + * The material that was parsed + * @return The parsed material or null if none was found + */ + public Material getMaterial() { + return material; + } + + /** + * Whether or not the material string of this event has a parsed material + * @return True if an material was successfully parsed; false if not + */ + public boolean hasMaterial() { + return material != null; + } + +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java index e7f4a8e..4f99d71 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Modules/PriceRestrictionModule.java @@ -1,9 +1,10 @@ package com.Acrobot.ChestShop.Listeners.Modules; -import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.PriceUtil; import com.Acrobot.ChestShop.ChestShop; +import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -90,7 +91,9 @@ public class PriceRestrictionModule implements Listener { @EventHandler public void onPreShopCreation(PreShopCreationEvent event) { - ItemStack material = MaterialUtil.getItem(event.getSignLine(ITEM_LINE)); + ItemParseEvent parseEvent = new ItemParseEvent(event.getSignLine(ITEM_LINE)); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack material = parseEvent.getItem(); if (material == null) { return; 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 26cf614..53737b7 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -6,6 +6,7 @@ import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Containers.AdminInventory; import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent; +import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.PreTransactionEvent; import com.Acrobot.ChestShop.Events.TransactionEvent; import com.Acrobot.ChestShop.Permission; @@ -167,7 +168,9 @@ public class PlayerInteract implements Listener { Container shopBlock = uBlock.findConnectedContainer(sign); Inventory ownerInventory = (adminShop ? new AdminInventory() : shopBlock != null ? shopBlock.getInventory() : null); - ItemStack item = MaterialUtil.getItem(material); + ItemParseEvent parseEvent = new ItemParseEvent(material); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack item = parseEvent.getItem(); if (item == null || !NumberUtil.isInteger(quantity)) { player.sendMessage(Messages.prefix(Messages.INVALID_SHOP_DETECTED)); return null; 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 b45f152..c587a37 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java @@ -4,9 +4,11 @@ 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.uBlock; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.block.Container; import org.bukkit.event.EventHandler; @@ -30,7 +32,10 @@ public class ItemChecker implements Listener { @EventHandler(priority = EventPriority.LOWEST) public static void onPreShopCreation(PreShopCreationEvent event) { String itemCode = event.getSignLine(ITEM_LINE); - ItemStack item = MaterialUtil.getItem(itemCode); + + ItemParseEvent parseEvent = new ItemParseEvent(itemCode); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack item = parseEvent.getItem(); if (item == null) { if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) { @@ -67,7 +72,9 @@ public class ItemChecker implements Listener { } private static boolean isSameItem(String newCode, ItemStack item) { - ItemStack newItem = MaterialUtil.getItem(newCode); + ItemParseEvent parseEvent = new ItemParseEvent(newCode); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack newItem = parseEvent.getItem(); return newItem != null && MaterialUtil.equals(newItem, item); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PermissionChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PermissionChecker.java index ec2ba47..b5ab7bf 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PermissionChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/PermissionChecker.java @@ -1,9 +1,10 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation; -import com.Acrobot.Breeze.Utils.MaterialUtil; import com.Acrobot.Breeze.Utils.PriceUtil; +import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Permission; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -27,7 +28,9 @@ public class PermissionChecker implements Listener { String priceLine = event.getSignLine(PRICE_LINE); String itemLine = event.getSignLine(ITEM_LINE); - ItemStack item = MaterialUtil.getItem(itemLine); + ItemParseEvent parseEvent = new ItemParseEvent(itemLine); + Bukkit.getPluginManager().callEvent(parseEvent); + ItemStack item = parseEvent.getItem(); if (item == null) { if (!Permission.has(player, SHOP_CREATION)) { diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java new file mode 100644 index 0000000..b75f316 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/SignParseListener.java @@ -0,0 +1,20 @@ +package com.Acrobot.ChestShop.Listeners; + +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.Listener; + +public class SignParseListener implements Listener { + + @EventHandler + public static void onItemParse(ItemParseEvent event) { + event.setItem(MaterialUtil.getItem(event.getItemString())); + } + + @EventHandler + public static void onMaterialParse(MaterialParseEvent event) { + event.setMaterial(MaterialUtil.getMaterial(event.getMaterialString())); + } +}