Skip to content

Commit

Permalink
Added fieldValue parameter to the CustomFieldLoader deserialize Metho…
Browse files Browse the repository at this point in the history
…d and a default ListAdapter
  • Loading branch information
ignPurple committed May 24, 2022
1 parent a701f85 commit 1da64e3
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 57 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.ignPurple</groupId>
<artifactId>ignLib</artifactId>
<version>v1.1.0</version>
<version>v1.3.0</version>

<repositories>
<repository>
Expand Down Expand Up @@ -35,6 +35,11 @@
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.ignpurple.ignlib.configuration;

import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.annotation.ConfigurationField;
import me.ignpurple.ignlib.configuration.loader.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.manager.ConfigurationManager;
import me.ignpurple.ignlib.configuration.type.ConfigLoader;
import me.ignpurple.ignlib.enums.ConfigType;
Expand Down Expand Up @@ -49,6 +49,15 @@ public Path getDataFolder() {
return this.dataFolder;
}

/**
* Gets the configuration manager
*
* @return The configuration manager instance
*/
public ConfigurationManager getConfigurationManager() {
return this.configurationManager;
}

/**
* Load all the data from the configuration file with
* class fields which have the {@link ConfigurationField} annotation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package me.ignpurple.ignlib.configuration.loader;
package me.ignpurple.ignlib.configuration.adapter;

import me.ignpurple.ignlib.configuration.manager.ConfigurationManager;

public interface CustomFieldLoader {

Expand All @@ -8,14 +10,14 @@ public interface CustomFieldLoader {
* @param object The object stored in memory
* @return The new converted object to save to the configuration
*/
Object save(Object object);
Object serialize(ConfigurationManager configurationManager, Object object);

/**
* When the object is loaded from the configuration file
*
* @param object The object in the configuration
* @return The new converted object to load into memory
*/
Object load(Object object);
Object deserialize(ConfigurationManager configurationManager, Object fieldValue, Object object);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.ignpurple.ignlib.configuration.adapter;

import me.ignpurple.ignlib.configuration.manager.ConfigurationManager;

public class DefaultFieldLoader implements CustomFieldLoader {

@Override
public Object deserialize(ConfigurationManager configurationManager, Object fieldValue, Object object) {
return object;
}

@Override
public Object serialize(ConfigurationManager configurationManager, Object object) {
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.ignpurple.ignlib.configuration.adapter.defaults;

import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.manager.ConfigurationManager;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;

public class ListAdapter implements CustomFieldLoader {

@Override
public Object serialize(ConfigurationManager configurationManager, Object object) {
final List<Object> objects = (List<Object>) object;
final List<Object> serializedObjects = new ArrayList<>();
for (final Object obj : objects) {
serializedObjects.add(configurationManager.getLoader(obj.getClass()).serialize(configurationManager, obj));
}
return serializedObjects;
}

@Override
public Object deserialize(ConfigurationManager configurationManager, Object fieldValue, Object object) {
final List<Object> fieldList = (List<Object>) fieldValue;
final ParameterizedType parameterizedType = (ParameterizedType) fieldList;
final List<Object> objects = (List<Object>) object;
final List<Object> deserializedObjects = new ArrayList<>();
for (final Object obj : objects) {
deserializedObjects.add(configurationManager.getLoader((Class<?>) parameterizedType.getActualTypeArguments()[0]).deserialize(configurationManager, null, obj));
}
return deserializedObjects;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.ignpurple.ignlib.configuration.adapter.defaults;

import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.manager.ConfigurationManager;
import org.bukkit.Bukkit;
import org.bukkit.World;

public class WorldAdapter implements CustomFieldLoader {

@Override
public Object serialize(ConfigurationManager configurationManager, Object object) {
final World world = (World) object;
return world.getName();
}

@Override
public Object deserialize(ConfigurationManager configurationManager, Object fieldValue, Object object) {
final String worldName = (String) object;
try {
final World world = Bukkit.getWorld(worldName);
if (world == null) {
throw new IllegalStateException("Invalid World Specified! Consider adding Multiverse-Core as a soft-depend if you have it installed!");
}

return Bukkit.getWorld(worldName);
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public @interface ConfigurationField {

/**
* @return The field in the configuration where the data will be stored in
* @return The field in the configuration where the data will be stored
*/
String path();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package me.ignpurple.ignlib.configuration.manager;

import me.ignpurple.ignlib.configuration.Configuration;
import me.ignpurple.ignlib.configuration.loader.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.loader.DefaultFieldLoader;
import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.adapter.DefaultFieldLoader;
import me.ignpurple.ignlib.configuration.adapter.defaults.ListAdapter;
import me.ignpurple.ignlib.configuration.adapter.defaults.WorldAdapter;
import me.ignpurple.ignlib.configuration.type.ConfigField;
import org.bukkit.World;

import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

public class ConfigurationManager {
private final Map<Class<? extends Configuration>, Configuration> configurations;
private final Map<ConfigField<?>, CustomFieldLoader> typeAdapters;

private static final DefaultFieldLoader DEFAULT_FIELD_LOADER = new DefaultFieldLoader();
public static final DefaultFieldLoader DEFAULT_FIELD_LOADER = new DefaultFieldLoader();

public ConfigurationManager() {
this.configurations = new IdentityHashMap<>();
this.typeAdapters = new HashMap<>();

this.registerTypeAdapater(List.class, new ListAdapter());
this.registerTypeAdapater(World.class, new WorldAdapter());
}

/**
Expand Down Expand Up @@ -55,7 +62,7 @@ public void loadAll() {
* @return The loader to use for the object
*/
public CustomFieldLoader getLoader(Class<?> type) {
return this.typeAdapters.get(ConfigField.create(type));
return this.typeAdapters.getOrDefault(ConfigField.create(type), DEFAULT_FIELD_LOADER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.ignpurple.ignlib.configuration.type;

import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.annotation.ConfigurationField;
import me.ignpurple.ignlib.configuration.loader.CustomFieldLoader;

public interface ConfigLoader {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.ignpurple.ignlib.configuration.type.impl;

import me.ignpurple.ignlib.configuration.Configuration;
import me.ignpurple.ignlib.configuration.adapter.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.annotation.ConfigurationField;
import me.ignpurple.ignlib.configuration.loader.CustomFieldLoader;
import me.ignpurple.ignlib.configuration.type.ConfigLoader;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -19,9 +19,9 @@ public class YAMLConfig implements ConfigLoader {
public YAMLConfig(Configuration configuration) {
this.configuration = configuration;

final File file = configuration.getDataFolder().toFile();
if (!file.exists()) {
file.mkdirs();
this.file = configuration.getDataFolder().toFile();
if (!this.file.exists()) {
this.file.mkdirs();
}

this.load();
Expand Down Expand Up @@ -71,10 +71,10 @@ public Object getOrCreate(CustomFieldLoader customFieldLoader, ConfigurationFiel
final String path = configurationField.path();
if (this.yamlConfig.contains(configurationField.path())) {
final Object configValue = this.yamlConfig.get(path);
return customFieldLoader == null ? configValue : customFieldLoader.load(configValue);
return customFieldLoader == null ? configValue : customFieldLoader.deserialize(this.configuration.getConfigurationManager(), fieldValue, configValue);
}

final Object loadedObject = customFieldLoader == null ? fieldValue : customFieldLoader.save(fieldValue);
final Object loadedObject = customFieldLoader == null ? fieldValue : customFieldLoader.serialize(this.configuration.getConfigurationManager(), fieldValue);
this.yamlConfig.set(path, loadedObject);
return fieldValue;
}
Expand All @@ -89,7 +89,7 @@ public Object getOrCreate(CustomFieldLoader customFieldLoader, ConfigurationFiel
@Override
public void set(CustomFieldLoader customFieldLoader, ConfigurationField configurationField, Object fieldValue) {
final String path = configurationField.path();
final Object loadedObject = customFieldLoader == null ? fieldValue : customFieldLoader.save(fieldValue);
final Object loadedObject = customFieldLoader == null ? fieldValue : customFieldLoader.serialize(this.configuration.getConfigurationManager(), fieldValue);
this.yamlConfig.set(path, loadedObject);
}
}
Loading

0 comments on commit 1da64e3

Please sign in to comment.