diff --git a/.checkstyle/checkstyle.xml b/.checkstyle/checkstyle.xml index 716990df9f3..9777cb88fb5 100644 --- a/.checkstyle/checkstyle.xml +++ b/.checkstyle/checkstyle.xml @@ -37,14 +37,6 @@ - - - - - - - - diff --git a/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java b/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java index 14be12c1f5b..bc120aa033d 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java +++ b/Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.items.transform.PluginItemTransformer; import com.earth2me.essentials.textreader.BookInput; import com.earth2me.essentials.textreader.BookPager; import com.earth2me.essentials.textreader.IText; @@ -11,6 +12,7 @@ import com.google.common.base.Joiner; import net.ess3.api.IEssentials; import net.ess3.api.TranslatableException; +import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.DyeColor; import org.bukkit.FireworkEffect; @@ -46,6 +48,9 @@ public class MetaItemStack { private static final Map colorMap = new HashMap<>(); private static final Map fireworkShape = new HashMap<>(); + + //Contains plugin registered item meta data transformers. + private static final transient Map itemTransformers = new HashMap<>(); private static boolean useNewSkullMethod = true; static { @@ -77,6 +82,32 @@ public MetaItemStack(final ItemStack stack) { this.stack = stack.clone(); } + /** + * Registers an item transformer, belonging to a plugin, that can manipulate certain item metadata. + * @param key the key for the transformer. + * @param itemTransformer the actual transformer. + */ + public static void registerItemTransformer(String key, PluginItemTransformer itemTransformer){ + //Warn people if they're trying to register over top of someone else. + if(itemTransformers.containsKey(key)){ + Bukkit.getLogger().warning(String.format("[Essentials] - Plugin transformer registered to \"%s\" attempted to register already existing item transformer \"%s\" belonging to \"%s\"!", + itemTransformer.getPlugin().getName(), + key, + itemTransformers.get(key).getPlugin().getName())); + return; + } + + itemTransformers.put(key, itemTransformer); + } + + /** + * Unregisters a certain item transformer under key "key". + * @param key the transformer key. + */ + public static void unregisterItemTransformer(String key){ + itemTransformers.remove(key); + } + private static void setSkullOwner(final IEssentials ess, final ItemStack stack, final String owner) { if (!(stack.getItemMeta() instanceof SkullMeta)) return; @@ -319,11 +350,28 @@ public void addStringMeta(final CommandSource sender, final boolean allowUnsafe, } else { throw new TranslatableException("leatherSyntax"); } + } else if (split.length > 1 && itemTransformers.containsKey(split[0])) { + transformItem(split[0], split[1]); } else { parseEnchantmentStrings(sender, allowUnsafe, split, ess); } } + private void transformItem(String key, String data){ + final PluginItemTransformer transformer = itemTransformers.get(key); + + //Ignore, the plugin is disabled. + if(!transformer.getPlugin().isEnabled()) + return; + + try{ + stack = transformer.apply(data, stack); + }catch(Throwable thr){ + Bukkit.getLogger().severe(String.format("[Essentials] - Error applying data \"%s\" to itemstack! Plugin: %s, Key: %s", data, transformer.getPlugin().getName(), key)); + thr.printStackTrace(); + } + } + public void addItemFlags(final String string) throws Exception { final String[] separate = splitPattern.split(string, 2); if (separate.length != 2) { diff --git a/Essentials/src/main/java/com/earth2me/essentials/items/transform/PluginItemTransformer.java b/Essentials/src/main/java/com/earth2me/essentials/items/transform/PluginItemTransformer.java new file mode 100644 index 00000000000..f1df7291c34 --- /dev/null +++ b/Essentials/src/main/java/com/earth2me/essentials/items/transform/PluginItemTransformer.java @@ -0,0 +1,21 @@ +package com.earth2me.essentials.items.transform; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +public abstract class PluginItemTransformer { + private final Plugin plugin; + + public PluginItemTransformer(Plugin thePlugin){ + if(thePlugin == null) + throw new IllegalArgumentException("Plugin cannot be null!"); + + this.plugin = thePlugin; + } + + public abstract ItemStack apply(String data, ItemStack original); + + public Plugin getPlugin() { + return plugin; + } +} diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java index d9d6602d7b1..3399a462f29 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/StringUtil.java @@ -13,7 +13,7 @@ public final class StringUtil { private static final Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9-]"); private static final Pattern STRICTINVALIDCHARS = Pattern.compile("[^a-z0-9]"); @SuppressWarnings("CheckStyle") - private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]"); + private static final Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r -~\u0085\u00A0-\uD7FF\uE000-\uFFFC]"); private StringUtil() { }