Make Item and Material parsing events

This commit is contained in:
Phoenix616 2019-01-17 19:50:47 +01:00
parent c8d0590614
commit 91a372bd05
11 changed files with 186 additions and 13 deletions

View File

@ -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;
}

View File

@ -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());

View File

@ -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();
}
}

View File

@ -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)) {

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -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)) {

View File

@ -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()));
}
}