From d0185eb56e5d1df07bbfe3cfe3db63a2d202d5ac Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Wed, 6 Jan 2021 00:34:04 -0800
Subject: [PATCH] Expand world key API

---
 .../craftbukkit/CraftRegionAccessor.java      |  5 +++++
 .../org/bukkit/craftbukkit/CraftServer.java   | 21 ++++++++++++++++---
 .../craftbukkit/util/CraftMagicNumbers.java   |  5 +++++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
index 15da29058f..a070b2a83e 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java
@@ -521,5 +521,10 @@ public abstract class CraftRegionAccessor implements RegionAccessor {
     public io.papermc.paper.world.MoonPhase getMoonPhase() {
         return io.papermc.paper.world.MoonPhase.getPhase(this.getHandle().dayTime() / 24000L);
     }
+
+    @Override
+    public org.bukkit.NamespacedKey getKey() {
+        return org.bukkit.craftbukkit.util.CraftNamespacedKey.fromMinecraft(this.getHandle().getLevel().dimension().location());
+    }
     // Paper end
 }
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 5ae8f64608..3cfacacd1d 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -1185,9 +1185,15 @@ public final class CraftServer implements Server {
         File folder = new File(this.getWorldContainer(), name);
         World world = this.getWorld(name);
 
-        if (world != null) {
-            return world;
+        // Paper start
+        World worldByKey = this.getWorld(creator.key());
+        if (world != null || worldByKey != null) {
+            if (world == worldByKey) {
+                return world;
+            }
+            throw new IllegalArgumentException("Cannot create a world with key " + creator.key() + " and name " + name + " one (or both) already match a world that exists");
         }
+        // Paper end
 
         if (folder.exists()) {
             Preconditions.checkArgument(folder.isDirectory(), "File (%s) exists and isn't a folder", name);
@@ -1313,7 +1319,7 @@ public final class CraftServer implements Server {
         } else if (name.equals(levelName + "_the_end")) {
             worldKey = net.minecraft.world.level.Level.END;
         } else {
-            worldKey = ResourceKey.create(Registries.DIMENSION, ResourceLocation.withDefaultNamespace(name.toLowerCase(Locale.ROOT)));
+            worldKey = ResourceKey.create(Registries.DIMENSION, ResourceLocation.fromNamespaceAndPath(creator.key().namespace(), creator.key().value()));
         }
 
         // If set to not keep spawn in memory (changed from default) then adjust rule accordingly
@@ -1409,6 +1415,15 @@ public final class CraftServer implements Server {
         return null;
     }
 
+    // Paper start
+    @Override
+    public World getWorld(net.kyori.adventure.key.Key worldKey) {
+        ServerLevel worldServer = console.getLevel(ResourceKey.create(net.minecraft.core.registries.Registries.DIMENSION, io.papermc.paper.adventure.PaperAdventure.asVanilla(worldKey)));
+        if (worldServer == null) return null;
+        return worldServer.getWorld();
+    }
+    // Paper end
+
     public void addWorld(World world) {
         // Check if a World already exists with the UID.
         if (this.getWorld(world.getUID()) != null) {
diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 5f0fd1d4fe..8d9380b504 100644
--- a/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/paper-server/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -538,6 +538,11 @@ public final class CraftMagicNumbers implements UnsafeValues {
     public int nextEntityId() {
         return net.minecraft.world.entity.Entity.nextEntityId();
     }
+
+    @Override
+    public String getMainLevelName() {
+        return ((net.minecraft.server.dedicated.DedicatedServer) net.minecraft.server.MinecraftServer.getServer()).getProperties().levelName;
+    }
     // Paper end
 
     /**