Skip to content

Commit

Permalink
Fix accessing unloaded chunks (#10390)
Browse files Browse the repository at this point in the history
Fix three cases where we unintentionally accessed unloaded chunks triggering a load on demand
  • Loading branch information
someaddons authored Nov 3, 2024
1 parent 4abf648 commit 778ab72
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/minecolonies/core/colony/Colony.java
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ && getConfig().getServer().forceLoadColony.get())
{
checkChunkAndRegisterTicket(chunkPos, chunk);
}
else
else if (buildingManager.keepChunkColonyLoaded(chunk))
{
this.pendingChunks.add(chunkPos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
import com.minecolonies.api.colony.buildings.registry.BuildingEntry;
import com.minecolonies.api.compatibility.newstruct.BlueprintMapping;
import com.minecolonies.api.tileentities.AbstractTileEntityColonyBuilding;
import com.minecolonies.api.util.*;
import com.minecolonies.core.tileentities.TileEntityColonyBuilding;
import com.minecolonies.api.util.BlockPosUtil;
import com.minecolonies.api.util.FireworkUtils;
import com.minecolonies.api.util.Log;
import com.minecolonies.api.util.MessageUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Tuple;
Expand Down Expand Up @@ -331,6 +328,11 @@ public int getRotation()
return cachedRotation;
}

if (!WorldUtil.isBlockLoaded(colony.getWorld(), getPosition()))
{
return -1;
}

try
{
Blueprint blueprint = StructurePacks.getBlueprint(this.structurePack, this.path, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.minecolonies.api.colony.requestsystem.location.ILocation;
import com.minecolonies.api.colony.requestsystem.request.IRequest;
import com.minecolonies.api.colony.requestsystem.request.RequestState;
import com.minecolonies.api.colony.requestsystem.requestable.Tool;
import com.minecolonies.api.colony.requestsystem.requestable.IDeliverable;
import com.minecolonies.api.colony.requestsystem.requestable.RequestTag;
import com.minecolonies.api.colony.requestsystem.requestable.Stack;
import com.minecolonies.api.colony.requestsystem.requestable.Tool;
import com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.entity.ai.statemachine.AIEventTarget;
Expand Down Expand Up @@ -43,7 +43,6 @@
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -1470,28 +1469,9 @@ public BlockPos getWorkingPosition(final BlockPos targetPosition)
*/
public BlockPos getWorkingPosition(final int distance, final BlockPos targetPos, final int offset)
{
if (offset > MAX_ADDITIONAL_RANGE_TO_BUILD)
{
return targetPos;
}

// TODO: Use pathfinding for this instead? Or find around Util

@NotNull final Direction[] directions = {Direction.EAST, Direction.WEST, Direction.NORTH, Direction.SOUTH};

//then get a solid place with two air spaces above it in any direction.
for (final Direction direction : directions)
{
@NotNull final BlockPos positionInDirection = getPositionInDirection(direction, distance + offset, targetPos);
if (EntityUtils.checkForFreeSpace(world, positionInDirection)
&& world.getBlockState(positionInDirection.above()).is(BlockTags.SAPLINGS))
{
return positionInDirection;
}
}

//if necessary we call it recursively and add some "offset" to the sides.
return getWorkingPosition(distance, targetPos, offset + 1);
// TODO: Use pathfinding for this instead! Get rid of all those getWork position stuff
final BlockPos workPos = BlockPosUtil.findSpawnPosAround(world, targetPos);
return workPos == null ? targetPos : workPos;
}

/**
Expand Down

0 comments on commit 778ab72

Please sign in to comment.