Skip to content

Commit

Permalink
add bukkit-config-converter
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 6, 2024
1 parent 3be7370 commit 1556798
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
21 changes: 21 additions & 0 deletions hscore-bukkit/hscore-bukkit-config-converter/pom.xml
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>
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;
}
}
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();
}
}
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;
1 change: 1 addition & 0 deletions hscore-bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>hscore-bukkit-utils</module>
<module>hscore-bukkit-command-sub</module>
<module>hscore-bukkit-config</module>
<module>hscore-bukkit-config-converter</module>
<module>hscore-bukkit-click-type</module>
<module>hscore-bukkit-skull</module>
<module>hscore-bukkit-item</module>
Expand Down

0 comments on commit 1556798

Please sign in to comment.