BreakBlockEvent problems?

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

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

    Gonmarte

    Hi guys =)
    Im working on a blockbreakevent and im getting some issues...
    What i want: When i break a iron blockit will need to check if there is a iron block up the block, down, right and left, if there isnt one of the 4 it will remove just the ones that have. Then i need to break then at the same time i break the iron block. So this is the far i had got:
    Code:
    Block iron_ore = event.getBlock();
                   iron_ore.getType().equals(Material.IRON_ORE);
                   int x = iron_ore.getLocation().getBlockX();
                    int y= iron_ore.getLocation().getBlockY();
                    int z = iron_ore.getLocation().getBlockZ();
                    int x1= x+1;
                    int x2 = x-1;
                   int y1 = y+1;
                   int y2 = y-1;
    
    Thank you!
     
  2. Online

    timtower Administrator Administrator Moderator

    @Gonmarte getBlock().getRelative(BlockFace)
     
  3. Offline

    RoboticPlayer

    Use == instead of .equals() when comparing enums.
     
  4. Offline

    Gonmarte

    @timtower I need to use 4 ints and i just can use 3, what i do?
    Code:
    //error
    iron_ore.getRelative(x1, x2, y1, y2);
    
    
    Edit: Oh i thinki understand, i need to get the getRelative for each block, and for each block i need to get their 3 coords right? like this i got the block that his +1 x of the event.getBlock()
    Code:
                   int x = iron_ore.getLocation().getBlockX();
                    int y= iron_ore.getLocation().getBlockY();
                    int z = iron_ore.getLocation().getBlockZ();
                    int x1= x+1;
                     
                   
                   iron_ore.getRelative(x1,y,z);
    
     
    Last edited: Oct 6, 2015
  5. Offline

    Hawktasard

  6. Offline

    RoboticPlayer

  7. Offline

    Hawktasard

    @henderry2019
    Actually, you can do it like this and never have to worry about getting an NPE
    Code:java
    1. if(Enums.ENUM.equals(variable))
    2. {
    3. return;
    4. }
     
  8. Offline

    boomboompower

  9. Offline

    Scimiguy

    Shouldn't need a try and catch for this.

    Use == instead of .equals as already stated, and use getBlock().getRelative(BlockFace) as Tower stated.
     
  10. Offline

    Zombie_Striker

    @Hawktasard
    Even though it gets the same thing done, that just seems like a way to "make your point". Not the way it should be done (replacing a 2[==] char funchion with 9[.equals()]), but it makes you correct.

    As for
    @boomboompower
    You should not have anything where you expect that it could cause exceptions. If you are doing something with the chance of throwing an exception, you're doing it wrong.
     
  11. Offline

    boomboompower

  12. Offline

    RoboticPlayer

    @Zombie_Striker Not necessarily. What about things like saving custom config files & creating data files.
     
  13. Offline

    Scimiguy

    @henderry2019 Those methods explicitly throw Exceptions, requiring you to either define your method as throwing them, or use a try-catch statement.

    Using try-catch on most other circumstances is the wrong way of doing it, unless there's a high, unavoidable chance of error with catastrophic results
     
Thread Status:
Not open for further replies.

Share This Page