Search in sources :

Example 1 with WrappedBlacklistAssignmentRequestManager

use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager 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;
}
Also used : RequestMappingHandler(com.minecolonies.api.colony.requestsystem.manager.RequestMappingHandler) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) java.util(java.util) UNCHECKED(com.minecolonies.api.util.constant.Suppression.UNCHECKED) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) ImmutableCollection(com.google.common.collect.ImmutableCollection) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) TypeToken(com.google.common.reflect.TypeToken) IRequestable(com.minecolonies.api.colony.requestsystem.requestable.IRequestable) Collectors(java.util.stream.Collectors) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager) Nullable(org.jetbrains.annotations.Nullable) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) ImmutableList(com.google.common.collect.ImmutableList) MineColonies(com.minecolonies.coremod.MineColonies) RAWTYPES(com.minecolonies.api.util.constant.Suppression.RAWTYPES) ReflectionUtils(com.minecolonies.api.util.ReflectionUtils) NotNull(org.jetbrains.annotations.NotNull) AssigningStrategy(com.minecolonies.api.colony.requestsystem.manager.AssigningStrategy) WrappedBlacklistAssignmentRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager) IStandardRequestManager(com.minecolonies.coremod.colony.requestsystem.management.IStandardRequestManager) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) WrappedBlacklistAssignmentRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) TypeToken(com.google.common.reflect.TypeToken) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Nullable(org.jetbrains.annotations.Nullable) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Aggregations

ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 AssigningStrategy (com.minecolonies.api.colony.requestsystem.manager.AssigningStrategy)1 RequestMappingHandler (com.minecolonies.api.colony.requestsystem.manager.RequestMappingHandler)1 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)1 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)1 IRequestable (com.minecolonies.api.colony.requestsystem.requestable.IRequestable)1 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)1 IRequestResolver (com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver)1 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)1 ReflectionUtils (com.minecolonies.api.util.ReflectionUtils)1 RAWTYPES (com.minecolonies.api.util.constant.Suppression.RAWTYPES)1 UNCHECKED (com.minecolonies.api.util.constant.Suppression.UNCHECKED)1 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)1 MineColonies (com.minecolonies.coremod.MineColonies)1 IStandardRequestManager (com.minecolonies.coremod.colony.requestsystem.management.IStandardRequestManager)1 WrappedBlacklistAssignmentRequestManager (com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedBlacklistAssignmentRequestManager)1 WrappedStaticStateRequestManager (com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)1 java.util (java.util)1