title: How do I check if the a block is indirectly connected to another specific block in Minecraft
body: <p>I am working on a mod for Minecraft which will allow the user to create large moving structures similar to the way that RedPower did. Source available <a href="https://github.com/Cethric/Stuff-MincraftForgeMod/tree/master/src/main" rel="nofollow">here</a>
  I am using forge 1.8-11.14.1.1357, an I am trying to check if a block is connected to the controller block either by another block or directly to it. My current method however raises a StackOverflow error due to the recursion getting to deep. How can I go about simplifying the code so that recursion can be avoided if possible.
      <pre><code>public boolean isAttachedToController(BlockPos pos, World world) {
          IBlockState state;
          boolean up;
          state = world.getBlockState(pos.up());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              up = true;
          } else {
              if (Arrays.asList(Stuff.getConfig().get("allowed_blocks", "Blocks", new String[]{}).getStringList()).contains(state.getBlock().getClass().getCanonicalName())) {
                  up = isAttachedToController(pos.up(), world);
              } else {
                  up = false;
              }
          }
          boolean down;
          state = world.getBlockState(pos.down());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              down = true;
          } else {
              if (! state.getBlock().getClass().getCanonicalName().equals("net.minecraft.block.BlockAir")) {
                  down = isAttachedToController(pos.down(), world);
              } else {
                  down = false;
              }
          }
          boolean north;
          state = world.getBlockState(pos.north());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              north = true;
          } else {
              if (! state.getBlock().getClass().getCanonicalName().equals("net.minecraft.block.BlockAir")) {
                  north = isAttachedToController(pos.north(), world);
              } else {
                  north = false;
              }
          }
          boolean south;
          state = world.getBlockState(pos.south());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              south = true;
          } else {
              if (! state.getBlock().getClass().getCanonicalName().equals("net.minecraft.block.BlockAir")) {
                  south = isAttachedToController(pos.south(), world);
              } else {
                  south = false;
              }
          }
          boolean east;
          state = world.getBlockState(pos.east());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              east = true;
          } else {
              if (! state.getBlock().getClass().getCanonicalName().equals("net.minecraft.block.BlockAir")) {
                  east = isAttachedToController(pos.up(), world);
              } else {
                  east = false;
              }
          }
          boolean west;
          state = world.getBlockState(pos.west());
          if (state.getBlock().getClass().getCanonicalName().equals("cethric.stuff.block.BlockMovingController")) {
              west = true;
          } else {
              if (! state.getBlock().getClass().getCanonicalName().equals("net.minecraft.block.BlockAir")) {
                  west = isAttachedToController(pos.west(), world);
              } else {
                  west = false;
              }
          }
          return up || down || north || south || east || west;
      }</code></pre></p>
  
  <p>Stacktrace is available as a GitHub Gist <a href="https://gist.github.com/Cethric/362f801aeea36a877750" rel="nofollow">here</a>
  BlockMovingController.java source <a href="https://github.com/Cethric/Stuff-MincraftForgeMod/blob/master/src/main/java/cethric/stuff/block/BlockMovingController.java" rel="nofollow">here</a></p>