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?
http://jd.bukkit.org/doxygen/de/dd3/classorg_1_1bukkit_1_1material_1_1Lever.html Code:java Lever lever = <your lever>;lever.setPowered(true);
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 BlockState state = block.getState(); Lever lever = (Lever) state.getData(); lever.setPowered(on); state.setData(lever); state.update();
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)?
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.
You basically have to resort to NMS code, sadly. https://github.com/Ne0nx3r0/Quantum...3r0/quantum/circuits/CircuitManager.java#L286
Im sorry but I am really bad with NMS code.... Can you please give me an example of turning the lever on and off with it?
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.
Im sorry but I cant figure out how to compile the line plugin.getAPIVersion(). What is the import for this method?
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.
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?
I'm not sure what the reason is for this code, but it is possible to flip levers without using NMS code. Here's an example: http://forums.bukkit.org/threads/having-problems-flipping-levers.125306/#post-1528514 I used this code to flip levers that surrounded a specific block, then un-flip them after a certain amount of time.
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: