Set block above clicked block?

Discussion in 'Plugin Development' started by Phasesaber, Dec 21, 2013.

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

    Phasesaber

    Heyo!
    I was wondering how to set the block above the clicked block to a block like redstone dust.

    Here is what I have so far:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void redstoneOnBlock(PlayerInteractEvent e){
    4. if (e.getAction() == Action.RIGHT_CLICK_AIR) return;
    5. if (!(e.getItem().getType() == Material.STICK)) return;
    6. for(Block loc:e.getPlayer().getLineOfSight(null, 100)){
    7. ((Location) loc).setY(loc.getY() + 1);
    8. Block b = ((Location) loc).getBlock();
    9. if(b.getType() == Material.AIR){
    10. b.setType(Material.REDSTONE_WIRE);
    11. }
    12. }
    13. }
     
  2. Offline

    ZeusAllMighty11

    Code:
    b.getLocation().add(0,1,0).setType(Material.REDSTONE_WIRE); // gets the clicked block, adds 1 to it's y and sets type
     
    XvBaseballkidvX likes this.
  3. Offline

    Phasesaber


    How would I set it if it is out of eyesight?

    I tried:
    Code:java
    1. for(Block b21 : e.getPlayer().getLineOfSight(null, 100)){
    2. if(b21.getType() == Material.AIR){
    3. b21.setType(Material.REDSTONE_WIRE);
    4. }


    Bump-i-dump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    Spadar Faar



    This seems like a rather round-about way to do it... Why are you using e.getPlayer().getLineOfSight() to get the block's location?

    The Player Interact event can return the block that was right clicked, and then you can use the worlds getBlockAt(x,y+1,z), and then modify the block returned as needed.
     
Thread Status:
Not open for further replies.

Share This Page