我正在開發一個適用於Minecraft的mod,它允許用戶創建與RedPower類似的大型移動結構。源可用here 我正在使用僞造1.8-11.14.1.1357,我試圖檢查一個塊是否通過另一個塊或直接連接到控制器塊。然而,我的當前方法由於遞歸深入而引發了StackOverflow錯誤。我怎樣才能簡化代碼,以便儘可能避免遞歸。 如何檢查一個塊是否間接連接到Minecraft中的另一個特定塊
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;
}
堆棧跟蹤可作爲GitHub的要點here BlockMovingController.java源here
請向我們展示異常堆棧跟蹤。 – Unihedron