Tutorial STARTER GUIDE: using return statement instead of brackets

Discussion in 'Resources' started by MrGeneralQ, Nov 30, 2016.

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

    MrGeneralQ

    Okay if you are very familiair with coding you better leave this thread.

    Okay so did you ever had a feeling that your code had way too many brackets? You probably experienced this before: you were coding but after a while your code was just 1 big ball of if , else and brackets. To solve this you can use the "return" statement.

    I did exactly the same thing at first but after a while I only used return statements.


    Let's get started!



    For this example we are going to use a basic event. If you are not familair with events then this thread is not for you!

    In this example I created a basic blockBreakEvent:

    Code:
    @EventHandler
    
    public void onBreak(BLockBreakEvent e)
    {
    Player player = e.getPlayer();
    BLock block = e.getBlock();
    }
    As you can see in the examples I also retrieved some variables from the event. Now let's say that the event shouldn't do anything in case the material is not stonebrick. In that case we can use the return statement. We just invert our if statement so it becomes IF NOT . .... stop running.

    Code:
    @EventHandler
    
    public void onBreak(BLockBreakEvent e)
    {
    Player player = e.getPlayer();
    BLock block = e.getBlock();
    
    if(!(block.getType() == Material.STONEBRICK)) return;
    }
    the code will then stop at the return statement. Let's say you want to send a message when it is the case, then we can use the brackets and put the return statement in there. Like this;


    Code:
    @EventHandler
    
    public void onBreak(BLockBreakEvent e)
    {
    Player player = e.getPlayer();
    BLock block = e.getBlock();
    
    if(!(block.getType() == Material.STONEBRICK))
    {
    player.sendMessage("The block is not a stonebrick!");
    e.setCancelled(true);
    return;
    }
    }
    You can check for any condition and return if it's not the case. Let's finnish our project with one final test. Let's say that the player needs permission to destroy the stonebrick block.

    As we learned, we can use the brackets to send a message if the player doesn't have permission or if we don't need any other actions then we can simply use our return statement without the brackets.
    Our final code would look like this.

    Code:
    @EventHandler
    
    public void onBreak(BLockBreakEvent e)
    {
    Player player = e.getPlayer();
    BLock block = e.getBlock();
    
    if(!(block.getType() == Material.STONEBRICK))return;
    
    if(!(player.hasPermission('brick.break"))
    {
    player.sendMessage("You don't have permission to break this block!");
    e.setCancelled(true);
    return;
    }
    
    player.sendMessage("You destroyed the block!")
    }
    As you can see in the example, we first checked if the material was NOT stone brick, in that case we should return, if not move on to the next check -> if the player HAS NO permission to break the block, then we send the player a message and return code. So if we passed all the conditions then the code will run.

    There you go! This should help you in making your code much cleaner. Please note this was my very first bukkit tutorial and I didn't use eclipse to write this tutorial. Any feedback would be much appreciated!
     
    Last edited: Nov 30, 2016
    LordDarthBob likes this.
  2. Offline

    LordDarthBob

  3. @MrGeneralQ Just a tip you can use != rather than doing ! Enum == Enum
     
  4. Offline

    MrGeneralQ

    I am aware of this, however, it may have confused them if I did that. That's why I used (!( everywhere. But thanks for the tip :)
     
Thread Status:
Not open for further replies.

Share This Page