Attempts to properly comment migrated config values
Note that for some reason, YamlConfiguration cannot load the output created by StargateYamlConfiguration causing a major crash
This commit is contained in:
		@@ -25,6 +25,7 @@ import net.knarcraft.stargate.thread.BlockChangeThread;
 | 
			
		||||
import net.knarcraft.stargate.thread.ChunkUnloadThread;
 | 
			
		||||
import net.knarcraft.stargate.thread.StarGateThread;
 | 
			
		||||
import net.knarcraft.stargate.utility.BStatsHelper;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.Server;
 | 
			
		||||
import org.bukkit.command.PluginCommand;
 | 
			
		||||
import org.bukkit.configuration.InvalidConfigurationException;
 | 
			
		||||
@@ -73,7 +74,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
@SuppressWarnings("unused")
 | 
			
		||||
public class Stargate extends JavaPlugin {
 | 
			
		||||
 | 
			
		||||
    private static File configFile;
 | 
			
		||||
    private static final String configFileName = "config.yml";
 | 
			
		||||
    private static final Queue<BlockChangeRequest> blockChangeRequestQueue = new LinkedList<>();
 | 
			
		||||
    private static final Queue<ChunkUnloadRequest> chunkUnloadQueue = new PriorityQueue<>();
 | 
			
		||||
 | 
			
		||||
@@ -247,7 +248,14 @@ public class Stargate extends JavaPlugin {
 | 
			
		||||
     * @param message  <p>The message to log</p>
 | 
			
		||||
     */
 | 
			
		||||
    private static void log(Level severity, String message) {
 | 
			
		||||
        logger.log(severity, getBackupString("prefix") + message);
 | 
			
		||||
        if (logger == null) {
 | 
			
		||||
            logger = Bukkit.getLogger();
 | 
			
		||||
        }
 | 
			
		||||
        if (getInstance() == null) {
 | 
			
		||||
            logger.log(severity, "[Stargate]: " + message);
 | 
			
		||||
        } else {
 | 
			
		||||
            logger.log(severity, getBackupString("prefix") + message);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -349,9 +357,9 @@ public class Stargate extends JavaPlugin {
 | 
			
		||||
        super.reloadConfig();
 | 
			
		||||
        this.configuration = new StargateYamlConfiguration();
 | 
			
		||||
        try {
 | 
			
		||||
            configuration.load(configFile);
 | 
			
		||||
            configuration.load(new File(getDataFolder(), configFileName));
 | 
			
		||||
        } catch (IOException | InvalidConfigurationException e) {
 | 
			
		||||
            Stargate.logSevere(e.getMessage());
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -359,9 +367,9 @@ public class Stargate extends JavaPlugin {
 | 
			
		||||
    public void saveConfig() {
 | 
			
		||||
        super.saveConfig();
 | 
			
		||||
        try {
 | 
			
		||||
            configuration.save(configFile);
 | 
			
		||||
            configuration.save(new File(getDataFolder(), configFileName));
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            logSevere(e.getMessage());
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -375,16 +383,16 @@ public class Stargate extends JavaPlugin {
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onEnable() {
 | 
			
		||||
        configFile = new File(this.getDataFolder(), "config.yml");
 | 
			
		||||
        this.saveDefaultConfig();
 | 
			
		||||
        this.getConfig();
 | 
			
		||||
        PluginDescriptionFile pluginDescriptionFile = this.getDescription();
 | 
			
		||||
        pluginManager = getServer().getPluginManager();
 | 
			
		||||
        configuration = new StargateYamlConfiguration();
 | 
			
		||||
        try {
 | 
			
		||||
            configuration.load(configFile);
 | 
			
		||||
            configuration.load(new File(getDataFolder(), configFileName));
 | 
			
		||||
        } catch (IOException | InvalidConfigurationException e) {
 | 
			
		||||
            Stargate.logSevere(e.getMessage());
 | 
			
		||||
            getLogger().log(Level.SEVERE, e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
        this.saveDefaultConfig();
 | 
			
		||||
        configuration.options().copyDefaults(true);
 | 
			
		||||
 | 
			
		||||
        logger = Logger.getLogger("Minecraft");
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.World;
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.configuration.file.FileConfiguration;
 | 
			
		||||
import org.bukkit.configuration.file.YamlConfiguration;
 | 
			
		||||
import org.bukkit.plugin.messaging.Messenger;
 | 
			
		||||
import org.dynmap.DynmapAPI;
 | 
			
		||||
 | 
			
		||||
@@ -434,18 +435,25 @@ public final class StargateConfig {
 | 
			
		||||
    /**
 | 
			
		||||
     * Changes all configuration values from the old name to the new name
 | 
			
		||||
     *
 | 
			
		||||
     * @param newConfig <p>The config to read from and write to</p>
 | 
			
		||||
     * @param currentConfiguration <p>The current config to back up</p>
 | 
			
		||||
     */
 | 
			
		||||
    private void migrateConfig(FileConfiguration newConfig) {
 | 
			
		||||
    private void migrateConfig(FileConfiguration currentConfiguration) {
 | 
			
		||||
        //Save the old config just in case something goes wrong
 | 
			
		||||
        try {
 | 
			
		||||
            newConfig.save(dataFolderPath + "/config.yml.old");
 | 
			
		||||
            currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            Stargate.debug("Stargate::migrateConfig", "Unable to save old backup and do migration");
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Load old and new configuration
 | 
			
		||||
        Stargate.getInstance().reloadConfig();
 | 
			
		||||
        FileConfiguration oldConfiguration = Stargate.getInstance().getConfig();
 | 
			
		||||
        YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
 | 
			
		||||
                FileHelper.getBufferedReaderFromInputStream(
 | 
			
		||||
                        FileHelper.getInputStreamForInternalFile("/config.yml")));
 | 
			
		||||
 | 
			
		||||
        //Read all available config migrations
 | 
			
		||||
        Map<String, String> migrationFields;
 | 
			
		||||
        try {
 | 
			
		||||
@@ -460,15 +468,30 @@ public final class StargateConfig {
 | 
			
		||||
 | 
			
		||||
        //Replace old config names with the new ones
 | 
			
		||||
        for (String key : migrationFields.keySet()) {
 | 
			
		||||
            if (newConfig.contains(key)) {
 | 
			
		||||
            if (oldConfiguration.contains(key)) {
 | 
			
		||||
                String newPath = migrationFields.get(key);
 | 
			
		||||
                Object oldValue = newConfig.get(key);
 | 
			
		||||
                Object oldValue = oldConfiguration.get(key);
 | 
			
		||||
                if (!newPath.trim().isEmpty()) {
 | 
			
		||||
                    newConfig.set(newPath, oldValue);
 | 
			
		||||
                    oldConfiguration.set(newPath, oldValue);
 | 
			
		||||
                }
 | 
			
		||||
                newConfig.set(key, null);
 | 
			
		||||
                oldConfiguration.set(key, null);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Copy all keys to the new config
 | 
			
		||||
        for (String key : oldConfiguration.getKeys(true)) {
 | 
			
		||||
            Stargate.logInfo("Setting " + key + " to " + oldConfiguration.get(key));
 | 
			
		||||
            newConfiguration.set(key, oldConfiguration.get(key));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            newConfiguration.save(new File(dataFolderPath, "config.yml"));
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            Stargate.debug("Stargate::migrateConfig", "Unable to save migrated config");
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Stargate.getInstance().reloadConfig();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,12 @@ public class StargateYamlConfiguration extends YamlConfiguration {
 | 
			
		||||
    static public final String END_OF_COMMENT = "_endOfComment_";
 | 
			
		||||
    static public final String START_OF_COMMENT = "comment_";
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @SuppressWarnings("deprecation")
 | 
			
		||||
    protected @NotNull String buildHeader() {
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public @NotNull String saveToString() {
 | 
			
		||||
        return this.convertYAMLMappingsToComments(super.saveToString());
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user