From 239774167ff333a7211e79ccb748da86cd907286 Mon Sep 17 00:00:00 2001 From: MidnightTale Date: Mon, 24 Apr 2023 21:56:09 +0700 Subject: [PATCH] change to use onspawn --- build.gradle | 2 +- .../java/xyz/hynse/foliaflow/FoliaFlow.java | 60 ++++++++++--------- src/main/resources/plugin.yml | 5 ++ 3 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 src/main/resources/plugin.yml diff --git a/build.gradle b/build.gradle index 8548dd7..ac8ebdc 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { def loader = 'folia' def server = '1.19.4' group = 'xyz.hynse' -version = "1.4a-${server}-${loader}" +version = "1.4b-${server}-${loader}" repositories { mavenCentral() diff --git a/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java b/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java index 90ac0e0..115a1cd 100644 --- a/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java +++ b/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java @@ -12,6 +12,7 @@ import org.bukkit.entity.FallingBlock; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.Vector; @@ -55,34 +56,6 @@ public class FoliaFlow extends JavaPlugin implements Listener { } catch (NullPointerException block) { getServer().getLogger().info("Region Scheduler erorr (likly chunky it not load)"); } - AsyncScheduler scheduler = getServer().getAsyncScheduler(); - task = scheduler.runAtFixedRate(this, (scheduledTask) -> Bukkit.getScheduler().runTask(this, () -> { - for (World world : Bukkit.getWorlds()) { - for (Entity entity : world.getEntities()) { - if (entity.getType() == EntityType.FALLING_BLOCK && entity.getWorld().getEnvironment() == World.Environment.THE_END) { - Location centerLoc = new Location(entity.getWorld(), 100, 48.5, 0); - Location loc = entity.getLocation(); - if (loc.distance(centerLoc) <= 1) { - // Set the initial velocity of the falling block only if it doesn't have a velocity stored - if (!velocitiesMap.containsKey(entity)) { - try { - int index = counter % 4; - counter++; - Vector velocity = velocities[index]; - entity.setVelocity(velocity); - velocitiesMap.put(entity, velocity); // Store the velocity in the map - movingBlocks.add(entity.getLocation()); // Add the location to the set - } catch (NullPointerException e) { - // Handle the exception here, for example: - e.printStackTrace(); - getServer().getLogger().info("An error occurred: " + e.getMessage()); - } - } - } - } - } - } - }), 0L, 1L, TimeUnit.MILLISECONDS); getServer().getPluginManager().registerEvents(this, this); getServer().getConsoleSender().sendMessage(""); getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " ______________ "); @@ -102,6 +75,36 @@ public class FoliaFlow extends JavaPlugin implements Listener { super.onDisable(); } + @EventHandler + public void onEntitySpawn(EntitySpawnEvent e) { + if (e.getEntityType() == EntityType.FALLING_BLOCK && e.getLocation().getWorld().getEnvironment() == World.Environment.THE_END) { + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getType() == EntityType.FALLING_BLOCK && entity.getWorld().getEnvironment() == World.Environment.THE_END) { + Location centerLoc = new Location(entity.getWorld(), 100, 48.5, 0); + Location loc = entity.getLocation(); + if (loc.distance(centerLoc) <= 1) { + // Set the initial velocity of the falling block only if it doesn't have a velocity stored + if (!velocitiesMap.containsKey(entity)) { + try { + int index = counter % 4; + counter++; + Vector velocity = velocities[index]; + entity.setVelocity(velocity); + velocitiesMap.put(entity, velocity); // Store the velocity in the map + movingBlocks.add(entity.getLocation()); // Add the location to the set + } catch (NullPointerException x) { + // Handle the exception here, for example: + x.printStackTrace(); + getServer().getLogger().info("An error occurred: " + x.getMessage()); + } + } + } + } + } + } + } + } @EventHandler public void onFallingBlockToBlock(EntityChangeBlockEvent e){ if(e.getEntityType() == EntityType.FALLING_BLOCK){ @@ -158,7 +161,6 @@ public class FoliaFlow extends JavaPlugin implements Listener { case 'y' -> relative = loc.getBlock().getRelative(0, (int) Math.signum(max), 0); case 'z' -> relative = loc.getBlock().getRelative(0, 0, (int) Math.signum(max)); } - //debug("Moving falling block from location " + loc.toString() + " to location " + dir); return relative; } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..19f50a9 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,5 @@ +name: FoliaFlow +version: '${version}' +main: xyz.hynse.foliaflow.FoliaFlow +api-version: 1.19 +folia-supported: true \ No newline at end of file