mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
Fix and implement file copying
This commit is contained in:
1 parent
c4d618f0cb
commit
77992f8391
5 files changed
+82
-19
No files matched your search
@@ -23,6 +23,8 @@ dependencies {
|
||||
api 'cloud.commandframework:cloud-core:1.1.0'
|
||||
|
||||
api 'org.spongepowered:configurate-yaml:4.0.0-SNAPSHOT'
|
||||
api 'org.spongepowered:configurate-hocon:4.0.0-SNAPSHOT'
|
||||
|
||||
api 'org.slf4j:slf4j-api:1.7.25'
|
||||
|
||||
api 'net.luckperms:api:5.1'
|
||||
|
||||
@@ -17,6 +17,7 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -32,6 +33,8 @@ public interface CarbonChat {
|
||||
|
||||
void reloadConfig();
|
||||
|
||||
@NonNull File dataDirectory();
|
||||
|
||||
@NonNull UUID resolveUUID(@NonNull String name);
|
||||
|
||||
@NonNull String resolveName(@NonNull UUID uuid);
|
||||
|
||||
@@ -7,6 +7,7 @@ description = 'CarbonChat-Bukkit'
|
||||
|
||||
dependencies {
|
||||
compile 'org.spongepowered:configurate-yaml:4.0.0-SNAPSHOT'
|
||||
compile 'org.spongepowered:configurate-hocon:4.0.0-SNAPSHOT'
|
||||
compile 'net.kyori:adventure-text-serializer-bungeecord:4.0.0-SNAPSHOT'
|
||||
compile 'net.kyori:adventure-platform-bukkit:4.0.0-SNAPSHOT'
|
||||
|
||||
|
||||
@@ -191,6 +191,8 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
if (this.carbonSettings().showTips() && !FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
|
||||
this.logger().error("Item linking disabled! Please use Paper 1.16.2 #172 or newer.");
|
||||
}
|
||||
|
||||
this.logger().info("CarbonChat startup finished! Enjoy :)");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -250,28 +252,23 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
this.getDataFolder().mkdirs();
|
||||
|
||||
try {
|
||||
final ConfigLoader<YamlConfigurationLoader> loader = new ConfigLoader<>();
|
||||
|
||||
this.carbonSettings = CarbonSettings.loadFrom(loader.loadAndSaveNode(YamlConfigurationLoader.builder(),
|
||||
new File(this.getDataFolder(), "config.yml"), true));
|
||||
|
||||
this.commandSettings = CommandSettingsRegistry.loadFrom(loader.loadAndSaveNode(YamlConfigurationLoader.builder(),
|
||||
new File(this.getDataFolder(), "commands.yml"), true));
|
||||
|
||||
this.channelSettings = ChannelSettings.loadFrom(loader.loadAndSaveNode(YamlConfigurationLoader.builder(),
|
||||
new File(this.getDataFolder(), "channels.yml"), true));
|
||||
|
||||
this.moderationSettings = ModerationSettings.loadFrom(loader.loadAndSaveNode(YamlConfigurationLoader.builder(),
|
||||
new File(this.getDataFolder(), "moderation.yml"), true));
|
||||
|
||||
this.translations = CarbonTranslations.loadFrom(loader.loadAndSaveNode(YamlConfigurationLoader.builder(),
|
||||
new File(this.getDataFolder(), "translations.yml"), true));
|
||||
final ConfigLoader<YamlConfigurationLoader> loader = new ConfigLoader<>(YamlConfigurationLoader.class);
|
||||
|
||||
this.carbonSettings = CarbonSettings.loadFrom(loader.loadAndSaveNode("config.yml", true));
|
||||
this.commandSettings = CommandSettingsRegistry.loadFrom(loader.loadAndSaveNode("commands.yml", true));
|
||||
this.channelSettings = ChannelSettings.loadFrom(loader.loadAndSaveNode("channels.yml", true));
|
||||
this.translations = CarbonTranslations.loadFrom(loader.loadAndSaveNode("language.yml", true));
|
||||
this.moderationSettings = ModerationSettings.loadFrom(loader.loadAndSaveNode("moderation.yml", true));
|
||||
} catch(final ConfigurateException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public File dataDirectory() {
|
||||
return this.getDataFolder();
|
||||
}
|
||||
|
||||
private void registerContexts() {
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("Towny")) {
|
||||
this.getServer().getPluginManager().registerEvents(new TownyContext(this), this);
|
||||
|
||||
@@ -1,19 +1,38 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.sound.Sound;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
public class ConfigLoader<L extends AbstractConfigurationLoader<CommentedConfigurationNode>> {
|
||||
|
||||
public <B extends AbstractConfigurationLoader.Builder<?, L>> CommentedConfigurationNode loadAndSaveNode(final @NonNull B builder,
|
||||
final @NonNull File config,
|
||||
final boolean save) throws ConfigurateException {
|
||||
private final @NonNull Class<L> format;
|
||||
|
||||
public ConfigLoader(final @NonNull Class<L> format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public <B extends AbstractConfigurationLoader.Builder<?, L>> CommentedConfigurationNode loadAndSaveNode(final @NonNull File config, final boolean save) throws ConfigurateException {
|
||||
final B builder;
|
||||
|
||||
if (this.format == HoconConfigurationLoader.class) {
|
||||
builder = (B) HoconConfigurationLoader.builder();
|
||||
} else {
|
||||
builder = (B) YamlConfigurationLoader.builder();
|
||||
}
|
||||
|
||||
final L loader = this.loadConfigFile(builder, config, save);
|
||||
final CommentedConfigurationNode node = loader.load();
|
||||
|
||||
@@ -22,6 +41,47 @@ public class ConfigLoader<L extends AbstractConfigurationLoader<CommentedConfigu
|
||||
return node;
|
||||
}
|
||||
|
||||
public @NonNull CommentedConfigurationNode loadAndSaveNode(final @NonNull String config, final boolean save) throws ConfigurateException {
|
||||
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);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public boolean copyFileToDataDir(final @NonNull InputStream source) {
|
||||
try {
|
||||
Files.copy(source, CarbonChatProvider.carbonChat().dataFolder(), StandardCopyOption.REPLACE_EXISTING);
|
||||
} 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
|
||||
|
||||
|
||||
Reference in new issue
Block a user