Skip to content

Commit

Permalink
Fix/double warehouse (#10149)
Browse files Browse the repository at this point in the history
Make courier not accept tasks of other warehouses
Make warhouses partial resolve
Couple of performance improvements
Fix some spammy debug logging
  • Loading branch information
Raycoms authored Aug 22, 2024
1 parent e185b01 commit bb16d07
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public interface IDeliverable extends IRetryable
*/
IDeliverable copyWithCount(final int newCount);


/**
* Can this type of request be resolved by the building, or only by external resolvers.
* @return true if so.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public Stack(final ItemStack stack, final boolean matchDamage, final boolean mat
this.matchOreDic = matchOreDic;
this.result = result;
this.count = count;
this.minCount = minCount;
this.minCount = Math.min(minCount, count);
this.canBeResolvedByBuilding = canBeResolvedByBuilding;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecolonies.api.tileentities;

import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.inventory.InventoryCitizen;
import com.minecolonies.api.util.Tuple;
import com.minecolonies.core.tileentities.TileEntityColonyBuilding;
Expand Down Expand Up @@ -69,6 +70,14 @@ public AbstractTileEntityWareHouse(final BlockEntityType<? extends AbstractTileE
@NotNull
public abstract List<Tuple<ItemStack, BlockPos>> getMatchingItemStacksInWarehouse(@NotNull Predicate<ItemStack> itemStackSelectionPredicate);

/**
* Get the count up to some maxCount for some itemstorage in the warehouse.
* @param storage the storage.
* @param maxCount the count.
* @return the maxCount or less.
*/
public abstract int getCountInWarehouse(@NotNull final ItemStorage storage, int maxCount);

/**
* Dump the inventory of a citizen into the warehouse. Go through all items and search the right chest to dump it in.
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/minecolonies/core/colony/ColonyView.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,10 @@ public IMessage handleColonyViewMessage(@NotNull final FriendlyByteBuf buf, @Not

if (buf.readBoolean())
{
this.requestManager = new StandardRequestManager(this);
if (this.requestManager == null)
{
this.requestManager = new StandardRequestManager(this);
}
this.requestManager.deserialize(StandardFactoryController.getInstance(), buf);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,15 @@ private IToken<?> resolve(
//TODO: Change this false to simulation.
resolver.onRequestAssigned(manager, request, false);

for (final IToken<?> childRequestToken :
attemptResult)
for (final IToken<?> childRequestToken : attemptResult)
{
final IRequest<?> childRequest = manager.getRequestHandler().getRequest(childRequestToken);

childRequest.setParent(request.getId());
request.addChild(childRequest.getId());
}

for (final IToken<?> childRequestToken :
attemptResult)
for (final IToken<?> childRequestToken : attemptResult)
{
final IRequest<?> childRequest = manager.getRequestHandler().getRequest(childRequestToken);

Expand Down Expand Up @@ -338,8 +336,7 @@ public void onRequestResolved(final IToken<?> token)
}

//Assign the followup request if need be
if (followupRequests != null && !followupRequests.isEmpty() &&
followupRequests.stream().anyMatch(followupRequest -> !isAssigned(followupRequest.getId())))
if (followupRequests != null && !followupRequests.isEmpty())
{
followupRequests.stream()
.filter(followupRequest -> !isAssigned(followupRequest.getId()))
Expand Down Expand Up @@ -614,8 +611,6 @@ public IRequest<?> getRequest(final IToken<?> token)
@Override
public IRequest<?> getRequestOrNull(final IToken<?> token)
{
manager.log("Retrieving the request for: " + token);

return manager.getRequestIdentitiesDataStore().getIdentities().get(token);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ public IRequestResolver<? extends IRequestable> getResolver(final IToken<?> toke
throw new IllegalArgumentException("The given token for a resolver is not known to this manager!");
}

manager.log("Retrieving resolver for: " + token);

return manager.getRequestResolverIdentitiesDataStore().getIdentities().get(token);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.minecolonies.core.colony.buildings.modules.BuildingModules;
import com.minecolonies.core.colony.buildings.modules.CourierAssignmentModule;
import com.minecolonies.core.colony.buildings.modules.WarehouseRequestQueueModule;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse;
import com.minecolonies.core.colony.jobs.JobDeliveryman;
import com.minecolonies.core.colony.requestsystem.resolvers.core.AbstractRequestResolver;
import net.minecraft.network.chat.MutableComponent;
Expand Down Expand Up @@ -44,6 +45,12 @@ public boolean canResolveRequest(@NotNull final IRequestManager manager, final I
return false;
}

if (manager.getColony().getBuildingManager().getBuilding(requestToCheck.getRequester().getLocation().getInDimensionLocation()) instanceof IWareHouse
&& !requestToCheck.getRequester().getLocation().equals(getLocation()))
{
return false;
}

return hasCouriers(manager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable;
import com.minecolonies.api.colony.requestsystem.requestable.Stack;
import com.minecolonies.api.colony.requestsystem.token.IToken;
import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse;
import com.minecolonies.core.colony.requestsystem.resolvers.core.AbstractWarehouseRequestResolver;
import com.minecolonies.core.tileentities.TileEntityWareHouse;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -27,42 +29,41 @@ public WarehouseConcreteRequestResolver(
}

@Override
protected boolean internalCanResolve(final List<TileEntityWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck)
protected boolean internalCanResolve(final Level level, final List<BuildingWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck)
{
final IDeliverable deliverable = requestToCheck.getRequest();

if(deliverable instanceof IConcreteDeliverable)
if (deliverable instanceof IConcreteDeliverable)
{
boolean ignoreNBT = false;
boolean ignoreDamage = false;
if (deliverable instanceof Stack)
if (deliverable instanceof Stack stack)
{
if (!((Stack) requestToCheck.getRequest()).matchNBT())
{
ignoreNBT = true;
}
if (!((Stack) requestToCheck.getRequest()).matchDamage())
{
ignoreDamage = true;
}
ignoreNBT = !stack.matchNBT();
ignoreDamage = !stack.matchDamage();
}
for(final ItemStack possible : ((IConcreteDeliverable) deliverable).getRequestedItems())
int totalCount = 0;
for (final ItemStack possible : ((IConcreteDeliverable) deliverable).getRequestedItems())
{
for (final TileEntityWareHouse wareHouse : wareHouses)
for (final BuildingWareHouse wareHouse : wareHouses)
{
if (requestToCheck.getRequest() instanceof INonExhaustiveDeliverable)
if (wareHouse.getTileEntity() == null)
{
continue;
}

if (requestToCheck.getRequest() instanceof INonExhaustiveDeliverable neDeliverable)
{
if (wareHouse.hasMatchingItemStackInWarehouse(possible, requestToCheck.getRequest().getMinimumCount(), ignoreNBT, ignoreDamage, ((INonExhaustiveDeliverable) requestToCheck.getRequest()).getLeftOver()))
{
return true;
}
totalCount += Math.max(0, wareHouse.getTileEntity().getCountInWarehouse(new ItemStorage(possible, requestToCheck.getRequest().getMinimumCount(), ignoreDamage, ignoreNBT), requestToCheck.getRequest().getMinimumCount()) - neDeliverable.getLeftOver());
}
else
{
if (wareHouse.hasMatchingItemStackInWarehouse(possible, requestToCheck.getRequest().getMinimumCount(), ignoreNBT, ignoreDamage, 0))
{
return true;
}
totalCount += wareHouse.getTileEntity().getCountInWarehouse(new ItemStorage(possible, requestToCheck.getRequest().getMinimumCount(), ignoreDamage, ignoreNBT), requestToCheck.getRequest().getMinimumCount());
}

if (totalCount >= requestToCheck.getRequest().getMinimumCount())
{
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import com.minecolonies.api.colony.requestsystem.requestable.IConcreteDeliverable;
import com.minecolonies.api.colony.requestsystem.requestable.IDeliverable;
import com.minecolonies.api.colony.requestsystem.token.IToken;
import com.minecolonies.api.util.WorldUtil;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse;
import com.minecolonies.core.colony.requestsystem.resolvers.core.AbstractWarehouseRequestResolver;
import com.minecolonies.core.tileentities.TileEntityWareHouse;
import com.minecolonies.core.tileentities.TileEntityRack;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -24,18 +29,30 @@ public WarehouseRequestResolver(
}

@Override
protected boolean internalCanResolve(final List<TileEntityWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck)
protected boolean internalCanResolve(final Level level, final List<BuildingWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck)
{
if(requestToCheck.getRequest() instanceof IConcreteDeliverable)
if (requestToCheck.getRequest() instanceof IConcreteDeliverable)
{
return false;
return false;
}

for (final TileEntityWareHouse wareHouse : wareHouses)
int totalCount = 0;
for (final BuildingWareHouse wareHouse : wareHouses)
{
if (wareHouse.hasMatchingItemStackInWarehouse(itemStack -> requestToCheck.getRequest().matches(itemStack), requestToCheck.getRequest().getMinimumCount()))
for (@NotNull final BlockPos pos : wareHouse.getContainers())
{
return true;
if (WorldUtil.isBlockLoaded(level, pos))
{
final BlockEntity entity = level.getBlockEntity(pos);
if (entity instanceof final TileEntityRack rack && !rack.isEmpty())
{
totalCount += rack.getItemCount(itemStack -> requestToCheck.getRequest().matches(itemStack));
if (totalCount >= requestToCheck.getRequest().getMinimumCount())
{
return true;
}
}
}
}
}
return false;
Expand Down
Loading

0 comments on commit bb16d07

Please sign in to comment.