Skip to content

Commit ae85381

Browse files
committed
Add ability to register custom item transformers
1 parent 9f57b3e commit ae85381

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Essentials/src/main/java/com/earth2me/essentials/MetaItemStack.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.earth2me.essentials;
22

3+
import com.earth2me.essentials.items.transform.PluginItemTransformer;
34
import com.earth2me.essentials.textreader.BookInput;
45
import com.earth2me.essentials.textreader.BookPager;
56
import com.earth2me.essentials.textreader.IText;
@@ -53,6 +54,7 @@
5354
public class MetaItemStack {
5455
private static final Map<String, DyeColor> colorMap = new HashMap<>();
5556
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();
57+
private static final transient Map<String, PluginItemTransformer> itemTransformers = new HashMap<>();
5658
private static boolean useNewSkullMethod = true;
5759

5860
static {
@@ -84,6 +86,32 @@ public MetaItemStack(final ItemStack stack) {
8486
this.stack = stack.clone();
8587
}
8688

89+
/**
90+
* Registers an item transformer, belonging to a plugin, that can manipulate certain item metadata.
91+
* @param key the key for the transformer.
92+
* @param itemTransformer the actual transformer.
93+
*/
94+
public static void registerItemTransformer(final String key, final PluginItemTransformer itemTransformer) {
95+
//Warn people if they're trying to register over top of someone else.
96+
if (itemTransformers.containsKey(key)) {
97+
Essentials.getWrappedLogger().log(Level.WARNING, String.format("Plugin transformer registered to \"%s\" attempted to register already existing item transformer \"%s\" belonging to \"%s\"!",
98+
itemTransformer.getPlugin().getName(),
99+
key,
100+
itemTransformers.get(key).getPlugin().getName()));
101+
return;
102+
}
103+
104+
itemTransformers.put(key, itemTransformer);
105+
}
106+
107+
/**
108+
* Unregisters a certain item transformer under key "key".
109+
* @param key the transformer key.
110+
*/
111+
public static void unregisterItemTransformer(final String key) {
112+
itemTransformers.remove(key);
113+
}
114+
87115
private static void setSkullOwner(final IEssentials ess, final ItemStack stack, final String owner) {
88116
if (!(stack.getItemMeta() instanceof SkullMeta)) return;
89117

@@ -358,11 +386,28 @@ public void addStringMeta(final CommandSource sender, final boolean allowUnsafe,
358386
armorMeta.setTrim(new ArmorTrim(material, pattern));
359387

360388
stack.setItemMeta(armorMeta);
389+
} else if (split.length > 1 && itemTransformers.containsKey(split[0])) {
390+
transformItem(split[0], split[1]);
361391
} else {
362392
parseEnchantmentStrings(sender, allowUnsafe, split, ess);
363393
}
364394
}
365395

396+
private void transformItem(final String key, final String data){
397+
final PluginItemTransformer transformer = itemTransformers.get(key);
398+
399+
//Ignore, the plugin is disabled.
400+
if (!transformer.getPlugin().isEnabled()) {
401+
return;
402+
}
403+
404+
try {
405+
stack = transformer.apply(data, stack);
406+
} catch(final Throwable thr) {
407+
Essentials.getWrappedLogger().log(Level.SEVERE, String.format("Error applying data \"%s\" to itemstack! Plugin: %s, Key: %s", data, transformer.getPlugin().getName(), key), thr);
408+
}
409+
}
410+
366411
public void addItemFlags(final String string) throws Exception {
367412
final String[] separate = splitPattern.split(string, 2);
368413
if (separate.length != 2) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.earth2me.essentials.items.transform;
2+
3+
import org.bukkit.inventory.ItemStack;
4+
import org.bukkit.plugin.Plugin;
5+
6+
public abstract class PluginItemTransformer {
7+
private final Plugin plugin;
8+
9+
public PluginItemTransformer(final Plugin thePlugin) {
10+
if (thePlugin == null) {
11+
throw new IllegalArgumentException("Plugin cannot be null!");
12+
}
13+
14+
this.plugin = thePlugin;
15+
}
16+
17+
public abstract ItemStack apply(String data, ItemStack original);
18+
19+
public Plugin getPlugin() {
20+
return plugin;
21+
}
22+
}

0 commit comments

Comments
 (0)