Implement proper reloading of worlds config

This commit is contained in:
Ben Woo 2023-09-03 15:39:57 +08:00
parent dff192f3ce
commit 3356757c13
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
5 changed files with 45 additions and 20 deletions

View File

@ -17,6 +17,7 @@ import com.onarandombox.MultiverseCore.commandtools.MultiverseCommand;
import com.onarandombox.MultiverseCore.config.MVCoreConfig;
import com.onarandombox.MultiverseCore.event.MVConfigReloadEvent;
import com.onarandombox.MultiverseCore.utils.MVCorei18n;
import com.onarandombox.MultiverseCore.worldnew.WorldManager;
import jakarta.inject.Inject;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
@ -28,7 +29,7 @@ public class ReloadCommand extends MultiverseCommand {
private final MVCoreConfig config;
private final AnchorManager anchorManager;
private final MVWorldManager worldManager;
private final WorldManager worldManager;
private final PluginManager pluginManager;
@Inject
@ -36,7 +37,7 @@ public class ReloadCommand extends MultiverseCommand {
@NotNull MVCommandManager commandManager,
@NotNull MVCoreConfig config,
@NotNull AnchorManager anchorManager,
@NotNull MVWorldManager worldManager,
@NotNull WorldManager worldManager,
@NotNull PluginManager pluginManager
) {
super(commandManager);
@ -51,14 +52,17 @@ public class ReloadCommand extends MultiverseCommand {
@Description("{@@mv-core.reload.description}")
public void onReloadCommand(@NotNull BukkitCommandIssuer issuer) {
issuer.sendInfo(MVCorei18n.RELOAD_RELOADING);
this.config.load();
this.worldManager.loadWorldsConfig();
this.worldManager.loadWorlds(true);
this.anchorManager.loadAnchors();
try {
this.config.load();
this.worldManager.initAllWorlds();
this.anchorManager.loadAnchors();
} catch (Exception e) {
e.printStackTrace();
}
List<String> configsLoaded = new ArrayList<>();
configsLoaded.add("Multiverse-Core - config.yml");
configsLoaded.add("Multiverse-Core - worlds.yml");
configsLoaded.add("Multiverse-Core - worlds2.yml");
configsLoaded.add("Multiverse-Core - anchors.yml");
MVConfigReloadEvent configReload = new MVConfigReloadEvent(configsLoaded);

View File

@ -19,6 +19,10 @@ public class MVWorld extends OfflineWorld {
super(world.getName(), worldConfig);
this.worldUid = world.getUID();
setupWorldConfig(world);
}
private void setupWorldConfig(World world) {
worldConfig.setMVWorld(this);
worldConfig.load();
worldConfig.setEnvironment(world.getEnvironment());
@ -28,4 +32,10 @@ public class MVWorld extends OfflineWorld {
public Option<World> getBukkitWorld() {
return Option.of(Bukkit.getWorld(worldUid));
}
@Override
void setWorldConfig(WorldConfig worldConfig) {
super.setWorldConfig(worldConfig);
setupWorldConfig(getBukkitWorld().get());
}
}

View File

@ -14,7 +14,7 @@ import java.util.List;
public class OfflineWorld {
protected final String worldName;
protected final WorldConfig worldConfig;
protected WorldConfig worldConfig;
OfflineWorld(String worldName, WorldConfig worldConfig) {
this.worldName = worldName;
@ -205,4 +205,8 @@ public class OfflineWorld {
WorldConfig getWorldConfig() {
return worldConfig;
}
void setWorldConfig(WorldConfig worldConfig) {
this.worldConfig = worldConfig;
}
}

View File

@ -4,7 +4,6 @@ import com.dumptruckman.minecraft.util.Logging;
import com.google.common.base.Strings;
import com.onarandombox.MultiverseCore.utils.file.FileUtils;
import com.onarandombox.MultiverseCore.utils.result.Result;
import com.onarandombox.MultiverseCore.utils.result.SuccessReason;
import com.onarandombox.MultiverseCore.world.WorldNameChecker;
import com.onarandombox.MultiverseCore.worldnew.config.WorldConfig;
import com.onarandombox.MultiverseCore.worldnew.config.WorldsConfigManager;
@ -25,10 +24,12 @@ import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class WorldManager {
@ -38,14 +39,14 @@ public class WorldManager {
private final WorldsConfigManager worldsConfigManager;
@Inject
WorldManager(@NotNull WorldsConfigManager worldsConfigFile) {
WorldManager(@NotNull WorldsConfigManager worldsConfigManager) {
this.offlineWorldsMap = new HashMap<>();
this.worldsMap = new HashMap<>();
this.worldsConfigManager = worldsConfigFile;
this.worldsConfigManager = worldsConfigManager;
}
public void initAllWorlds() {
populateOfflineWorlds();
populateWorldFromConfig();
loadDefaultWorlds();
getOfflineWorlds().forEach(world -> {
if (isMVWorld(world) || !world.getAutoLoad()) {
@ -58,12 +59,17 @@ public class WorldManager {
saveWorldsConfig();
}
private void populateOfflineWorlds() {
// TODO: Check for worlds that are removed after config reload
private void populateWorldFromConfig() {
worldsConfigManager.load();
worldsConfigManager.getAllWorldConfigs().forEach(worldConfig -> {
OfflineWorld offlineWorld = new OfflineWorld(worldConfig.getWorldName(), worldConfig);
offlineWorldsMap.put(worldConfig.getWorldName(), offlineWorld);
Logging.fine("Loaded world %s from config", worldConfig.getWorldName());
getMVWorld(worldConfig.getWorldName())
.peek(mvWorld -> mvWorld.setWorldConfig(worldConfig));
getOfflineWorld(worldConfig.getWorldName())
.peek(offlineWorld -> offlineWorld.setWorldConfig(worldConfig))
.onEmpty(() -> {
OfflineWorld offlineWorld = new OfflineWorld(worldConfig.getWorldName(), worldConfig);
offlineWorldsMap.put(offlineWorld.getName(), offlineWorld);
});
});
}

View File

@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Service
public class WorldsConfigManager {
@ -28,9 +29,9 @@ public class WorldsConfigManager {
load();
}
private void load() {
// TODO: Migration from old worlds.yml
public void load() {
worldsConfig = YamlConfiguration.loadConfiguration(worldConfigFile);
worldConfigMap.clear();
for (String worldName : getAllWorldsInConfig()) {
worldConfigMap.put(worldName, new WorldConfig(worldName, getWorldConfigSection(worldName)));
}
@ -48,7 +49,7 @@ public class WorldsConfigManager {
}
}
public Collection<String> getAllWorldsInConfig() {
public Set<String> getAllWorldsInConfig() {
return worldsConfig.getKeys(false);
}