Set levers in off state?

Discussion in 'Plugin Development' started by lilkoopa, Jun 29, 2011.

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

    lilkoopa

    I have no problem setting levers in the on state, but every time I try setting a lever off it turns out weird and glitched.
     
  2. Offline

    mattmoss

    Here's how I did it.

    Code:
    Block lever = ...;    // insert code: however you gain access to your lever block
    
    
    // Switch lever on.
     lever.setData((byte) (lever.getData() | 0x8));    // set ON bit
    
    
    // Switch lever off.
    lever.setData((byte) (lever.getData() & ~0x8));    // clear ON bit
    
     
  3. Offline

    lilkoopa

    Ah I see, that makes sense, thank you. [diamond][diamond][diamond][diamond][diamond][diamond][diamond]

    It still doesn't seem to be working. The lever turns off alright, but the problem is it becomes glitched and strange. This did not fix the issue. I've done exactly what you did and still nothing. Is it a problem if the lever is attached to the side of a block rather than just to the ground? Because the lever is on the side of a block.

    Shameless self bump, but I'm noticing something. I think the problem lies in how I change it, what's happening is that the change occurs during a scheduler task, here's my code.
    Code:
                plugin.getServer().getScheduler().scheduleAsyncRepeatingTask(plugin, new Runnable() {
    
                    public void run() {
                        if (lever.getWorld().getTime() >= 12000 && lever.getWorld().getTime() <= 24000) {
                            data = data & ~0x08;
                            currentTime = (long) Math.abs(1200 - lever.getWorld().getTime());
                        } else {
                            data = data | 0x08;
                            currentTime = (long) Math.abs(600 - lever.getWorld().getTime());
                        }
                        lever.setData((byte) data, true);
                    }
    
                }, 0L, currentTime);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  4. Offline

    feildmaster

    I believe turning "off" is
    Code:
    lever.getData() ^  0x8
    to reverse the |
     
  5. Offline

    mattmoss

    ^ is xor, which is a toggle, not explicitly clearing the bit. It's not the opposite of |.

    To clear the bit:
    value & ~0x8 // note the bitwise negation

    To set the bit:
    value | 0x8

    To toggle the bit:
    value ^ 0x8

    Try this with scheduleSyncRepeatingTask and see if it "fixes" the problem.

    Well, that's certainly something you should test... I know that there are (or, at least, have been) differences in how ground levers and wall levers operate. I don't see that as being the issue, but getting additional test data can help to eliminate/narrow down the issue.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  6. Offline

    feildmaster

    Oh i see, i never really learned this, so i was going off a plugin i was looking at.
     
Thread Status:
Not open for further replies.

Share This Page