Skip to content

Commit 897c702

Browse files
committed
Add ability to register custom item transformers.
1 parent 7bc91f1 commit 897c702

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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

Lines changed: 48 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;
@@ -13,6 +14,7 @@
1314
import net.ess3.provider.BannerDataProvider;
1415
import net.ess3.provider.ItemUnbreakableProvider;
1516
import net.ess3.provider.PotionMetaProvider;
17+
import org.bukkit.Bukkit;
1618
import org.bukkit.Color;
1719
import org.bukkit.DyeColor;
1820
import org.bukkit.FireworkEffect;
@@ -47,6 +49,9 @@
4749
public class MetaItemStack {
4850
private static final Map<String, DyeColor> colorMap = new HashMap<>();
4951
private static final Map<String, FireworkEffect.Type> fireworkShape = new HashMap<>();
52+
53+
//Contains plugin registered item meta data transformers.
54+
private static final transient Map<String, PluginItemTransformer> itemTransformers = new HashMap<>();
5055
private static boolean useNewSkullMethod = true;
5156

5257
static {
@@ -78,6 +83,32 @@ public MetaItemStack(final ItemStack stack) {
7883
this.stack = stack.clone();
7984
}
8085

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

@@ -343,11 +374,28 @@ public void addStringMeta(final CommandSource sender, final boolean allowUnsafe,
343374
} else {
344375
throw new TranslatableException("leatherSyntax");
345376
}
377+
} else if (split.length > 1 && itemTransformers.containsKey(split[0])) {
378+
transformItem(split[0], split[1]);
346379
} else {
347380
parseEnchantmentStrings(sender, allowUnsafe, split, ess);
348381
}
349382
}
350383

384+
private void transformItem(String key, String data){
385+
PluginItemTransformer transformer = itemTransformers.get(key);
386+
387+
//Ignore, the plugin is disabled.
388+
if(!transformer.getPlugin().isEnabled())
389+
return;
390+
391+
try{
392+
stack = transformer.apply(data, stack);
393+
}catch(Throwable thr){
394+
Bukkit.getLogger().severe(String.format("[Essentials] - Error applying data \"%s\" to itemstack! Plugin: %s, Key: %s", data, transformer.getPlugin().getName(), key));
395+
thr.printStackTrace();
396+
}
397+
}
398+
351399
public void addItemFlags(final String string) throws Exception {
352400
final String[] separate = splitPattern.split(string, 2);
353401
if (separate.length != 2) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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(Plugin thePlugin){
10+
if(thePlugin == null)
11+
throw new IllegalArgumentException("Plugin cannot be null!");
12+
13+
this.plugin = thePlugin;
14+
}
15+
16+
public abstract ItemStack apply(String data, ItemStack original);
17+
18+
public Plugin getPlugin() {
19+
return plugin;
20+
}
21+
}

0 commit comments

Comments
 (0)