Made Adapter its own annotation.

This commit is contained in:
tastybento 2018-02-05 11:35:50 -08:00
parent 26956d8386
commit 812594783e
9 changed files with 91 additions and 56 deletions

View File

@ -14,10 +14,11 @@ import org.bukkit.potion.PotionEffectType;
import us.tastybento.bskyblock.Constants.GameType; import us.tastybento.bskyblock.Constants.GameType;
import us.tastybento.bskyblock.api.configuration.ConfigEntry; import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.api.configuration.ISettings; import us.tastybento.bskyblock.api.configuration.ISettings;
import us.tastybento.bskyblock.api.configuration.PotionEffectListAdpater;
import us.tastybento.bskyblock.api.configuration.StoreAt; import us.tastybento.bskyblock.api.configuration.StoreAt;
import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.database.BSBDatabase.DatabaseType; import us.tastybento.bskyblock.database.BSBDatabase.DatabaseType;
import us.tastybento.bskyblock.database.objects.adapters.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.PotionEffectListAdapter;
/** /**
* All the plugin settings are here * All the plugin settings are here
@ -237,7 +238,8 @@ public class Settings implements ISettings<Settings> {
@ConfigEntry(path = "acid.damage.rain", specificTo = GameType.ACIDISLAND) @ConfigEntry(path = "acid.damage.rain", specificTo = GameType.ACIDISLAND)
private int acidRainDamage = 1; private int acidRainDamage = 1;
@ConfigEntry(path = "acid.damage.effects", specificTo = GameType.ACIDISLAND, adapter = PotionEffectListAdpater.class) @ConfigEntry(path = "acid.damage.effects", specificTo = GameType.ACIDISLAND)
@Adapter(PotionEffectListAdapter.class)
private List<PotionEffectType> acidEffects = new ArrayList<>(Arrays.asList(PotionEffectType.CONFUSION, PotionEffectType.SLOW)); private List<PotionEffectType> acidEffects = new ArrayList<>(Arrays.asList(PotionEffectType.CONFUSION, PotionEffectType.SLOW));
/* SCHEMATICS */ /* SCHEMATICS */

View File

@ -16,12 +16,11 @@ import us.tastybento.bskyblock.Constants.GameType;
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface ConfigEntry { public @interface ConfigEntry {
String path() default ""; String path();
String since() default "1.0"; String since() default "1.0";
boolean overrideOnChange() default false; boolean overrideOnChange() default false;
boolean experimental() default false; boolean experimental() default false;
boolean needsReset() default false; boolean needsReset() default false;
GameType specificTo() default GameType.BOTH; GameType specificTo() default GameType.BOTH;
Class<?> adapter() default Adapter.class;
} }

View File

@ -26,11 +26,12 @@ import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.Constants; import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.Constants.GameType; import us.tastybento.bskyblock.Constants.GameType;
import us.tastybento.bskyblock.api.configuration.Adapter;
import us.tastybento.bskyblock.api.configuration.ConfigEntry; import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.api.configuration.StoreAt; import us.tastybento.bskyblock.api.configuration.StoreAt;
import us.tastybento.bskyblock.database.DatabaseConnecter; import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler; import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.adapters.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.AdapterInterface;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
/** /**
@ -179,21 +180,24 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
continue; continue;
} }
// TODO: Add handling of other ConfigEntry elements // TODO: Add handling of other ConfigEntry elements
if (!configEntry.adapter().equals(Adapter.class)) { }
// A conversion adapter has been defined Adapter adapterNotation = field.getAnnotation(Adapter.class);
Object value = config.get(storageLocation); if (adapterNotation != null && AdapterInterface.class.isAssignableFrom(adapterNotation.value())) {
method.invoke(instance, ((Adapter<?,?>)configEntry.adapter().newInstance()).serialize(value)); if (DEBUG)
if (DEBUG) { plugin.getLogger().info("DEBUG: there is an adapter");
plugin.getLogger().info("DEBUG: value = " + value); // A conversion adapter has been defined
plugin.getLogger().info("DEBUG: property type = " + propertyDescriptor.getPropertyType()); Object value = config.get(storageLocation);
plugin.getLogger().info("DEBUG: " + value.getClass()); method.invoke(instance, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).serialize(value));
} if (DEBUG) {
if (value != null && !value.getClass().equals(MemorySection.class)) { plugin.getLogger().info("DEBUG: value = " + value);
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType())); plugin.getLogger().info("DEBUG: property type = " + propertyDescriptor.getPropertyType());
} plugin.getLogger().info("DEBUG: " + value.getClass());
// We are done here
continue;
} }
if (value != null && !value.getClass().equals(MemorySection.class)) {
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
}
// We are done here
continue;
} }
// Look in the YAML Config to see if this field exists (it should) // Look in the YAML Config to see if this field exists (it should)
@ -352,18 +356,24 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
storageLocation = configEntry.path(); storageLocation = configEntry.path();
} }
// TODO: add in game-specific saving // TODO: add in game-specific saving
if (!configEntry.adapter().equals(Adapter.class)) {
// A conversion adapter has been defined
try {
config.set(storageLocation, ((Adapter<?,?>)configEntry.adapter().newInstance()).deserialize(value));
} catch (InstantiationException e) {
e.printStackTrace();
}
// We are done here
continue fields;
}
} }
Adapter adapterNotation = field.getAnnotation(Adapter.class);
if (adapterNotation != null && AdapterInterface.class.isAssignableFrom(adapterNotation.value())) {
if (DEBUG)
plugin.getLogger().info("DEBUG: there is an adapter");
// A conversion adapter has been defined
// A conversion adapter has been defined
try {
config.set(storageLocation, ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).deserialize(value));
} catch (InstantiationException e) {
e.printStackTrace();
}
// We are done here
continue fields;
}
//plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName()); //plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName());
// Depending on the vale type, it'll need serializing differenty // Depending on the vale type, it'll need serializing differenty
// Check if this field is the mandatory UniqueId field. This is used to identify this instantiation of the class // Check if this field is the mandatory UniqueId field. This is used to identify this instantiation of the class

View File

@ -32,10 +32,11 @@ import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import us.tastybento.bskyblock.api.configuration.Adapter;
import us.tastybento.bskyblock.api.configuration.ConfigEntry; import us.tastybento.bskyblock.api.configuration.ConfigEntry;
import us.tastybento.bskyblock.database.DatabaseConnecter; import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler; import us.tastybento.bskyblock.database.managers.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.objects.adapters.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.AdapterInterface;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
/** /**
@ -423,14 +424,15 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (configEntry != null) { if (configEntry != null) {
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: there is a configEntry"); plugin.getLogger().info("DEBUG: there is a configEntry");
if (!configEntry.adapter().equals(Adapter.class)) { }
if (DEBUG) Adapter adapterNotation = field.getAnnotation(Adapter.class);
plugin.getLogger().info("DEBUG: there is an adapter"); if (adapterNotation != null && AdapterInterface.class.isAssignableFrom(adapterNotation.value())) {
// A conversion adapter has been defined if (DEBUG)
value = ((Adapter<?,?>)configEntry.adapter().newInstance()).deserialize(value); plugin.getLogger().info("DEBUG: there is an adapter");
if (DEBUG) // A conversion adapter has been defined
plugin.getLogger().info("DEBUG: value now after deserialization = " + value); value = ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).deserialize(value);
} if (DEBUG)
plugin.getLogger().info("DEBUG: value now after deserialization = " + value);
} }
// Create set and map table inserts if this is a Collection // Create set and map table inserts if this is a Collection
if (propertyDescriptor.getPropertyType().equals(Set.class) || if (propertyDescriptor.getPropertyType().equals(Set.class) ||
@ -485,7 +487,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Entry<?,?> en = (Entry<?, ?>) it.next(); Entry<?,?> en = (Entry<?, ?>) it.next();
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: entry ket = " + en.getKey()); plugin.getLogger().info("DEBUG: entry ket = " + en.getKey());
// Get the key and serialize it // Get the key and serialize it
Object key = serialize(en.getKey(), en.getKey().getClass()); Object key = serialize(en.getKey(), en.getKey().getClass());
if (DEBUG) if (DEBUG)
@ -789,14 +791,16 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (configEntry != null) { if (configEntry != null) {
if (DEBUG) if (DEBUG)
plugin.getLogger().info("DEBUG: there is a configEntry"); plugin.getLogger().info("DEBUG: there is a configEntry");
if (!configEntry.adapter().equals(Adapter.class)) { // TODO: add config entry handling
if (DEBUG) }
plugin.getLogger().info("DEBUG: there is an adapter"); Adapter adapterNotation = field.getAnnotation(Adapter.class);
// A conversion adapter has been defined if (adapterNotation != null && AdapterInterface.class.isAssignableFrom(adapterNotation.value())) {
value = ((Adapter<?,?>)configEntry.adapter().newInstance()).serialize(value); if (DEBUG)
if (DEBUG) plugin.getLogger().info("DEBUG: there is an adapter");
plugin.getLogger().info("DEBUG: value now after serialization = " + value); // A conversion adapter has been defined
} value = ((AdapterInterface<?,?>)adapterNotation.value().newInstance()).serialize(value);
if (DEBUG)
plugin.getLogger().info("DEBUG: value now after deserialization = " + value);
} }
if (DEBUG) { if (DEBUG) {
plugin.getLogger().info("DEBUG: invoking method " + method.getName()); plugin.getLogger().info("DEBUG: invoking method " + method.getName());

View File

@ -23,6 +23,8 @@ import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandLockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent; import us.tastybento.bskyblock.api.events.island.IslandEvent.IslandUnlockEvent;
import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason; import us.tastybento.bskyblock.api.events.island.IslandEvent.Reason;
import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.database.objects.adapters.Adapter;
import us.tastybento.bskyblock.database.objects.adapters.FlagSerializer;
import us.tastybento.bskyblock.managers.RanksManager; import us.tastybento.bskyblock.managers.RanksManager;
import us.tastybento.bskyblock.util.Util; import us.tastybento.bskyblock.util.Util;
@ -80,7 +82,7 @@ public class Island implements DataObject {
private boolean purgeProtected = false; private boolean purgeProtected = false;
//// Protection flags //// //// Protection flags ////
@ConfigEntry(adapter = FlagSerializer.class) @Adapter(FlagSerializer.class)
private HashMap<Flag, Integer> flags = new HashMap<>(); private HashMap<Flag, Integer> flags = new HashMap<>();
private int levelHandicap; private int levelHandicap;

View File

@ -0,0 +1,19 @@
package us.tastybento.bskyblock.database.objects.adapters;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* Denotes which adapter should be used to serialize or deserialize this field
* @author tastybento
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Adapter {
Class<?> value();
}

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.api.configuration; package us.tastybento.bskyblock.database.objects.adapters;
/** /**
@ -8,7 +8,7 @@ package us.tastybento.bskyblock.api.configuration;
* @param <S> * @param <S>
* @param <V> * @param <V>
*/ */
public interface Adapter<S,V> { public interface AdapterInterface<S,V> {
/** /**
* Serialize object * Serialize object

View File

@ -1,4 +1,4 @@
package us.tastybento.bskyblock.database.objects; package us.tastybento.bskyblock.database.objects.adapters;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -7,7 +7,6 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.MemorySection;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.configuration.Adapter;
import us.tastybento.bskyblock.api.flags.Flag; import us.tastybento.bskyblock.api.flags.Flag;
/** /**
@ -17,7 +16,7 @@ import us.tastybento.bskyblock.api.flags.Flag;
* @author tastybento * @author tastybento
* *
*/ */
public class FlagSerializer implements Adapter<HashMap<Flag, Integer>, HashMap<String, Integer>> { public class FlagSerializer implements AdapterInterface<HashMap<Flag, Integer>, HashMap<String, Integer>> {
@Override @Override
public HashMap<Flag, Integer> serialize(Object object) { public HashMap<Flag, Integer> serialize(Object object) {

View File

@ -1,11 +1,11 @@
package us.tastybento.bskyblock.api.configuration; package us.tastybento.bskyblock.database.objects.adapters;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
public class PotionEffectListAdpater implements Adapter<List<PotionEffectType>, List<String>> { public class PotionEffectListAdapter implements AdapterInterface<List<PotionEffectType>, List<String>> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override