use of com.minecolonies.api.util.constant.Suppression.RAWTYPES in project minecolonies by Minecolonies.
the class RequestHandler method assignRequestDefault.
/**
* Method used to assign a given request to a resolver. Does take a given blacklist of resolvers into account.
* Uses the default assigning strategy: {@link AssigningStrategy#PRIORITY_BASED}
*
* @param manager The manager to modify.
* @param request The request to assign.
* @param resolverTokenBlackList Each resolver that has its token in this blacklist will be skipped when checking for a possible resolver.
* @return The token of the resolver that has gotten the request assigned, null if none was found.
*
* @throws IllegalArgumentException is thrown when the request is unknown to this manager.
*/
@SuppressWarnings({ UNCHECKED, RAWTYPES })
public static IToken<?> assignRequestDefault(final IStandardRequestManager manager, final IRequest request, final Collection<IToken<?>> resolverTokenBlackList) {
// Check if the request is registered
getRequest(manager, request.getToken());
LogHandler.log("Starting resolver assignment search for request: " + request);
request.setState(new WrappedStaticStateRequestManager(manager), RequestState.ASSIGNING);
final Set<TypeToken> requestTypes = ReflectionUtils.getSuperClasses(request.getRequestType());
requestTypes.remove(TypeConstants.OBJECT);
final List<TypeToken> typeIndexList = new LinkedList<>(requestTypes);
final Set<IRequestResolver<?>> resolvers = requestTypes.stream().filter(typeToken -> manager.getRequestableTypeRequestResolverAssignmentDataStore().getAssignments().containsKey(typeToken)).flatMap(type -> manager.getRequestableTypeRequestResolverAssignmentDataStore().getAssignments().get(type).stream().map(iToken -> ResolverHandler.getResolver(manager, iToken))).filter(iRequestResolver -> typeIndexList.contains(iRequestResolver.getRequestType())).sorted(Comparator.comparingInt((IRequestResolver<?> r) -> -1 * r.getPriority()).thenComparingInt((IRequestResolver<?> r) -> typeIndexList.indexOf(r.getRequestType()))).collect(Collectors.toCollection(LinkedHashSet::new));
for (final IRequestResolver<?> resolver : resolvers) {
// Skip when the resolver is in the blacklist.
if (resolverTokenBlackList.contains(resolver.getRequesterId())) {
continue;
}
// Skip if preliminary check fails
if (!resolver.canResolve(manager, request)) {
continue;
}
@Nullable final List<IToken<?>> attemptResult = resolver.attemptResolve(new WrappedBlacklistAssignmentRequestManager(manager, resolverTokenBlackList), request);
// Skip if attempt failed (aka attemptResult == null)
if (attemptResult == null) {
continue;
}
// Successfully found a resolver. Registering
LogHandler.log("Finished resolver assignment search for request: " + request + " successfully");
ResolverHandler.addRequestToResolver(manager, resolver, request);
for (final IToken<?> childRequestToken : attemptResult) {
@SuppressWarnings(RAWTYPES) final IRequest childRequest = RequestHandler.getRequest(manager, childRequestToken);
childRequest.setParent(request.getToken());
request.addChild(childRequest.getToken());
if (!isAssigned(manager, childRequestToken)) {
assignRequest(manager, childRequest, resolverTokenBlackList);
}
}
if (request.getState().ordinal() < RequestState.IN_PROGRESS.ordinal()) {
request.setState(new WrappedStaticStateRequestManager(manager), RequestState.IN_PROGRESS);
if (!request.hasChildren()) {
resolveRequest(manager, request);
}
}
return resolver.getRequesterId();
}
return null;
}
use of com.minecolonies.api.util.constant.Suppression.RAWTYPES in project minecolonies by Minecolonies.
the class AbstractEntityAIBasic method lookForRequests.
/**
* Utility method to search for items currently needed.
* Poll this until all items are there.
*/
@NotNull
private AIState lookForRequests() {
if (!this.getOwnBuilding().hasWorkerOpenRequestsFiltered(worker.getCitizenData(), r -> !worker.getCitizenData().isRequestAsync(r.getToken())) && !getOwnBuilding().hasCitizenCompletedRequests(worker.getCitizenData())) {
return IDLE;
}
if (!walkToBuilding() && getOwnBuilding().hasCitizenCompletedRequests(worker.getCitizenData())) {
delay += DELAY_RECHECK;
@SuppressWarnings(RAWTYPES) final ImmutableList<IRequest> completedRequests = getOwnBuilding().getCompletedRequests(worker.getCitizenData());
completedRequests.stream().filter(r -> !(r.canBeDelivered())).forEach(r -> getOwnBuilding().markRequestAsAccepted(worker.getCitizenData(), r.getToken()));
@SuppressWarnings(RAWTYPES) final IRequest firstDeliverableRequest = completedRequests.stream().filter(IRequest::canBeDelivered).findFirst().orElse(null);
if (firstDeliverableRequest != null) {
boolean async = false;
if (worker.getCitizenData().isRequestAsync(firstDeliverableRequest.getToken())) {
async = true;
job.getAsyncRequests().remove(firstDeliverableRequest.getToken());
}
getOwnBuilding().markRequestAsAccepted(worker.getCitizenData(), firstDeliverableRequest.getToken());
final ItemStack deliveredItemStack = firstDeliverableRequest.getDelivery();
final int count = InventoryUtils.getItemCountInItemHandler(new InvWrapper(worker.getInventoryCitizen()), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true));
if (count >= deliveredItemStack.getCount() && getTotalRequiredAmount(deliveredItemStack) <= count) {
return NEEDS_ITEM;
}
// Takes one Stack from the hut if existent
if (InventoryUtils.getItemCountInProvider(getOwnBuilding(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true)) >= deliveredItemStack.getCount() && InventoryUtils.transferXOfFirstSlotInProviderWithIntoNextFreeSlotInItemHandler(getOwnBuilding(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true), deliveredItemStack.getCount(), new InvWrapper(worker.getInventoryCitizen()))) {
return NEEDS_ITEM;
} else {
// Lets try this again.
if (async) {
worker.getCitizenData().createRequestAsync(firstDeliverableRequest.getRequest());
} else {
worker.getCitizenData().createRequest(firstDeliverableRequest.getRequest());
}
}
}
}
return NEEDS_ITEM;
}
use of com.minecolonies.api.util.constant.Suppression.RAWTYPES in project minecolonies by Minecolonies.
the class WindowCitizen method getOpenRequestsOfCitizen.
@SuppressWarnings(RAWTYPES)
private ImmutableList<IRequest> getOpenRequestsOfCitizen() {
final ArrayList<IRequest> requests = new ArrayList<>();
if (citizen.getWorkBuilding() != null) {
requests.addAll(getOpenRequestsOfCitizenFromBuilding(citizen.getWorkBuilding()));
}
if (citizen.getHomeBuilding() != null && !citizen.getHomeBuilding().equals(citizen.getWorkBuilding())) {
requests.addAll(getOpenRequestsOfCitizenFromBuilding(citizen.getHomeBuilding()));
}
final BlockPos playerPos = Minecraft.getMinecraft().player.getPosition();
requests.sort(Comparator.comparing((IRequest request) -> request.getRequester().getDeliveryLocation().getInDimensionLocation().getDistance(playerPos.getX(), playerPos.getY(), playerPos.getZ())).thenComparingInt(request -> request.getToken().hashCode()));
return ImmutableList.copyOf(requests);
}
Aggregations