Created
August 19, 2014 00:07
-
-
Save DarkSeraphim/fb4d7098342f60284437 to your computer and use it in GitHub Desktop.
static Settings without static
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.darkseraphim.settings; | |
| import com.google.common.collect.Sets; | |
| import org.bukkit.Color; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.inventory.ItemStack; | |
| import org.bukkit.metadata.FixedMetadataValue; | |
| import org.bukkit.util.NumberConversions; | |
| import org.bukkit.util.Vector; | |
| import java.util.*; | |
| import java.util.regex.Pattern; | |
| /** | |
| * @author DarkSeraphim | |
| */ | |
| @SuppressWarnings("UnusedDeclaration") | |
| public abstract class Settings | |
| { | |
| private static final Pattern ints = Pattern.compile("[0-9]+"); | |
| private static final Pattern doubles = Pattern.compile("[0-9]+\\.?[0-9]*"); | |
| private static final Set<String> validatesTrue = Sets.newHashSet("true", "yes", "on", "1"); | |
| private static final Set<String> validatesFalse = Sets.newHashSet("false", "no", "off", "0"); | |
| public static Set<String> getKeys(Player player) | |
| { | |
| return Collections.unmodifiableSet(SettingsBase.getKeys(player)); | |
| } | |
| public static boolean contains(Player player, String key) | |
| { | |
| return SettingsBase.get(player, key) != null; | |
| } | |
| public static boolean isSet(Player player, String key) | |
| { | |
| return contains(player, key); | |
| } | |
| public static Object get(Player player, String key) | |
| { | |
| return SettingsBase.get(player, key); | |
| } | |
| public static Object get(Player player, String key, Object def) | |
| { | |
| Object o = get(player, key); | |
| return o != null ? o : def; | |
| } | |
| public static void set(Player player, String key, Object value) | |
| { | |
| if(value == null || (value instanceof FixedMetadataValue && ((FixedMetadataValue)value).value() == null)) | |
| { | |
| remove(player, key); | |
| } | |
| else | |
| { | |
| SettingsBase.set(player, key, value); | |
| } | |
| } | |
| public static void remove(Player player, String key) | |
| { | |
| SettingsBase.remove(player, key); | |
| } | |
| public static String getString(Player player, String key) | |
| { | |
| return getString(player, key, null); | |
| } | |
| public static String getString(Player player, String key, String def) | |
| { | |
| Object o = get(player, key); | |
| return o != null && o instanceof String ? (String) o : def; | |
| } | |
| public static boolean isString(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o instanceof String; | |
| } | |
| public static int getInt(Player player, String key) | |
| { | |
| return getInt(player, key, 0); | |
| } | |
| public static int getInt(Player player, String key, int def) | |
| { | |
| Object o = get(player, key); | |
| return o != null ? NumberConversions.toInt(o) : def; | |
| } | |
| public static boolean isInt(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o != null && (o instanceof Integer || ints.matcher(key).matches()); | |
| } | |
| public static boolean getBoolean(Player player, String key) | |
| { | |
| return getBoolean(player, key, false); | |
| } | |
| public static boolean getBoolean(Player player, String key, boolean def) | |
| { | |
| Object o = get(player, key); | |
| if(o == null) | |
| return def; | |
| if(o instanceof Boolean) | |
| return (Boolean) o; | |
| return validatesTrue.contains(o.toString()); | |
| } | |
| public static boolean isBoolean(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o != null && (o instanceof Boolean || validatesTrue.contains(o.toString()) || validatesFalse.contains(o.toString())); | |
| } | |
| public static double getDouble(Player player, String key) | |
| { | |
| return getDouble(player, key, 0D); | |
| } | |
| public static double getDouble(Player player, String key, double def) | |
| { | |
| Object o = get(player, key); | |
| return o != null ? NumberConversions.toDouble(o) : def; | |
| } | |
| public static boolean isDouble(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o != null && (o instanceof Double || doubles.matcher(o.toString()).matches()); | |
| } | |
| public static long getLong(Player player, String key) | |
| { | |
| return getLong(player, key, 0L); | |
| } | |
| public static long getLong(Player player, String key, long def) | |
| { | |
| Object o = get(player, key); | |
| return o != null ? NumberConversions.toLong(o) : def; | |
| } | |
| public static boolean isLong(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o != null && (o instanceof Long || ints.matcher(o.toString()).matches()); | |
| } | |
| public static List<?> getList(Player player, String key) | |
| { | |
| return getList(player, key, null); | |
| } | |
| public static List<?> getList(Player player, String key, List<?> def) | |
| { | |
| Object o = get(player, key); | |
| return o instanceof List<?> ? (List<?>)o : def; | |
| } | |
| public static boolean isList(Player player, String key) | |
| { | |
| Object o = get(player, key); | |
| return o instanceof List<?>; | |
| } | |
| public static List<String> getStringList(Player player, String key) | |
| { | |
| List<String> dest = new ArrayList<String>(); | |
| List<?> src = getList(player, key); | |
| if(src == null) | |
| return dest; | |
| for(Object o : src) | |
| { | |
| if(o instanceof String || isPrimitiveWrapper(o)) | |
| dest.add(String.valueOf(o)); | |
| } | |
| return dest; | |
| } | |
| public static List<Integer> getIntegerList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Integer>(0); | |
| } | |
| List<Integer> result = new ArrayList<Integer>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Integer) | |
| { | |
| result.add((Integer) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Integer.valueOf((String) object)); | |
| } catch (Exception ignored) {} | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((int) (Character) object); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).intValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Boolean> getBooleanList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Boolean>(0); | |
| } | |
| List<Boolean> result = new ArrayList<Boolean>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Boolean) | |
| { | |
| result.add((Boolean) object); | |
| } | |
| else | |
| { | |
| if (object instanceof String) | |
| { | |
| if (validatesTrue.contains(object)) | |
| { | |
| result.add(true); | |
| } | |
| else if (validatesFalse.contains(object)) | |
| { | |
| result.add(false); | |
| } | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Double> getDoubleList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Double>(0); | |
| } | |
| List<Double> result = new ArrayList<Double>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Double) | |
| { | |
| result.add((Double) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Double.valueOf((String) object)); | |
| } catch (Exception ignored) { } | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((double) (Character) object); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).doubleValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Float> getFloatList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Float>(0); | |
| } | |
| List<Float> result = new ArrayList<Float>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Float) | |
| { | |
| result.add((Float) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Float.valueOf((String) object)); | |
| } catch (Exception ignored) { } | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((float) (Character) object); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).floatValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Long> getLongList(Player player, String key) { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Long>(0); | |
| } | |
| List<Long> result = new ArrayList<Long>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Long) | |
| { | |
| result.add((Long) object); | |
| } | |
| else | |
| { | |
| if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Long.valueOf((String) object)); | |
| } | |
| catch (Exception ignored) | |
| { | |
| } | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((long) (Character) object); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).longValue()); | |
| } | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Byte> getByteList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Byte>(0); | |
| } | |
| List<Byte> result = new ArrayList<Byte>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Byte) | |
| { | |
| result.add((Byte) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Byte.valueOf((String) object)); | |
| } catch (Exception ignored) { } | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((byte) ((Character) object).charValue()); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).byteValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Character> getCharacterList(Player player, String key) { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Character>(0); | |
| } | |
| List<Character> result = new ArrayList<Character>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Character) | |
| { | |
| result.add((Character) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| String str = (String) object; | |
| if (str.length() == 1) | |
| { | |
| result.add(str.charAt(0)); | |
| } | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add((char) ((Number) object).intValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Short> getShortList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| if (list == null) | |
| { | |
| return new ArrayList<Short>(0); | |
| } | |
| List<Short> result = new ArrayList<Short>(); | |
| for (Object object : list) | |
| { | |
| if (object instanceof Short) | |
| { | |
| result.add((Short) object); | |
| } | |
| else if (object instanceof String) | |
| { | |
| try | |
| { | |
| result.add(Short.valueOf((String) object)); | |
| } catch (Exception ignored) { } | |
| } | |
| else if (object instanceof Character) | |
| { | |
| result.add((short) ((Character) object).charValue()); | |
| } | |
| else if (object instanceof Number) | |
| { | |
| result.add(((Number) object).shortValue()); | |
| } | |
| } | |
| return result; | |
| } | |
| public static List<Map<?, ?>> getMapList(Player player, String key) | |
| { | |
| List<?> list = getList(player, key); | |
| List<Map<?, ?>> result = new ArrayList<Map<?, ?>>(); | |
| if (list == null) | |
| { | |
| return result; | |
| } | |
| for (Object object : list) | |
| { | |
| if (object instanceof Map) | |
| { | |
| result.add((Map<?, ?>) object); | |
| } | |
| } | |
| return result; | |
| } | |
| public static Vector getVector(Player player, String path) | |
| { | |
| return getVector(player, path, null); | |
| } | |
| public static Vector getVector(Player player, String key, Vector def) | |
| { | |
| Object val = get(player, key); | |
| return (val instanceof Vector) ? (Vector) val : def; | |
| } | |
| public static boolean isVector(Player player, String key) | |
| { | |
| Object val = get(player, key); | |
| return val instanceof Vector; | |
| } | |
| public static ItemStack getItemStack(Player player, String key) | |
| { | |
| return getItemStack(player, key, null); | |
| } | |
| public static ItemStack getItemStack(Player player, String key, ItemStack def) | |
| { | |
| Object val = get(player, key); | |
| return (val instanceof ItemStack) ? (ItemStack) val : def; | |
| } | |
| public static boolean isItemStack(Player player, String key) | |
| { | |
| Object val = get(player, key); | |
| return val instanceof ItemStack; | |
| } | |
| public static Color getColor(Player player, String key) | |
| { | |
| return getColor(player, key, null); | |
| } | |
| public static Color getColor(Player player, String key, Color def) | |
| { | |
| Object val = get(player, key); | |
| return (val instanceof Color) ? (Color) val : def; | |
| } | |
| public static boolean isColor(Player player, String key) | |
| { | |
| Object val = get(player, key); | |
| return val instanceof Color; | |
| } | |
| protected static boolean isPrimitiveWrapper(Object input) | |
| { | |
| return input instanceof Integer || input instanceof Boolean || | |
| input instanceof Character || input instanceof Byte || | |
| input instanceof Short || input instanceof Double || | |
| input instanceof Long || input instanceof Float; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package net.darkseraphim.settings; | |
| import org.apache.commons.lang.Validate; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.OfflinePlayer; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.metadata.FixedMetadataValue; | |
| import org.bukkit.metadata.LazyMetadataValue; | |
| import org.bukkit.metadata.MetadataStoreBase; | |
| import org.bukkit.metadata.MetadataValue; | |
| import org.bukkit.plugin.Plugin; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import java.lang.ref.WeakReference; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.Method; | |
| import java.util.*; | |
| import java.util.concurrent.Callable; | |
| /** | |
| * @author DarkSeraphim | |
| */ | |
| public abstract class SettingsBase | |
| { | |
| private static WeakReference<Plugin> instance; | |
| private final static String keyPrefix = "Settings:"; | |
| // Reserved key list | |
| private final static String keyList = "SettingsKeyList"; | |
| protected static void initialize(Plugin plugin) | |
| { | |
| Validate.notNull(plugin, "Plugin cannot be null"); | |
| instance = new WeakReference<Plugin>(plugin); | |
| } | |
| private static void validate() | |
| { | |
| Validate.notNull(instance, "mainClass reference was null"); | |
| Validate.notNull(instance.get(), "mainClass was null"); | |
| } | |
| protected static Object get(Player player, String key) | |
| { | |
| validate(); | |
| List<MetadataValue> values = player.getMetadata(keyPrefix+key); | |
| for(MetadataValue value : values) | |
| { | |
| if(value.getOwningPlugin() == instance.get()) | |
| return value.value(); | |
| } | |
| return null; | |
| } | |
| protected static void set(Player player, String key, Object value) | |
| { | |
| validate(); | |
| if(!(value instanceof MetadataValue)) | |
| if(value instanceof Callable) | |
| value = new LazyMetadataValue(instance.get(), LazyMetadataValue.CacheStrategy.NEVER_CACHE, (Callable)value); | |
| else | |
| value = new FixedMetadataValue(instance.get(), value); | |
| player.setMetadata(keyPrefix+key, (MetadataValue)value); | |
| addKey(player, key); | |
| } | |
| protected static void remove(Player player, String key) | |
| { | |
| validate(); | |
| player.removeMetadata(keyPrefix+key, instance.get()); | |
| removeKey(player, key); | |
| } | |
| private static void addKey(Player player, String key) | |
| { | |
| Set<String> keys = getKeys(player); | |
| keys.add(key); | |
| } | |
| private static void removeKey(Player player, String key) | |
| { | |
| Set<String> keys = getKeys(player); | |
| keys.remove(key); | |
| } | |
| protected static Set<String> getKeys(Player player) | |
| { | |
| List<MetadataValue> values = player.getMetadata(SettingsBase.keyList); | |
| for(MetadataValue value : values) | |
| { | |
| if(value.getOwningPlugin() == instance.get()) | |
| { | |
| Object o = value.value(); | |
| Validate.isTrue(o instanceof List, "Settings keylist was changed to an invalid value"); | |
| return (Set<String>)o; | |
| } | |
| } | |
| Set<String> keys = new HashSet<String>(); | |
| player.setMetadata(SettingsBase.keyList, new FixedMetadataValue(instance.get(), keys)); | |
| return keys; | |
| } | |
| protected static void destroy() | |
| { | |
| if(instance != null) | |
| instance.clear(); | |
| instance = null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment