How to press a button from a plugin

Discussion in 'Plugin Development' started by 7eggert, Jul 19, 2011.

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

    7eggert

    I'd like to add the ability of pressing a button* to a plugin. This button should behave as if a player pressed the button.
    Pressing the button by setting the data |= 8 does not result in the desired behaviour (RB 9xx), so I'm wondering what is the correct way to "press" a button?

    tia

    *) I'm modifying the DropChest plugin to switch a lever when it is nearly full and to push a button when it swallows an item.
     
  2. Offline

    THEK

    Pressing a button can be done in exactly the same way a Level can be toggled. You have to set it's byte data as 0x8. This will push the button in and revert back after an amount of time.
     
  3. Offline

    Hretsam

    Dont know if there more easy ways to do this. But the following code works.
    Do note that this is a function your normally not supposed to be using. But its the Minecraft one.

    You just need to make block, the block that is the button.
    And leave the check in there, even if your sure its always a button. Otherwise you could trigger a block you don't want to.

    Btw, for this to work you must build with CraftBukkit.

    Code:
            Block block = //The block;
            if (block.getType() == Material.STONE_BUTTON) {
                Location loc = block.getLocation();
                net.minecraft.server.Block.byId[((CraftWorld) block.getWorld()).getHandle().getTypeId(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())].b(((CraftChunk) ((CraftBlock) block).getChunk()).getHandle().world, loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), ((CraftPlayer) player).getHandle());
            }
     
  4. Offline

    EternityCoder

    -OR-

    button.setData(0x8);

    :D
     
  5. Offline

    Hretsam

    Yes but that does not trigger the redstone... (when i tried that)
     
  6. Offline

    EternityCoder

    Are you sure? Otherwise, just add a block.setPower(), right? Or manually tell the block to update somehow...
     
  7. Offline

    Hretsam

    Dont know if there is an other way. But from what i know you cant 'set' the block power.
     
  8. Offline

    7eggert

    Yes, toggling the button moved the button, but it didn't have any power (IIRC) and the delay was very random.

    Toggling the lever does not give power, too, unless you add redstone. Redstone Circuits does require the same trick. This trick does not work for buttons.

    I don't have a player variable either, because it's an automatic chest.
     
  9. Offline

    nisovin

    The method @Hretsam provided is the only real way to do this at the moment, though it can be simplified quite a bit:

    Code:
    net.minecraft.server.Block.STONE_BUTTON.interact( ((CraftWorld)world).getHandle(), x, y, z, null);
     
  10. Offline

    7eggert

    thanks to you all, I'll try that.
     
Thread Status:
Not open for further replies.

Share This Page