Need help detecting what a block was placed on.

Discussion in 'Plugin Development' started by Snipey, Jul 27, 2013.

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

    Snipey

    As the title says i need help with this specific situation.

    This is what i have so far.

    Code:java
    1.  
    2. @EventHandler
    3. public void beaconPlace(BlockPlaceEvent event){
    4. Player p = event.getPlayer();
    5. Block block = event.getBlock();
    6.  
    7. if(event.getBlockPlaced().getType() == Material.BEACON){
    8. if(event.getBlockAgainst().getType() == Material.WOOL){
    9. if(block.getState().getData() instanceof Wool){
    10. DyeColor color = ((Wool)block.getState().getData()).getColor();
    11. if(color == DyeColor.BLUE)
    12. {
    13. p.sendMessage("You placed a §eBeacon §fon Blue Wool");
    14.  
    15. }
    16. if(color == DyeColor.RED){
    17.  
    18. p.sendMessage("You placed a §eBeacon §fon Red Wool");
    19. }
    20. }
    21. }
    22.  
    23. }
    24. }
    25.  
     
  2. Offline

    soulofw0lf

    use .equals when comparing objects not ==
    Code:java
    1. @EventHandler
    2. public void beaconPlace(BlockPlaceEvent event){
    3. Player p = event.getPlayer();
    4. Block block = event.getBlock();
    5. if(event.getBlockPlaced().getType().equals(Material.BEACON)){
    6. if(event.getBlockAgainst().getType().equals(Material.WOOL)){
    7. if(block.getState().getData() instanceof Wool){
    8. DyeColor color = ((Wool)block.getState().getData()).getColor();
    9. if(color.equals(DyeColor.BLUE))
    10. {
    11. p.sendMessage("You placed a §eBeacon §fon Blue Wool");
    12.  
    13. }
    14. if(color.equals(DyeColor.RED)){
    15.  
    16. p.sendMessage("You placed a §eBeacon §fon Red Wool");
    17. }
    18. }
    19. }
    20. }
    21. }
     
  3. Offline

    Snipey

    I tried doing this and it still did not work properly. .equals() and == are the same thing :/
     
  4. They both differ very much in their significance. equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects. That means, the contents of the objects. Whereas the ‘==’ operator is expected to check the actual object instances are same or not.

    Therefore no, they are not the same thing.

    Back to your question:
    You're checking the block "block" data.
    Code:
    Block block = event.getBlock();
    Code:
    if(block.getState().getData() instanceof Wool){
    Shouldn't you get the data of getBlockAgainst instead?
     
  5. Offline

    Snipey

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void beaconPlace(BlockPlaceEvent event){
    3. Player p = event.getPlayer();
    4. Block block = event.getBlock();
    5.  
    6. if(event.getBlockPlaced().getType().equals(Material.BEACON)){
    7. p.sendMessage("You placed a §eBeacon");
    8. if(block.getRelative(BlockFace.DOWN).getType().equals(Material.WOOL)){
    9. if(block.getState().getData() instanceof Wool){
    10. DyeColor color = ((Wool)block.getState().getData()).getColor();
    11. if(color.equals(DyeColor.BLUE))
    12. {
    13. p.sendMessage("You placed a §eBeacon §fon Blue Wool");
    14.  
    15. }
    16. if(color.equals(DyeColor.RED)){
    17. p.sendMessage("You placed a §eBeacon §fon Red Wool");
    18. }
    19. }
    20. }
    21.  
    22. }


    This is what i have now
     
  6. Offline

    iFamasssxD

    You could just get the location of the block under what you placed and go from there.
    Code:
    Location l = block.getLocation();
    Block blockUnder = l.add(0,-1,0).getBlock();
     
Thread Status:
Not open for further replies.

Share This Page