How to Make a Method Call Itself Twice?

Discussion in 'Plugin Development' started by CodeOrange, Dec 13, 2020.

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

    CodeOrange

    Hey! I'm working on a slightly modified version of the veinminer plugin, and I'm not quite sure how to go about checking all of the blocks and breaking them.

    Here's my code:
    @EventHandler
    public void onBreakBlock(BlockBreakEvent event) {

    checkDirections(event.getBlock());

    }

    public void checkDirections(Block block) {

    if(block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)));
    block.getWorld().getBlockAt(block.getLocation().add(0, 1, 0)).breakNaturally();
    }
    if(block.getWorld().getBlockAt(block.getLocation().add(0, -1, 0)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(0, -1, 0)));
    block.getWorld().getBlockAt(block.getLocation().add(0, -1, 0)).breakNaturally();
    }
    if(block.getWorld().getBlockAt(block.getLocation().add(1, 0, 0)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(1, 0, 0)));
    block.getWorld().getBlockAt(block.getLocation().add(1, 0, 0)).breakNaturally();
    }
    if(block.getWorld().getBlockAt(block.getLocation().add(-1, 0, 0)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(-1, 0, 0)));
    block.getWorld().getBlockAt(block.getLocation().add(-1, 0, 0)).breakNaturally();
    }
    if(block.getWorld().getBlockAt(block.getLocation().add(0, 0, 1)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(0, 0, 1)));
    block.getWorld().getBlockAt(block.getLocation().add(0, 0, 1)).breakNaturally();
    }
    if(block.getWorld().getBlockAt(block.getLocation().add(0, 0, -1)).getType() == block.getType()) {
    checkDirections(block.getWorld().getBlockAt(block.getLocation().add(0, 0, -1)));
    block.getWorld().getBlockAt(block.getLocation().add(0, 0, -1)).breakNaturally();
    }
    }

    As you can see, I am trying to have a method call itself. But when I call it, none of the code below runs. This is probably a bad way to do this. Do you know of a way to make this work? Thanks!
     
    Last edited: Dec 13, 2020
  2. Offline

    CraftCreeper6

    That's a good way to get a StackOverflow. Imagine mining stone with vein miner, it would crash the server for sure.

    Print out the value of event.getBlock().getType(), what is it?
     
  3. Offline

    Kars

    Having a method call itself is called recursion.
    In recursion, you need to start with a base case (and work towards that), otherwise the method calls itself infinitely and
    So you have to specify a condition in which you exit your method at the top of your method, and work towards that goal in the remainder of your method. If this is not possible, recursion should not be used.
    Hope that made sense.

    If not, study the article.
     
  4. Offline

    CodeOrange

    This is just a test and I will check for certain blocks in the future.
     
  5. Offline

    Strahan

Thread Status:
Not open for further replies.

Share This Page