fix teleports on folia - untested (#44)

This commit is contained in:
Pyrbu 2023-05-10 18:06:14 +01:00
parent bf071f4028
commit 37a093b037
3 changed files with 28 additions and 2 deletions

@ -6,6 +6,7 @@ import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.znpcsplus.npc.NpcEntryImpl; import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl; import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl; import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import lol.pyr.znpcsplus.util.FoliaUtil;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -19,7 +20,7 @@ public class TeleportCommand implements CommandHandler {
context.setUsage(context.getLabel() + " teleport <npc_id>"); context.setUsage(context.getLabel() + " teleport <npc_id>");
Player player = context.ensureSenderIsPlayer(); Player player = context.ensureSenderIsPlayer();
NpcImpl npc = context.parse(NpcEntryImpl.class).getNpc(); 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)); context.send(Component.text("Teleported to NPC!", NamedTextColor.GREEN));
} }

@ -4,9 +4,11 @@ import com.mojang.authlib.GameProfile;
import lol.pyr.znpcsplus.reflection.types.ClassReflection; import lol.pyr.znpcsplus.reflection.types.ClassReflection;
import lol.pyr.znpcsplus.reflection.types.FieldReflection; import lol.pyr.znpcsplus.reflection.types.FieldReflection;
import lol.pyr.znpcsplus.reflection.types.MethodReflection; import lol.pyr.znpcsplus.reflection.types.MethodReflection;
import lol.pyr.znpcsplus.util.VersionUtil;
import lol.pyr.znpcsplus.util.FoliaUtil; import lol.pyr.znpcsplus.util.FoliaUtil;
import lol.pyr.znpcsplus.util.VersionUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -89,4 +91,10 @@ public final class Reflections {
.withParameterTypes(Plugin.class, Consumer.class, long.class, long.class, TimeUnit.class) .withParameterTypes(Plugin.class, Consumer.class, long.class, long.class, TimeUnit.class)
.withExpectResult(SCHEDULED_TASK_CLASS) .withExpectResult(SCHEDULED_TASK_CLASS)
.setStrict(FoliaUtil.isFolia())); .setStrict(FoliaUtil.isFolia()));
public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC = new MethodReflection(
new ReflectionBuilder(Entity.class)
.withMethodName("teleportAsync")
.withParameterTypes(Location.class)
.setStrict(FoliaUtil.isFolia()));
} }

@ -1,5 +1,12 @@
package lol.pyr.znpcsplus.util; 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 { public class FoliaUtil {
private static final Boolean FOLIA = isFolia(); private static final Boolean FOLIA = isFolia();
public static boolean isFolia() { public static boolean isFolia() {
@ -11,4 +18,14 @@ public class FoliaUtil {
return false; 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();
}
}
} }