Solved Activate lever

Discussion in 'Plugin Development' started by MinecraftDorado, Sep 28, 2016.

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

    MinecraftDorado

    I was trying different methods to make the code work, but still the same. Someone knows how to make the state of the lever to change?
    Code:
    if(cmd.getName().equalsIgnoreCase("code")){
                    Lever lever = (Lever) player.getLocation().getBlock().getState();
                    lever.setPowered(true);
     
    Last edited: Sep 28, 2016
  2. Offline

    Lordloss

    You have to update the blockState after changing it, or it will not do anything.
    I hope you make sure it is a lever before youre casting it to one ;-)
     
  3. Offline

    MinecraftDorado

    @Lordloss
    Thanks for your help. A query could tell me how to update the state of the lever?
     
  4. Code:
    blockstate.update(true);
     
  5. Offline

    MinecraftDorado

    @FisheyLP
    I tried to do but does not work
    Code:
            if(cmd.getName().equalsIgnoreCase("code")){
                if(args[0].equalsIgnoreCase("power")){
                    BlockState block = player.getLocation().getBlock().getState();
                    ((Lever) block).setPowered(true);
                    block.update(true);
                }
         }
     
  6. Online

    timtower Administrator Administrator Moderator

  7. it isn't a blockstate indeed, it's actually from blockstate.getData()
    powering that won't change the lever block, that's why setting the new data and updating is needed
     
    Last edited: Sep 30, 2016
    timtower likes this.
  8. Offline

    MinecraftDorado

    @timtower @FisheyLP
    I tried to do what they told me but still not Configo it work. And that is not what is wrong
    Code:
                    BlockState block = player.getLocation().getBlock().getState();
                    Lever lever = (Lever) block;
                    lever.setPowered(true);
                    block.setData(lever);
     
  9. I justed tested this and it works for me:
    Code:java
    1. BlockState state = block.getState();
    2. Lever lever = (Lever) state.getData();
    3.  
    4. lever.setPowered(e.isSneaking()); // Used in PlayerToggleSneakEvent
    5. // You need to put something other in it
    6. state.setData(lever);
    7. state.update(true);

    Notice that I casted blockstate.getData() instead of the raw blockstate. Because a Lever is an instance of MaterialData, and .getData() returns a MaterialData Object (just look in the bukkit javadocs).

    Result (spigot 1.10.2):
    [​IMG]
     
    Last edited: Sep 30, 2016
    cococow123 and timtower like this.
  10. Offline

    MinecraftDorado

  11. Please mark the thread as solved if it solved your problem

    EDIT: nvm ;)
     
Thread Status:
Not open for further replies.

Share This Page