Solved Stop frame freezing

Discussion in 'Spigot Plugin Development' started by JewishBanana, Sep 24, 2021.

  1. Alright so I have this code running on an async task
    Code (Text):
    Player temp = (Player) Bukkit.getOnlinePlayers().toArray()[rand.nextInt(Bukkit.getOnlinePlayers().size())];
    Location place = new Location(world, temp.getLocation().getBlockX()+(rand.nextInt(30)-15), temp.getLocation().getBlockY(), temp.getLocation().getBlockZ()+(rand.nextInt(30)-15));
    Location spawn = new Location(world, temp.getLocation().getBlockX()+(rand.nextInt(100)-50), temp.getLocation().getBlockY()+70, temp.getLocation().getBlockZ()+(rand.nextInt(100)-50));
    list.add(new Meteor(spawn, place, rand.nextInt(5)+1, 1, world, list));
    And since its on an async task it should not affect the servers performance, however every time this code runs it causes a small lag spike which is really annoying. I think it has something to do with the thread syncing but if I run it on the main thread it has the exact same lag spike no improvement. This is the structure for creating a new Meteor
    Code (Text):
    public Meteor(Location loc, Location dest, int size, int type, World world, List<Meteor> list) {
            this.loc = loc;
            this.type = type;
            this.size = size;
   = world;
            if (type == 1) material = Material.DEEPSLATE;
            else if (type == 2) material = Material.MAGMA_BLOCK;
            else material = Material.BASALT;
            this.speed = 1.2 - (size / 10);
            this.depth = size * 5;
            this.list = list;
            this.vec = new Vector(loc.getX() - dest.getX(), loc.getY() - dest.getY(), loc.getZ() - dest.getZ()).normalize().multiply(-speed);
            this.squared = size * size;
    If anyone has a suggestion for improving performance please let me know. Also, this code is only run on loaded chunks so it is thread-safe and I am not getting any errors before you suggest something against async.
  2. Try find random player with:

    Code (Text):

    for (Player all : Bukkit.getOnlinePlayer() {
    • Like Like x 1
  3. Is this lag spike server performance or client lag? I am just curious since the name of the thread includes 'Frame.' Off the top of my head, I see no real issue with your code. Maybe cache temp's location as a variable so that you have even less to worry about regarding thread safety? Though, CraftEntity's getLocation method is hardly taxing.
    Have you tried running any timings reports?
    I see this is a 'Meteor' class. Does this spawn a block high up? A falling block entity wouldn't cause a scenario such as FPS lag, but I know that clients lag tremendously when there is a light update from above due to blocks. Especially so when the floor can't be seen.
    • Informative Informative x 1
  4. Since you are executing this code in another thread it can throw ArrayIndexOutOfBoundIndex when a player logs out whilst rand already got a value that has not been passed to the toArray()[] element index
    Did you try /timings paste to see what's going on?
  5. Yes it is client lag and I did not think about that, I will try using falling blocks instead and see how that goes.
    No, it won't.

    Edit: @Keehl254 I switched it to falling blocks and it is sooo much smoother and performance-friendly thanks so much for the idea!
    #5 JewishBanana, Sep 24, 2021
    Last edited: Sep 25, 2021
    • Friendly Friendly x 1
  6. You'll have a surprise... probably once a month, or less. Anyway, glad you had your issue resolved
  7. No, you won't have any issues lol.

    The low level implementation of the player list is a copyonwritearraylist.

    And you wouldn't get an out of bounds but a concurrent modification exception in a case like this.
  8. It could be either error but for what I am doing a case like this would be impossible, I only shared a snippet of the method.
  9. The player is only called within the same task so that will never happen.