Created
January 20, 2016 06:31
-
-
Save tterrag1098/152ae2d1bcfed056e9f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fireball1725.graves.helpers; | |
import net.minecraft.block.BlockAir; | |
import net.minecraft.block.state.IBlockState; | |
import net.minecraft.util.BlockPos; | |
import net.minecraft.world.World; | |
public class SafeBlockReplacer { | |
private static boolean CheckBlock(World world, BlockPos blockPos, boolean forceAir) { | |
IBlockState blockState = world.getBlockState(blockPos); | |
if (blockState == null) | |
return true; | |
if (blockState.getBlock() instanceof BlockAir && forceAir) | |
return true; | |
else if (forceAir) | |
return false; | |
if (blockState.getBlock().hasTileEntity(blockState)) | |
return false; | |
if (blockState.getBlock().getBlockHardness(null, null) == -1.0F) | |
return false; | |
return true; | |
} | |
private static boolean CheckGraveSite(World world, BlockPos blockPos) { | |
int x = blockPos.getX(); | |
int y = blockPos.getY(); | |
int z = blockPos.getZ(); | |
boolean placeGrave = true; | |
if (!CheckBlock(world, blockPos, true)) | |
placeGrave = false; | |
if (!CheckBlock(world, blockPos.east()), true)) | |
placeGrave = false; | |
if (!CheckBlock(world, blockPos.down()), false)) | |
placeGrave = false; | |
if (!CheckBlock(world, blockPos.east().down(), false)) | |
placeGrave = false; | |
return placeGrave; | |
} | |
public static BlockPos GetSafeGraveSite(World world, BlockPos blockPos) { | |
BlockPos finalBlockPos = CheckSafeGraveSite(world, blockPos); | |
if (finalBlockPos == null) { | |
LogHelper.info(">>> Unable to find place to put grave..."); | |
finalBlockPos = world.getTopSolidOrLiquidBlock(blockPos); | |
finalBlockPos = CheckSafeGraveSite(world, finalBlockPos); | |
} | |
if (finalBlockPos == null) { | |
LogHelper.info(">>> Sorry, still can't find a good place..."); | |
finalBlockPos = blockPos; | |
} | |
return finalBlockPos; | |
} | |
public static BlockPos CheckSafeGraveSite(World world, BlockPos blockPos) { | |
int x = blockPos.getX(); | |
int y = blockPos.getY(); | |
int z = blockPos.getZ(); | |
if (y < 1) { | |
y = 9; | |
} | |
if (y > 256) { | |
y = 246; | |
} | |
int searchRadius = 8; | |
for (int searchY = -searchRadius; searchY <= searchRadius; searchY++) { | |
for (int searchX = -searchRadius; searchX <= searchRadius; searchX++) { | |
for (int searchZ = -searchRadius; searchZ <= searchRadius; searchZ++) { | |
BlockPos pos = new BlockPos(x + searchX, y + searchY, z + searchZ); | |
if (CheckGraveSite(world, pos) { | |
return pos; | |
} | |
} | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment