Skip to content

Commit dd93016

Browse files
committed
Allow WorldCreator to pass custom environment key
1 parent 7799bf2 commit dd93016

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

paper-api/src/main/java/org/bukkit/WorldCreator.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
* Represents various types of options that may be used to create a world.
1616
*/
1717
public class WorldCreator {
18-
private final NamespacedKey key; // Paper
18+
private final NamespacedKey key;
1919
private final String name;
2020
private long seed;
2121
private World.Environment environment = World.Environment.NORMAL;
22+
private @Nullable NamespacedKey customEnvironmentKey = null;
2223
private ChunkGenerator generator = null;
2324
private BiomeProvider biomeProvider = null;
2425
private WorldType type = WorldType.NORMAL;
@@ -173,6 +174,7 @@ public WorldCreator copy(@NotNull WorldCreator creator) {
173174

174175
seed = creator.seed();
175176
environment = creator.environment();
177+
customEnvironmentKey = creator.customEnvironmentKey();
176178
generator = creator.generator();
177179
biomeProvider = creator.biomeProvider();
178180
type = creator.type();
@@ -243,6 +245,30 @@ public WorldCreator environment(@NotNull World.Environment env) {
243245
return this;
244246
}
245247

248+
/**
249+
* Gets the environment key that will be used to create or load the world
250+
* if {@link #environment()} is {@link World.Environment#CUSTOM CUSTOM}.
251+
*
252+
* @return World environment key
253+
*/
254+
public @Nullable NamespacedKey customEnvironmentKey() {
255+
return this.customEnvironmentKey;
256+
}
257+
258+
/**
259+
* Sets the environment key that will be used to create or load the world and
260+
* set the environment to {@link World.Environment#CUSTOM CUSTOM}.
261+
*
262+
* @param customEnvironmentKey World environment key
263+
* @return This object, for chaining
264+
*/
265+
public @NotNull WorldCreator customEnvironmentKey(@Nullable NamespacedKey customEnvironmentKey) {
266+
this.environment = World.Environment.CUSTOM;
267+
this.customEnvironmentKey = customEnvironmentKey;
268+
269+
return this;
270+
}
271+
246272
/**
247273
* Gets the type of the world that will be created or loaded
248274
*

paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,12 +1195,23 @@ public World createWorld(WorldCreator creator) {
11951195
biomeProvider = this.getBiomeProvider(name);
11961196
}
11971197

1198-
ResourceKey<LevelStem> actualDimension = switch (creator.environment()) {
1199-
case NORMAL -> LevelStem.OVERWORLD;
1200-
case NETHER -> LevelStem.NETHER;
1201-
case THE_END -> LevelStem.END;
1202-
default -> throw new IllegalArgumentException("Illegal dimension (" + creator.environment() + ")");
1203-
};
1198+
if (creator.environment() == Environment.CUSTOM && creator.customEnvironmentKey() == null) {
1199+
throw new IllegalArgumentException("Custom worlds must have a custom environment key");
1200+
}
1201+
1202+
ResourceKey<LevelStem> actualDimension;
1203+
if (creator.environment() == Environment.NORMAL) {
1204+
actualDimension = LevelStem.OVERWORLD;
1205+
} else if (creator.environment() == Environment.NETHER) {
1206+
actualDimension = LevelStem.NETHER;
1207+
} else if (creator.environment() == Environment.THE_END) {
1208+
actualDimension = LevelStem.END;
1209+
} else {
1210+
if (creator.customEnvironmentKey() == null) {
1211+
throw new IllegalArgumentException("Custom worlds must have a custom environment key");
1212+
}
1213+
actualDimension = CraftNamespacedKey.toResourceKey(Registries.LEVEL_STEM, creator.customEnvironmentKey());
1214+
}
12041215

12051216
final ResourceKey<net.minecraft.world.level.Level> dimensionKey = PaperWorldLoader.dimensionKey(creator.key());
12061217
WorldLoader.DataLoadContext context = this.console.worldLoaderContext;

0 commit comments

Comments
 (0)