Replacing nearby blocks

Discussion in 'Plugin Development' started by Tomass, Jun 23, 2015.

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

    Tomass

    Hello guys,

    I need to replace near players enderchests to simple chest. How can I do that as optimized as possible without extra lag or chunk load delay.

    Now I'm trying this way which works fine but also has a small delay before loading chunks.
    Code:
        public void onChunkLoad(ChunkLoadEvent e) {
            Chunk chunk = e.getChunk();
                for (int x = 0; x < 16; x++) {
                    for (int y = 0; y < 256; y++) {
                        for(int z = 0; z <16; z++) {
                            Block block = chunk.getBlock(x, y, z);
                            if(block.getType() == Material.ENDER_CHEST){
                                block.setType(Material.CHEST);
                                if (!plugin.tier2_chests.contains(block.getLocation())) {
                                    plugin.tier2_chests.add(block.getLocation());
                                }
                            }
                         }
                     }
                 }
        }
    Maybe some developers know more efficient and optimized solution?

    Thank you in advance guys! I will appreciate all kind of help!
     
  2. You can put your code in a scheduler that runs about 5 ticks later. This should give it enough time to load and then you can change all the chests, this should remove the delay.
     
  3. Offline

    Boomer

    I actually have some code to do the same with signs, and it is based on chunks - the system is effective at doing it based on a for-each-thing-in-chunk-do construction, and you can then look at the chunk the player is in.
    I should be able to find the sample code in about 10 mins

    It seems that I decimated the plugin that I was thinking of - it was a one-and-done conversion plugin, I likely purged from my workspace. However, I did find a snip of code from a different project that looks like it was partially part of the testing for that. Change the material type, and you can feed this function with the x,z coordinate (or some other way to identify the chunk to test, player-location, then convert that to world/chunk info..). My "appendEntry" function was where I did the work on a positive hit, i recorded the coordinates in a file...

    Code:
      public void scanchunk(Integer x, Integer z){
    //     Bukkit.broadcastMessage("Checking chunk " + x + " " + z);
         Chunk c = Bukkit.getWorld(this.worldname).getChunkAt(x, z);
         BlockState[] bs = c.getTileEntities();
         for (BlockState bs1:bs){
           if (bs1.getType() == Material.SIGN){
               AppendEntry(bs1.getLocation() );         
           }
         }
       }
    
    But definitely the way to go for what you want to do, as it looks through the map data file for the tile data (chests are tiles) rather than check each coordinate position. 3 tiles in the entire 16x16x256 -- it scans the chunk data layout and finds 3 results to report back immediately, many magnitudes faster, and without locking up the server

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

    Tomass

    Thank you.
     
Thread Status:
Not open for further replies.

Share This Page