chunk load my best friend
This commit is contained in:
parent
f3168eefa0
commit
cc112aeb29
1 changed files with 32 additions and 23 deletions
|
|
@ -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,26 +98,33 @@ 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();
|
||||||
Location loc = entity.getLocation();
|
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();
|
||||||
|
|
||||||
debug("Falling block spawned at location " + loc);
|
debug("Falling block spawned at location " + loc);
|
||||||
|
|
||||||
// Set the initial velocity of the falling block
|
// Set the initial velocity of the falling block
|
||||||
int index = counter % 4;
|
int index = counter % 4;
|
||||||
counter ++;
|
counter ++;
|
||||||
Vector velocity = velocities[index];
|
Vector velocity = velocities[index];
|
||||||
entity.setVelocity(velocity);
|
entity.setVelocity(velocity);
|
||||||
|
|
||||||
// Remove the block from the movingBlocks set after a delay, to prevent it from being immediately moved again
|
// Remove the block from the movingBlocks set after a delay, to prevent it from being immediately moved again
|
||||||
getServer().getScheduler().runTaskLater(this, () -> movingBlocks.remove(loc.getBlock().getLocation()), 20L);
|
getServer().getScheduler().runTaskLater(this, () -> movingBlocks.remove(loc.getBlock().getLocation()), 20L);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Block getBlockMovingTo(Location loc, Vector vel) {
|
Block getBlockMovingTo(Location loc, Vector vel) {
|
||||||
double absMax = 0, max = 0;
|
double absMax = 0, max = 0;
|
||||||
char dir = ' ';
|
char dir = ' ';
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue