From 891ef64b631bdd0b3de9106703a36274848cba5f Mon Sep 17 00:00:00 2001 From: MidnightTale Date: Mon, 24 Apr 2023 23:53:21 +0700 Subject: [PATCH] fake block with block display --- .../java/xyz/hynse/foliaflow/FoliaFlow.java | 142 ++++++++---------- 1 file changed, 63 insertions(+), 79 deletions(-) diff --git a/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java b/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java index 8d94f41..1559a65 100644 --- a/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java +++ b/src/main/java/xyz/hynse/foliaflow/FoliaFlow.java @@ -1,23 +1,26 @@ package xyz.hynse.foliaflow; +import io.papermc.paper.threadedregions.scheduler.AsyncScheduler; import io.papermc.paper.threadedregions.scheduler.RegionScheduler; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.*; import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; import org.bukkit.block.data.type.Slab; +import org.bukkit.entity.BlockDisplay; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.FallingBlock; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.entity.EntityChangeBlockEvent; -import org.bukkit.event.entity.EntitySpawnEvent; -import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.Vector; import java.util.*; +import java.util.concurrent.TimeUnit; + +import static org.bukkit.Bukkit.getScheduler; public class FoliaFlow extends JavaPlugin implements Listener { private final double vh = 0.2; @@ -30,20 +33,15 @@ public class FoliaFlow extends JavaPlugin implements Listener { private final Vector[] velocities = {velocity1, velocity2, velocity3, velocity4}; private int counter = 0; private final Set movingBlocks = new HashSet<>(); - private final Map velocitiesMap = new HashMap<>(); // Create a map to store velocities + private final Map velocitiesMap = new HashMap<>(); + private ScheduledTask task; private ScheduledTask blockktask; - private World endWorld; - - + private boolean blockCreated = false; @Override public void onEnable() { super.onEnable(); - - // Get the region scheduler for the server try { RegionScheduler schedulerblock = getServer().getRegionScheduler(); - - // Schedule a repeating task to run every tick using runAtFixedRate() method blockktask = schedulerblock.runAtFixedRate(this, Objects.requireNonNull(Bukkit.getWorld("world_the_end")), 1, 1, (schedulerTask) -> { Block block = Objects.requireNonNull(Bukkit.getWorld("world_the_end")).getBlockAt(100, 48, 0); if (block.getType() == Material.OBSIDIAN) { @@ -51,73 +49,59 @@ public class FoliaFlow extends JavaPlugin implements Listener { Slab slab = (Slab) block.getBlockData(); slab.setType(Slab.Type.BOTTOM); block.setBlockData(slab); + } + }, 1L, 1L); } catch (NullPointerException block) { getServer().getLogger().info("Region Scheduler erorr (likly chunky it not load)"); } + try { + AsyncScheduler scheduler = getServer().getAsyncScheduler(); + task = scheduler.runAtFixedRate(this, (scheduledTask) -> 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) { + if (!velocitiesMap.containsKey(entity)) { + int index = counter % 4; + counter++; + Vector velocity = velocities[index]; + entity.setVelocity(velocity); + velocitiesMap.put(entity, velocity); + movingBlocks.add(entity.getLocation()); + } + } + } + } + } + }), 0L, 1L, TimeUnit.MILLISECONDS); + } catch (UnsupportedOperationException ignored) { + } + + Location location = new Location(Bukkit.getWorld("world_the_end"), 100, 48, 0); + if (!blockCreated) { + World world = location.getWorld(); + if (world != null) { + Block block = world.getBlockAt(location); + BlockData blockData = block.getBlockData(); + BlockDisplay blockDisplay = (BlockDisplay) world.spawnEntity(location, EntityType.BLOCK_DISPLAY); + blockDisplay.setBlock(blockData); + blockCreated = true; + } + } getServer().getPluginManager().registerEvents(this, this); - getServer().getConsoleSender().sendMessage(""); - getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " ______________ "); - getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " / ____/ ____/ /___ _ __"); - getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " / /_ / /_ / / __ \\ | /| / /"); - getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " / __/ / __/ / / /_/ / |/ |/ / "); - getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "/_/ /_/ /_/\\____/|__/|__/ "); - getServer().getConsoleSender().sendMessage(""); - getServer().getConsoleSender().sendMessage(ChatColor.YELLOW + "Plugin started successfully!"); - getServer().getConsoleSender().sendMessage(""); } @Override public void onDisable() { + task.cancel(); blockktask.cancel(); super.onDisable(); } -// @EventHandler -// public void onFallingBlockLand(EntityChangeBlockEvent event) { -// Entity entity = event.getEntity(); -// if (entity instanceof FallingBlock && entity.getWorld().getEnvironment() == World.Environment.THE_END) { -// getLogger().info("found falling block" + entity); -// Location centerLoc = new Location(entity.getWorld(), 100, 48.5, 0); -// Location loc = entity.getLocation(); -// if (loc.distance(centerLoc) <= 1) { -// if (!velocitiesMap.containsKey(entity)) { -// getLogger().info("add velocity falling block" + entity); -// int index = counter % 4; -// getLogger().info("[1]" + entity); -// counter++; -// getLogger().info("[2]" + entity); -// Vector velocity = velocities[index]; -// getLogger().info("[3]" + entity); -// entity.setVelocity(velocity); -// getLogger().info("[4]" + entity); -// velocitiesMap.put(entity, velocity); // Store the velocity in the map -// getLogger().info("[5]" + entity); -// movingBlocks.add(entity.getLocation()); // Add the location to the set -// getLogger().info("[6]" + entity); -// } -// } -// } -// } - @EventHandler - public void onBlockBreak(BlockBreakEvent event) { - Block block = event.getBlock(); - if (block.getWorld().getEnvironment() == World.Environment.THE_END) { - // The block is in the End world - if (block.getBlockData() instanceof FallingBlock) { - // The block that was broken is a falling block - String message = "I found a falling block in the End!"; - Bukkit.broadcastMessage(message); - } - } - } - - - - - - @EventHandler public void onFallingBlockToBlock(EntityChangeBlockEvent e){ if(e.getEntityType() == EntityType.FALLING_BLOCK){ @@ -127,24 +111,24 @@ public class FoliaFlow extends JavaPlugin implements Listener { Block movingTo = getBlockMovingTo(loc, vel); try { - if(movingTo != null && movingTo.getType() == Material.END_PORTAL){ - Location spawnLoc = movingTo.getLocation(); - spawnLoc.setX(spawnLoc.getX()+0.5); - spawnLoc.setY(spawnLoc.getY()-0.25); - spawnLoc.setZ(spawnLoc.getZ()+0.5); + if(movingTo != null && movingTo.getType() == Material.END_PORTAL){ + Location spawnLoc = movingTo.getLocation(); + spawnLoc.setX(spawnLoc.getX()+0.5); + spawnLoc.setY(spawnLoc.getY()-0.25); + spawnLoc.setZ(spawnLoc.getZ()+0.5); - FallingBlock dummy = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData()); - dummy.setDropItem(true); - dummy.setHurtEntities(true); - dummy.setGravity(true); - Vector dummyVel = vel.clone(); - dummyVel.setY(-dummyVel.getY()); - dummyVel.multiply(new Vector(2, 1, 2)); + FallingBlock dummy = loc.getWorld().spawnFallingBlock(spawnLoc, ((FallingBlock) entity).getBlockData()); + dummy.setDropItem(true); + dummy.setHurtEntities(true); + dummy.setGravity(true); + Vector dummyVel = vel.clone(); + dummyVel.setY(-dummyVel.getY()); + dummyVel.multiply(new Vector(2, 1, 2)); - dummyVel.add(new Vector(0, 1, 0)); + dummyVel.add(new Vector(0, 1, 0)); - dummy.setVelocity(dummyVel); - } + dummy.setVelocity(dummyVel); + } } catch (NullPointerException dummy) { getServer().getLogger().info("onFallingBlockToBlock erorr (likly chunky it not load)"); }