From 37a093b037a110883842602b76fd9f54ff566ba5 Mon Sep 17 00:00:00 2001 From: Pyrbu Date: Wed, 10 May 2023 18:06:14 +0100 Subject: [PATCH] fix teleports on folia - untested (#44) --- .../pyr/znpcsplus/commands/TeleportCommand.java | 3 ++- .../pyr/znpcsplus/reflection/Reflections.java | 10 +++++++++- .../java/lol/pyr/znpcsplus/util/FoliaUtil.java | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java index 82ec5a1..55fd0fe 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/commands/TeleportCommand.java @@ -6,6 +6,7 @@ import lol.pyr.director.common.command.CommandExecutionException; import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl; +import lol.pyr.znpcsplus.util.FoliaUtil; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.entity.Player; @@ -19,7 +20,7 @@ public class TeleportCommand implements CommandHandler { context.setUsage(context.getLabel() + " teleport "); Player player = context.ensureSenderIsPlayer(); NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); - player.teleport(npc.getLocation().toBukkitLocation(npc.getWorld())); + FoliaUtil.teleport(player, npc.getLocation().toBukkitLocation(npc.getWorld())); context.send(Component.text("Teleported to NPC!", NamedTextColor.GREEN)); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java b/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java index c1df534..5e9fa79 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/reflection/Reflections.java @@ -4,9 +4,11 @@ import com.mojang.authlib.GameProfile; import lol.pyr.znpcsplus.reflection.types.ClassReflection; import lol.pyr.znpcsplus.reflection.types.FieldReflection; import lol.pyr.znpcsplus.reflection.types.MethodReflection; -import lol.pyr.znpcsplus.util.VersionUtil; import lol.pyr.znpcsplus.util.FoliaUtil; +import lol.pyr.znpcsplus.util.VersionUtil; import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; import java.lang.reflect.Method; @@ -89,4 +91,10 @@ public final class Reflections { .withParameterTypes(Plugin.class, Consumer.class, long.class, long.class, TimeUnit.class) .withExpectResult(SCHEDULED_TASK_CLASS) .setStrict(FoliaUtil.isFolia())); + + public static final ReflectionLazyLoader FOLIA_TELEPORT_ASYNC = new MethodReflection( + new ReflectionBuilder(Entity.class) + .withMethodName("teleportAsync") + .withParameterTypes(Location.class) + .setStrict(FoliaUtil.isFolia())); } diff --git a/plugin/src/main/java/lol/pyr/znpcsplus/util/FoliaUtil.java b/plugin/src/main/java/lol/pyr/znpcsplus/util/FoliaUtil.java index 309310b..c8eb36d 100644 --- a/plugin/src/main/java/lol/pyr/znpcsplus/util/FoliaUtil.java +++ b/plugin/src/main/java/lol/pyr/znpcsplus/util/FoliaUtil.java @@ -1,5 +1,12 @@ package lol.pyr.znpcsplus.util; +import lol.pyr.znpcsplus.ZNpcsPlus; +import lol.pyr.znpcsplus.reflection.Reflections; +import org.bukkit.Location; +import org.bukkit.entity.Entity; + +import java.lang.reflect.InvocationTargetException; + public class FoliaUtil { private static final Boolean FOLIA = isFolia(); public static boolean isFolia() { @@ -11,4 +18,14 @@ public class FoliaUtil { return false; } } + + public static void teleport(Entity entity, Location location) { + if (!isFolia()) entity.teleport(location); + else try { + Reflections.FOLIA_TELEPORT_ASYNC.get().invoke(entity, location); + } catch (IllegalAccessException | InvocationTargetException e) { + ZNpcsPlus.LOGGER.severe("Error while teleporting entity:"); + e.printStackTrace(); + } + } }