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

    NeatMonster

    Hello,

    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

    Pencil

    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

    NeatMonster

    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:
    Code:java
    1. pm.registerEvent(Type.CHUNK_LOAD, new WATWorldListener(this),
    2. Priority.Normal, this);
    Code:java
    1. package me.neatmonster.windandtorch;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Chunk;
    6. import org.bukkit.World;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.event.world.ChunkLoadEvent;
    9. import org.bukkit.event.world.WorldListener;
    10.  
    11. public class WATWorldListener extends WorldListener {
    12. private WindAndTorch plugin;
    13. public static Logger logger = Logger.getLogger("minecraft");
    14.  
    15. public WATWorldListener(WindAndTorch plugin) {
    16. this.plugin = plugin;
    17. }
    18.  
    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;
    25.  
    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. logger.info("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

    Taco

    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: http://jd.bukkit.org/doxygen/df/d76/classorg_1_1bukkit_1_1event_1_1server_1_1MapInitializeEvent.html 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

    NeatMonster

    I'll try that. Thank you very much!

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

    Terradominik

    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

    kreashenz

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

    LucasEmanuel

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

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

    resba

    Locked~
     
Thread Status:
Not open for further replies.

Share This Page