Add Configuration files
This commit is contained in:
parent
765001257c
commit
366860ac55
|
|
@ -0,0 +1,4 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
public interface Configuration {
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.yaml.NodeStyle;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class ConfigurationContainer<C extends Configuration> {
|
||||
private final AtomicReference<C> config;
|
||||
private final YamlConfigurationLoader loader;
|
||||
private final CommentedConfigurationNode node;
|
||||
private final Class<C> clazz;
|
||||
private final Logger logger;
|
||||
|
||||
private ConfigurationContainer(final C config, final Class<C> clazz, final YamlConfigurationLoader loader, final CommentedConfigurationNode node, final Logger logger) {
|
||||
this.config = new AtomicReference<>(config);
|
||||
this.loader = loader;
|
||||
this.node = node;
|
||||
this.clazz = clazz;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> reload() {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
final CommentedConfigurationNode node = loader.load();
|
||||
C newConfig = node.get(clazz);
|
||||
node.set(clazz, newConfig);
|
||||
loader.save(node);
|
||||
config.set(newConfig);
|
||||
return true;
|
||||
} catch (ConfigurateException exception) {
|
||||
logger.error("Could not reload {} configuration file", clazz.getSimpleName(), exception);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Boolean> save() {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
node.set(clazz, get());
|
||||
loader.save(node);
|
||||
return true;
|
||||
} catch (ConfigurateException exception) {
|
||||
logger.error("Could not reload {} configuration file", clazz.getSimpleName(), exception);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public @NotNull C get() {
|
||||
return this.config.get();
|
||||
}
|
||||
|
||||
public static <C extends Configuration> ConfigurationContainer<C> load(final Logger logger, final Path path, final Class<C> clazz, final String file) {
|
||||
final YamlConfigurationLoader loader = YamlConfigurationLoader.builder()
|
||||
.defaultOptions(options -> options
|
||||
.shouldCopyDefaults(true)
|
||||
.header("")
|
||||
)
|
||||
.path(path.resolve(file + ".yml"))
|
||||
.nodeStyle(NodeStyle.BLOCK)
|
||||
.build();
|
||||
try {
|
||||
final CommentedConfigurationNode node = loader.load();
|
||||
final C config = node.get(clazz);
|
||||
node.set(clazz, config);
|
||||
loader.save(node);
|
||||
return new ConfigurationContainer<>(config, clazz, loader, node, logger);
|
||||
} catch (ConfigurateException exception){
|
||||
logger.error("Could not load {} configuration file", clazz.getSimpleName(), exception);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
|
||||
@ConfigSerializable
|
||||
@Getter
|
||||
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
|
||||
public class DatabaseConfiguration implements Configuration {
|
||||
|
||||
private String ip = "0.0.0.0";
|
||||
private String port = "3306";
|
||||
private String database = "CometSkyBlock";
|
||||
private String user = "root";
|
||||
private String password = "toor";
|
||||
private String maximumConnections = "10";
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
|
||||
@ConfigSerializable
|
||||
@Getter
|
||||
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
|
||||
public class IslandConfiguration implements Configuration {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
|
||||
@ConfigSerializable
|
||||
@Getter
|
||||
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
|
||||
public class MessageConfiguration implements Configuration {
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.alttd.cometskyblock.configuration;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
|
||||
@ConfigSerializable
|
||||
@Getter
|
||||
@SuppressWarnings({"CanBeFinal", "FieldMayBeFinal"})
|
||||
public class PluginConfiguration implements Configuration {
|
||||
|
||||
@Comment("Generate debug messages")
|
||||
private boolean debug = true;
|
||||
|
||||
private MasterWorldGenerator masterWorldGenerator = new MasterWorldGenerator();
|
||||
@ConfigSerializable @Getter
|
||||
public static class MasterWorldGenerator {
|
||||
|
||||
private int centerX = 0;
|
||||
private int centerZ = 0;
|
||||
private int size = 100;
|
||||
|
||||
private int spawnX = 0;
|
||||
private int spawnY = 63;
|
||||
private int spawnz = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user