Search code examples
javaminecraftbukkit

Bukkit: Set a block powered


I'm trying to get into bukkit programming for minecraft, but for some reason I'm stuck with trying to set a block powered. I don't want do set a redstone-torch under the block.

I tried to change the data to an active lever. If there is anyone who can help me, please.

Code I tried:

Block n = my block;
Block block = your lever;
Lever l = (Lever) block;
l.setPowered(true); //toggles on
n.setData(l.getData());

Solution

  • Unfortunately, it's not possible to power a block that's not a Powerable Block directly.

    However, you can do some tricks to pretend it's powered:

    Detect whenever you want your block to power nearby blocks (for example onInteractEvent), and then send power to the blocks.

    @EventHandler
    public void onInteractEvent(PlayerInteractEvent e) {
    Block interacted = e.getClickedBlock();
    //Check if this is the block you want
    
    Block block= interacted.getRelative(BlockFace.UP, 1);    //Can be changed to any block face, and any distance
    
    
    switch(block.getType()) {
    case LEVER:
    case REDSTONE:
    case DISPENSER:
    case POWERED_RAILS:
    case whatever other blocks can be powered:
    {
          block.setData(block.getData() | 0x8); // sets the 0x8 bit to the data, making it powered
    } }