use of com.minecolonies.api.colony.requestsystem.requestable.IDeliverable in project minecolonies by Minecolonies.
the class WindowCitizen method fulfill.
/**
* On Button click transfert Items and fullfil.
*
* @param button the clicked button.
*/
private void fulfill(@NotNull final Button button) {
final int row = resourceList.getListElementIndexByPane(button);
if (getOpenRequestTreeOfCitizen().size() > row && row >= 0) {
@NotNull final IRequest tRequest = getOpenRequestTreeOfCitizen().get(row).getRequest();
if (!(tRequest.getRequest() instanceof IDeliverable)) {
return;
}
@NotNull final IRequest<? extends IDeliverable> request = (IRequest<? extends IDeliverable>) tRequest;
final Predicate<ItemStack> requestPredicate = stack -> request.getRequest().matches(stack);
final int amount = request.getRequest().getCount();
final int count = InventoryUtils.getItemCountInItemHandler(new InvWrapper(inventory), requestPredicate);
if (!isCreative && count <= 0) {
return;
}
// The itemStack size should not be greater than itemStack.getMaxStackSize, We send 1 instead
// and use quantity for the size
@NotNull final ItemStack itemStack;
if (isCreative) {
itemStack = request.getDisplayStacks().stream().findFirst().orElse(ItemStack.EMPTY);
} else {
itemStack = inventory.getStackInSlot(InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(inventory), requestPredicate));
}
MineColonies.getNetwork().sendToServer(new TransferItemsToCitizenRequestMessage(citizen, itemStack, isCreative ? amount : Math.min(amount, count), citizen.getColonyId()));
MineColonies.getNetwork().sendToServer(new UpdateRequestStateMessage(citizen.getColonyId(), request.getToken(), RequestState.OVERRULED, itemStack));
}
button.disable();
}
Aggregations