Cant use == in getBlockAt()

Discussion in 'Plugin Development' started by Gonmarte, Oct 10, 2015.

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

    Gonmarte

    Hi,
    Im working on a blockbreak event and i dont know why but i cant check if the getBlockAt() is == to a block... BUT i can check if its != null ... but thats not what i want i want to check if it == to a block

    Code with !=null that works
    Code:
            if(event.getBlock().getType().equals(Material.IRON_ORE)) {
                               
                                Block iron_ore = event.getBlock();
                               
                               int x = iron_ore.getLocation().getBlockX();
                                int y= iron_ore.getLocation().getBlockY();
                                int z = iron_ore.getLocation().getBlockZ();
                                int x1= x+1;
                                 
                                if(iron_ore.getWorld().getBlockAt(x1, y,z) != null) {
                               
                                  iron_ore.getWorld().getBlockAt(x1, y,z).breakNaturally();
                             
                                }
                               
                            }
    
    Code that doesnt work with == block
    Code:
            if(event.getBlock().getType().equals(Material.IRON_ORE)) {
                               
                                Block iron_ore = event.getBlock();
                                   Block diamond = event.getBlock();
    diamond.getType()==Material.DIAMOND_ORE;
                               
                               int x = iron_ore.getLocation().getBlockX();
                                int y= iron_ore.getLocation().getBlockY();
                                int z = iron_ore.getLocation().getBlockZ();
                                int x1= x+1;
                                                                                                             // problem
                                if(iron_ore.getWorld().getBlockAt(x1, y,z) == diamond) {
                               
                                  iron_ore.getWorld().getBlockAt(x1, y,z).breakNaturally();
                             
                                }
                               
                            }
    
     
  2. Offline

    Scimiguy

    @Gonmarte
    Did you try .equals() instead?
     
  3. Use .equals instead of ==
     
  4. Offline

    RoboticPlayer

    @Gonmarte Firstly, use == instead of .equals() when comparing enums.
    Edit: Ninja'd by @FisheyLP
    Second, if you are trying to set the type of the diamond block, use
    Code:
    diamond.getType() = Material.DIAMOND_ORE;
    Use a single equals sign for setting values, a dual equals sign for comparing.
     
  5. Offline

    Scimiguy

    Um. What?
     
  6. Offline

    RoboticPlayer

    @Scimiguy Well he didn't really explain what he was trying to do.
     
  7. Offline

    Scimiguy

    @henderry2019
    True, I'm not really sure what he's wanting either, but getType() = ? won't do anything

    Secondly, both .equals() and == are correct for enums.
    == is null-safe for enums, but .equals() may have been overridden by bukkit to extend its functionality

    EDIT: Oh... I see now
    @Gonmarte You haven't got that == in an if statement, you've just thrown it in there for some reason
     
  8. Offline

    Gonmarte

  9. Offline

    RoboticPlayer

    @Gonmarte What exactly are you trying to do?
     
  10. Offline

    Gonmarte

    @henderry2019 if i break a iron ore block and then if there is a diamond ore at the same loc but +1 in the coord x it will break the diamond ore too.
     
  11. Offline

    Scimiguy

    @Gonmarte read the edit, you haven't got that == in an if statement

    Also, your "diamond" block is the block you are breaking in the event
    When would the block above/next to it ever be the same block?

    Create a new Block instance of type Diamond, and compare that instead
     
  12. Offline

    caderape

    @Gonmarte

    A block cannot be null.

    1. Block iron_ore = event.getBlock();
    2. Block diamond = event.getBlock();
    3. if(iron_ore.getWorld().getBlockAt(x1, y,z) == diamond)
    Do you really think this can work ?

    iron_ore.getWorld().getBlockAt(x1, y,z).getType() == Material.DIAMOND
     
  13. Offline

    Gonmarte

    @Scimiguy @caderape
    Like this
    Code:
                        if(event.getBlock().getType().equals(Material.IRON_ORE)) {
                                   
                            Block iron_ore = event.getBlock();
                            Block diamond = event.getBlock();
                            diamond.getType().equals(Material.DIAMOND_ORE);
                           
                            int x = iron_ore.getLocation().getBlockX();
                            int y = iron_ore.getLocation().getBlockY();
                            int z  =iron_ore.getLocation().getBlockZ() ;
                            int x1 = x+1;
                           
                            if(iron_ore.getWorld().getBlockAt(x1, y, z) == diamond.getLocation()) {
                               
                            iron_ore.getWorld().getBlockAt(x1,y,z).breakNaturally();
                               
                            }
                           
                       
                        }
    
    
     
  14. Offline

    Scimiguy

    @Gonmarte
    This line will do nothing: diamond.getType().equals(Material.DIAMOND_ORE);
    your diamond is still the same block as the one you're breaking.

    Block diamond = new Block()
    Work with that instead
     
Thread Status:
Not open for further replies.

Share This Page