Skip to content

Commit

Permalink
Fix/warehouse issue (#10218)
Browse files Browse the repository at this point in the history
Fix warehouse accepting task it has no items for
  • Loading branch information
Raycoms authored Sep 15, 2024
1 parent eda970a commit bf1dc33
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -70,14 +69,6 @@ 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
12 changes: 2 additions & 10 deletions src/main/java/com/minecolonies/api/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,8 @@ public static int hasBuildingEnoughElseCount(@NotNull final IBuilding provider,
{
totalCount += ((TileEntityRack) entity).getCount(stack);
}
else if (entity instanceof ChestBlockEntity)
{
totalCount += getItemCountInProvider(entity, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack.getItemStack(), !stack.ignoreDamageValue(), !stack.ignoreNBT() ));
}

if (totalCount > count)
if (totalCount >= count)
{
return Integer.MAX_VALUE;
}
Expand Down Expand Up @@ -791,7 +787,7 @@ public static int hasBuildingEnoughElseCount(@NotNull final IBuilding provider,
totalCount += ((TileEntityRack) entity).getItemCount(stack);
}

if (totalCount > count)
if (totalCount >= count)
{
return totalCount;
}
Expand Down Expand Up @@ -841,10 +837,6 @@ public static int getCountFromBuilding(@NotNull final IBuilding provider, @NotNu
{
totalCount += ((TileEntityRack) entity).getCount(stack);
}
else if (entity instanceof ChestBlockEntity)
{
totalCount += getItemCountInProvider(entity, itemStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(itemStack, stack.getItemStack()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
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.api.util.InventoryUtils;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse;
import com.minecolonies.core.colony.requestsystem.resolvers.core.AbstractWarehouseRequestResolver;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
* ----------------------- Not Documented Object ---------------------
*/
Expand All @@ -29,46 +27,41 @@ public WarehouseConcreteRequestResolver(
}

@Override
protected boolean internalCanResolve(final Level level, final List<BuildingWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck)
protected int getWarehouseInternalCount(final BuildingWareHouse wareHouse, final IRequest<? extends IDeliverable> requestToCheck)
{
final IDeliverable deliverable = requestToCheck.getRequest();
if (!(deliverable instanceof IConcreteDeliverable))
{
return 0;
}

if (deliverable instanceof IConcreteDeliverable)
boolean ignoreNBT = false;
boolean ignoreDamage = false;
if (deliverable instanceof Stack stack)
{
ignoreNBT = !stack.matchNBT();
ignoreDamage = !stack.matchDamage();
}
int totalCount = 0;
for (final ItemStack possible : ((IConcreteDeliverable) deliverable).getRequestedItems())
{
boolean ignoreNBT = false;
boolean ignoreDamage = false;
if (deliverable instanceof Stack stack)
if (requestToCheck.getRequest() instanceof INonExhaustiveDeliverable neDeliverable)
{
ignoreNBT = !stack.matchNBT();
ignoreDamage = !stack.matchDamage();
totalCount += Math.max(0, InventoryUtils.hasBuildingEnoughElseCount(wareHouse,
new ItemStorage(possible, requestToCheck.getRequest().getMinimumCount(), ignoreDamage, ignoreNBT), requestToCheck.getRequest().getCount() + neDeliverable.getLeftOver()) - neDeliverable.getLeftOver());
}
int totalCount = 0;
for (final ItemStack possible : ((IConcreteDeliverable) deliverable).getRequestedItems())
else
{
for (final BuildingWareHouse wareHouse : wareHouses)
{
if (wareHouse.getTileEntity() == null)
{
continue;
}

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

if (totalCount >= requestToCheck.getRequest().getMinimumCount())
{
return true;
}
}
if (totalCount >= requestToCheck.getRequest().getCount())
{
return totalCount;
}
}
return false;
return totalCount;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
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.api.util.InventoryUtils;
import com.minecolonies.core.colony.buildings.workerbuildings.BuildingWareHouse;
import com.minecolonies.core.colony.requestsystem.resolvers.core.AbstractWarehouseRequestResolver;
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;

/**
* ----------------------- Not Documented Object ---------------------
*/
Expand All @@ -29,32 +23,13 @@ public WarehouseRequestResolver(
}

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

int totalCount = 0;
for (final BuildingWareHouse wareHouse : wareHouses)
{
for (@NotNull final BlockPos pos : wareHouse.getContainers())
{
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;
return InventoryUtils.hasBuildingEnoughElseCount(wareHouse, itemStack -> requestToCheck.getRequest().matches(itemStack), requestToCheck.getRequest().getCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -57,12 +56,12 @@ public TypeToken<? extends IDeliverable> getRequestType()
}

/**
* Override to implement decendent specific checks during canResolveRequest
* @param wareHouses
* @param requestToCheck
* @return
* Override to implement specific warehouse counting rules.
* @param wareHouse the warehouse to check.
* @param requestToCheck the requested item.
* @return the available quantity.
*/
protected abstract boolean internalCanResolve(final Level level, final List<BuildingWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck);
protected abstract int getWarehouseInternalCount(final BuildingWareHouse wareHouse, final IRequest<? extends IDeliverable> requestToCheck);

@Override
public boolean canResolveRequest(@NotNull final IRequestManager manager, final IRequest<? extends IDeliverable> requestToCheck)
Expand Down Expand Up @@ -91,23 +90,31 @@ public boolean canResolveRequest(@NotNull final IRequestManager manager, final I
}
}


if (!isRequestChainValid(manager, requestToCheck, requestToCheck))
{
return false;
}

int totalCount = getWarehouseInternalCount((BuildingWareHouse) wareHouse, requestToCheck);
if (totalCount <= 0)
{
return false;
}

try
{
final List<BuildingWareHouse> wareHouses = new ArrayList<>();
for (final Map.Entry<BlockPos, IBuilding> building : colony.getBuildingManager().getBuildings().entrySet())
{
if (building.getValue().getBuildingType() == ModBuildings.wareHouse.get())
if (building.getValue().getBuildingType() == ModBuildings.wareHouse.get() && building.getValue() != wareHouse)
{
wareHouses.add((BuildingWareHouse) building.getValue());
totalCount += getWarehouseInternalCount((BuildingWareHouse) building.getValue(), requestToCheck);
if (totalCount >= requestToCheck.getRequest().getCount())
{
return true;
}
}
}
return internalCanResolve(colony.getWorld(), wareHouses, requestToCheck);
return totalCount >= requestToCheck.getRequest().getCount();
}
catch (Exception e)
{
Expand Down Expand Up @@ -161,9 +168,11 @@ public List<IToken<?>> attemptResolveRequest(@NotNull final IRequestManager mana
}

final Colony colony = (Colony) manager.getColony();

final TileEntityWareHouse wareHouse = (TileEntityWareHouse) colony.getBuildingManager().getBuilding(getLocation().getInDimensionLocation()).getTileEntity();

if (wareHouse == null)
{
return Lists.newArrayList();
}
final int totalRequested = request.getRequest().getCount();
int totalAvailable = 0;
if (request.getRequest() instanceof INonExhaustiveDeliverable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.minecolonies.core.tileentities;

import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.inventory.InventoryCitizen;
import com.minecolonies.api.tileentities.AbstractTileEntityRack;
import com.minecolonies.api.tileentities.AbstractTileEntityWareHouse;
Expand Down Expand Up @@ -65,32 +64,6 @@ public boolean hasMatchingItemStackInWarehouse(@NotNull final Predicate<ItemStac
return false;
}

@Override
public int getCountInWarehouse(@NotNull final ItemStorage storage, int count)
{
int totalCount = 0;
if (getBuilding() != null)
{
for (@NotNull final BlockPos pos : getBuilding().getContainers())
{
if (WorldUtil.isBlockLoaded(level, pos))
{
final BlockEntity entity = getLevel().getBlockEntity(pos);
if (entity instanceof final TileEntityRack rack && !rack.isEmpty())
{
totalCount += rack.getCount(storage);
if (totalCount >= count)
{
return totalCount;
}
}
}
}
}

return totalCount;
}

@Override
public boolean hasMatchingItemStackInWarehouse(@NotNull final ItemStack itemStack, final int count, final boolean ignoreNBT)
{
Expand Down

0 comments on commit bf1dc33

Please sign in to comment.