Skip to content

Commit 83923b1

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

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

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

Lines changed: 46 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;
@@ -10,6 +11,7 @@
1011
import com.google.common.base.Joiner;
1112
import net.ess3.api.IEssentials;
1213
import net.ess3.api.TranslatableException;
14+
import org.bukkit.Bukkit;
1315
import net.ess3.provider.BannerDataProvider;
1416
import net.ess3.provider.ItemUnbreakableProvider;
1517
import net.ess3.provider.PotionMetaProvider;
@@ -53,6 +55,7 @@
5355
public class MetaItemStack {
5456
private static final Map<String, DyeColor> colorMap = new HashMap<>();
5557
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();
58+
private static final transient Map<String, PluginItemTransformer> itemTransformers = new HashMap<>();
5659
private static boolean useNewSkullMethod = true;
5760

5861
static {
@@ -84,6 +87,32 @@ public MetaItemStack(final ItemStack stack) {
8487
this.stack = stack.clone();
8588
}
8689

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

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

360389
stack.setItemMeta(armorMeta);
390+
} else if (split.length > 1 && itemTransformers.containsKey(split[0])) {
391+
transformItem(split[0], split[1]);
361392
} else {
362393
parseEnchantmentStrings(sender, allowUnsafe, split, ess);
363394
}
364395
}
365396

397+
private void transformItem(final String key, final String data){
398+
final PluginItemTransformer transformer = itemTransformers.get(key);
399+
400+
//Ignore, the plugin is disabled.
401+
if (!transformer.getPlugin().isEnabled()) {
402+
return;
403+
}
404+
405+
try {
406+
stack = transformer.apply(data, stack);
407+
} catch(final Throwable thr) {
408+
Essentials.getWrappedLogger().log(Level.SEVERE, String.format("Error applying data \"%s\" to itemstack! Plugin: %s, Key: %s", data, transformer.getPlugin().getName(), key), thr);
409+
}
410+
}
411+
366412
public void addItemFlags(final String string) throws Exception {
367413
final String[] separate = splitPattern.split(string, 2);
368414
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)