fix: bad world lookup on Fabric

This commit is contained in:
William committed 2024-06-24 11:43:05 +01:00
1 parent 332157b2d0
commit 6223c3153f
3 files changed
+31 -14

No files matched your search

@@ -34,6 +34,7 @@ import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.ModContainer;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.platform.fabric.FabricServerAudiences; import net.kyori.adventure.platform.fabric.FabricServerAudiences;
import net.minecraft.entity.Entity;
import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
@@ -87,6 +88,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -412,17 +414,15 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
} }
@NotNull @NotNull
public static TeleportTarget adapt(@NotNull Location location, @NotNull MinecraftServer server) { public static TeleportTarget adapt(@NotNull Location location, @NotNull MinecraftServer server,
@NotNull Consumer<Entity> runAfterTeleport) {
return new TeleportTarget( return new TeleportTarget(
adapt(location.getWorld(), server), adapt(location.getWorld(), server),
new Vec3d(location.getX(), location.getY(), location.getZ()), new Vec3d(location.getX(), location.getY(), location.getZ()),
Vec3d.ZERO, Vec3d.ZERO,
location.getYaw(), location.getYaw(),
location.getPitch(), location.getPitch(),
entity -> entity.refreshPositionAndAngles( runAfterTeleport::accept
location.getX(), location.getY(), location.getZ(),
location.getYaw(), location.getPitch()
)
); );
} }
@@ -439,7 +439,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
@NotNull @NotNull
public static World adapt(@NotNull net.minecraft.world.World world) { public static World adapt(@NotNull net.minecraft.world.World world) {
return World.from( return World.from(
world.getRegistryKey().getRegistry().asMinimalString(), world.getRegistryKey().getValue().asMinimalString(),
UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes()) UUID.nameUUIDFromBytes(world.getRegistryKey().getValue().asString().getBytes())
); );
} }
@@ -19,6 +19,7 @@
package net.william278.huskhomes.api; package net.william278.huskhomes.api;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
@@ -35,6 +36,8 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.function.Consumer;
/** /**
* The HuskHomes API implementation for the Fabric platform, providing methods to access ServerPlayerEntity data, * The HuskHomes API implementation for the Fabric platform, providing methods to access ServerPlayerEntity data,
* homes, warps and process teleports * homes, warps and process teleports
@@ -111,6 +114,19 @@ public class FabricHuskHomesAPI extends BaseHuskHomesAPI {
return FabricHuskHomes.Adapter.adapt(position, ((FabricHuskHomes) plugin).getMinecraftServer()); 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<Entity> afterTeleport) {
return FabricHuskHomes.Adapter.adapt(location, ((FabricHuskHomes) plugin).getMinecraftServer(), afterTeleport);
}
/** /**
* Returns the Fabric {@link TeleportTarget} being represented by the given {@link Location}. * Returns the Fabric {@link TeleportTarget} being represented by the given {@link Location}.
* *
@@ -120,7 +136,8 @@ public class FabricHuskHomesAPI extends BaseHuskHomesAPI {
*/ */
@Nullable @Nullable
public TeleportTarget getTeleportTarget(@NotNull Location location) { public TeleportTarget getTeleportTarget(@NotNull Location location) {
return FabricHuskHomes.Adapter.adapt(location, ((FabricHuskHomes) plugin).getMinecraftServer()); return getTeleportTarget(location, (entity) -> {
});
} }
/** /**
@@ -134,17 +134,17 @@ public class FabricUser extends OnlineUser {
if (server == null) { if (server == null) {
throw new TeleportationException(TeleportationException.Type.ILLEGAL_TARGET_COORDINATES, plugin); 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); final ServerWorld world = FabricHuskHomes.Adapter.adapt(location.getWorld(), server);
if (world == null) { if (world == null) {
throw new TeleportationException(TeleportationException.Type.WORLD_NOT_FOUND, plugin); 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 @Override