Set lever on/off

Discussion in 'Plugin Development' started by Ne0nx3r0, Jan 20, 2013.

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

    Ne0nx3r0

    I'm having some trouble sorting this out and most methods I've tried are either buggy or problematic to update...

    What's the proper way to set a lever to it's on/off states?
     
  2. Offline

    chasechocolate

  3. Offline

    Ne0nx3r0

    You know I tried that, but it didn't seem to work...

    This is the cleanest setup I've gotten to work so far:
    Code:java
    1.  
    2. BlockState state = block.getState();
    3. Lever lever = (Lever) state.getData();
    4. lever.setPowered(on);
    5. state.setData(lever);
    6. state.update();
    7.  
     
  4. Offline

    fireblast709

    note that it is a MaterialData (block.getState().getData()) and not a BlockState ;3
    (Just a warning before people started complaining, but you already posted code)

    try getting the Lever, setting the power, and setData(MaterialData)?
     
  5. Offline

    Ne0nx3r0

    So... Oddly, this seems to fail if it's against a wall; it doesn't propagate the redstone current through the wall, for example to a piston.
     
    Tooner101 likes this.
  6. Offline

    SnowGears

    Have you figured this out? I need the current to go to a piston and it doesnt...
     
  7. Offline

    Ne0nx3r0

  8. Offline

    SnowGears

  9. Offline

    Ne0nx3r0

    I just did ; ).

    Lines 286-339, which are:

    Code:
    private static void setReceiver(Block block, boolean on){
            Material mBlock = block.getType();
            int iData = (int) block.getData();
     
            if(mBlock == Material.LEVER)
            {         
                if(plugin.getAPIVersion().equalsIgnoreCase("v1_4_R1"))
                {
                    if (on && (iData & 0x08) != 0x08)
                    { // Revenge of the massive annoyance
                        iData |= 0x08; //send power on
                    }
                    else if (!on && (iData & 0x08) == 0x08)
                    {
                        iData ^= 0x08; //send power off
                    }
     
                    int i1 = iData & 7;
     
                    net.minecraft.server.v1_4_R1.WorldServer w = ((CraftWorld) block.getWorld()).getHandle();
     
                    Location l = block.getLocation();
     
                    int i = (int) l.getX();
                    int j = (int) l.getY();
                    int k = (int) l.getZ();
                    int id = block.getTypeId();
                    w.setData(i, j, k, iData);
                    w.applyPhysics(i, j, k, id);
     
                    if (i1 == 1)
                    {
                        w.applyPhysics(i - 1, j, k, id);
                    }
                    else if (i1 == 2)
                    {
                        w.applyPhysics(i + 1, j, k, id);
                    }
                    else if (i1 == 3)
                    {
                        w.applyPhysics(i, j, k - 1, id);
                    }
                    else if (i1 == 4)
                    {
                        w.applyPhysics(i, j, k + 1, id);
                    }
                    else
                    {
                        w.applyPhysics(i, j - 1, k, id);
                    }
                }
                else
                {
                    BlockState state = block.getState(); 
                    Lever lever = (Lever) state.getData();
                    lever.setPowered(on);
                    state.setData(lever);
                    state.update();
                }
            }
    The "on" variable is a boolean that represents whether or not the lever should be turned on or off.

    Notably the way I handle it is by falling back to basic Bukkit support when the version changes and the plugin is not updated. This is because frankly I don't see the point in adding a different copy of the NMS code for each version when it hasn't actually changed, that's just redundant. Also, even if I did basically the same thing would happen, just with more copies of this NMS code.

    If you wanted to add support for outdated versions, you could follow some of the instructions that are scattered around the forum for doing so.
     
    Tooner101 likes this.
  10. Offline

    SnowGears

    Im sorry but I cant figure out how to compile the line plugin.getAPIVersion().
    What is the import for this method?
     
  11. Offline

    bleachisback

    According to this page:
    http://jd.bukkit.org/beta/apidocs/index-all.html#_G_
    That method does not exist in Bukkit, though it looks like it isn't neccesary.
     
  12. Offline

    SnowGears

    Okay thanks. I got the code to work (it flips the lever on and off) but it doesnt seem to be supplying power. The lever moves but nothing happens to the piston next to it. Can you help me?
     
  13. Offline

    bleachisback

  14. Offline

    Ne0nx3r0

    What is the import for this method?[/quote]
    Sorry I can see how that is ambiguous; the method is just a wrapper to grab the version number, it doesn't exist in Bukkit. You could skip it, however you will still want to deal with Bukkit's versioning problem somehow if you are using NMS code.

    This is the problem I ran into; without testing I can't say for sure if your method solves the issue or not but I suppose you could:
     
Thread Status:
Not open for further replies.

Share This Page