1.17.x How to find blocks by ID in the world?

Discussion in 'Spigot Plugin Development' started by MineAdicto, Jul 9, 2021.

  1. Hello, I would like to do a scan of the entire map to find pistons and tell me the coordinates where they are
    I have this code but I don't know how to transform it so you can tell me the X and Z coordinates but they are not the chunk
    Might you help me?
    Thank you

    for (World world : Bukkit.getServer().getWorlds()) {
    for (Chunk chunk : world.getLoadedChunks()) {
    int cx = chunk.getX() << 4;
    int cz = chunk.getZ() << 4;
    for (int x = cx; x < cx + 16; x++) {
    for (int z = cz; z < cz + 16; z++) {
    for (int y = 0; y < 128; y++) {
    if (world.getBlockAt(x, y, z).getType() == Material.EMERALD_ORE) {
    Bukkit.getConsoleSender().sendMessage("Se ha encontrado un piston pegajoso en "+ x + ", "+ y + ", " + z);

  2. That would lag the server way to much when compared to other ways
    Pistons are not generated randomly so it would probaly be way more efficient to create an blockplace event, check if block type is a piston and save it's location to the config or whatever other way you wish to save.
  3. how could i do that?
  4. Code (Java):
    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onMonitorBlockPlace(BlockPlaceEvent event) {
        final Block block = event.getBlock();
        if (block.getType() == Material.PISTON) {
             final Location location = block.getLocation();
             // SAVE THE LOCATION
    Be aware that if you save the location in the cache (ArrayList, etc.) you will loose all the pistons after each restart or reload.
    This said, I recommend learning about YML or SQL in case you havent yet in order to permanently save this data.

    EDIT: The code is written as pseudocode, so if it does not work there might be some caps mistakes.