Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to register custom item transformers. #4871

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@
<!-- https://checkstyle.org/config_imports.html#AvoidStarImport -->
<module name="AvoidStarImport"/>

<!-- https://checkstyle.org/config_misc.html#AvoidEscapedUnicodeCharacters -->
<module name="AvoidEscapedUnicodeCharacters">
<property name="allowByTailComment" value="true"/>
<property name="allowEscapesForControlCharacters" value="true"/>
<property name="allowNonPrintableEscapes" value="true"/>
<property name="severity" value="info"/>
</module>

<!-- https://checkstyle.org/config_misc.html#CommentsIndentation -->
<module name="CommentsIndentation"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -46,6 +48,9 @@
public class MetaItemStack {
private static final Map<String, DyeColor> colorMap = new HashMap<>();
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();

//Contains plugin registered item meta data transformers.
private static final transient Map<String, PluginItemTransformer> itemTransformers = new HashMap<>();
private static boolean useNewSkullMethod = true;

static {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down