Prevent farms on a chunk edge from loading unloaded chunks

This commit is contained in:
mezz committed 2016-09-14 14:23:06 -07:00
1 parent 79c56311c2
commit d35075d681
3 files changed
+46 -27

No files matched your search

+22 -18
View File
@@ -11,15 +11,11 @@
package forestry.farming;
import com.google.common.collect.ImmutableSet;
import forestry.api.farming.FarmDirection;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import forestry.api.farming.FarmDirection;
import forestry.api.multiblock.IFarmComponent;
public class FarmHelper {
@@ -37,21 +33,29 @@ public class FarmHelper {
return FarmDirection.getFarmDirection(forgeDirectionOpposite);
}
public static BlockPos getFarmMultiblockCorner(World world, BlockPos start, FarmDirection farmSide, FarmDirection layoutDirection) {
BlockPos edge = getFarmMultiblockEdge(world, start, farmSide);
return getFarmMultiblockEdge(world, edge, getOpposite(layoutDirection));
/**
* @return the corner of the farm for the given side and layout. Returns null if the corner is not in a loaded chunk.
*/
public static BlockPos getFarmMultiblockCorner(BlockPos start, FarmDirection farmSide, FarmDirection layoutDirection, BlockPos minFarmCoord, BlockPos maxFarmCoord) {
BlockPos edge = getFarmMultiblockEdge(start, farmSide, maxFarmCoord, minFarmCoord);
return getFarmMultiblockEdge(edge, getOpposite(layoutDirection), maxFarmCoord, minFarmCoord);
}
private static BlockPos getFarmMultiblockEdge(World world, BlockPos start, FarmDirection direction) {
BlockPos.MutableBlockPos edge = new BlockPos.MutableBlockPos(start);
while (world.getTileEntity(edge) instanceof IFarmComponent) {
edge.move(direction.getFacing());
/**
* @return the edge of the farm for the given starting point and direction.
*/
private static BlockPos getFarmMultiblockEdge(BlockPos start, FarmDirection direction, BlockPos maxFarmCoord, BlockPos minFarmCoord) {
switch (direction) {
case NORTH: // -z
return new BlockPos(start.getX(), start.getY(), minFarmCoord.getZ());
case EAST: // +x
return new BlockPos(maxFarmCoord.getX(), start.getY(), start.getZ());
case SOUTH: // +z
return new BlockPos(start.getX(), start.getY(), maxFarmCoord.getZ());
case WEST: // -x
return new BlockPos(minFarmCoord.getX(), start.getY(), start.getZ());
default:
throw new IllegalArgumentException("Invalid farm direction: " + direction);
}
FarmDirection oppositeDirection = getOpposite(direction);
edge.move(oppositeDirection.getFacing());
return edge.toImmutable();
}
}
@@ -10,12 +10,13 @@
******************************************************************************/
package forestry.farming;
import javax.annotation.Nullable;
import forestry.api.farming.FarmDirection;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import forestry.api.farming.FarmDirection;
public class FarmTarget {
private final BlockPos start;
@@ -47,7 +48,7 @@ public class FarmTarget {
return direction;
}
public void setExtentAndYOffset(World world, BlockPos platformPosition) {
public void setExtentAndYOffset(World world, @Nullable BlockPos platformPosition) {
if (platformPosition == null) {
extent = 0;
return;
@@ -55,6 +56,9 @@ public class FarmTarget {
BlockPos.MutableBlockPos position = new BlockPos.MutableBlockPos(platformPosition);
for (extent = 0; extent < limit; extent++) {
if (!world.isBlockLoaded(position)) {
break;
}
IBlockState blockState = world.getBlockState(position);
if (!FarmHelper.bricks.contains(blockState.getBlock())) {
break;
@@ -11,6 +11,7 @@
package forestry.farming.multiblock;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -512,11 +513,11 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
// Set the maximum allowed extent.
allowedExtent = Math.max(sizeNorthSouth, sizeEastWest) * Config.farmSize + 1;
createTargets(worldObj, targets, targetStart, allowedExtent, sizeNorthSouth, sizeEastWest);
createTargets(worldObj, targets, targetStart, allowedExtent, sizeNorthSouth, sizeEastWest, min, max);
setExtents(worldObj, targets);
}
private static void createTargets(World world, Map<FarmDirection, List<FarmTarget>> targets, BlockPos targetStart, final int allowedExtent, final int farmSizeNorthSouth, final int farmSizeEastWest) {
private static void createTargets(World world, Map<FarmDirection, List<FarmTarget>> targets, BlockPos targetStart, final int allowedExtent, final int farmSizeNorthSouth, final int farmSizeEastWest, BlockPos minFarmCoord, BlockPos maxFarmCoord) {
for (FarmDirection farmSide : FarmDirection.values()) {
@@ -532,7 +533,7 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
FarmDirection layoutDirection = getLayoutDirection(farmSide);
BlockPos targetLocation = FarmHelper.getFarmMultiblockCorner(world, targetStart, farmSide, layoutDirection);
BlockPos targetLocation = FarmHelper.getFarmMultiblockCorner(targetStart, farmSide, layoutDirection, minFarmCoord, maxFarmCoord);
BlockPos firstLocation = targetLocation.offset(farmSide.getFacing());
BlockPos firstGroundPosition = getGroundPosition(world, firstLocation);
if (firstGroundPosition == null) {
@@ -545,9 +546,8 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
targetLocation = targetLocation.offset(farmSide.getFacing());
BlockPos groundLocation = new BlockPos(targetLocation.getX(), groundHeight, targetLocation.getZ());
int targetLimit = targetMaxLimit;
if (!Config.squareFarms) {
targetLimit = targetMaxLimit - i - 1;
if (!world.isBlockLoaded(groundLocation)) {
break;
}
IBlockState blockState = world.getBlockState(groundLocation);
@@ -555,6 +555,11 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
break;
}
int targetLimit = targetMaxLimit;
if (!Config.squareFarms) {
targetLimit = targetMaxLimit - i - 1;
}
FarmTarget target = new FarmTarget(targetLocation, layoutDirection, targetLimit);
farmSideTargets.add(target);
}
@@ -563,7 +568,12 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
}
}
@Nullable
private static BlockPos getGroundPosition(World world, BlockPos targetPosition) {
if (!world.isBlockLoaded(targetPosition)) {
return null;
}
for (int yOffset = 2; yOffset > -4; yOffset--) {
BlockPos position = targetPosition.add(0, yOffset, 0);
IBlockState blockState = world.getBlockState(position);
@@ -571,6 +581,7 @@ public class FarmController extends RectangularMultiblockControllerBase implemen
return position;
}
}
return null;
}