Skip to content
Merged
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
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 Down Expand Up @@ -53,6 +54,7 @@
public class MetaItemStack {
private static final Map<String, DyeColor> colorMap = new HashMap<>();
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();
private static final transient Map<String, PluginItemTransformer> itemTransformers = new HashMap<>();
private static boolean useNewSkullMethod = true;

static {
Expand Down Expand Up @@ -84,6 +86,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(final String key, final PluginItemTransformer itemTransformer) {
//Warn people if they're trying to register over top of someone else.
if (itemTransformers.containsKey(key)) {
Essentials.getWrappedLogger().log(Level.WARNING, String.format("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(final 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 @@ -358,11 +386,28 @@ public void addStringMeta(final CommandSource sender, final boolean allowUnsafe,
armorMeta.setTrim(new ArmorTrim(material, pattern));

stack.setItemMeta(armorMeta);
} else if (split.length > 1 && itemTransformers.containsKey(split[0])) {
transformItem(split[0], split[1]);
} else {
parseEnchantmentStrings(sender, allowUnsafe, split, ess);
}
}

private void transformItem(final String key, final 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(final Throwable thr) {
Essentials.getWrappedLogger().log(Level.SEVERE, String.format("Error applying data \"%s\" to itemstack! Plugin: %s, Key: %s", data, transformer.getPlugin().getName(), key), thr);
}
}

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,22 @@
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(final 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;
}
}
Loading