From 9921a197e7908942aa4bcbaa6f225f04b90411ac Mon Sep 17 00:00:00 2001
From: kokiriglade <60290002+kokiriglade@users.noreply.github.com>
Date: Mon, 19 Aug 2024 10:41:55 +0100
Subject: [PATCH] Add getWorld method that uses adventure Key (#11199)

---
 patches/api/Add-BlockLockCheckEvent.patch    |  9 ++++----
 patches/api/Expand-world-key-API.patch       | 24 +++++++++++++++++++-
 patches/server/Add-BlockLockCheckEvent.patch |  2 +-
 patches/server/Expand-world-key-API.patch    |  4 ++--
 4 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/patches/api/Add-BlockLockCheckEvent.patch b/patches/api/Add-BlockLockCheckEvent.patch
index fd08737c84..8cd0f1ce36 100644
--- a/patches/api/Add-BlockLockCheckEvent.patch
+++ b/patches/api/Add-BlockLockCheckEvent.patch
@@ -34,6 +34,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +import net.kyori.adventure.sound.Sound;
 +import net.kyori.adventure.text.Component;
 +import org.bukkit.block.Block;
++import org.bukkit.block.BlockState;
 +import org.bukkit.entity.Player;
 +import org.bukkit.event.HandlerList;
 +import org.bukkit.event.block.BlockEvent;
@@ -53,7 +54,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +
 +    private static final HandlerList HANDLER_LIST = new HandlerList();
 +
-+    private final LockableTileState state;
 +    private final Player player;
 +    private Component lockedMessage;
 +    private Sound lockedSound;
@@ -61,9 +61,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    private Result result = Result.DEFAULT;
 +
 +    @ApiStatus.Internal
-+    public BlockLockCheckEvent(final @NotNull Block block, final @NotNull LockableTileState state, final @NotNull Player player, final @NotNull Component lockedMessage, final @NotNull Sound lockedSound) {
++    public BlockLockCheckEvent(final @NotNull Block block, final @NotNull Player player, final @NotNull Component lockedMessage, final @NotNull Sound lockedSound) {
 +        super(block);
-+        this.state = state;
 +        this.player = player;
 +        this.lockedMessage = lockedMessage;
 +        this.lockedSound = lockedSound;
@@ -76,7 +75,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +     * @return the snapshot block state.
 +     */
 +    public @NotNull LockableTileState getBlockState() {
-+        return this.state;
++        final BlockState blockState = this.getBlock().getState();
++        Preconditions.checkState(blockState instanceof LockableTileState, "Block state of lock-checked block is no longer a lockable tile state!");
++        return (LockableTileState) blockState;
 +    }
 +
 +    /**
diff --git a/patches/api/Expand-world-key-API.patch b/patches/api/Expand-world-key-API.patch
index 9bc2f7f562..b444860b98 100644
--- a/patches/api/Expand-world-key-API.patch
+++ b/patches/api/Expand-world-key-API.patch
@@ -23,6 +23,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    public static World getWorld(@NotNull NamespacedKey worldKey) {
 +        return server.getWorld(worldKey);
 +    }
++
++    /**
++     * Gets the world from the given Key
++     *
++     * @param worldKey the Key of the world to retrieve
++     * @return a world with the given Key, or null if none exists
++     */
++    @Nullable
++    public static World getWorld(@NotNull net.kyori.adventure.key.Key worldKey) {
++        return server.getWorld(worldKey);
++    }
 +    // Paper end
  
      /**
@@ -71,7 +82,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +     * @return a world with the given NamespacedKey, or null if none exists
 +     */
 +    @Nullable
-+    public World getWorld(@NotNull NamespacedKey worldKey);
++    default World getWorld(@NotNull NamespacedKey worldKey) {
++        return getWorld((net.kyori.adventure.key.Key) worldKey);
++    }
++
++    /**
++     * Gets the world from the given Key
++     *
++     * @param worldKey the Key of the world to retrieve
++     * @return a world with the given Key, or null if none exists
++     */
++    @Nullable
++    World getWorld(@NotNull net.kyori.adventure.key.Key worldKey);
 +    // Paper end
 +
      /**
diff --git a/patches/server/Add-BlockLockCheckEvent.patch b/patches/server/Add-BlockLockCheckEvent.patch
index 5bf9837d9c..f62c406e6c 100644
--- a/patches/server/Add-BlockLockCheckEvent.patch
+++ b/patches/server/Add-BlockLockCheckEvent.patch
@@ -26,7 +26,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +            final org.bukkit.block.Block block = org.bukkit.craftbukkit.block.CraftBlock.at(blockEntity.getLevel(), blockEntity.getBlockPos());
 +            net.kyori.adventure.text.Component lockedMessage = net.kyori.adventure.text.Component.translatable("container.isLocked", io.papermc.paper.adventure.PaperAdventure.asAdventure(containerName));
 +            net.kyori.adventure.sound.Sound lockedSound = net.kyori.adventure.sound.Sound.sound(org.bukkit.Sound.BLOCK_CHEST_LOCKED, net.kyori.adventure.sound.Sound.Source.BLOCK, 1.0F, 1.0F);
-+            final io.papermc.paper.event.block.BlockLockCheckEvent event = new io.papermc.paper.event.block.BlockLockCheckEvent(block, (io.papermc.paper.block.LockableTileState) block.getState(), serverPlayer.getBukkitEntity(), lockedMessage, lockedSound);
++            final io.papermc.paper.event.block.BlockLockCheckEvent event = new io.papermc.paper.event.block.BlockLockCheckEvent(block, serverPlayer.getBukkitEntity(), lockedMessage, lockedSound);
 +            event.callEvent();
 +            if (event.getResult() == org.bukkit.event.Event.Result.ALLOW) {
 +                return true;
diff --git a/patches/server/Expand-world-key-API.patch b/patches/server/Expand-world-key-API.patch
index 8f70b085a9..f8595def4f 100644
--- a/patches/server/Expand-world-key-API.patch
+++ b/patches/server/Expand-world-key-API.patch
@@ -56,8 +56,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
  
 +    // Paper start
 +    @Override
-+    public World getWorld(NamespacedKey worldKey) {
-+        ServerLevel worldServer = console.getLevel(ResourceKey.create(net.minecraft.core.registries.Registries.DIMENSION, CraftNamespacedKey.toMinecraft(worldKey)));
++    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();
 +    }