Reputation: 1
I'm pretty new to Minecraft modding and would like to know if there's a better way to test for a block in a radius around my player. my current implementation is the following:
if ((world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.TORCH
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.WALL_TORCH
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.FIRE
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.CAMPFIRE
&& world.getMaxLocalRawBrightness(new BlockPos(x + sx, y + sy, z + sz)) == 15
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.LANTERN
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.LAVA
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.LAVA_CAULDRON
|| (world.getBlockState(new BlockPos(x + sx, y + sy, z + sz))).getBlock() == Blocks.FURNACE
&& world.getMaxLocalRawBrightness(new BlockPos(x + sx, y + sy, z + sz)) == 13)
This basically loops through each block in a radius of 10 and returns true if a block is found. I know there is a way to optimise this code but I can't think how.
My implementation works, but is there anything else I could try which would be less taxing on the cpu?
Upvotes: 0
Views: 216
Reputation: 74
You can start by using local variables!
BlockPos blockPos = new BlockPos(x + sx, y + sy, z + sz);
Block block = world.getBlockState(blockPos).getBlock();
int lightLevel = world.getMaxLocalRawBrightness(blockPos);
if (block == Blocks.TORCH || block == Blocks.WALL_TORCH || block == Blocks.FIRE || block == Blocks.CAMPFIRE && lightLevel == 15 ...) {
...
}
That makes it way more cleaner, easyer to read and more efficient! But I don't understand what are you doing with this code!
If you are looking if there is a lightsource around the player, than use only the if (block == Blocks.TORCH ||...)
and if you are looking for the lightlevel, thann use only if (lightLevel >= 13)
!
Because if you have more than one mod installed, it is not advisable to go through all the blocks (that emit light)!
Upvotes: 0