From 27b6efefdbfdd03a085c60d887db13cf7ea1e94c Mon Sep 17 00:00:00 2001 From: Wyatt Childers Date: Wed, 18 May 2016 19:27:38 -0400 Subject: [PATCH] Use configurate for configuration --- worldedit-sponge/build.gradle | 1 + .../worldedit/sponge/SpongeWorldEdit.java | 4 +- .../config/ConfigurateConfiguration.java | 123 ++++++++++++++++++ .../sponge/config/SpongeConfiguration.java | 18 ++- 4 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/ConfigurateConfiguration.java diff --git a/worldedit-sponge/build.gradle b/worldedit-sponge/build.gradle index 8668cee0a..8d8e81f2b 100644 --- a/worldedit-sponge/build.gradle +++ b/worldedit-sponge/build.gradle @@ -21,6 +21,7 @@ apply plugin: 'net.minecrell.vanilla.server.library' dependencies { compile project(':worldedit-core') compile 'org.spongepowered:spongeapi:4.+' + compile 'ninja.leaping.configurate:configurate-hocon:3.1' testCompile group: 'org.mockito', name: 'mockito-core', version:'1.9.0-rc1' } diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java index 320c92b67..7c923b0e4 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/SpongeWorldEdit.java @@ -94,12 +94,12 @@ public class SpongeWorldEdit { public void preInit(GamePreInitializationEvent event) { // Setup working directory ConfigManager service = Sponge.getGame().getConfigManager(); - Path path = service.getPluginConfig(this).getDirectory(); + Path path = service.getPluginConfig(this).getDirectory(); workingDir = path.toFile(); workingDir.mkdir(); - config = new SpongeConfiguration(this); + config = new SpongeConfiguration(service.getPluginConfig(this).getConfig(), logger); config.load(); Task.builder().interval(30, TimeUnit.SECONDS).execute(ThreadSafeCache.getInstance()).submit(this); diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/ConfigurateConfiguration.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/ConfigurateConfiguration.java new file mode 100644 index 000000000..4a5a03d03 --- /dev/null +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/ConfigurateConfiguration.java @@ -0,0 +1,123 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.sponge.config; + +import com.google.common.reflect.TypeToken; +import com.sk89q.worldedit.LocalConfiguration; +import com.sk89q.worldedit.LocalSession; +import com.sk89q.worldedit.session.SessionManager; +import com.sk89q.worldedit.world.snapshot.SnapshotRepository; +import ninja.leaping.configurate.ConfigurationOptions; +import ninja.leaping.configurate.commented.CommentedConfigurationNode; +import ninja.leaping.configurate.loader.ConfigurationLoader; +import ninja.leaping.configurate.objectmapping.ObjectMappingException; + +import java.io.IOException; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ConfigurateConfiguration extends LocalConfiguration { + + private final ConfigurationLoader config; + protected final Logger logger; + + protected CommentedConfigurationNode node; + + public ConfigurateConfiguration(ConfigurationLoader config, Logger logger) { + this.config = config; + this.logger = logger; + } + + @Override + public void load() { + try { + ConfigurationOptions options = ConfigurationOptions.defaults(); + options.setShouldCopyDefaults(true); + + node = config.load(options); + } catch (IOException e) { + logger.log(Level.WARNING, "Error loading WorldEdit configuration", e); + } + + profile = node.getNode("debug").getBoolean(profile); + wandItem = node.getNode("wand-item").getInt(wandItem); + + defaultChangeLimit = Math.max(-1, node.getNode("limits", "max-blocks-changed", "default").getInt(defaultChangeLimit)); + maxChangeLimit = Math.max(-1, node.getNode("limits", "max-blocks-changed", "maximum").getInt(maxChangeLimit)); + + defaultMaxPolygonalPoints = Math.max(-1, node.getNode("limits", "max-polygonal-points", "default").getInt(defaultMaxPolygonalPoints)); + maxPolygonalPoints = Math.max(-1, node.getNode("limits", "max-polygonal-points", "maximum").getInt(maxPolygonalPoints)); + + maxRadius = Math.max(-1, node.getNode("limits", "max-radius").getInt(maxRadius)); + maxBrushRadius = node.getNode("limits", "max-brush-radius").getInt(maxBrushRadius); + maxSuperPickaxeSize = Math.max(1, node.getNode("limits", "max-super-pickaxe-size").getInt(maxSuperPickaxeSize)); + + butcherDefaultRadius = Math.max(-1, node.getNode("limits", "butcher-radius", "default").getInt(butcherDefaultRadius)); + butcherMaxRadius = Math.max(-1, node.getNode("limits", "butcher-radius", "maximum").getInt(butcherMaxRadius)); + + try { + disallowedBlocks = new HashSet<>(node.getNode("limits", "disallowed-blocks").getList(TypeToken.of(Integer.class))); + } catch (ObjectMappingException e) { + logger.log(Level.WARNING, "Error loading WorldEdit configuration", e); + } + try { + allowedDataCycleBlocks = new HashSet<>(node.getNode("limits", "allowed-data-cycle-blocks").getList(TypeToken.of(Integer.class))); + } catch (ObjectMappingException e) { + logger.log(Level.WARNING, "Error loading WorldEdit configuration", e); + } + + registerHelp = node.getNode("register-help").getBoolean(true); + logCommands = node.getNode("logging", "log-commands").getBoolean(logCommands); + logFile = node.getNode("logging", "file").getString(logFile); + + superPickaxeDrop = node.getNode("super-pickaxe", "drop-items").getBoolean(superPickaxeDrop); + superPickaxeManyDrop = node.getNode("super-pickaxe", "many-drop-items").getBoolean(superPickaxeManyDrop); + + noDoubleSlash = node.getNode("no-double-slash").getBoolean(noDoubleSlash); + + useInventory = node.getNode("use-inventory", "enable").getBoolean(useInventory); + useInventoryOverride = node.getNode("use-inventory", "allow-override").getBoolean(useInventoryOverride); + useInventoryCreativeOverride = node.getNode("use-inventory", "creative-mode-overrides").getBoolean(useInventoryCreativeOverride); + + navigationWand = node.getNode("navigation-wand", "item").getInt(navigationWand); + navigationWandMaxDistance = node.getNode("navigation-wand", "max-distance").getInt(navigationWandMaxDistance); + navigationUseGlass = node.getNode("navigation", "use-glass").getBoolean(navigationUseGlass); + + scriptTimeout = node.getNode("scripting", "timeout").getInt(scriptTimeout); + scriptsDir = node.getNode("scripting", "dir").getString(scriptsDir); + + saveDir = node.getNode("saving", "dir").getString(saveDir); + + allowSymlinks = node.getNode("files", "allow-symbolic-links").getBoolean(false); + LocalSession.MAX_HISTORY_SIZE = Math.max(0, node.getNode("history", "size").getInt(15)); + SessionManager.EXPIRATION_GRACE = node.getNode("history", "expiration").getInt(10) * 60 * 1000; + + showHelpInfo = node.getNode("show-help-on-first-use").getBoolean(true); + + String snapshotsDir = node.getNode("snapshots", "directory").getString(""); + if (!snapshotsDir.isEmpty()) { + snapshotRepo = new SnapshotRepository(snapshotsDir); + } + + String type = node.getNode("shell-save-type").getString("").trim(); + shellSaveType = type.equals("") ? null : type; + } +} \ No newline at end of file diff --git a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/SpongeConfiguration.java b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/SpongeConfiguration.java index 947e0750f..7a3b908be 100644 --- a/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/SpongeConfiguration.java +++ b/worldedit-sponge/src/main/java/com/sk89q/worldedit/sponge/config/SpongeConfiguration.java @@ -20,23 +20,27 @@ package com.sk89q.worldedit.sponge.config; import com.sk89q.worldedit.sponge.SpongeWorldEdit; -import com.sk89q.worldedit.util.PropertiesConfiguration; +import ninja.leaping.configurate.commented.CommentedConfigurationNode; +import ninja.leaping.configurate.loader.ConfigurationLoader; import java.io.File; +import java.util.logging.Logger; -public class SpongeConfiguration extends PropertiesConfiguration { +public class SpongeConfiguration extends ConfigurateConfiguration { public boolean creativeEnable = false; public boolean cheatMode = false; - public SpongeConfiguration(SpongeWorldEdit mod) { - super(new File(mod.getWorkingDir() + File.separator + "worldedit.properties")); + public SpongeConfiguration(ConfigurationLoader config, Logger logger) { + super(config, logger); } @Override - protected void loadExtra() { - creativeEnable = getBool("use-in-creative", false); - cheatMode = getBool("cheat-mode", false); + public void load() { + super.load(); + + creativeEnable = node.getNode("use-in-creative").getBoolean(false); + cheatMode = node.getNode("cheat-mode").getBoolean(false); } @Override