chunk load my best friend

This commit is contained in:
MidnightTale 2023-04-13 00:18:03 +07:00
commit cc112aeb29

View file

@ -1,6 +1,5 @@
package xyz.hynse.foliaflow; package xyz.hynse.foliaflow;
import io.papermc.paper.event.entity.EntityMoveEvent;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -9,7 +8,7 @@ import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent; import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -54,14 +53,17 @@ public class FoliaFlow extends JavaPlugin implements Listener {
} }
@EventHandler @EventHandler
public void onEntityMove(EntityMoveEvent event) { public void onChunkLoad(ChunkLoadEvent event) {
handleMove(event.getEntity().getLocation()); Block obsidianBlock = obsidianLocation.getBlock();
if (event.getWorld() == endWorld && obsidianBlock.getType() == Material.OBSIDIAN) { // check if the chunk is in the end and the block is obsidian
obsidianBlock.setType(Material.AIR); // set the block to air
}
} }
private void handleMove(Location location) { @EventHandler
if (location.getWorld() == endWorld && location.distance(obsidianLocation) <= 1.0) { // check if the location is in the end and within 1 block of the obsidian platform public void onBlockChange(org.bukkit.event.block.BlockFromToEvent event) {
Block obsidianBlock = obsidianLocation.getBlock(); if (event.getBlock().getLocation().equals(obsidianLocation)) { // check if the block change event is for the obsidian block location
obsidianBlock.setType(Material.AIR); // set the block to air event.setCancelled(true); // cancel the event to prevent the obsidian block from changing
} }
} }
@ -96,9 +98,13 @@ public class FoliaFlow extends JavaPlugin implements Listener {
@EventHandler @EventHandler
public void onFallingBlockSpawn(EntitySpawnEvent e) { public void onChunkLoadend(ChunkLoadEvent e) {
if (e.getEntityType() == EntityType.FALLING_BLOCK && e.getEntity().getWorld().getEnvironment() == World.Environment.THE_END) { Chunk chunk = e.getChunk();
Entity entity = e.getEntity(); World world = chunk.getWorld();
if (world.getEnvironment() == World.Environment.THE_END) {
// Iterate through all the entities in the chunk
for (Entity entity : chunk.getEntities()) {
if (entity.getType() == EntityType.FALLING_BLOCK) {
Location loc = entity.getLocation(); Location loc = entity.getLocation();
debug("Falling block spawned at location " + loc); debug("Falling block spawned at location " + loc);
@ -113,6 +119,9 @@ public class FoliaFlow extends JavaPlugin implements Listener {
getServer().getScheduler().runTaskLater(this, () -> movingBlocks.remove(loc.getBlock().getLocation()), 20L); getServer().getScheduler().runTaskLater(this, () -> movingBlocks.remove(loc.getBlock().getLocation()), 20L);
} }
} }
}
}