Search in sources :

Example 1 with MemorySection

use of com.plotsquared.core.configuration.MemorySection in project PlotSquared by IntellectualSites.

the class Reload method onCommand.

@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
    try {
        // The following won't affect world generation, as that has to be
        // loaded during startup unfortunately.
        PlotSquared.get().setupConfigs();
        this.worldConfiguration = PlotSquared.get().getWorldConfiguration();
        this.worldFile = PlotSquared.get().getWorldsFile();
        PlotSquared.get().loadCaptionMap();
        this.plotAreaManager.forEachPlotArea(area -> {
            ConfigurationSection worldSection = this.worldConfiguration.getConfigurationSection("worlds." + area.getWorldName());
            if (worldSection == null) {
                return;
            }
            if (area.getType() != PlotAreaType.PARTIAL || !worldSection.contains("areas")) {
                area.saveConfiguration(worldSection);
                area.loadDefaultConfiguration(worldSection);
            } else {
                ConfigurationSection areaSection = worldSection.getConfigurationSection("areas." + area.getId() + "-" + area.getMin() + "-" + area.getMax());
                YamlConfiguration clone = new YamlConfiguration();
                for (String key : areaSection.getKeys(true)) {
                    if (areaSection.get(key) instanceof MemorySection) {
                        continue;
                    }
                    if (!clone.contains(key)) {
                        clone.set(key, areaSection.get(key));
                    }
                }
                for (String key : worldSection.getKeys(true)) {
                    if (worldSection.get(key) instanceof MemorySection) {
                        continue;
                    }
                    if (!key.startsWith("areas") && !clone.contains(key)) {
                        clone.set(key, worldSection.get(key));
                    }
                }
                area.saveConfiguration(clone);
                // netSections is the combination of
                for (String key : clone.getKeys(true)) {
                    if (clone.get(key) instanceof MemorySection) {
                        continue;
                    }
                    if (!worldSection.contains(key)) {
                        worldSection.set(key, clone.get(key));
                    } else {
                        Object value = worldSection.get(key);
                        if (Objects.equals(value, clone.get(key))) {
                            areaSection.set(key, clone.get(key));
                        }
                    }
                }
                area.loadDefaultConfiguration(clone);
            }
        });
        this.worldConfiguration.save(this.worldFile);
        player.sendMessage(TranslatableCaption.of("reload.reloaded_configs"));
    } catch (Exception e) {
        e.printStackTrace();
        player.sendMessage(TranslatableCaption.of("reload.reload_failed"));
    }
    return true;
}
Also used : MemorySection(com.plotsquared.core.configuration.MemorySection) YamlConfiguration(com.plotsquared.core.configuration.file.YamlConfiguration) ConfigurationSection(com.plotsquared.core.configuration.ConfigurationSection)

Aggregations

ConfigurationSection (com.plotsquared.core.configuration.ConfigurationSection)1 MemorySection (com.plotsquared.core.configuration.MemorySection)1 YamlConfiguration (com.plotsquared.core.configuration.file.YamlConfiguration)1