[SOLVED] Checking if block equals on ChunkLoad

Discussion in 'Spigot Plugin Development' started by Stefan15ist, May 17, 2015.

  1. nvm
     
    #1 Stefan15ist, May 17, 2015
    Last edited: May 17, 2015
  2. Hey, can you post your code in case you found out how to do it? The ChunkLoadEvent seems to be skipped very very often for me, so my check for a specific block in every new loaded chunk never works...
     
  3. this is what I ended up doing:

    Main class:

    Code (Text):
    package info.reborncraft.NoSpawners;

    import org.bukkit.Bukkit;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.world.WorldInitEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{
        public static Plugin p;
       
        public void onEnable() {
            p = this;
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onInit(WorldInitEvent event) {
            event.getWorld().getPopulators().add(new CustomPopulator());
        }
    }
     
    Block Populator Class:

    Code (Text):
    package info.reborncraft.NoSpawners;

    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.Chunk;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.generator.BlockPopulator;

    public class CustomPopulator extends BlockPopulator {
       
        public void populate(final World world, Random random, final Chunk chunk) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(Main.p, new Runnable(){
                public void run() {
                    int maxY = 0;
                    if(world.getName().contains("nether")){
                        maxY = 200;
                    }else{
                        maxY = 100;
                    }
                    for (int x = 0; x < 16; x++) {
                        for (int z = 0; z < 16; z++) {
                            for (int y = 0; y < maxY; y++) {
                                if (chunk.getBlock(x, y, z).getType() == Material.MOB_SPAWNER) {
                                    Block block = chunk.getBlock(x, y, z);
                                   
                                    block.setType(Material.SIGN_POST);
                                    if(block.getType() == Material.SIGN_POST){
                                        Sign sign = (Sign) block.getState();
                                        sign.setLine(0, "§4Error 404");
                                        sign.setLine(1, "§cSpawner");
                                        sign.setLine(2, "§cNot Found");
                                        sign.update();      
                                    }else{
                                        block.setType(Material.AIR);
                                    }
                                   
                                    Bukkit.getConsoleSender().sendMessage("§6Spawner Removed at: §a"+block.getX()+" "+block.getY()+" "+block.getZ()+" world: "+block.getWorld().getName());
                                }
                            }
                        }
                    }
                }
            },20);
        }
    }