help with setting a button to powered

Discussion in 'Plugin Development' started by alexh, Nov 11, 2011.

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

    alexh

    hey im currently trying to set a button to powered. im trying to use this:
    Code:
    ((Button) target).setPowered(true);
    but it returns an error... target is the block at a given location and selecting the button already works :) just need to set that button to powered.

    thanks in advance for any help
     
  2. Offline

    Lolmewn

    check if the block is a button first.
     
  3. Offline

    alexh

    *facepalm*
    i already said "target is the block at a given location and selecting the button already works"

    ive had it return the block selected and it returns STONE_BUTTON so thats works

    im not that much of a noob mate.
     
  4. Offline

    Lolmewn

    Well, you could have been. I don't know your Java skills, and am only trying to help here.

    Your code should work then.
     
  5. Offline

    alexh

    ill post the server error when i get home... and i appreciate your help.

    maybe if i describe the function of the plugin or at least the first 1/4 of it.

    It is designed that if an arrow hits a button it will "push" the button... upon the arrow hitting the button ((Button) target).setPowered(true); triggers (but this line returns the error otherwise its perfect)

    trigger is when a projectile hits somthing.
    arrow is what the projectile is.
    computed mean it has found the position of impact and knows what block it is
    STONE_BUTTON is what the block is in this case STONE_BUTTON
    and done just means that it was a stone button so the if statment is true and execute the setpowered code

    Code:
    2011-11-11 11:04:39 [INFO] [snipbutton trigger
    2011-11-11 11:04:39 [INFO] [snipbutton arrow
    2011-11-11 11:04:39 [INFO] [snipbutton computed
    2011-11-11 11:04:39 [INFO] [snipbutton STONE_BUTTON
    2011-11-11 11:04:39 [INFO] [snipbutton done
    2011-11-11 11:04:39 [SEVERE] Could not pass event PROJECTILE_HIT to sniper_button
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.material.Button
        at me.alexhaslam.sniper_button.Sniper_buttonEntityListener.onProjectileHit(Sniper_buttonEntityListener.java:43)
        at org.bukkit.plugin.java.JavaPluginLoader$77.execute(JavaPluginLoader.java:798)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:339)
        at net.minecraft.server.EntityArrow.x_(EntityArrow.java:170)
        at net.minecraft.server.World.entityJoinedWorld(World.java:1238)
        at net.minecraft.server.WorldServer.entityJoinedWorld(WorldServer.java:104)
        at net.minecraft.server.World.playerJoinedWorld(World.java:1220)
        at net.minecraft.server.World.tickEntities(World.java:1127)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:502)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:409)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:454)
    #

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

    halley

    You shot down lolmewn when they suggested you "check if it's a Button first," but this message vindicates them.

    Code:
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.material.Button
    It means that target is a org.bukkit.craftbukkit.block.CraftBlock, and that it is not an org.bukkit.material.Button, and it cannot be cast to it with your expression ((Button) target).

    One is a block and one is a materialdata.

    I googled around, and found this link: http://forums.bukkit.org/threads/solved-block-cast-bug.41037/

    You want something like:

    Code:
    Button struck = (Button) target.getState().getData();
    struck.setPowered(true);
    
     
    alexh likes this.
  7. Offline

    alexh

    thanks very much :) ill try it and he said check its a button and it was returning the expected value...

    thanks that worked NO ERRORS! but it either isnt updating the blocks to powered... or its not actully chaging the state to powered

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

    nisovin

    Code:
    BlockState state = target.getState();
    Button button = (Button)state.getData();
    button.setPowered(true);
    state.update();
     
    alexh likes this.
  9. Offline

    alexh

    cheers mate ill try that :)
    im stil very much a novice... this is my first time working with blocks.


    once i have finished this and released it ill make a plugin ill call "ApocalypseNigh"

    IT WORKED! thanks guys!

    credit to those who provided example code.

    ive added levers now too... only 2 bugs but im fixing them...

    bugs:

    Buttons get stuck
    buttons and levers do the change animation but dont power (somtimes)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page