-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>me.hsgamer</groupId> | ||
<artifactId>hscore-bukkit</artifactId> | ||
<version>4.4.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>hscore-bukkit-config-converter</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>me.hsgamer</groupId> | ||
<artifactId>hscore-config-annotation</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
49 changes: 49 additions & 0 deletions
49
...ig-converter/src/main/java/me/hsgamer/hscore/bukkit/config/converter/BukkitConverter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package me.hsgamer.hscore.bukkit.config.converter; | ||
|
||
import me.hsgamer.hscore.config.annotation.converter.Converter; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.serialization.ConfigurationSerializable; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
|
||
/** | ||
* The {@link Converter} for Bukkit's {@link ConfigurationSerializable} | ||
*/ | ||
public class BukkitConverter implements Converter { | ||
private final Method deserializeMethod; | ||
|
||
public BukkitConverter(Class<?> type) { | ||
if (!ConfigurationSerializable.class.isAssignableFrom(type)) { | ||
throw new IllegalArgumentException("The class must implement ConfigurationSerializable"); | ||
} | ||
|
||
try { | ||
deserializeMethod = type.getMethod("deserialize", Map.class); | ||
} catch (NoSuchMethodException e) { | ||
throw new IllegalArgumentException("Cannot find deserialize method", e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object convert(Object raw) { | ||
if (raw instanceof Map) { | ||
Map<?, ?> map = (Map<?, ?>) raw; | ||
try { | ||
return deserializeMethod.invoke(null, map); | ||
} catch (Exception e) { | ||
Bukkit.getLogger().log(Level.WARNING, "Error occurred while deserializing " + raw, e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object convertToRaw(Object value) { | ||
if (value instanceof ConfigurationSerializable) { | ||
return ((ConfigurationSerializable) value).serialize(); | ||
} | ||
return null; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...rter/src/main/java/me/hsgamer/hscore/bukkit/config/converter/BukkitConverterProvider.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package me.hsgamer.hscore.bukkit.config.converter; | ||
|
||
import me.hsgamer.hscore.config.annotation.converter.Converter; | ||
import me.hsgamer.hscore.config.annotation.converter.ConverterProvider; | ||
import me.hsgamer.hscore.config.annotation.converter.manager.DefaultConverterManager; | ||
import org.bukkit.configuration.serialization.ConfigurationSerializable; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* The {@link ConverterProvider} for Bukkit | ||
*/ | ||
public class BukkitConverterProvider implements ConverterProvider { | ||
/** | ||
* Register the provider | ||
*/ | ||
public static void register() { | ||
DefaultConverterManager.registerProvider(new BukkitConverterProvider()); | ||
} | ||
|
||
@Override | ||
public Optional<Converter> getConverter(Class<?> type) { | ||
return ConfigurationSerializable.class.isAssignableFrom(type) ? Optional.of(new BukkitConverter(type)) : Optional.empty(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...onfig-converter/src/main/java/me/hsgamer/hscore/bukkit/config/converter/package-info.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Contains the implementations of {@link me.hsgamer.hscore.config.annotation.converter.Converter} and {@link me.hsgamer.hscore.config.annotation.converter.ConverterProvider} for the Bukkit config | ||
*/ | ||
package me.hsgamer.hscore.bukkit.config.converter; |
This file contains 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