@Override public void change(Event e, Object[] delta, Changer.ChangeMode mode){ if (object.getSingle(e) instanceof Metadatable) { Metadatable metadata = (Metadatable)object.getSingle(e); if (mode == ChangeMode.SET) { if (metadata.hasMetadata(string.getSingle(e))) { metadata.removeMetadata(string.getSingle(e), Skellett.plugin); } metadata.setMetadata(string.getSingle(e), (MetadataValue)new FixedMetadataValue(Skellett.plugin, (Object)(delta[0]))); } else if (mode == ChangeMode.RESET || mode == ChangeMode.DELETE) { if (metadata.hasMetadata(string.getSingle(e))) { metadata.removeMetadata(string.getSingle(e), Skellett.plugin); } } } }
public static Object getMetadata(JavaPlugin plugin, Metadatable object, String key) { List<MetadataValue> values = object.getMetadata(key); for (MetadataValue value : values) { if (value.getOwningPlugin().equals(plugin)) { return value.value(); } } return null; }
/** * Set a metadata value. * @param metadata - The object to set the metadata of. * @param key - The key to index the metadata by. * @param o - The object value to set. */ public static void setMetadata(Metadatable metadata, String key, Object o) { if (o instanceof MetadataValue) o = ((MetadataValue) o).value(); if (o instanceof Enum<?>) o = ((Enum<?>) o).name(); metadata.setMetadata(key, new FixedMetadataValue(Core.getInstance(), o)); }
/** * Does this player have a cooldown? Alerts them if they do. * @param meta * @param cooldown * @return hasCooldown */ public static boolean alertCooldown(Metadatable meta, String cooldown) { boolean has = hasCooldown(meta, cooldown); if (has && meta instanceof CommandSender) ((CommandSender) meta).sendMessage(ChatColor.RED + "You must wait " + Utils.formatTime(meta.getMetadata(cooldown).get(0).asLong() - System.currentTimeMillis()) + " before doing this."); return has; }
public Boolean isNPC(Object object) { if ( ! (object instanceof Metadatable)) return false; Metadatable metadatable = (Metadatable) object; try { return metadatable.hasMetadata("NPC"); } catch (UnsupportedOperationException e) { return false; } }
@Override @Nullable protected Object[] get(Event e) { Metadatable metadata; if (object.getSingle(e) instanceof Metadatable && (metadata = (Metadatable)object.getSingle(e)).hasMetadata(string.getSingle(e))) { return new Object[]{((MetadataValue)metadata.getMetadata(string.getSingle(e)).iterator().next()).value()}; } return null; }
/** * Gets metadata * * @param obj object * @param key key * @return metadata value */ public static MetadataValue getMetadata(Metadatable obj, String key) { for(MetadataValue value : obj.getMetadata(key)) { if(value.getOwningPlugin().getDescription().getName().equals(plugin.getDescription().getName())) { return value; } } return null; }
@Override public final GameMetadata getMetadata(Metadatable object, String key) { for (MetadataValue v : object.getMetadata(key)) { if (v instanceof GameMetadata) { GameMetadata data = (GameMetadata) v; if (data.getOwningPlugin() == fakePlugin) { return data; } } } return null; }
private Metadatable resolveObject(Parameter object) { if ( object instanceof PlayerParameter ) return object.as(org.bukkit.entity.Player.class); if ( object instanceof EntityParameter ) return object.as(org.bukkit.entity.Entity.class); if ( object instanceof WorldParameter) return object.as(org.bukkit.World.class); if ( object instanceof BlockParameter) return object.as(org.bukkit.block.Block.class); if ( object instanceof LocationParameter) return object.as(org.bukkit.block.Block.class); throw new FizzleException("Couldn't convert object into something we can use metadata with: " + object.toString()); }
public boolean hasKeys(Metadatable m, String... keys) { for (String key : keys) { if (!hasKey(m, key)) { return false; } } return true; }
/** * Convenience wrapper to get the metadata value set by STB. * * @param m the metadatable object * @param key the metadata key * @return the metadata value, or null if there is none */ public static Object getMetadataValue(Metadatable m, String key) { for (MetadataValue mv : m.getMetadata(key)) { if (mv.getOwningPlugin() == SensibleToolboxPlugin.getInstance()) { return mv.value(); } } return null; }
public static Object getMetadata(Metadatable object, String key, Plugin plugin) { List<MetadataValue> values = object.getMetadata(key); for (MetadataValue value : values) { if (value.getOwningPlugin() == plugin) { return value.value(); } } return null; }
MetadataValue getMetadata(Metadatable metadatable, String key) { List<MetadataValue> values = metadatable.getMetadata(key); if(values == null) return null; for(MetadataValue value : values) { if(this.equals(value.getOwningPlugin())) return value; } return null; }
public static boolean isNpc(Object object) { if (!(object instanceof Metadatable)) { return false; } Metadatable metadatable = (Metadatable) object; return metadatable.hasMetadata("NPC"); }
public static void setMetadata(JavaPlugin plugin, Metadatable object, String key, Object value) { object.setMetadata(key, new FixedMetadataValue(plugin, value)); }
public static boolean is(Metadatable entity) { return entity.hasMetadata(METADATA_KEY); }
public static @Nullable Grenade get(Metadatable entity) { return entity.hasMetadata(METADATA_KEY) ? (Grenade) entity.getMetadata(METADATA_KEY).get(0).value() : null; }
public void set(Plugin plugin, Metadatable entity) { entity.setMetadata(METADATA_KEY, new FixedMetadataValue(plugin, this)); }
/** * Remove metadata from the given object. * @param obj - Object with the metadata. * @param key - The key to remove. */ public static <T> T removeMetadata(Metadatable obj, String key) { T value = getValue(obj ,key); obj.removeMetadata(key, Core.getInstance()); return value; }
@Override public final void removeMetadata(Metadatable object, String key) { object.removeMetadata(key, fakePlugin); }
public static MetadataValue getMetadata(Metadatable obj, String key) { return Iterables.getFirst(obj.getMetadata(key), null); }
public Object get(Metadatable m, String key) { return hasKey(m, key) ? m.getMetadata(pluginName + "_" + key).get(0).value() : null; }
public boolean hasKey(Metadatable m, String key) { return m.getMetadata(pluginName + "_" + key).size() > 0; }
public void remove(Metadatable m, String key) { m.removeMetadata(pluginName + "_" + key, plugin); }
public void removeAll(Metadatable m, String... keys) { for (String k : keys) { remove(m, k); } }
public void set(Metadatable m, String key, Object value) { m.setMetadata(pluginName + "_" + key, new FixedMetadataValue(plugin, value)); }
public static void setMetadata(Metadatable object, String key, Object value, Plugin plugin) { object.setMetadata(key, new FixedMetadataValue(plugin, value)); }
void setMetadata(Metadatable metadatable, String key, Object value) { metadatable.setMetadata(key, new FixedMetadataValue(this, value)); }
/** * Checks if this player has the given cooldown, and if they don't, give it to them after telling them they have it. * Returns whether or not the player had the cooldown before this was called. * * @param meta * @param cooldown * @param ticks * @return hasCooldown */ public static boolean updateCooldown(Metadatable meta, String cooldown, int ticks) { boolean has = alertCooldown(meta, cooldown); if (!has) setCooldown(meta, cooldown, ticks); return has; }
/** * Checks if this player has the given cooldown, and if they don't, give it to them silently. * Returns whether or not the player had the cooldown before this was called. * * @param meta * @param cooldown * @param ticks * @return hasCooldown */ public static boolean updateCooldownSilently(Metadatable meta, String cooldown, int ticks) { boolean has = hasCooldown(meta, cooldown); if (!has) setCooldown(meta, cooldown, ticks); return has; }
/** * Does this object have the given metadata? * @param obj - Object with metadata * @param type - Metadata key * @return hasKey */ public static boolean hasMetadata(Metadatable obj, String type) { return obj.hasMetadata(type); }
/** * Get a metadata value from a player. * @param obj - The object with the metadata. * @parma key - The key to get the metadata from. * @return getMetadata */ public static MetadataValue getMetadata(Metadatable obj, String key) { return hasMetadata(obj, key) ? obj.getMetadata(key).get(0) : null; }
/** * Get a metadata value as an enum. * @param obj - The object to get the value from * @param key - The key the value is indexed by. * @param clazz - The enum class we want to load. * @param <E> * @return enumValue */ public static <E extends Enum<E>> E getEnum(Metadatable obj, String key, Class<E> clazz) { return Utils.getEnum(getValue(obj, key), clazz); }
/** * Get a metadata value from a metadatable object. * @param obj - The object with the metadata. * @param key - The key the metadata is indexed by. * @param <T> - The type to return the value as. * @return value */ @SuppressWarnings({"unchecked", "ConstantConditions"}) public static <T> T getValue(Metadatable obj, String key) { return (T) (hasMetadata(obj, key) ? getMetadata(obj, key).value() : null); }
/** * Get a metadata value from a given object, if the metadata key is not present it will return the fallback value. * If the fallback value is returned, it is stored as the actual value. * @param obj - The object with the metadata. * @param key - The key the metadata is indexed by. * @param fallback - The fallback value. * @param <T> * @return value */ public static <T> T getValue(Metadatable obj, String key, T fallback) { if (!hasMetadata(obj, key)) setMetadata(obj, key, fallback); return getValue(obj, key); }
/** * Enact a cooldown on the given type. * * IMPORTANT: * Cooldowns are seperate from the metadata enum because the metadata enum will get removed on logout. * Also, it's inconvenient to add them to the enum. * * @param obj * @param cooldown * @param ticks */ public static void setCooldown(Metadatable obj, String cooldown, int ticks) { obj.setMetadata(cooldown, new FixedMetadataValue(Core.getInstance(), System.currentTimeMillis() + (ticks * 50))); }
/** * Does this object have an active cooldown? * @param obj * @param cooldown * @return hasCooldown - Only will be true if the cooldown has not expired as well. */ public static boolean hasCooldown(Metadatable obj, String cooldown) { return obj.hasMetadata(cooldown) && obj.getMetadata(cooldown).get(0).asLong() > System.currentTimeMillis(); }
/** * Sets metadata * * @param obj object * @param key key * @param value value */ public static void setMetadata(Metadatable obj, String key, Object value) { obj.setMetadata(key, new FixedMetadataValue(plugin, value)); }
/** * Removes metadata * * @param obj object * @param key key */ public static void removeMetadata(Metadatable obj, String key) { obj.removeMetadata(key, plugin); }
/** * Retrieves the metadata for a metadatable object using key * and this game instance to ensure correct selection * @param object a metadatable object * @param key metadata key * @return game metadata */ public GameMetadata getMetadata(Metadatable object, String key);