Toggle lever with code in 1.9.4 or 1.10

Discussion in 'Spigot Plugin Development' started by CrimsonCrow98, Jun 26, 2016.

  1. Hello,
    The title says it, I need to toggle a lever. I found many resources on the internet but none of them seemed to work. I tried this in a redstone event with all the proper location and material checks:
    Code (Text):
    BlockState state = block.getState();
    Lever lever = (Lever) state.getData();
    lever.setPowered(shouldBeOn);
    state.setData(lever);
    state.update();
    (ShouldBeOn is a boolean)The lever didn't toggle back to it's previous state. I also tried this:
    Code (Text):
    block.setData((byte)(block.getData()+8),true);
    in order to activate it, and this:
    Code (Text):
    block.setData((byte)(block.getData()-8),true);
    to deactivate it.
    I even tried some NMS code I found:
    Code (Text):

    int iData = block.getData();
    if ((ShouldBeOn && (iData & 0x08) != 0x08) || (!ShouldBeOn && (iData & 0x08) == 0x08)){
                                CraftBlock cbBlock = (CraftBlock) block;
                                BlockState cbState = cbBlock.getState();
                                net.minecraft.server.v1_9_R2.WorldServer w = ((CraftWorld) block.getWorld()).getHandle();

                                Location l = block.getLocation();

                                int blockX = l.getBlockX();
                                int blockY = l.getBlockY();
                                int blockZ = l.getBlockZ();

                                BlockPosition bp = new BlockPosition(blockX,blockY,blockZ);

                                int blockData = cbState.getRawData();
                                int j1 = blockData & 7;
                                int k1 = 8 - (blockData & 8);

                                net.minecraft.server.v1_9_R2.Block netBlock =  w.getType(bp).getBlock();

                                w.setTypeAndData(bp,netBlock.fromLegacyData(j1 + k1),3);
                               
                                w.applyPhysics(bp, netBlock);

                                if (j1 == 1) {
                                    w.applyPhysics(new BlockPosition(blockX - 1, blockY, blockZ), netBlock);
                                }
                                else if (j1 == 2) {
                                    w.applyPhysics(new BlockPosition(blockX + 1, blockY, blockZ), netBlock);
                                }
                                else if (j1 == 3) {
                                    w.applyPhysics(new BlockPosition(blockX, blockY, blockZ - 1), netBlock);
                                }
                                else if (j1 == 4) {
                                    w.applyPhysics(new BlockPosition(blockX, blockY, blockZ + 1), netBlock);
                                }
                                else if (j1 != 5 && j1 != 6) {
                                    if(j1 == 0 || j1 == 7) {
                                        w.applyPhysics(new BlockPosition(blockX, blockY + 1, blockZ), netBlock);
                                    }
                                }
                                else {
                                    w.applyPhysics(new BlockPosition(blockX, blockY - 1, blockZ), netBlock);
                                }
                            }
    Could the byte operations be wrong, because I don't really understand some of the variables.
    Am I doing something wrong here and is it normal that none of these work for me?
     
  2. Dunno if bumping is allowed, found nothing about it in the forum section's guidelines. Sorry if it's not allowed.
     
  3. Thanks for your reply and sorry about the early bump. I am aware about the 0x8 bit meaning the lever is activated, thus why I add 8 to an inactive lever and substract 8 to an active lever.. But it's like the .setData() method doesn't work for me or something, and I know it should work (even if deprecated). I'm clueless about the source of my problem and I'm just throwing some ideas. Can anyone test if my examples really are outdated for 1.9.4 or if it's just me?
     
    #4 CrimsonCrow98, Jun 27, 2016
    Last edited: Jun 27, 2016
  4. Yep I never really played with levers but use NMS XD