001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.configuration.ConfigurationSection;
024import com.plotsquared.core.configuration.MemorySection;
025import com.plotsquared.core.configuration.caption.TranslatableCaption;
026import com.plotsquared.core.configuration.file.YamlConfiguration;
027import com.plotsquared.core.inject.annotations.WorldConfig;
028import com.plotsquared.core.inject.annotations.WorldFile;
029import com.plotsquared.core.player.PlotPlayer;
030import com.plotsquared.core.plot.PlotAreaType;
031import com.plotsquared.core.plot.world.PlotAreaManager;
032import org.checkerframework.checker.nullness.qual.NonNull;
033
034import java.io.File;
035import java.util.Objects;
036
037@CommandDeclaration(command = "reload",
038        aliases = "rl",
039        permission = "plots.admin.command.reload",
040        usage = "/plot reload",
041        category = CommandCategory.ADMINISTRATION)
042public class Reload extends SubCommand {
043
044    private final PlotAreaManager plotAreaManager;
045    private YamlConfiguration worldConfiguration;
046    private File worldFile;
047
048    @Inject
049    public Reload(
050            final @NonNull PlotAreaManager plotAreaManager,
051            @WorldConfig final @NonNull YamlConfiguration worldConfiguration,
052            @WorldFile final @NonNull File worldFile
053    ) {
054        this.plotAreaManager = plotAreaManager;
055        this.worldConfiguration = worldConfiguration;
056        this.worldFile = worldFile;
057    }
058
059    @Override
060    public boolean onCommand(PlotPlayer<?> player, String[] args) {
061        try {
062            // The following won't affect world generation, as that has to be
063            // loaded during startup unfortunately.
064            PlotSquared.get().setupConfigs();
065            this.worldConfiguration = PlotSquared.get().getWorldConfiguration();
066            this.worldFile = PlotSquared.get().getWorldsFile();
067            PlotSquared.get().loadCaptionMap();
068            this.plotAreaManager.forEachPlotArea(area -> {
069                ConfigurationSection worldSection = this.worldConfiguration
070                        .getConfigurationSection("worlds." + area.getWorldName());
071                if (worldSection == null) {
072                    return;
073                }
074                if (area.getType() != PlotAreaType.PARTIAL || !worldSection.contains("areas")) {
075                    area.saveConfiguration(worldSection);
076                    area.loadDefaultConfiguration(worldSection);
077                } else {
078                    ConfigurationSection areaSection = worldSection.getConfigurationSection(
079                            "areas." + area.getId() + "-" + area.getMin() + "-" + area.getMax());
080                    YamlConfiguration clone = new YamlConfiguration();
081                    for (String key : areaSection.getKeys(true)) {
082                        if (areaSection.get(key) instanceof MemorySection) {
083                            continue;
084                        }
085                        if (!clone.contains(key)) {
086                            clone.set(key, areaSection.get(key));
087                        }
088                    }
089                    for (String key : worldSection.getKeys(true)) {
090                        if (worldSection.get(key) instanceof MemorySection) {
091                            continue;
092                        }
093                        if (!key.startsWith("areas") && !clone.contains(key)) {
094                            clone.set(key, worldSection.get(key));
095                        }
096                    }
097                    area.saveConfiguration(clone);
098                    // netSections is the combination of
099                    for (String key : clone.getKeys(true)) {
100                        if (clone.get(key) instanceof MemorySection) {
101                            continue;
102                        }
103                        if (!worldSection.contains(key)) {
104                            worldSection.set(key, clone.get(key));
105                        } else {
106                            Object value = worldSection.get(key);
107                            if (Objects.equals(value, clone.get(key))) {
108                                areaSection.set(key, clone.get(key));
109                            }
110                        }
111                    }
112                    area.loadDefaultConfiguration(clone);
113                }
114            });
115            this.worldConfiguration.save(this.worldFile);
116            player.sendMessage(TranslatableCaption.of("reload.reloaded_configs"));
117        } catch (Exception e) {
118            e.printStackTrace();
119            player.sendMessage(TranslatableCaption.of("reload.reload_failed"));
120        }
121        return true;
122    }
123
124}