Solved Searching large radius near player... Async?

Discussion in 'Spigot Plugin Development' started by moosecanoes, Jun 13, 2016.

  1. Currently trying to search near player to get nearest spawner, and teleport. I realize it's a heavy load, but I tried to run the task asynchronously and it failed, probably because I'm running commands of the Bukkit API.

    Any help is appreciated.

    Code (Text):
     if (command.getName().equalsIgnoreCase("random"))
            {
                World world = player.getWorld();
               final BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
             
                if (args[0] != null)
                {
                    try
                    {
                        double xxx = 1000;
                        player.sendMessage(ChatColor.YELLOW + "Searching please wait...");
                        Location e = player.getLocation();
                        int x = e.getBlockX() - (int)xxx;
                        int y = e.getBlockY() - (int)xxx;
                     
                        int xx = 0;
                        int yy = 0;
                        int zz = 0;
                     
                        for (zz = 0; zz < 256; zz++)
                        {
                        for (xx = x; xx < (x + (xxx * 2)); xx++)
                        {
                         if (xx <= e.getBlockX() + 5 && xx >= e.getBlockX() - 5) continue;
                        for (yy = y; yy < (y + (xxx * 2)); yy++)
                        {
                            if (yy <= e.getBlockY() + 5 && xx >= e.getBlockY() - 5) continue;
                            Block b = world.getBlockAt(xx, yy, zz);
                            if (b.getType() == (Material.MOB_SPAWNER))
                            {
                                        player.teleport(b.getLocation());
                                        return true;    
                                    }
                            }
                        }
                        }
                        player.sendMessage("NO spawner found in area");
                    }
                    catch (Exception e)
                    {
                        player.sendMessage(e.getMessage() + e.getLocalizedMessage());
                    }
                }
                return false;
            }
     
    #1 moosecanoes, Jun 13, 2016
    Last edited: Jun 13, 2016
  2. You haven't used the scheduler.

    I would use a sync repeating task. With that there is no problem with the bukkit api and tps drops.

    Code (Text):
    int id = Bukkit.getScheduler().scheduleSyncRepeatingTask(yourPlugin, new Runnable() {
                @Override
                public void run() {
                    //This Code will be executed every "timebetweencalls". After "delaytillfirststart" for the first time.
                }
            },delaytillfirststart,timebetweencalls);
    And stop it again:
    Code (Text):
    Bukkit.getScheduler().cancelTask(id);
    But you have to store your current lookup position somewhere out of this method. And be carefull if this command get executed more than once at the same time.
     
  3. This is what I use to get all entities in a region for my copy paste system.
    Code (Text):

    //minl is the well chunk at smaller coordinates
    //maxl is the chunk with higher coordinates if you know what I mean by that XD
    for (int x = minl.getX(); x <= maxl.getX(); x++)
    {
        for (int z = minl.getZ(); z <= maxl.getZ(); z++)
        {
            for (org.bukkit.entity.Entity e : loc.getWorld().getChunkAt(x, z).getTileEntities())
            {
                //Stuff
            }
        }
    }
    You could maybe put this in an async thread with a delay and such. . .
     
  4. Code (Text):
    //you wil need a command if your looking for one so


    Public bloom

    And the commands
     
  5. If you know what mean xD
     
  6. Thanks it's working now with titleEntities! Now to find out if I can filter by type of spawner...
     
  7. Try
    Code (Text):

    if (tileEntity instanceof TileEntityMobSpawner)
    {
        //More stuff!
    }