use of com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult in project minecolonies by Minecolonies.
the class AbstractEntityAIStructure method hasListOfResInInvOrRequest.
/**
* Check the placers inventory for the items in the itemList and remove it of the list if found.
*
* @param placer the placer.
* @param itemList the list to check.
* @param force if force insertion.
* @return true if need to request.
*/
public static <J extends AbstractJobStructure<?, J>, B extends AbstractBuildingStructureBuilder> ItemCheckResult hasListOfResInInvOrRequest(@NotNull final AbstractEntityAIStructure<J, B> placer, final List<ItemStack> itemList, final boolean force) {
for (final ItemStack stack : itemList) {
if (!InventoryUtils.hasItemInItemHandler(placer.getInventory(), stack1 -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, stack1)) && !placer.getOwnBuilding().hasResourceInBucket(stack)) {
return RECALC;
}
}
final List<ItemStack> foundStacks = InventoryUtils.filterItemHandler(placer.getWorker().getInventoryCitizen(), itemStack -> itemList.stream().anyMatch(targetStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(targetStack, itemStack)));
if (force) {
for (final ItemStack foundStack : new ArrayList<>(foundStacks)) {
final Optional<ItemStack> opt = itemList.stream().filter(targetStack -> ItemStackUtils.compareItemStacksIgnoreStackSize(targetStack, foundStack)).findFirst();
if (opt.isPresent()) {
final ItemStack stack = opt.get();
itemList.remove(stack);
if (stack.getCount() > foundStack.getCount()) {
stack.setCount(stack.getCount() - foundStack.getCount());
itemList.add(stack);
}
}
}
} else {
itemList.removeIf(itemStack -> ItemStackUtils.isEmpty(itemStack) || foundStacks.stream().anyMatch(target -> ItemStackUtils.compareItemStacksIgnoreStackSize(target, itemStack)));
}
itemList.removeIf(itemstack -> itemstack.getItem() instanceof BlockItem && isBlockFree(((BlockItem) itemstack.getItem()).getBlock()));
final Map<ItemStorage, Integer> list = new HashMap<>();
for (final ItemStack stack : itemList) {
ItemStorage tempStorage = new ItemStorage(stack.copy());
if (list.containsKey(tempStorage)) {
final int oldSize = list.get(tempStorage);
tempStorage.setAmount(tempStorage.getAmount() + oldSize);
}
list.put(tempStorage, placer.getTotalAmount(tempStorage.getItemStack()).getCount());
}
for (final Map.Entry<ItemStorage, Integer> placedStack : list.entrySet()) {
if (ItemStackUtils.isEmpty(placedStack.getKey().getItemStack())) {
return FAIL;
}
final ImmutableList<IRequest<? extends IDeliverable>> requests = placer.getOwnBuilding().getOpenRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(placedStack.getKey().getItemStack()));
final ImmutableList<IRequest<? extends IDeliverable>> completedRequests = placer.getOwnBuilding().getCompletedRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(placedStack.getKey().getItemStack()));
if (requests.isEmpty() && completedRequests.isEmpty()) {
final com.minecolonies.api.colony.requestsystem.requestable.Stack stackRequest = new Stack(placedStack.getKey().getItemStack(), placedStack.getValue(), 1);
placer.getWorker().getCitizenData().createRequest(stackRequest);
placer.registerBlockAsNeeded(placedStack.getKey().getItemStack());
return FAIL;
} else {
for (final IRequest<? extends IDeliverable> request : requests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
for (final IRequest<? extends IDeliverable> request : completedRequests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
}
return FAIL;
}
return SUCCESS;
}
use of com.minecolonies.coremod.entity.ai.basic.AbstractEntityAIStructure.ItemCheckResult in project minecolonies by ldtteam.
the class AbstractEntityAIStructure method hasListOfResInInvOrRequest.
/**
* Check the placers inventory for the items in the itemList and remove it of the list if found.
*
* @param placer the placer.
* @param itemList the list to check.
* @param force if force insertion.
* @return true if need to request.
*/
public static <J extends AbstractJobStructure<?, J>, B extends AbstractBuildingStructureBuilder> ItemCheckResult hasListOfResInInvOrRequest(@NotNull final AbstractEntityAIStructure<J, B> placer, final List<ItemStack> itemList, final boolean force) {
final Map<ItemStorage, Integer> requestedMap = new HashMap<>();
for (final ItemStack stack : itemList) {
if (stack.getItem() instanceof BlockItem && isBlockFree(((BlockItem) stack.getItem()).getBlock())) {
continue;
}
ItemStorage tempStorage = new ItemStorage(stack.copy());
if (requestedMap.containsKey(tempStorage)) {
final int oldSize = requestedMap.get(tempStorage);
tempStorage.setAmount(tempStorage.getAmount() + oldSize);
}
requestedMap.put(tempStorage, tempStorage.getAmount());
}
for (final ItemStorage stack : requestedMap.keySet()) {
if (!InventoryUtils.hasItemInItemHandler(placer.getInventory(), stack1 -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack.getItemStack(), stack1)) && !placer.getOwnBuilding().hasResourceInBucket(stack.getItemStack())) {
return RECALC;
}
}
final List<ItemStack> foundStacks = InventoryUtils.filterItemHandler(placer.getWorker().getInventoryCitizen(), itemStack -> requestedMap.keySet().stream().anyMatch(storage -> ItemStackUtils.compareItemStacksIgnoreStackSize(storage.getItemStack(), itemStack)));
final Map<ItemStorage, Integer> localMap = new HashMap<>();
for (final ItemStack stack : foundStacks) {
ItemStorage tempStorage = new ItemStorage(stack.copy());
if (localMap.containsKey(tempStorage)) {
final int oldSize = localMap.get(tempStorage);
tempStorage.setAmount(tempStorage.getAmount() + oldSize);
}
localMap.put(tempStorage, tempStorage.getAmount());
}
if (force) {
for (final Map.Entry<ItemStorage, Integer> local : localMap.entrySet()) {
int req = requestedMap.getOrDefault(local.getKey(), 0);
if (req != 0) {
if (local.getValue() >= req) {
requestedMap.remove(local.getKey());
} else {
requestedMap.put(local.getKey(), req - local.getValue());
}
}
}
} else {
requestedMap.entrySet().removeIf(entry -> ItemStackUtils.isEmpty(entry.getKey().getItemStack()) || foundStacks.stream().anyMatch(target -> ItemStackUtils.compareItemStacksIgnoreStackSize(target, entry.getKey().getItemStack())));
}
for (final Map.Entry<ItemStorage, Integer> placedStack : requestedMap.entrySet()) {
final ItemStack stack = placedStack.getKey().getItemStack();
if (ItemStackUtils.isEmpty(stack)) {
return FAIL;
}
final ImmutableList<IRequest<? extends IDeliverable>> requests = placer.getOwnBuilding().getOpenRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
final ImmutableList<IRequest<? extends IDeliverable>> completedRequests = placer.getOwnBuilding().getCompletedRequestsOfTypeFiltered(placer.getWorker().getCitizenData(), TypeConstants.DELIVERABLE, (IRequest<? extends IDeliverable> r) -> r.getRequest().matches(stack));
if (requests.isEmpty() && completedRequests.isEmpty()) {
final com.minecolonies.api.colony.requestsystem.requestable.Stack stackRequest = new Stack(stack, placer.getTotalAmount(stack).getCount(), 1);
placer.getWorker().getCitizenData().createRequest(stackRequest);
placer.registerBlockAsNeeded(stack);
return FAIL;
} else {
for (final IRequest<? extends IDeliverable> request : requests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
for (final IRequest<? extends IDeliverable> request : completedRequests) {
if (placer.worker.getCitizenJobHandler().getColonyJob().getAsyncRequests().contains(request.getId())) {
placer.worker.getCitizenJobHandler().getColonyJob().markRequestSync(request.getId());
}
}
}
return FAIL;
}
return SUCCESS;
}
Aggregations