static wait

Lego12

New member
Aug 9, 2023
5
1
1
the plugin works fine but there is spam in the console:


Code:
[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.TeleportUtils.generateLocation(TeleportUtils.java:47)

[15:09:06 ERROR]:               RandomTP-1.0-SNAPSHOT.jar//me.lego12.randomtp.Commands.RandomTPCommand.onCommand(RandomTPCommand.java:26)

[15:09:06 ERROR]:               org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)

[15:09:06 ERROR]:               org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:155)

[15:09:06 ERROR]:               org.bukkit.craftbukkit.v1_20_R1.CraftServer.dispatchCommand(CraftServer.java:1007)

[15:09:06 ERROR]:               org.bukkit.craftbukkit.v1_20_R1.command.BukkitCommandWrapper.run(BukkitCommandWrapper.java:64)

[15:09:06 ERROR]:               com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:265)

[15:09:06 ERROR]:               net.minecraft.commands.Commands.performCommand(Commands.java:324)

[15:09:06 ERROR]:               net.minecraft.commands.Commands.performCommand(Commands.java:308)

[15:09:06 ERROR]:               net.minecraft.server.network.ServerGamePacketListenerImpl.performChatCommand(ServerGamePacketListenerImpl.java:2301)

[15:09:06 ERROR]:               net.minecraft.server.network.ServerGamePacketListenerImpl.lambda$handleChatCommand$20(ServerGamePacketListenerImpl.java:2261)

[15:09:06 ERROR]:               net.minecraft.server.network.PlayerConnection$$Lambda$6829/0x000002c102197820.run(Unknown Source)

[15:09:06 ERROR]:               net.minecraft.util.thread.BlockableEventLoop.lambda$submitAsync$0(BlockableEventLoop.java:59)

[15:09:06 ERROR]:               net.minecraft.util.thread.IAsyncTaskHandler$$Lambda$6830/0x000002c102197a38.get(Unknown Source)

[15:09:06 ERROR]:               [email protected]/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)

[15:09:06 ERROR]:               net.minecraft.server.TickTask.run(TickTask.java:18)

[15:09:06 ERROR]:               net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153)

[15:09:06 ERROR]:               net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1339)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.d(MinecraftServer.java:197)

[15:09:06 ERROR]:               net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1316)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1309)

[15:09:06 ERROR]:               net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1386)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1171)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:318)

[15:09:06 ERROR]:               net.minecraft.server.MinecraftServer$$Lambda$4637/0x000002c101a2bbb8.run(Unknown Source)

[15:09:06 ERROR]:               [email protected]/java.lang.Thread.runWith(Thread.java:1636)

[15:09:06 ERROR]:               [email protected]/java.lang.Thread.run(Thread.java:1623)

[15:09:06 ERROR]: ------------------------------

[15:09:06 ERROR]: --- DO NOT REPORT THIS TO PAPER - THIS IS NOT A BUG OR A CRASH ---

[15:09:06 ERROR]: ------------------------------

[15:09:08 WARN]: Can't keep up! Is the server overloaded? Running 24705ms or 494 ticks behind
[/CODE]

JavaScript:
package me.lego12.randomtp;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import org.bukkit.Location;

import java.util.HashSet;
import java.util.Random;

public class TeleportUtils {

    static Main plugin;

    public TeleportUtils(Main plugin) {
        this.plugin = plugin;
    }

    static HashSet<Material> bad_blocks = new HashSet<>();

    static {
        bad_blocks.add(Material.LAVA);
        bad_blocks.add(Material.WATER);
    }

    public static Location generateLocation(Player p) {

        Random random = new Random();

        int size = plugin.getConfig().getInt("border")/2;

        int x = random.nextInt(size)*(random.nextBoolean() ?-1:1);
        int y = 100;
        int z = random.nextInt(size)*(random.nextBoolean() ?-1:1);


        Location randomLocation = new Location(p.getWorld(), x, y, z);

        y = randomLocation.getWorld().getHighestBlockYAt(randomLocation);
        randomLocation.setY(y);

 
        if (isLocationSafe(randomLocation)){//This is where the loop is
            p.sendMessage("1 attempt to find a safe area to teleport, wait a few seconds.");
            randomLocation = generateLocation(p);
            //put wait here
        }else{
            return randomLocation;
        }
        return randomLocation;
    }

    public static boolean isLocationSafe(Location location){

        int x = location.getBlockX();
        int y = location.getBlockY();
        int z = location.getBlockZ();

        Block block = location.getWorld().getBlockAt(x, y, z);
        Block below = location.getWorld().getBlockAt(x, y-1, z);
        Block above = location.getWorld().getBlockAt(x, y+1, z);



        return (bad_blocks.contains(below.getType()) || (block.getType().isSolid()) || (above.getType().isSolid()));
    }

}


loop close up:

JavaScript:
        if (isLocationSafe(randomLocation)){
            p.sendMessage("1 attempt to find a safe area to teleport, wait a few seconds.");
            randomLocation = generateLocation(p);
            //put wait here
        }else{
            return randomLocation;
        }
        return randomLocation;
 
Version Output
1.20.1

stefvanschie

Moderator
Staff member
Dec 17, 2021
115
7
18
18
This is probably because you're loading chunks and the server can't keep up. I'd recommend loading the chunks asynchronously, so they do not halt the main server thread.