Solved Searching for a structure in a cuboid region

Discussion in 'Spigot Plugin Development' started by Nykorrin, Jun 30, 2021.

  1. Hi so I'm trying to prevent my players from claiming strongholds, end portals, and gateways. However, looping through all the cuboids blocks and checking for the material is crashing my server/causing lag (obv). I was wondering how I might be able to just check if the cuboid contains a location that contains a structure.

    Any ideas?

    This is my method I was using to search for the material:
    Code (Java):
    public boolean containsMaterial(Material material) {
            for (Block b : this) {
                if (b.getType() == material) {
                    return true;
                }
            }
            return false;
        }
    This is another method that checks for the world spawn location. I want it to look for structures inside the cuboid though:
    Code (Java):
    if (cuboid.containsLocation(this.plugin.getServer().getWorld("world").getSpawnLocation())) {
                                player.sendMessage(ChatColor.RED + "Your claim is too close to spawn or contains the spawn location.");
                                return;
                            }
    ^ for this, I have no idea how to search for a specific structure within the region..
     
  2. How large are these cuboids normally? Are they a set size? If so, what is the size, and if not, what are the max parameters for the cuboid sizes?
     
  3. What plugin do you use for claims?
     
  4. Barbadosian

    Barbadosian Previously Aaron_Mao

    If you are doing it through brute force, the only way to not lag your server is probably only use BukkitRunnable and run it async, or even use ExecutorService to further parallelize the search using non-main thread.

    However I believe there is a much more elegant NMS solution, which is detailed here.
    Solved - How to find closest generated structure? | SpigotMC - High Performance Minecraft

    Edit: iTejie's response includes a method that does not involve NMS, so if you don't want to headache over NMS you could use that.
     
  5. It's for my players to claim land so there is no max.

    My own.
     
  6. You should probably check out the link @iTeije provided. If you still want to use "manual" checking... get all the chunks in your selection, get ChunkSnapshot every few ticks and check those async, and if any contains "blacklisted" block, stop collecting snapshot and warn your user.
     
  7. Yup, I did something similar. I'll post the way I did it for anyone else who runs into this issue in a bit!
     
  8. Fixed!

    In essence, I created a BukkitRunnable that uses locateNearestStructure() and stores that location in a data.yml. When players are claiming, I added a check to see if their cuboid regions contains the locations in data.yml by looping and checking.
     
    • Like Like x 1
    • Informative Informative x 1