From 4c056d1be261d6db57fdaa05532e7e07e2088771 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 15 Jul 2023 13:37:12 +0100 Subject: [PATCH] Use fabric-dimensions API for x-dimension teleportation, fix #457 --- common/build.gradle | 1 + fabric/build.gradle | 3 ++- .../william278/huskhomes/user/FabricUser.java | 27 +++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/common/build.gradle b/common/build.gradle index 05afe848..2b13740c 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -16,6 +16,7 @@ dependencies { api 'net.william278:DesertWell:2.0.4' api 'dev.dejvokep:boosted-yaml:1.3.1' api 'net.kyori:adventure-api:4.14.0' + api 'net.kyori:adventure-text-serializer-gson:4.14.0' api('com.zaxxer:HikariCP:5.0.1') { exclude module: 'slf4j-api' } diff --git a/fabric/build.gradle b/fabric/build.gradle index 5055df6e..a3dd6a1b 100644 --- a/fabric/build.gradle +++ b/fabric/build.gradle @@ -16,7 +16,8 @@ dependencies { 'fabric-lifecycle-events-v1', 'fabric-entity-events-v1', 'fabric-command-api-v2', - 'fabric-networking-api-v1' + 'fabric-networking-api-v1', + 'fabric-dimensions-v1', ] fabricApiModules.forEach { modImplementation(fabricApi.module(it, fabric_api_version)) diff --git a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java index 4e772256..32b7a8e2 100644 --- a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java +++ b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java @@ -20,6 +20,7 @@ package net.william278.huskhomes.user; import me.lucko.fabric.api.permissions.v0.Permissions; +import net.fabricmc.fabric.api.dimension.v1.FabricDimensions; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; import net.kyori.adventure.audience.Audience; import net.minecraft.network.PacketByteBuf; @@ -28,6 +29,8 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Vec3d; +import net.minecraft.world.TeleportTarget; import net.william278.huskhomes.FabricHuskHomes; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; @@ -129,15 +132,23 @@ public class FabricUser extends OnlineUser { @Override public void teleportLocally(@NotNull Location location, boolean async) throws TeleportationException { final MinecraftServer server = player.getServer(); - assert server != null : "Server is null"; - final Identifier worldId = Identifier.tryParse(location.getWorld().getName()); - player.teleport( + if (server == null) { + throw new TeleportationException(TeleportationException.Type.ILLEGAL_TARGET_COORDINATES, plugin); + } + + FabricDimensions.teleport( + player, server.getWorld(server.getWorldRegistryKeys().stream() - .filter(key -> key.getValue().equals(worldId)).findFirst().orElseThrow( - () -> new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin)) - ), - location.getX(), location.getY(), location.getZ(), - location.getYaw(), location.getPitch() + .filter(key -> key.getValue().equals(Identifier.tryParse(location.getWorld().getName()))) + .findFirst().orElseThrow( + () -> new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin) + )), + new TeleportTarget( + new Vec3d(location.getX(), location.getY(), location.getZ()), + Vec3d.ZERO, + location.getYaw(), + location.getPitch() + ) ); }