Skip to content

Commit

Permalink
Improve bed obstruction tweak
Browse files Browse the repository at this point in the history
Account for blockstates without EnumFacing property
  • Loading branch information
ACGaming committed Oct 17, 2024
1 parent 03d7e52 commit 5a39340
Showing 1 changed file with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
public class UTBedObstructionMixin
{
@Inject(at = @At("HEAD"), method = "getBedSpawnLocation(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Z)Lnet/minecraft/util/math/BlockPos;", cancellable = true)
private static void utBedSpawnLocation(World worldIn, BlockPos bedLocation, boolean forceSpawn, CallbackInfoReturnable<BlockPos> callback)
private static void utBedSpawnLocation(World world, BlockPos bedLocation, boolean forceSpawn, CallbackInfoReturnable<BlockPos> cir)
{
if (!UTConfigTweaks.BLOCKS.utBedObstructionToggle) return;
if (UTConfigGeneral.DEBUG.utDebugToggle) UniversalTweaks.LOGGER.debug("UTBedObstruction ::: Get bed spawn location");
IBlockState state = worldIn.getBlockState(bedLocation);
IBlockState state = world.getBlockState(bedLocation);
Block block = state.getBlock();
if (!block.isBed(state, worldIn, bedLocation, null))
if (!block.isBed(state, world, bedLocation, null))
{
if (!forceSpawn) callback.setReturnValue(null);
else callback.setReturnValue(iterateSpawnPoint(worldIn, bedLocation));
if (!forceSpawn) cir.setReturnValue(null);
else cir.setReturnValue(ut$iterateSpawnPoint(world, bedLocation));
}
else callback.setReturnValue(iterateBedPoint(worldIn, bedLocation));
else cir.setReturnValue(ut$iterateBedPoint(world, bedLocation));
}

@Unique
private static BlockPos iterateSpawnPoint(World world, BlockPos pos)
private static BlockPos ut$iterateSpawnPoint(World world, BlockPos pos)
{
if (isValidSpawnPos(world, pos, false)) return pos;
if (ut$isValidSpawnPos(world, pos, false)) return pos;
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
Expand All @@ -48,31 +48,32 @@ private static BlockPos iterateSpawnPoint(World world, BlockPos pos)
for (int zIter = z - 1; zIter <= z + 1; ++zIter)
{
BlockPos blockPos = new BlockPos(xIter, y, zIter);
if (isValidSpawnPos(world, blockPos, false)) return blockPos;
if (ut$isValidSpawnPos(world, blockPos, false)) return blockPos;
}
}
return null;
}

@Unique
private static BlockPos iterateBedPoint(World world, BlockPos pos)
private static BlockPos ut$iterateBedPoint(World world, BlockPos pos)
{
EnumFacing enumfacing = world.getBlockState(pos).getValue(BlockHorizontal.FACING);
IBlockState blockState = world.getBlockState(pos);
EnumFacing enumFacing = blockState.getPropertyKeys().contains(BlockHorizontal.FACING) ? blockState.getValue(BlockHorizontal.FACING) : EnumFacing.NORTH;
int x = pos.getX();
int y = pos.getY();
int z = pos.getZ();
for (int yOffset = 0; yOffset <= 1; ++yOffset)
{
for (int facingOffset = 0; facingOffset <= 1; ++facingOffset)
{
int xOffset = x - enumfacing.getXOffset() * facingOffset;
int zOffset = z - enumfacing.getZOffset() * facingOffset;
int xOffset = x - enumFacing.getXOffset() * facingOffset;
int zOffset = z - enumFacing.getZOffset() * facingOffset;
for (int xIter = xOffset - 1; xIter <= xOffset + 1; ++xIter)
{
for (int zIter = zOffset - 1; zIter <= zOffset + 1; ++zIter)
{
BlockPos blockPos = new BlockPos(xIter, y + yOffset, zIter);
if (isValidSpawnPos(world, blockPos, true)) return blockPos;
if (ut$isValidSpawnPos(world, blockPos, true)) return blockPos;
}
}
}
Expand All @@ -81,7 +82,7 @@ private static BlockPos iterateBedPoint(World world, BlockPos pos)
}

@Unique
private static boolean isValidSpawnPos(World worldIn, BlockPos blockPos, boolean requireFloor)
private static boolean ut$isValidSpawnPos(World worldIn, BlockPos blockPos, boolean requireFloor)
{
return (worldIn.getBlockState(blockPos.down()).getMaterial().isSolid() || !requireFloor) && worldIn.getBlockState(blockPos).getBlock().canSpawnInBlock() && worldIn.getBlockState(blockPos.up()).getBlock().canSpawnInBlock();
}
Expand Down

0 comments on commit 5a39340

Please sign in to comment.