[Basic] Detecting Redstone power on normal blocks

Discussion in 'Resources' started by Celeixen, Oct 5, 2011.

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

    Celeixen

    I haven't been able to find any thing on redstone events in the resources section, so i thought i may as well write a little tutorial.

    First you need to register the Redstone_Change event:
    Code:
    Bukkit.getServer().getPluginManager().registerEvent(Event.Type.REDSTONE_CHANGE,(new RedstoneClass()),Event.Priority.Normal, this)
    Then you need to create a new blocklistener class and have the following:
    Code:
        public void    onBlockRedstoneChange(BlockRedstoneEvent event){
            Block block = event.getBlock();
            for (Block b : SurroundingBlocks(block)){
                if (CheckBlock(b)){
                if (b.isBlockIndirectlyPowered()){
                    // Do something when block is powered
                    //eg. set to gold
                    b.setType(Material.GOLD_BLOCK);
    
                }
                else{
                    //Do something when block is not powered
                }}
            }
            }
    You can do this many different ways but i decided it would be best to get all of the surrounding blocks like so:
    Code:
            public ArrayList<Block> SurroundingBlocks(Block block){
                ArrayList<Block> Blocks = new ArrayList<Block>();
                for (BlockFace face : BlockFace.values()){
                if (face == BlockFace.UP){
                Block above = block.getRelative(BlockFace.UP);
                Block above2 = above.getRelative(BlockFace.UP);
                Blocks.add(above);
                Blocks.add(above2);}
                Blocks.add(block.getRelative(face));
                }
                return Blocks;
            }
    And then you need to create a way to check if the block is the correct type you want to edit:
    Code:
        public boolean CheckBlock(Block block){
            // Check if the block matches the one your testing
             //e.g test if the block is dirt
             if (block.getType() == Material.DIRT){
                 return true;
             }
             return false;
            }
    Hopefully you understand this because its kinda basic i didn't really know what to write so i just explained it while i was writing the code.
     
Thread Status:
Not open for further replies.

Share This Page