Get a list of all blocks of a type?

Discussion in 'Plugin Development' started by NeatMonster, Aug 16, 2011.

Thread Status:
Not open for further replies.
  1. Offline



    I'm the actual developer of WindAndTorch. For future updates, I need to get a list of all torches on server start and server stop, to check some parameters on torches and maybe to break them.

    But I really don't know how to get this list. And what is the best event to use (I think I must the event when chunks are loaded)?

    Amicably, NeatMonster.
  2. Offline


    I doubt there is a ''list''. But I guess you could make your own, onblockplace check for a torch and add it to your 'list'. for existing ones you could check onchunkload and add that to your list if it doesnt exist yet.
  3. Offline


    That's what I've done in my first release.

    That's what was looking for, but I was not sure it was possible.

    Edit: Thank you.

    This is my actual code:
    1. pm.registerEvent(Type.CHUNK_LOAD, new WATWorldListener(this),
    2. Priority.Normal, this);
    1. package me.neatmonster.windandtorch;
    3. import java.util.logging.Logger;
    5. import org.bukkit.Chunk;
    6. import org.bukkit.World;
    7. import org.bukkit.block.Block;
    8. import;
    9. import;
    11. public class WATWorldListener extends WorldListener {
    12. private WindAndTorch plugin;
    13. public static Logger logger = Logger.getLogger("minecraft");
    15. public WATWorldListener(WindAndTorch plugin) {
    16. this.plugin = plugin;
    17. }
    19. @Override
    20. public void onChunkLoad(ChunkLoadEvent event) {
    21. final World world = event.getWorld();
    22. Chunk chunk = event.getChunk();
    23. int chunkX = chunk.getX() << 4;
    24. int chunkZ = chunk.getZ() << 4;
    26. for (int searchX = chunkX; searchX < chunkX + 16; searchX++) {
    27. for (int searchZ = chunkZ; searchZ < chunkZ + 16; searchZ++) {
    28. for (int searchY = 0; searchY < 128; searchY++) {
    29. final Block torch = world.getBlockAt(searchX, searchY, searchZ);
    30. if (world.getBlockTypeIdAt(searchX, searchY, searchZ) == 16) {
    31."Torch found!");
    32. plugin.loadTorch(torch);
    33. }
    34. }
    35. }
    36. }
    37. }
    38. }

    The text is never displayed, even if I put torches near me, stop & start the server. I'm sure the event is called. For testing, I tried to replace all the blocks by air, but no changes. Any idea?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: May 18, 2016
  4. Offline


    It may only be called when the chunk is loaded for reasons other than server start. Try setting a warp to a chunk far away from spawn, fill it with torches, and then go to spawn. Then reboot the server and log back in and warp to the area that you loaded with the torches and see if that works.

    Edit: Maybe this is what you want: I'm not familiar with the map object, but you could use this to get the world and then the loaded chunks in that world. That could then be used to find out what blocks are already loaded. Hope that helps.
  5. Offline


    I'll try that. Thank you very much!

    Edit: You should read that. :p
  6. Offline


    Saving a List of all Torches may be very expensive, a better way would be to save their coords in relation to their chunk or tile and only when that one gets loaded, do something.
  7. Offline


    This.. Was.. Posted.. Almost.. 2.. Years.. AGO?! Why bump it with "cool"? !??!?!?! MinecraftShamrock
  8. Offline


    Looping through 32768 blocks every time a chunk gets loaded doesn't sound very performance friendly.

    lol, didn't see how old the thread was ;)
  9. Offline


Thread Status:
Not open for further replies.

Share This Page