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));
}
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;
}
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));
}
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;
}
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;
}
Aggregations