Skip to content

Commit 4c45443

Browse files
authored
Merge pull request #3270 from Multiverse/fix/dot-world-name
Fix world not loading when the world name has `.` in it
2 parents 3356724 + 755fab0 commit 4c45443

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/main/java/org/mvplugins/multiverse/core/world/WorldsConfigManager.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ private void migrateRemoveOldConfigSerializable() {
121121
* @return A tuple containing a list of the new WorldConfigs added and a list of the worlds removed from the config.
122122
*/
123123
private NewAndRemovedWorlds parseNewAndRemovedWorlds() {
124-
Set<String> allWorldsInConfig = worldsConfig.getKeys(false);
124+
List<String> allWorldsInConfig = worldsConfig.getKeys(false)
125+
.stream()
126+
.map(this::decodeWorldName)
127+
.toList();
128+
125129
List<WorldConfig> newWorldsAdded = new ArrayList<>();
126130

127131
for (String worldName : allWorldsInConfig) {
@@ -174,7 +178,7 @@ public Try<Void> save() {
174178
worldConfig.save().onFailure(e -> {
175179
throw new RuntimeException("Failed to save world config: " + worldName, e);
176180
});
177-
worldsConfig.set(worldName, worldConfig.getConfigurationSection());
181+
worldsConfig.set(encodeWorldName(worldName), worldConfig.getConfigurationSection());
178182
});
179183
worldsConfig.save(worldConfigFile);
180184
}).onFailure(e -> {
@@ -214,7 +218,7 @@ public Try<Void> save() {
214218
*/
215219
public void deleteWorldConfig(@NotNull String worldName) {
216220
worldConfigMap.remove(worldName);
217-
worldsConfig.set(worldName, null);
221+
worldsConfig.set(encodeWorldName(worldName), null);
218222
}
219223

220224
/**
@@ -225,11 +229,23 @@ public void deleteWorldConfig(@NotNull String worldName) {
225229
* @return The {@link ConfigurationSection} for the given world.
226230
*/
227231
private ConfigurationSection getWorldConfigSection(String worldName) {
232+
worldName = encodeWorldName(worldName);
228233
return worldsConfig.isConfigurationSection(worldName)
229234
? worldsConfig.getConfigurationSection(worldName)
230235
: worldsConfig.createSection(worldName);
231236
}
232237

238+
/**
239+
* Dot is a special character in YAML that causes sub-path issues.
240+
*/
241+
private String encodeWorldName(String worldName) {
242+
return worldName.replace(".", "[dot]");
243+
}
244+
245+
private String decodeWorldName(String worldName) {
246+
return worldName.replace("[dot]", ".");
247+
}
248+
233249
private static final class ConfigMigratedException extends RuntimeException {
234250
private ConfigMigratedException() {
235251
super("Config migrated");

src/test/java/org/mvplugins/multiverse/core/world/WorldConfigMangerTest.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ class WorldConfigMangerTest : TestWithMockBukkit() {
6363

6464
@Test
6565
fun `Add a new world to config`() {
66-
val worldConfig = worldConfigManager.addWorldConfig("newworld")
66+
val worldConfig = worldConfigManager.addWorldConfig("new.world")
6767
assertNotNull(worldConfig)
68-
assertEquals("newworld", worldConfig.worldName)
68+
assertEquals("new.world", worldConfig.worldName)
6969
assertTrue(worldConfigManager.save().isSuccess)
7070
assertConfigEquals("/worlds/newworld_worlds.yml", "worlds.yml")
71+
72+
// Make sure the world still can be loaded after save
73+
assertTrue(worldConfigManager.load().isSuccess)
74+
assertEquals("new.world", worldConfigManager.getWorldConfig("new.world").orNull?.worldName)
75+
assertConfigEquals("/worlds/newworld_worlds.yml", "worlds.yml")
7176
}
7277

7378
@Test

src/test/resources/worlds/newworld_worlds.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ world_nether:
7474
spawning: {}
7575
world-blacklist: []
7676
version: 1.2
77-
newworld:
77+
new[dot]world:
7878
adjust-spawn: false
7979
alias: ''
8080
allow-advancement-grant: true

0 commit comments

Comments
 (0)