Change blocks in async thread

Discussion in 'Plugin Help/Development/Requests' started by BlueMoony, Jan 3, 2015.

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

    BlueMoony

    Hello,

    as I recently picked up my plugin again, I tried to update it to the latest version. However, using Spigot etc, my plugin doesn't work how it should.

    Code:
    private synchronized void fillMine()
        {
            // Method to randomly fill a block listed.
           
            Block block = blockList.get((int)Math.floor(Math.random()*blockList.size()));        // Get random block
            block.setType(Material.getMaterial(data.getMaterialName()));       
            //block.setType(Material.COAL);
            blockList.remove(block);            // Remove block from the list
            updateMineArray(block, false);       
        }
    this code is executed in a seperate thread. Now, this Mine class extends thread and so is asynchronous. However, Spigot complains about async block change and I would like to fix that issue. Is it possible to call the
    Code:
    block.setType(material);
    in a synchronized way?


    thanks in advance!

    I came up with my own solution for anybody looking for an answer:

    I made a separate class that implements Runnable.

    Code:
    public class SyncBlockChanger implements Runnable{
      
        private Block block;
        private Material material;
    
        public SyncBlockChanger(Block block, Material material){
            this.block = block;
            this.material = material;
        }
      
        @Override
        public void run() {
            block.setType(material);
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
Thread Status:
Not open for further replies.

Share This Page