From a5f969edb26387ac630b73773ea24a35115dbe48 Mon Sep 17 00:00:00 2001 From: MidnightTale Date: Sat, 8 Apr 2023 09:15:14 +0700 Subject: [PATCH] folia ? --- build.gradle | 2 +- src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java | 13 ++++---- .../java/xyz/hynse/bbsdfia/Compatibility.java | 30 +++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/main/java/xyz/hynse/bbsdfia/Compatibility.java diff --git a/build.gradle b/build.gradle index 5bff7ce..9bf734f 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { } dependencies { - compileOnly "io.papermc.paper:paper-api:1.19.4-R0.1-SNAPSHOT" + compileOnly "dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT" } def targetJavaVersion = 17 diff --git a/src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java b/src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java index 4e44ae0..075cf14 100644 --- a/src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java +++ b/src/main/java/xyz/hynse/bbsdfia/Bbsdfia.java @@ -27,18 +27,16 @@ public class Bbsdfia extends JavaPlugin implements Listener { } @EventHandler - public void onFallingBlockToBlock(EntityChangeBlockEvent e){ - if(e.getEntityType() == EntityType.FALLING_BLOCK){ + public void onFallingBlockToBlock(EntityChangeBlockEvent e) { + if (e.getEntityType() == EntityType.FALLING_BLOCK) { Entity entity = e.getEntity(); Location loc = entity.getLocation(); Vector vel = entity.getVelocity(); Block movingTo = getBlockMovingTo(loc, vel); - if(movingTo != null && movingTo.getType() == Material.END_PORTAL){ - Location spawnLoc = movingTo.getLocation(); - spawnLoc.setX(spawnLoc.getX()+0.5); - spawnLoc.setY(spawnLoc.getY()+0.5); - spawnLoc.setZ(spawnLoc.getZ()+0.5); + if (movingTo != null && movingTo.getType() == Material.END_PORTAL) { + Location obsidianPlatformCenter = new Location(movingTo.getWorld(), 100, 50, 0); // change coordinates to the center of the obsidian platform in the end dimension + Location spawnLoc = obsidianPlatformCenter.clone().add(0, 1, 0); // spawn the new falling block entity one block above the obsidian platform FallingBlock dummy = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData()); Vector dummyVel = vel.clone(); @@ -54,6 +52,7 @@ public class Bbsdfia extends JavaPlugin implements Listener { } + Block getBlockMovingTo(Location loc, Vector vel){ double absMax = 0, max = 0; char dir = ' '; diff --git a/src/main/java/xyz/hynse/bbsdfia/Compatibility.java b/src/main/java/xyz/hynse/bbsdfia/Compatibility.java new file mode 100644 index 0000000..dc4ac48 --- /dev/null +++ b/src/main/java/xyz/hynse/bbsdfia/Compatibility.java @@ -0,0 +1,30 @@ +package xyz.hynse.bbsdfia; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +public class Compatibility { + + public static Boolean IS_FOLIA = null; + + private static boolean methodExist(Class clazz, String methodName, Class... parameterTypes) { + try { + clazz.getDeclaredMethod(methodName, parameterTypes); + return true; + } catch (Throwable ignored) {} + return false; + } + + public static Boolean isFolia() { + if (IS_FOLIA == null) IS_FOLIA = methodExist(Bukkit.class, "getGlobalRegionScheduler"); + return IS_FOLIA; + } + + public static void runTaskAsync(Plugin plugin, Runnable runnable) { + if (isFolia()) { + Bukkit.getAsyncScheduler().runNow(plugin, (task) -> runnable.run()); + return; + } + Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable); + } +} \ No newline at end of file