mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
Redo config handling
This commit is contained in:
1 parent
e95a17c025
commit
224c7723d6
2 files changed
+36
-48
No files matched your search
@@ -254,11 +254,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
try {
|
try {
|
||||||
final ConfigLoader<YamlConfigurationLoader> loader = new ConfigLoader<>(YamlConfigurationLoader.class);
|
final ConfigLoader<YamlConfigurationLoader> loader = new ConfigLoader<>(YamlConfigurationLoader.class);
|
||||||
|
|
||||||
this.carbonSettings = CarbonSettings.loadFrom(loader.loadAndSaveNode("config.yml", true));
|
this.carbonSettings = CarbonSettings.loadFrom(loader.loadConfig("config.yml"));
|
||||||
this.commandSettings = CommandSettingsRegistry.loadFrom(loader.loadAndSaveNode("commands.yml", true));
|
this.commandSettings = CommandSettingsRegistry.loadFrom(loader.loadConfig("commands.yml"));
|
||||||
this.channelSettings = ChannelSettings.loadFrom(loader.loadAndSaveNode("channels.yml", true));
|
this.channelSettings = ChannelSettings.loadFrom(loader.loadConfig("channels.yml"));
|
||||||
this.translations = CarbonTranslations.loadFrom(loader.loadAndSaveNode("language.yml", true));
|
this.translations = CarbonTranslations.loadFrom(loader.loadConfig("language.yml"));
|
||||||
this.moderationSettings = ModerationSettings.loadFrom(loader.loadAndSaveNode("moderation.yml", true));
|
this.moderationSettings = ModerationSettings.loadFrom(loader.loadConfig("moderation.yml"));
|
||||||
} catch(final ConfigurateException ignored) {
|
} catch(final ConfigurateException ignored) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-43
@@ -24,7 +24,7 @@ public class ConfigLoader<L extends AbstractConfigurationLoader<CommentedConfigu
|
|||||||
this.format = format;
|
this.format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <B extends AbstractConfigurationLoader.Builder<?, L>> CommentedConfigurationNode loadAndSaveNode(final @NonNull File config, final boolean save) throws ConfigurateException {
|
public <B extends AbstractConfigurationLoader.Builder<?, L>> CommentedConfigurationNode loadConfig(final @NonNull String file) throws ConfigurateException {
|
||||||
final B builder;
|
final B builder;
|
||||||
|
|
||||||
if (this.format == HoconConfigurationLoader.class) {
|
if (this.format == HoconConfigurationLoader.class) {
|
||||||
@@ -33,58 +33,22 @@ public class ConfigLoader<L extends AbstractConfigurationLoader<CommentedConfigu
|
|||||||
builder = (B) YamlConfigurationLoader.builder().nodeStyle(NodeStyle.BLOCK);
|
builder = (B) YamlConfigurationLoader.builder().nodeStyle(NodeStyle.BLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
final L loader = this.loadConfigFile(builder, config, save);
|
final File config = this.findAndCopyFile(this.fileInDataDir(file));
|
||||||
|
final L loader = this.loadConfigFile(builder, config);
|
||||||
final CommentedConfigurationNode node = loader.load();
|
final CommentedConfigurationNode node = loader.load();
|
||||||
|
|
||||||
|
if (!config.exists()) {
|
||||||
loader.save(node);
|
loader.save(node);
|
||||||
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public @NonNull CommentedConfigurationNode loadAndSaveNode(final @NonNull String config, final boolean save) throws ConfigurateException {
|
private @NonNull File fileInDataDir(final @NonNull String file) {
|
||||||
final File dataDirFile = this.fileInDataDir(config);
|
|
||||||
|
|
||||||
if (dataDirFile.exists()) {
|
|
||||||
return this.loadAndSaveNode(dataDirFile, save);
|
|
||||||
}
|
|
||||||
|
|
||||||
final String file;
|
|
||||||
|
|
||||||
if (config.startsWith("/")) {
|
|
||||||
file = config;
|
|
||||||
} else {
|
|
||||||
file = "/" + config;
|
|
||||||
}
|
|
||||||
|
|
||||||
final InputStream inputStream = this.getClass().getResourceAsStream(file);
|
|
||||||
|
|
||||||
if (inputStream == null) {
|
|
||||||
throw new IllegalStateException("File not found! [" + file + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.copyFileToDataDir(inputStream, file);
|
|
||||||
|
|
||||||
return this.loadAndSaveNode(dataDirFile, save);
|
|
||||||
}
|
|
||||||
|
|
||||||
public @NonNull File fileInDataDir(final @NonNull String file) {
|
|
||||||
CarbonChatProvider.carbonChat().logger().info("Loading file " + file);
|
|
||||||
return new File(CarbonChatProvider.carbonChat().dataFolder().toFile(), file);
|
return new File(CarbonChatProvider.carbonChat().dataFolder().toFile(), file);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean copyFileToDataDir(final @NonNull InputStream source, final @NonNull String file) {
|
private <B extends AbstractConfigurationLoader.Builder<?, L>> L loadConfigFile(final B builder, final @NonNull File config) {
|
||||||
try {
|
|
||||||
Files.copy(source, new File(CarbonChatProvider.carbonChat().dataFolder().toFile(), file).toPath());
|
|
||||||
} catch (final IOException ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private <B extends AbstractConfigurationLoader.Builder<?, L>> L loadConfigFile(final B builder, final @NonNull File config, final boolean save) {
|
|
||||||
// TODO: save file from jar if it doesn't exist
|
|
||||||
|
|
||||||
return builder
|
return builder
|
||||||
.defaultOptions(opts -> {
|
.defaultOptions(opts -> {
|
||||||
return opts.shouldCopyDefaults(true).serializers(serializerBuilder -> {
|
return opts.shouldCopyDefaults(true).serializers(serializerBuilder -> {
|
||||||
@@ -96,4 +60,28 @@ public class ConfigLoader<L extends AbstractConfigurationLoader<CommentedConfigu
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private @NonNull File findAndCopyFile(final @NonNull File file) {
|
||||||
|
if (file.exists()) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String resource = "/" + file.getName();
|
||||||
|
final InputStream inputStream = this.getClass().getResourceAsStream(resource);
|
||||||
|
|
||||||
|
if (inputStream == null) {
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.copyFileToDataDir(inputStream, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public @NonNull File copyFileToDataDir(final @NonNull InputStream source, final @NonNull File file) {
|
||||||
|
try {
|
||||||
|
Files.copy(source, file.toPath());
|
||||||
|
} catch (final IOException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user