Search in sources :

Example 1 with INonExhaustiveDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable in project minecolonies by ldtteam.

the class AbstractWarehouseRequestResolver method attemptResolveRequest.

/*
     * Moving the curly braces really makes the code hard to read.
     */
@Nullable
@Override
@SuppressWarnings("squid:LeftCurlyBraceStartLineCheck")
public List<IToken<?>> attemptResolveRequest(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> request) {
    if (manager.getColony().getWorld().isClientSide) {
        return Lists.newArrayList();
    }
    if (!(manager.getColony() instanceof Colony)) {
        return Lists.newArrayList();
    }
    final Colony colony = (Colony) manager.getColony();
    final List<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony, request.getRequester().getLocation().getInDimensionLocation());
    final int totalRequested = request.getRequest().getCount();
    int totalAvailable = 0;
    if (request.getRequest() instanceof INonExhaustiveDeliverable) {
        totalAvailable -= ((INonExhaustiveDeliverable) request.getRequest()).getLeftOver();
    }
    for (final TileEntityWareHouse tile : wareHouses) {
        final List<Tuple<ItemStack, BlockPos>> inv = tile.getMatchingItemStacksInWarehouse(itemStack -> request.getRequest().matches(itemStack));
        for (final Tuple<ItemStack, BlockPos> stack : inv) {
            if (!stack.getA().isEmpty()) {
                totalAvailable += stack.getA().getCount();
            }
        }
    }
    if (totalAvailable >= totalRequested || totalAvailable >= request.getRequest().getMinimumCount()) {
        return Lists.newArrayList();
    }
    if (totalAvailable < 0) {
        totalAvailable = 0;
    }
    final int totalRemainingRequired = totalRequested - totalAvailable;
    final IDeliverable remainingRequest = request.getRequest().copyWithCount(totalRemainingRequired);
    return Lists.newArrayList(manager.createRequest(this, remainingRequest));
}
Also used : TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) Colony(com.minecolonies.coremod.colony.Colony) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with INonExhaustiveDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable in project minecolonies by ldtteam.

the class AbstractWarehouseRequestResolver method getFollowupRequestForCompletion.

@Nullable
@Override
public List<IRequest<?>> getFollowupRequestForCompletion(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> completedRequest) {
    if (manager.getColony().getWorld().isClientSide) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    final List<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony, completedRequest.getRequester().getLocation().getInDimensionLocation());
    List<IRequest<?>> deliveries = Lists.newArrayList();
    int remainingCount = completedRequest.getRequest().getCount();
    final Map<ItemStorage, Integer> storages = new HashMap<>();
    final int keep = completedRequest.getRequest() instanceof INonExhaustiveDeliverable ? ((INonExhaustiveDeliverable) completedRequest.getRequest()).getLeftOver() : 0;
    tileentities: for (final TileEntityWareHouse wareHouse : wareHouses) {
        final List<Tuple<ItemStack, BlockPos>> targetStacks = wareHouse.getMatchingItemStacksInWarehouse(itemStack -> completedRequest.getRequest().matches(itemStack));
        for (final Tuple<ItemStack, BlockPos> tuple : targetStacks) {
            if (ItemStackUtils.isEmpty(tuple.getA())) {
                continue;
            }
            int leftOver = tuple.getA().getCount();
            if (keep > 0) {
                int kept = storages.getOrDefault(new ItemStorage(tuple.getA()), 0);
                if (kept < keep) {
                    if (leftOver + kept <= keep) {
                        storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + tuple.getA().getCount());
                        continue;
                    }
                    int toKeep = (leftOver + kept) - keep;
                    leftOver -= toKeep;
                    storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + toKeep);
                }
            }
            int count = Math.min(remainingCount, leftOver);
            final ItemStack matchingStack = tuple.getA().copy();
            matchingStack.setCount(count);
            completedRequest.addDelivery(matchingStack);
            final ILocation itemStackLocation = manager.getFactoryController().getNewInstance(TypeConstants.ILOCATION, tuple.getB(), wareHouse.getLevel().dimension());
            final Delivery delivery = new Delivery(itemStackLocation, completedRequest.getRequester().getLocation(), matchingStack, getDefaultDeliveryPriority(true));
            final IToken<?> requestToken = manager.createRequest(manager.getFactoryController().getNewInstance(TypeToken.of(this.getClass()), completedRequest.getRequester().getLocation(), completedRequest.getId()), delivery);
            deliveries.add(manager.getRequestForToken(requestToken));
            remainingCount -= count;
            if (remainingCount <= 0) {
                break tileentities;
            }
        }
    }
    return deliveries.isEmpty() ? null : deliveries;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) AbstractDeliverymanRequestable.getDefaultDeliveryPriority(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.AbstractDeliverymanRequestable.getDefaultDeliveryPriority) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IFormattableTextComponent(net.minecraft.util.text.IFormattableTextComponent) TypeToken(com.google.common.reflect.TypeToken) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) CONST_WAREHOUSE_RESOLVER_PRIORITY(com.minecolonies.api.util.constant.RSConstants.CONST_WAREHOUSE_RESOLVER_PRIORITY) Lists(com.google.common.collect.Lists) Tuple(com.minecolonies.api.util.Tuple) BuildingBasedRequester(com.minecolonies.coremod.colony.requestsystem.requesters.BuildingBasedRequester) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) Log(com.minecolonies.api.util.Log) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) ItemStorage(com.minecolonies.api.crafting.ItemStorage) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) Colony(com.minecolonies.coremod.colony.Colony) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) BlockPos(net.minecraft.util.math.BlockPos) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with INonExhaustiveDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable in project minecolonies by Minecolonies.

the class AbstractWarehouseRequestResolver method attemptResolveRequest.

/*
     * Moving the curly braces really makes the code hard to read.
     */
@Nullable
@Override
@SuppressWarnings("squid:LeftCurlyBraceStartLineCheck")
public List<IToken<?>> attemptResolveRequest(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> request) {
    if (manager.getColony().getWorld().isClientSide) {
        return Lists.newArrayList();
    }
    if (!(manager.getColony() instanceof Colony)) {
        return Lists.newArrayList();
    }
    final Colony colony = (Colony) manager.getColony();
    final List<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony, request.getRequester().getLocation().getInDimensionLocation());
    final int totalRequested = request.getRequest().getCount();
    int totalAvailable = 0;
    if (request.getRequest() instanceof INonExhaustiveDeliverable) {
        totalAvailable -= ((INonExhaustiveDeliverable) request.getRequest()).getLeftOver();
    }
    for (final TileEntityWareHouse tile : wareHouses) {
        final List<Tuple<ItemStack, BlockPos>> inv = tile.getMatchingItemStacksInWarehouse(itemStack -> request.getRequest().matches(itemStack));
        for (final Tuple<ItemStack, BlockPos> stack : inv) {
            if (!stack.getA().isEmpty()) {
                totalAvailable += stack.getA().getCount();
            }
        }
    }
    if (totalAvailable >= totalRequested || totalAvailable >= request.getRequest().getMinimumCount()) {
        return Lists.newArrayList();
    }
    if (totalAvailable < 0) {
        totalAvailable = 0;
    }
    final int totalRemainingRequired = totalRequested - totalAvailable;
    final IDeliverable remainingRequest = request.getRequest().copyWithCount(totalRemainingRequired);
    return Lists.newArrayList(manager.createRequest(this, remainingRequest));
}
Also used : TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) Colony(com.minecolonies.coremod.colony.Colony) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with INonExhaustiveDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable in project minecolonies by Minecolonies.

the class AbstractWarehouseRequestResolver method getFollowupRequestForCompletion.

@Nullable
@Override
public List<IRequest<?>> getFollowupRequestForCompletion(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends IDeliverable> completedRequest) {
    if (manager.getColony().getWorld().isClientSide) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    final List<TileEntityWareHouse> wareHouses = getWareHousesInColony(colony, completedRequest.getRequester().getLocation().getInDimensionLocation());
    List<IRequest<?>> deliveries = Lists.newArrayList();
    int remainingCount = completedRequest.getRequest().getCount();
    final Map<ItemStorage, Integer> storages = new HashMap<>();
    final int keep = completedRequest.getRequest() instanceof INonExhaustiveDeliverable ? ((INonExhaustiveDeliverable) completedRequest.getRequest()).getLeftOver() : 0;
    tileentities: for (final TileEntityWareHouse wareHouse : wareHouses) {
        final List<Tuple<ItemStack, BlockPos>> targetStacks = wareHouse.getMatchingItemStacksInWarehouse(itemStack -> completedRequest.getRequest().matches(itemStack));
        for (final Tuple<ItemStack, BlockPos> tuple : targetStacks) {
            if (ItemStackUtils.isEmpty(tuple.getA())) {
                continue;
            }
            int leftOver = tuple.getA().getCount();
            if (keep > 0) {
                int kept = storages.getOrDefault(new ItemStorage(tuple.getA()), 0);
                if (kept < keep) {
                    if (leftOver + kept <= keep) {
                        storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + tuple.getA().getCount());
                        continue;
                    }
                    int toKeep = (leftOver + kept) - keep;
                    leftOver -= toKeep;
                    storages.put(new ItemStorage(tuple.getA()), storages.getOrDefault(new ItemStorage(tuple.getA()), 0) + toKeep);
                }
            }
            int count = Math.min(remainingCount, leftOver);
            final ItemStack matchingStack = tuple.getA().copy();
            matchingStack.setCount(count);
            completedRequest.addDelivery(matchingStack);
            final ILocation itemStackLocation = manager.getFactoryController().getNewInstance(TypeConstants.ILOCATION, tuple.getB(), wareHouse.getLevel().dimension());
            final Delivery delivery = new Delivery(itemStackLocation, completedRequest.getRequester().getLocation(), matchingStack, getDefaultDeliveryPriority(true));
            final IToken<?> requestToken = manager.createRequest(manager.getFactoryController().getNewInstance(TypeToken.of(this.getClass()), completedRequest.getRequester().getLocation(), completedRequest.getId()), delivery);
            deliveries.add(manager.getRequestForToken(requestToken));
            remainingCount -= count;
            if (remainingCount <= 0) {
                break tileentities;
            }
        }
    }
    return deliveries.isEmpty() ? null : deliveries;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) java.util(java.util) ItemStackUtils(com.minecolonies.api.util.ItemStackUtils) AbstractDeliverymanRequestable.getDefaultDeliveryPriority(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.AbstractDeliverymanRequestable.getDefaultDeliveryPriority) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IFormattableTextComponent(net.minecraft.util.text.IFormattableTextComponent) TypeToken(com.google.common.reflect.TypeToken) BuildingWareHouse(com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingWareHouse) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ItemStack(net.minecraft.item.ItemStack) CONST_WAREHOUSE_RESOLVER_PRIORITY(com.minecolonies.api.util.constant.RSConstants.CONST_WAREHOUSE_RESOLVER_PRIORITY) Lists(com.google.common.collect.Lists) Tuple(com.minecolonies.api.util.Tuple) BuildingBasedRequester(com.minecolonies.coremod.colony.requestsystem.requesters.BuildingBasedRequester) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) Log(com.minecolonies.api.util.Log) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) ItemStorage(com.minecolonies.api.crafting.ItemStorage) NotNull(org.jetbrains.annotations.NotNull) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) ItemStorage(com.minecolonies.api.crafting.ItemStorage) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) Colony(com.minecolonies.coremod.colony.Colony) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) BlockPos(net.minecraft.util.math.BlockPos) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Delivery(com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery) ItemStack(net.minecraft.item.ItemStack) Tuple(com.minecolonies.api.util.Tuple) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with INonExhaustiveDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable in project minecolonies by Minecolonies.

the class WarehouseConcreteRequestResolver method internalCanResolve.

@Override
protected boolean internalCanResolve(final List<TileEntityWareHouse> wareHouses, final IRequest<? extends IDeliverable> requestToCheck) {
    final IDeliverable deliverable = requestToCheck.getRequest();
    if (deliverable instanceof IConcreteDeliverable) {
        boolean ignoreNBT = false;
        boolean ignoreDamage = false;
        if (deliverable instanceof Stack) {
            if (!((Stack) requestToCheck.getRequest()).matchNBT()) {
                ignoreNBT = true;
            }
            if (!((Stack) requestToCheck.getRequest()).matchDamage()) {
                ignoreDamage = true;
            }
        }
        for (final ItemStack possible : ((IConcreteDeliverable) deliverable).getRequestedItems()) {
            for (final TileEntityWareHouse wareHouse : wareHouses) {
                if (requestToCheck.getRequest() instanceof INonExhaustiveDeliverable) {
                    if (wareHouse.hasMatchingItemStackInWarehouse(possible, requestToCheck.getRequest().getMinimumCount(), ignoreNBT, ignoreDamage, ((INonExhaustiveDeliverable) requestToCheck.getRequest()).getLeftOver())) {
                        return true;
                    }
                } else {
                    if (wareHouse.hasMatchingItemStackInWarehouse(possible, requestToCheck.getRequest().getMinimumCount(), ignoreNBT, ignoreDamage, 0)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : IConcreteDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IConcreteDeliverable) TileEntityWareHouse(com.minecolonies.coremod.tileentities.TileEntityWareHouse) INonExhaustiveDeliverable(com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable) ItemStack(net.minecraft.item.ItemStack) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) ItemStack(net.minecraft.item.ItemStack) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack)

Aggregations

IDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)6 INonExhaustiveDeliverable (com.minecolonies.api.colony.requestsystem.requestable.INonExhaustiveDeliverable)6 TileEntityWareHouse (com.minecolonies.coremod.tileentities.TileEntityWareHouse)6 ItemStack (net.minecraft.item.ItemStack)6 Tuple (com.minecolonies.api.util.Tuple)4 Colony (com.minecolonies.coremod.colony.Colony)4 BlockPos (net.minecraft.util.math.BlockPos)4 Nullable (org.jetbrains.annotations.Nullable)4 Lists (com.google.common.collect.Lists)2 TypeToken (com.google.common.reflect.TypeToken)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)2 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)2 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)2 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)2 IConcreteDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IConcreteDeliverable)2 Stack (com.minecolonies.api.colony.requestsystem.requestable.Stack)2 AbstractDeliverymanRequestable.getDefaultDeliveryPriority (com.minecolonies.api.colony.requestsystem.requestable.deliveryman.AbstractDeliverymanRequestable.getDefaultDeliveryPriority)2 Delivery (com.minecolonies.api.colony.requestsystem.requestable.deliveryman.Delivery)2 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)2