Redstone Power

Discussion in 'Plugin Development' started by gamerguy14, Nov 14, 2011.

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

    gamerguy14

    I have been going through the different classes and methods that bukkit has. The problem I have found is that I can't find out how to change redstone power.

    I found that the classes org.bukkit.material.Lever and org.bukkit.material.Button has a method called setPowered(boolean isPowered) but I can't seem to be able to get a Lever or Button object. I tried getting the block at a location then getting the type but it can only get the Material of the block at that location.

    So does anyone know how to change the redstone power of a block. I figure it must be possible since there are wireless redstone plugins.
     
  2. Offline

    LartTyler

    Not sure if it helps, but you can get the Lever instance like this:
    Code:
    if (ev.getBlock().getState() instanceof Lever) {
        Lever lever = (Lever)ev.getBlock().getState();
        // Remainder of your code here
    }
    Does that answer your question?

    Edit:
    Also, I know you can get the redstone state of a block using the Block isBlockPowered() method, but I haven't found anything that would let you change that... I'll keep looking and let you know if I find anything.
     
  3. Offline

    gamerguy14

    The problem with your example is the same problems I have been having. The Lever object is different than the Material.LEVER enumerator.
     
  4. Offline

    LartTyler

    Material.LEVER is simply that, an enumeration value. It isn't actually a class on it's own. It's just a name. You can get the Lever instance from a block using my example, but it won't do anything if the block isn't a lever. What are you trying to do in your plugin? I might be able to help you more if I knew.
     
  5. Offline

    gamerguy14

    Solved the problem!

    The trick was to look at the actual source of bukkit and following inheritance. Lever.java extends SimpleAttachableMaterial.java, which extends MaterialData.java. Their is a method in BlockSate called getData() which returns the MaterialData. So if you have a location that might have a Lever in it, you can change the redstone power like this:

    Code:
    public Lever getLever(Location location) {
        Lever lever = null;
        if (location.getBlock().getState().getData() instanceof Lever) {
            lever = (Lever) location.getBlock().getState().getData();
        }
        return lever;
    }
     
  6. Offline

    LartTyler

    Oh! I see what you did there :p So it takes a getData() to get an instance of Lever, not just getState()? I'll have to remember that, glad you managed to figure it out
     
  7. Offline

    bergerkiller

    Throwing in what I got...
    Code:
        public static void setLever(Block lever, boolean down) {
            byte data = lever.getData();
            int newData;
            if (down) {
                newData = data | 0x8;
            } else {
                newData = data & 0x7;
            }
            if (newData != data) {
                lever.setData((byte) newData, true);
            }
        }
     
  8. Offline

    halley

    Just a maintainable/robust coding tip. If you're trying to flip a bit but leave the rest unchanged, I would prefer to see:

    Code:
    byte mask = 0x8;
    if (down) {
        newData = data | mask;
    } else {
        newData = data & ~mask;
    }
    over your code of:

    Code:
    if (down) {
        newData = data | 0x8;
    } else {
        newData = data & 0x7;
    }
    The latter one has two different constant numbers, so you have to mentally decide what each one is doing and whether or not it is correct. And in fact, it would damage other newly used bits above 0x08 like 0x40. Today's system might not use those bits, but a future release of Minecraft or Bukkit may.
     
  9. Offline

    gamerguy14

    This method changes the direction of the lever right? I don't use hexadecimals ever so I don't know.
     
  10. Offline

    halley

    It'd take a while to explain fundamental concepts like hexadecimal or boolean operators. However, you can learn a lot about this from many sources. I just googled, and came across this nice page about the C language that explains the bitwise operators pretty well. You'll even see variants of my two expressions if you look for the "flags &= ~OPEN" on the page. Spend some time learning these now, and a LOT of things will make more sense as you develop your coding skills.

    In short, this code changes the state of just one bit in the eight-bit byte that represents the block's data. For levers, it's the bit which describes the state of the lever.
     
Thread Status:
Not open for further replies.

Share This Page