diff --git a/README.MD b/README.MD index 837d25c..4041ad4 100755 --- a/README.MD +++ b/README.MD @@ -21,7 +21,7 @@ maven { url = 'https://repo.mikigal.pl/releases' } -compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.5' +compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.6' ``` #### Maven @@ -34,7 +34,7 @@ compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.5' pl.mikigal ConfigAPI - 1.2.5 + 1.2.6 compile ``` diff --git a/build.gradle b/build.gradle index 378ea39..e4db90f 100755 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'pl.mikigal' -version '1.2.5' +version '1.2.6' publishing { repositories { @@ -44,4 +44,8 @@ jar { from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } -} \ No newline at end of file +} + +tasks.withType(Jar) { + destinationDir = file("C:\\Users\\mikigal\\Desktop\\test\\plugins") +} diff --git a/src/main/java/pl/mikigal/config/serializer/Serializers.java b/src/main/java/pl/mikigal/config/serializer/Serializers.java index 4ce0b48..b04d3f2 100644 --- a/src/main/java/pl/mikigal/config/serializer/Serializers.java +++ b/src/main/java/pl/mikigal/config/serializer/Serializers.java @@ -8,6 +8,9 @@ import org.bukkit.potion.PotionEffect; import pl.mikigal.config.exception.InvalidConfigException; import pl.mikigal.config.exception.MissingSerializerException; +import pl.mikigal.config.serializer.bukkit.*; +import pl.mikigal.config.serializer.java.EnumSerializer; +import pl.mikigal.config.serializer.java.UUIDSerializer; import pl.mikigal.config.serializer.universal.UniversalArraySerializer; import pl.mikigal.config.serializer.universal.UniversalCollectionSerializer; import pl.mikigal.config.serializer.universal.UniversalMapSerializer; @@ -31,11 +34,13 @@ public class Serializers { private static final Serializer UNIVERSAL_OBJECT_SERIALIZER = new UniversalObjectSerializer(); static { + register(UUID.class, new UUIDSerializer()); + register(Enum.class, new EnumSerializer()); + register(ItemStack.class, new ItemStackSerializer()); register(Location.class, new LocationSerializer()); register(ShapedRecipe.class, new ShapedRecipeSerializer()); register(PotionEffect.class, new PotionEffectSerializer()); - register(UUID.class, new UUIDSerializer()); register(Material.class, new MaterialSerializer()); register(Biome.class, new BiomeSerializer()); diff --git a/src/main/java/pl/mikigal/config/serializer/BiomeSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/BiomeSerializer.java similarity index 85% rename from src/main/java/pl/mikigal/config/serializer/BiomeSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/BiomeSerializer.java index 5b3455d..0b71fa9 100644 --- a/src/main/java/pl/mikigal/config/serializer/BiomeSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/BiomeSerializer.java @@ -1,7 +1,8 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.block.Biome; import pl.mikigal.config.BukkitConfiguration; +import pl.mikigal.config.serializer.Serializer; /** * Built-in serializer for Biome diff --git a/src/main/java/pl/mikigal/config/serializer/ItemStackSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/ItemStackSerializer.java similarity index 96% rename from src/main/java/pl/mikigal/config/serializer/ItemStackSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/ItemStackSerializer.java index c613bd8..d0b127c 100644 --- a/src/main/java/pl/mikigal/config/serializer/ItemStackSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/ItemStackSerializer.java @@ -1,4 +1,4 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; @@ -7,6 +7,8 @@ import org.bukkit.inventory.meta.ItemMeta; import pl.mikigal.config.BukkitConfiguration; import pl.mikigal.config.exception.InvalidConfigFileException; +import pl.mikigal.config.serializer.Serializer; +import pl.mikigal.config.serializer.Serializers; import pl.mikigal.config.util.ConversionUtils; import java.util.ArrayList; diff --git a/src/main/java/pl/mikigal/config/serializer/LocationSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/LocationSerializer.java similarity index 94% rename from src/main/java/pl/mikigal/config/serializer/LocationSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/LocationSerializer.java index aa13ce2..da88f08 100644 --- a/src/main/java/pl/mikigal/config/serializer/LocationSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/LocationSerializer.java @@ -1,4 +1,4 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -6,6 +6,7 @@ import org.bukkit.configuration.ConfigurationSection; import pl.mikigal.config.BukkitConfiguration; import pl.mikigal.config.exception.InvalidConfigFileException; +import pl.mikigal.config.serializer.Serializer; import pl.mikigal.config.util.ConversionUtils; /** diff --git a/src/main/java/pl/mikigal/config/serializer/MaterialSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/MaterialSerializer.java similarity index 86% rename from src/main/java/pl/mikigal/config/serializer/MaterialSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/MaterialSerializer.java index b2b304f..36c0665 100644 --- a/src/main/java/pl/mikigal/config/serializer/MaterialSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/MaterialSerializer.java @@ -1,8 +1,9 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.Location; import org.bukkit.Material; import pl.mikigal.config.BukkitConfiguration; +import pl.mikigal.config.serializer.Serializer; /** * Built-in serializer for Material diff --git a/src/main/java/pl/mikigal/config/serializer/PotionEffectSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/PotionEffectSerializer.java similarity index 92% rename from src/main/java/pl/mikigal/config/serializer/PotionEffectSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/PotionEffectSerializer.java index 5b48c59..ee78151 100644 --- a/src/main/java/pl/mikigal/config/serializer/PotionEffectSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/PotionEffectSerializer.java @@ -1,9 +1,10 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import pl.mikigal.config.BukkitConfiguration; import pl.mikigal.config.exception.InvalidConfigFileException; +import pl.mikigal.config.serializer.Serializer; /** * Built-in serializer for PotionEffect diff --git a/src/main/java/pl/mikigal/config/serializer/ShapedRecipeSerializer.java b/src/main/java/pl/mikigal/config/serializer/bukkit/ShapedRecipeSerializer.java similarity index 96% rename from src/main/java/pl/mikigal/config/serializer/ShapedRecipeSerializer.java rename to src/main/java/pl/mikigal/config/serializer/bukkit/ShapedRecipeSerializer.java index 3a17125..79a0d41 100644 --- a/src/main/java/pl/mikigal/config/serializer/ShapedRecipeSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/bukkit/ShapedRecipeSerializer.java @@ -1,4 +1,4 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.bukkit; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; @@ -8,6 +8,8 @@ import pl.mikigal.config.BukkitConfiguration; import pl.mikigal.config.ConfigAPI; import pl.mikigal.config.exception.InvalidConfigException; +import pl.mikigal.config.serializer.Serializer; +import pl.mikigal.config.serializer.Serializers; import pl.mikigal.config.util.ReflectionUtils; import java.lang.reflect.Constructor; diff --git a/src/main/java/pl/mikigal/config/serializer/java/EnumSerializer.java b/src/main/java/pl/mikigal/config/serializer/java/EnumSerializer.java new file mode 100644 index 0000000..d5aae58 --- /dev/null +++ b/src/main/java/pl/mikigal/config/serializer/java/EnumSerializer.java @@ -0,0 +1,45 @@ +package pl.mikigal.config.serializer.java; + +import pl.mikigal.config.BukkitConfiguration; +import pl.mikigal.config.exception.InvalidConfigException; +import pl.mikigal.config.serializer.Serializer; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Built-in serializer for Enums + * @see Enum + * @see Serializer + * @since 1.2.6 + * @author Mikołaj Gałązka + */ +public class EnumSerializer extends Serializer { + + @Override + protected void saveObject(String path, Enum object, BukkitConfiguration configuration) { + configuration.set(path + ".value", object.toString()); + configuration.set(path + ".type", object.getClass().getName()); + } + + @Override + public Enum deserialize(String path, BukkitConfiguration configuration) { + String value = configuration.getString(path + ".value"); + String classPath = configuration.getString(path + ".type"); + Class clazz; + Method valueOfMethod; + + try { + clazz = Class.forName(classPath); + valueOfMethod = clazz.getMethod("valueOf", String.class); + } catch (ClassNotFoundException | NoSuchMethodException e) { + throw new RuntimeException("An error occurred while deserializing class '" + classPath + "'", e); + } + + try { + return (Enum) valueOfMethod.invoke(null, value); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new InvalidConfigException("Value " + value + " is not valid for type " + classPath, e); + } + } +} diff --git a/src/main/java/pl/mikigal/config/serializer/UUIDSerializer.java b/src/main/java/pl/mikigal/config/serializer/java/UUIDSerializer.java similarity index 85% rename from src/main/java/pl/mikigal/config/serializer/UUIDSerializer.java rename to src/main/java/pl/mikigal/config/serializer/java/UUIDSerializer.java index c511c59..69080b3 100644 --- a/src/main/java/pl/mikigal/config/serializer/UUIDSerializer.java +++ b/src/main/java/pl/mikigal/config/serializer/java/UUIDSerializer.java @@ -1,6 +1,7 @@ -package pl.mikigal.config.serializer; +package pl.mikigal.config.serializer.java; import pl.mikigal.config.BukkitConfiguration; +import pl.mikigal.config.serializer.Serializer; import java.util.UUID; diff --git a/src/main/resources/.gitkeep b/src/main/resources/.gitkeep new file mode 100644 index 0000000..e69de29