From 6223c3153f9c7bef64d28e20187003a686627947 Mon Sep 17 00:00:00 2001 From: William Date: Mon, 24 Jun 2024 11:42:27 +0100 Subject: [PATCH] fix: bad world lookup on Fabric --- .../william278/huskhomes/FabricHuskHomes.java | 12 ++++++------ .../huskhomes/api/FabricHuskHomesAPI.java | 19 ++++++++++++++++++- .../william278/huskhomes/user/FabricUser.java | 14 +++++++------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java index 5a0e648b..14568016 100644 --- a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java +++ b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java @@ -34,6 +34,7 @@ import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.ModContainer; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.platform.fabric.FabricServerAudiences; +import net.minecraft.entity.Entity; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.server.MinecraftServer; @@ -87,6 +88,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.concurrent.ConcurrentMap; +import java.util.function.Consumer; import java.util.logging.Level; import java.util.stream.Collectors; @@ -412,17 +414,15 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes } @NotNull - public static TeleportTarget adapt(@NotNull Location location, @NotNull MinecraftServer server) { + public static TeleportTarget adapt(@NotNull Location location, @NotNull MinecraftServer server, + @NotNull Consumer runAfterTeleport) { return new TeleportTarget( adapt(location.getWorld(), server), new Vec3d(location.getX(), location.getY(), location.getZ()), Vec3d.ZERO, location.getYaw(), location.getPitch(), - entity -> entity.refreshPositionAndAngles( - location.getX(), location.getY(), location.getZ(), - location.getYaw(), location.getPitch() - ) + runAfterTeleport::accept ); } @@ -439,7 +439,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes @NotNull public static World adapt(@NotNull net.minecraft.world.World world) { return World.from( - world.getRegistryKey().getRegistry().asMinimalString(), + world.getRegistryKey().getValue().asMinimalString(), UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes()) ); } diff --git a/fabric/src/main/java/net/william278/huskhomes/api/FabricHuskHomesAPI.java b/fabric/src/main/java/net/william278/huskhomes/api/FabricHuskHomesAPI.java index 48886435..26cf22a4 100644 --- a/fabric/src/main/java/net/william278/huskhomes/api/FabricHuskHomesAPI.java +++ b/fabric/src/main/java/net/william278/huskhomes/api/FabricHuskHomesAPI.java @@ -19,6 +19,7 @@ package net.william278.huskhomes.api; +import net.minecraft.entity.Entity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.Vec3d; @@ -35,6 +36,8 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.function.Consumer; + /** * The HuskHomes API implementation for the Fabric platform, providing methods to access ServerPlayerEntity data, * homes, warps and process teleports @@ -111,6 +114,19 @@ public class FabricHuskHomesAPI extends BaseHuskHomesAPI { return FabricHuskHomes.Adapter.adapt(position, ((FabricHuskHomes) plugin).getMinecraftServer()); } + /** + * Returns the Fabric {@link TeleportTarget} being represented by the given {@link Location}. + * + * @param location the {@link Location} to get the {@link TeleportTarget} from + * @param afterTeleport the {@link Consumer} to run after the teleport completes + * @return the {@link TeleportTarget} being represented by the given {@link Location} + * @since 4.7 + */ + @Nullable + public TeleportTarget getTeleportTarget(@NotNull Location location, @NotNull Consumer afterTeleport) { + return FabricHuskHomes.Adapter.adapt(location, ((FabricHuskHomes) plugin).getMinecraftServer(), afterTeleport); + } + /** * Returns the Fabric {@link TeleportTarget} being represented by the given {@link Location}. * @@ -120,7 +136,8 @@ public class FabricHuskHomesAPI extends BaseHuskHomesAPI { */ @Nullable public TeleportTarget getTeleportTarget(@NotNull Location location) { - return FabricHuskHomes.Adapter.adapt(location, ((FabricHuskHomes) plugin).getMinecraftServer()); + return getTeleportTarget(location, (entity) -> { + }); } /** 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 a35c324e..69db231e 100644 --- a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java +++ b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java @@ -134,17 +134,17 @@ public class FabricUser extends OnlineUser { if (server == null) { throw new TeleportationException(TeleportationException.Type.ILLEGAL_TARGET_COORDINATES, plugin); } - - // Dismount users - player.stopRiding(); - player.getPassengerList().forEach(Entity::stopRiding); - - // Adapt and teleport final ServerWorld world = FabricHuskHomes.Adapter.adapt(location.getWorld(), server); if (world == null) { throw new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin); } - player.teleportTo(FabricHuskHomes.Adapter.adapt(location, server)); + + // Synchronously teleport + plugin.runSync(() -> { + player.stopRiding(); + player.getPassengerList().forEach(Entity::stopRiding); + player.teleportTo(FabricHuskHomes.Adapter.adapt(location, server, entity -> handleInvulnerability())); + }); } @Override