How to move entities to each block in a region one at a time.

Discussion in 'Spigot Plugin Development' started by Emperor_Koala, Jan 14, 2019.

  1. The title says it all.

    I'm trying to create a plugin that moves entities to each block in a region one at a time. The problem I'm running into is that I can't just use for-loops in conjunction with "Thread.sleep()" because I am expecting lots of these loops to all be running at once, and I imagine that creating a new thread for each one would be a disaster for the server.

    So my question: Is there a way to move entities around within a region such that the behavior acts like for-loops (i.e. going across and scanning line by line), while also not completely killing the server by creating threads for each loop?

    Another quick note: The regions themselves are not predefined. Instead, the regions are determined when the entity spawns by using width, depth, and height values to select the region in front of the entity.

    Any help is appreciated. Thanks!
  2. I must say, I'm really not following what you're trying to achieve.

    It sounds like you've got a bunch of entities and you want them each in a specific location.
    What do you mean by "one at a time"? Whatever you do, it'll always be one at a time. One gets teleported. Then the next. And so on.
    Do you want a delay between the entities appearing?

    If it's the delay you're looking for, you should make use of BukkitScheduler.
  3. What about the included scheduler in spigot?... I can't say for sure, but I'd assume that uses a pool of worker threads / list of delayed tasks. Still not ideal if you're doing thousands of tasks, but it's probably possible to do this with only a few task timers.

    EDIT: Ninja'd
  4. I don't know if this is what your looking for but maby take a look at this.

    Lol just noticed that 3 people give the same awnser
    • Funny Funny x 1
  5. What I'm looking for is this:

    Imagine I'm handling only one entity. When I spawn in that entity, I then use width, height, and depth values to select the region in front of that entity. From there, I need some way to move the entity to each block in that region, with a one-second delay between each movement.

    If I only ever expected there to be one entity running in this loop, it wouldn't be a problem. I would just put it on its own thread and use Thread.sleep(). I can't do this without causing memory and performance issues for the server, because I am expecting several of these types of entities to be active at any given time.

    So what I need to be able to do is take an entity, give it a region, and teleport it to each block in that region with a delay between each teleport.

    The part that I am trying to figure out is how to teleport the entity to each block. The delay I figure I could do with the Bukkit Scheduler and Runnable and stuff.

    Hopefully, that helps clear things up a bit.
  6. So... something like this?... I'm still having a hard time understanding what you're trying to do.
    Code (Java):
    public class RegionRunnable extends BukkitRunnable {
            LivingEntity entity;
            double maxX;
            double maxZ;
            double x;
            double z;

            public RegionRunnable(LivingEntity entity, Location min, Location max) {
                this.entity = entity;
                maxX = max.getX();
                maxZ = max.getZ();
                x = min.getX();
                z = min.getZ();

            public void run() {
                if (x <= maxX) {
                    if (z <= maxZ) {
                    } else z++;
                } else x++;
                entity.teleport(new Location(entity.getWorld(), x, 100, z));

Share This Page