Skip to content

Instantly share code, notes, and snippets.

@DarkBlade12
Last active January 3, 2016 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkBlade12/8425244 to your computer and use it in GitHub Desktop.
Save DarkBlade12/8425244 to your computer and use it in GitHub Desktop.
This a little util which allows you to easily use Metadata
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.Metadatable;
import org.bukkit.plugin.Plugin;
public final class MetadataUtil {
private Plugin plugin;
private String pluginName;
public MetadataUtil(Plugin plugin) {
this.plugin = plugin;
pluginName = plugin.getName();
}
public void set(Metadatable m, String key, Object value) {
m.setMetadata(pluginName + "_" + key, new FixedMetadataValue(plugin, value));
}
public void remove(Metadatable m, String key) {
m.removeMetadata(pluginName + "_" + key, plugin);
}
public boolean hasKey(Metadatable m, String key) {
return m.getMetadata(pluginName + "_" + key).size() > 0;
}
public boolean hasKeys(Metadatable m, String... keys) {
for (String key : keys)
if (!hasKey(m, key))
return false;
return true;
}
public Object get(Metadatable m, String key) {
return hasKey(m, key) ? m.getMetadata(pluginName + "_" + key).get(0).value() : null;
}
public void removeAll(Metadatable m, String... keys) {
for (String k : keys)
remove(m, k);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment