RandomTP

RandomTP

265k Downloads

[BUG] Server lags when command is ran

ItsaGlitch1 opened this issue ยท 3 comments

commented

Describe the bug
When running the command on a fabric server, the server hangs for around 5-10 seconds. During that time, the player is teleported, so if the player teleports into a block, they would be dead from suffocation before they realize. Also, other players will experience server lag too when someone runs /rtp

To Reproduce
Steps to reproduce the behavior:

  1. run a fabric server with the plugin
  2. run /rtp

Expected behavior
The server doesn't lag when the command is ran.

Desktop (please complete the following information):

  • RandomTP version: 7.1.0
  • Minecraft Version: 1.19.2

Additional context
Server is hosted by Shockbyte using the 8GB plan.
I think this happens because of the mutliple checks for safe locations.

I think to fix it, take the first set of coords that /rtp generated and change the y position to max height and prevent the player from taking fall damage for a few seconds, or check to see if a y position has a block above it, if not put the player there. (Like using the /top command in essential commands)
These methods will ensure the player is on the surface, but only works in open sky dimentions (unlike the nether, which has a ceiling)

commented

This definitely seems interesting. I am going to take a look into it and give feedback soon.

commented

I asked ChatGPT to improve

private static Pair<Integer, Integer> generateCoordinates(ServerLevel world, Player player, Random random) {

Simplify and unify the code for calculating X and Z coordinates: The current code has two separate branches for calculating X and Z coordinates, with the only difference being the direction (min to max or max to min). We can extract this logic into a separate function and reuse it for both X and Z calculations.

Avoid using the random.ints(minDistance, maxDistance).findAny().getAsInt() approach, as it can generate an IntStream, which is unnecessary for generating a single random integer. Instead, we can use random.nextInt(maxDistance - minDistance) + minDistance to directly generate a single random integer within the desired range.

private static Pair<Integer, Integer> generateCoordinates(ServerLevel world, Player player, Random random) {
    int x = generateCoordinate(random, world.getWorldBorder().getMinX(), world.getWorldBorder().getMaxX(),
                               player.getX(), Config.getMinDistance(), Config.getMaxDistance());
    int z = generateCoordinate(random, world.getWorldBorder().getMinZ(), world.getWorldBorder().getMaxZ(),
                               player.getZ(), Config.getMinDistance(), Config.getMaxDistance());
    return new Pair<>(x, z);
}

private static int generateCoordinate(Random random, double minWorldBorder, double maxWorldBorder,
                                      double playerPos, int minDistance, int maxDistance) {
    int distance = maxDistance == 0 ? (int) minWorldBorder : maxDistance;
    int maxDist = (int) (playerPos + distance);
    int minDist = (int) (playerPos - minDistance);

    if (random.nextInt(2) == 1) {
        maxDist = Math.max(maxDist, (int) minWorldBorder);
        minDist = Math.max(minDist, (int) minWorldBorder + 10);
    } else {
        maxDist = Math.min(maxDist, (int) maxWorldBorder);
        minDist = Math.min(minDist, (int) maxWorldBorder - 10);
    }

    if (maxDist < minDist) {
        int temp = maxDist;
        maxDist = minDist;
        minDist = temp;
    }
    if (maxDist == minDist) {
        minDist -= 1;
    }
    return random.nextInt(maxDist - minDist) + minDist;
}
commented

I agree that the RandomTP mod does still have some speed issues but the mod has improved a lot since its start. Version 7.2.0 was a huge improvement from 7.1.0 so I would suggest you update it. Unfortunately, you can not use the latest version of Minecraft 1.19.2 so I guess you won't be able to update. I will implement some of the Chat GPT suggestions and search more about how to fix this. I will keep this issue open until I think RandomTP is in a state where lag is no longer a priority.