Skip to content

Instantly share code, notes, and snippets.

@DRSchlaubi
Last active August 20, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRSchlaubi/7750461d2dfbcccda49c15a5d75d45a4 to your computer and use it in GitHub Desktop.
Save DRSchlaubi/7750461d2dfbcccda49c15a5d75d45a4 to your computer and use it in GitHub Desktop.
Bukit config serializer
package org.wlosp.varo;
import com.google.common.base.Preconditions;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* Serializer using {@link org.bukkit.configuration.file.YamlConfiguration}.
*
*/
public class ConfigurationSerializer {
private static final String OBJECT_NAME = "o";
/**
* Deserializes a file by it's name
* @param fileName the name or path of the file
* @param clazz the {@link Class} of the files type
* @param <T> the files type
* @return the instance of the file
* @throws IOException when an IOError occurs during loading
*
* @see #deserialize(Path, Class)
*/
public static <T extends ConfigurationSerializable> T deserialize(String fileName, Class<T> clazz) throws IOException {
return deserialize(Paths.get(fileName), clazz);
}
/**
* Serializes a {@link ConfigurationSerializable} to a {@link Path}
* @param fileName the name or path of the file
* @param object the {@link ConfigurationSerializable} instance to serialize
* @throws IOException when an IOError occurs during loading
*/
public static void serialize(String fileName, ConfigurationSerializable object) throws IOException {
serialize(Paths.get(fileName), object);
}
/**
* Deserializes a file located at a {@link Path}.
* @param path the {@link Path} where the file is located
* @param clazz the {@link Class} of the files type
* @param <T> the files type
* @return the instance of the file
* @throws IOException when an IOError occurs during loading
* @throws IllegalArgumentException if the file is not a file or does not exist
*/
public static <T extends ConfigurationSerializable> T deserialize(Path path, Class<T> clazz) throws IOException {
Preconditions.checkArgument(Files.isRegularFile(path), "The file needs to exist and be a file");
String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
YamlConfiguration configuration = new YamlConfiguration();
try {
configuration.loadFromString(content);
} catch (InvalidConfigurationException ignored) {
// Cannot happen since these files are written by normal config
}
return configuration.getSerializable(OBJECT_NAME, clazz);
}
/**
* Serializes a {@link ConfigurationSerializable} to a {@link Path}
* @param path the {@link Path} where the serialized file should be located
* @param object the {@link ConfigurationSerializable} instance to serialize
* @throws IOException when an IOError occurs during loading
*/
public static void serialize(Path path, ConfigurationSerializable object) throws IOException {
YamlConfiguration configuration = new YamlConfiguration();
configuration.set(OBJECT_NAME, object);
String content = configuration.saveToString();
try(FileChannel channel = FileChannel.open(path, StandardOpenOption.CREATE)) {
channel.write(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment