Skip to content

Commit

Permalink
Implemented support for serializing Enums, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikigal committed Apr 22, 2024
1 parent d6a338b commit 745c505
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,7 +34,7 @@ compile group: 'pl.mikigal', name: 'ConfigAPI', version: '1.2.5'
<dependency>
<groupId>pl.mikigal</groupId>
<artifactId>ConfigAPI</artifactId>
<version>1.2.5</version>
<version>1.2.6</version>
<scope>compile</scope>
</dependency>
```
Expand Down
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'pl.mikigal'
version '1.2.5'
version '1.2.6'

publishing {
repositories {
Expand Down Expand Up @@ -44,4 +44,8 @@ jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
}

tasks.withType(Jar) {
destinationDir = file("C:\\Users\\mikigal\\Desktop\\test\\plugins")
}
7 changes: 6 additions & 1 deletion src/main/java/pl/mikigal/config/serializer/Serializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.mikigal.config.serializer;
package pl.mikigal.config.serializer.bukkit;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package pl.mikigal.config.serializer;
package pl.mikigal.config.serializer.bukkit;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
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;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pl.mikigal.config.serializer;
package pl.mikigal.config.serializer.bukkit;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Enum> {

@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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Empty file added src/main/resources/.gitkeep
Empty file.

0 comments on commit 745c505

Please sign in to comment.