PolyhedralDev / Terra

Voxel world generation modding platform
MIT License
670 stars 88 forks source link

[Bug] Entities can spawn outside the border and cause crashes #466

Closed MorningMC closed 2 months ago

MorningMC commented 2 months ago

Pre-Issue Checklist

Environment

Name Value
Terra Version
Platform / Platform Version Folia
Any External Plugins or Mods
Terra Packs In Use
Terra Addons In Use

THIS IS NOT MY SERVER, I DONT HAVE DETAILS

Issue Description

Entities can spawn outside the border and cause crashes.

Steps to reproduce

  1. get to the border and load chunks.
  2. a bee or sth outside the border.
  3. crashes.

Expected behavior

Players should get to the border normally.

Actual behavior

When I get to the border and there is a entity outside the border, it actually crashes.

Full stacktrace

Exception Stacktrace ``` [11:07:12] [Tuinity Chunk System Worker #2/ERROR]: [ChunkTaskScheduler] Chunk system error at chunk (1875006,-1874553), holder: NewChunkHolder{world=world, chunkX=1875006, chunkZ=-1874553, entityChunkFromDisk=false, lastChunkCompletion={chunk_class=net.minecraft.world.level.chunk.ProtoChunk,status=minecraft:carvers}, currentGenStatus=minecraft:carvers, requestedGenStatus=minecraft:initialize_light, generationTask=ChunkProgressionTask{class: io.papermc.paper.chunk.system.scheduling.ChunkUpgradeGenericStatusTask, for world: world, chunk: (1875006,-1874553), hashcode: 1710002003, priority: COMPLETING, status: minecraft:features, scheduled: true}, generationTaskStatus=minecraft:features, priority=NORMAL, priorityLocked=false, neighbourRequestedPriority=NORMAL, effective_priority=NORMAL, oldTicketLevel=34, currentTicketLevel=34, totalNeighboursUsingThisChunk=15, fullNeighbourChunksLoadedBitset=0, chunkStatusRaw=0, currentChunkStatus=INACCESSIBLE, pendingChunkStatus=INACCESSIBLE, is_unload_safe=ticket_level, killed=false}, exception: -- 2 | java.lang.Throwable: java.lang.IllegalStateException: Entity EntityBee['Bee'/205216, uuid='9e6132d4-6a14-49e8-83b0-35f76a429d8f', l='ServerLevel[world]', x=30000000.00, y=125.00, z=-29992839.50, cpos=[1875000, -1874553], tl=0, v=false] is not in the region 3 | at io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.unrecoverableChunkSystemFailure(ChunkTaskScheduler.java:279) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 4 | at io.papermc.paper.chunk.system.scheduling.NewChunkHolder.lambda$setGenerationTask$4(NewChunkHolder.java:1700) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 5 | at io.papermc.paper.chunk.system.scheduling.ChunkProgressionTask.complete0(ChunkProgressionTask.java:95) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 6 | at io.papermc.paper.chunk.system.scheduling.ChunkProgressionTask.complete(ChunkProgressionTask.java:75) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 7 | at io.papermc.paper.chunk.system.scheduling.ChunkUpgradeGenericStatusTask.run(ChunkUpgradeGenericStatusTask.java:105) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 8 | at io.papermc.paper.chunk.system.scheduling.queue.RadiusAwarePrioritisedExecutor$Task.run(RadiusAwarePrioritisedExecutor.java:467) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 9 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue$PrioritisedTask.executeInternal(PrioritisedThreadedTaskQueue.java:351) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 10 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue.executeTask(PrioritisedThreadedTaskQueue.java:118) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 11 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadPool$PrioritisedThread.pollTasks(PrioritisedThreadPool.java:274) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 12 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedQueueExecutorThread.run(PrioritisedQueueExecutorThread.java:50) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 13 | Caused by: java.lang.IllegalStateException: Entity EntityBee['Bee'/205216, uuid='9e6132d4-6a14-49e8-83b0-35f76a429d8f', l='ServerLevel[world]', x=30000000.00, y=125.00, z=-29992839.50, cpos=[1875000, -1874553], tl=0, v=false] is not in the region 14 | at com.google.common.base.Preconditions.checkState(Preconditions.java:601) ~[guava-32.1.2-jre.jar:?] 15 | at org.bukkit.craftbukkit.v1_20_R3.generator.CraftLimitedRegion.saveEntities(CraftLimitedRegion.java:117) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 16 | at net.minecraft.world.level.chunk.ChunkGenerator.applyBiomeDecoration(ChunkGenerator.java:492) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 17 | at org.bukkit.craftbukkit.v1_20_R3.generator.CustomChunkGenerator.a(CustomChunkGenerator.java:300) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 18 | at com.dfsek.terra.bukkit.nms.v1_20_R3.NMSChunkGeneratorDelegate.a(NMSChunkGeneratorDelegate.java:76) ~[Terra-bukkit-6.4.3-BETA+ab60f14ff-shaded.jar:?] 19 | at net.minecraft.world.level.chunk.ChunkStatus.lambda$static$11(ChunkStatus.java:127) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 20 | at net.minecraft.world.level.chunk.ChunkStatus$SimpleGenerationTask.doWork(ChunkStatus.java:342) ~[?:?] 21 | at net.minecraft.world.level.chunk.ChunkStatus.generate(ChunkStatus.java:259) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 22 | at io.papermc.paper.chunk.system.scheduling.ChunkUpgradeGenericStatusTask.run(ChunkUpgradeGenericStatusTask.java:86) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 23 | ... 5 more 24 | [11:07:13] [Region Scheduler Thread #16/ERROR]: [io.papermc.paper.threadedregions.TickRegionScheduler] Region #3785 centered at chunk [1874998, -1874560] in world 'world' failed to tick: 25 | java.lang.RuntimeException: Chunk system crash propagated from unrecoverableChunkSystemFailure 26 | at io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.lambda$unrecoverableChunkSystemFailure$1(ChunkTaskScheduler.java:302) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 27 | at io.papermc.paper.threadedregions.RegionizedTaskQueue$PrioritisedQueue$ChunkBasedPriorityTask.executeInternal(RegionizedTaskQueue.java:504) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 28 | at io.papermc.paper.threadedregions.RegionizedTaskQueue$PrioritisedQueue.executeTask(RegionizedTaskQueue.java:434) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 29 | at io.papermc.paper.threadedregions.RegionizedTaskQueue$RegionTaskQueueData.drainTasks(RegionizedTaskQueue.java:264) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 30 | at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1634) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 31 | at io.papermc.paper.threadedregions.TickRegions$ConcreteRegionTickHandle.tickRegion(TickRegions.java:408) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 32 | at io.papermc.paper.threadedregions.TickRegionScheduler$RegionScheduleHandle.runTick(TickRegionScheduler.java:404) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 33 | at ca.spottedleaf.concurrentutil.scheduler.SchedulerThreadPool$TickThreadRunner.run(SchedulerThreadPool.java:525) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 34 | at java.lang.Thread.run(Thread.java:1583) ~[?:?] 35 | Caused by: net.minecraft.ReportedException: Chunk system error 36 | at io.papermc.paper.chunk.system.scheduling.ChunkTaskScheduler.unrecoverableChunkSystemFailure(ChunkTaskScheduler.java:285) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 37 | at io.papermc.paper.chunk.system.scheduling.NewChunkHolder.lambda$setGenerationTask$4(NewChunkHolder.java:1700) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 38 | at io.papermc.paper.chunk.system.scheduling.ChunkProgressionTask.complete0(ChunkProgressionTask.java:95) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 39 | at io.papermc.paper.chunk.system.scheduling.ChunkProgressionTask.complete(ChunkProgressionTask.java:75) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 40 | at io.papermc.paper.chunk.system.scheduling.ChunkUpgradeGenericStatusTask.run(ChunkUpgradeGenericStatusTask.java:105) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 41 | at io.papermc.paper.chunk.system.scheduling.queue.RadiusAwarePrioritisedExecutor$Task.run(RadiusAwarePrioritisedExecutor.java:467) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 42 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue$PrioritisedTask.executeInternal(PrioritisedThreadedTaskQueue.java:351) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 43 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue.executeTask(PrioritisedThreadedTaskQueue.java:118) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 44 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadPool$PrioritisedThread.pollTasks(PrioritisedThreadPool.java:274) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 45 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedQueueExecutorThread.run(PrioritisedQueueExecutorThread.java:50) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 46 | Caused by: java.lang.IllegalStateException: Entity EntityBee['Bee'/205216, uuid='9e6132d4-6a14-49e8-83b0-35f76a429d8f', l='ServerLevel[world]', x=30000000.00, y=125.00, z=-29992839.50, cpos=[1875000, -1874553], tl=0, v=false] is not in the region 47 | at com.google.common.base.Preconditions.checkState(Preconditions.java:601) ~[guava-32.1.2-jre.jar:?] 48 | at org.bukkit.craftbukkit.v1_20_R3.generator.CraftLimitedRegion.saveEntities(CraftLimitedRegion.java:117) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 49 | at net.minecraft.world.level.chunk.ChunkGenerator.applyBiomeDecoration(ChunkGenerator.java:492) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 50 | at org.bukkit.craftbukkit.v1_20_R3.generator.CustomChunkGenerator.a(CustomChunkGenerator.java:300) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 51 | at com.dfsek.terra.bukkit.nms.v1_20_R3.NMSChunkGeneratorDelegate.a(NMSChunkGeneratorDelegate.java:76) ~[Terra-bukkit-6.4.3-BETA+ab60f14ff-shaded.jar:?] 52 | at net.minecraft.world.level.chunk.ChunkStatus.lambda$static$11(ChunkStatus.java:127) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 53 | at net.minecraft.world.level.chunk.ChunkStatus$SimpleGenerationTask.doWork(ChunkStatus.java:342) ~[?:?] 54 | at net.minecraft.world.level.chunk.ChunkStatus.generate(ChunkStatus.java:259) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 55 | at io.papermc.paper.chunk.system.scheduling.ChunkUpgradeGenericStatusTask.run(ChunkUpgradeGenericStatusTask.java:86) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 56 | at io.papermc.paper.chunk.system.scheduling.queue.RadiusAwarePrioritisedExecutor$Task.run(RadiusAwarePrioritisedExecutor.java:467) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 57 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue$PrioritisedTask.executeInternal(PrioritisedThreadedTaskQueue.java:351) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 58 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadedTaskQueue.executeTask(PrioritisedThreadedTaskQueue.java:118) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 59 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedThreadPool$PrioritisedThread.pollTasks(PrioritisedThreadPool.java:274) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 60 | at ca.spottedleaf.concurrentutil.executor.standard.PrioritisedQueueExecutorThread.run(PrioritisedQueueExecutorThread.java:50) ~[luminol-1.20.4.jar:git-Luminol-"5a6f4bb"] 61 | [11:07:13] [Region shutdown thread/INFO]: [RegionShutdownThread] Awaiting scheduler termination for 60s 62 | [11:07:13] [Region shutdown thread/INFO]: [RegionShutdownThread] Scheduler halted 63 | [11:07:13] [Region shutdown thread/INFO]: Stopping server ```

Additional details

Our resolution:

package minecraft.morningmc.beefix;

import io.papermc.paper.event.entity.EntityMoveEvent;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.*;

/**
 * The event listener of the plugin.
 */
public class Listener implements org.bukkit.event.Listener {
    private final int border;

    /**
     * Constructs a new {@link Listener} instance.
     *
     * @param border The maximum x and z coordinate that entities can be before being removed.
     */
    public Listener(int border) {
        this.border = border;
    }

    /**
     * Remove entities that are outside the border. This is done in a separate thread to prevent lag.
     *
     * @param event The chunk load event.
     */
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onChunkLoad(ChunkLoadEvent event) {
        if (event.isNewChunk()) {
            Chunk chunk = event.getChunk();
            if (chunk.getX() >= border >> 5 || chunk.getZ() >= border >> 5) { // check if the chunk is next to the border
                Arrays.stream(chunk.getEntities())
                        .parallel()
                        .filter(entity -> entity.getX() >= border || entity.getZ() >= border)
                        .forEach(Entity::remove);
            }
        }
    }

    /**
     * Cancels the spawn of entities outside the border.
     *
     * @param event The entity spawn event.
     */
    @EventHandler
    public void onEntitySpawn(EntitySpawnEvent event) {
        Location location = event.getLocation();
        if (location.getX() >= border || location.getZ() >= border || location.getX() <= -border || location.getZ() <= -border) {
            event.setCancelled(true);
        }
    }

    /**
     * Teleports entities outside the border back to the border.
     *
     * @param event The entity move event.
     */
    @EventHandler
    public void onEntityMove(EntityMoveEvent event) {
        Location to = event.getTo();
        if (to.getX() >= border || to.getZ() >= border || to.getX() <= -border || to.getZ() <= -border) {
            Location actuallyTo = new Location(to.getWorld(), limit(to.getX(), border, -border), to.getY(), limit(to.getZ(), border, -border));
            event.setTo(actuallyTo);
        }
    }

    /**
     * Limits a value between a maximum and a minimum.
     *
     * @param value   The value to be limited.
     * @param maximum The maximum value.
     * @param minimum The minimum value.
     * @return The limited value.
     */
    private double limit(double value, double maximum, double minimum) {
        return Math.max(Math.min(value, maximum), minimum);
    }
}
solonovamax commented 2 months ago
MorningMC commented 2 months ago

idk the server has closed

solonovamax commented 2 months ago

Closing due to lack of information & being unable to provide more.