Search in sources :

Example 1 with IRequestResolver

use of com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver in project minecolonies by Minecolonies.

the class RequestHandler method resolveRequest.

/**
 * Method used to resolve a request.
 * When this method is called the given request has to be assigned.
 *
 * @param manager The manager requesting the resolving.
 * @param request The request about to be resolved.
 * @throws IllegalArgumentException when the request is unknown, not resolved, or cannot be resolved.
 */
@SuppressWarnings({ UNCHECKED, RAWTYPES })
public static void resolveRequest(final IStandardRequestManager manager, final IRequest request) {
    getRequest(manager, request.getToken());
    if (!isAssigned(manager, request.getToken())) {
        throw new IllegalArgumentException("The given request is not resolved");
    }
    if (request.getState() != RequestState.IN_PROGRESS) {
        throw new IllegalArgumentException("The given request is not in the right state. Required: " + RequestState.IN_PROGRESS + " - Found:" + request.getState());
    }
    if (request.hasChildren()) {
        throw new IllegalArgumentException("Cannot resolve request with open Children");
    }
    final IRequestResolver resolver = ResolverHandler.getResolverForRequest(manager, request);
    request.setState(new WrappedStaticStateRequestManager(manager), RequestState.IN_PROGRESS);
    resolver.resolve(manager, request);
}
Also used : IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) WrappedStaticStateRequestManager(com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)

Example 2 with IRequestResolver

use of com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver 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)

Example 3 with IRequestResolver

use of com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver in project minecolonies by Minecolonies.

the class RequestHandler method onRequestSuccessful.

/**
 * Method used to handle the successful resolving of a request.
 *
 * @param manager The manager that got notified of the successful resolving of the request.
 * @param token   The token of the request that got finished successfully.
 */
@SuppressWarnings(UNCHECKED)
public static void onRequestSuccessful(final IStandardRequestManager manager, final IToken<?> token) {
    @SuppressWarnings(RAWTYPES) final IRequest request = getRequest(manager, token);
    @SuppressWarnings(RAWTYPES) final IRequestResolver resolver = ResolverHandler.getResolverForRequest(manager, token);
    request.getRequester().onRequestComplete(manager, token);
    // Retrieve a followup request.
    @SuppressWarnings(RAWTYPES) final IRequest followupRequest = resolver.getFollowupRequestForCompletion(manager, request);
    // Check if the request has a parent
    if (request.hasParent()) {
        @SuppressWarnings(RAWTYPES) final IRequest parentRequest = getRequest(manager, request.getParent());
        // Assign the followup to the parent as a child so that processing is still halted.
        if (followupRequest != null) {
            parentRequest.addChild(followupRequest.getToken());
        }
        manager.updateRequestState(request.getToken(), RequestState.RECEIVED);
        parentRequest.removeChild(request.getToken());
        if (!parentRequest.hasChildren() && parentRequest.getState() == RequestState.IN_PROGRESS) {
            resolveRequest(manager, parentRequest);
        }
        request.setParent(null);
    }
    // Assign the followup request if need be
    if (followupRequest != null && !isAssigned(manager, followupRequest.getToken())) {
        assignRequest(manager, followupRequest);
    }
}
Also used : IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest)

Example 4 with IRequestResolver

use of com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver in project minecolonies by Minecolonies.

the class BuildingDeliveryman method getResolvers.

@Override
public ImmutableCollection<IRequestResolver<?>> getResolvers() {
    final ImmutableCollection<IRequestResolver<?>> supers = super.getResolvers();
    final ImmutableList.Builder<IRequestResolver<?>> builder = ImmutableList.builder();
    builder.addAll(supers);
    builder.add(new DeliveryRequestResolver(getRequester().getRequesterLocation(), getColony().getRequestManager().getFactoryController().getNewInstance(TypeConstants.ITOKEN)));
    return builder.build();
}
Also used : DeliveryRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.DeliveryRequestResolver) ImmutableList(com.google.common.collect.ImmutableList) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver)

Example 5 with IRequestResolver

use of com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver in project minecolonies by Minecolonies.

the class WindowRequestDetail method onOpened.

/**
 * Called when the GUI has been opened.
 * Will fill the fields and lists.
 */
@Override
public void onOpened() {
    final String[] labels = new String[] { request.getLongDisplayString().getFormattedText() };
    final Box box = findPaneOfTypeByID(BOX_ID_REQUEST, Box.class);
    int y = Y_OFFSET_EACH_TEXTFIELD;
    final int availableLabelWidth = box.getInteriorWidth() - 1 - box.getX();
    for (final String s : labels) {
        final String labelText = "§r§0" + s;
        // Temporary workaround until Labels support multi-line rendering
        final List<String> multilineLabelStrings = mc.fontRenderer.listFormattedStringToWidth(labelText, availableLabelWidth);
        for (final String splitLabelText : multilineLabelStrings) {
            final Label descriptionLabel = new Label();
            descriptionLabel.setColor(BLACK, BLACK);
            descriptionLabel.setLabelText(splitLabelText);
            box.addChild(descriptionLabel);
            descriptionLabel.setPosition(1, y);
            y += Y_OFFSET_EACH_TEXTFIELD;
        }
    }
    final ItemIcon exampleStackDisplay = findPaneOfTypeByID(LIST_ELEMENT_ID_REQUEST_STACK, ItemIcon.class);
    final List<ItemStack> displayStacks = request.getDisplayStacks();
    if (!displayStacks.isEmpty()) {
        exampleStackDisplay.setItem(displayStacks.get((lifeCount / LIFE_COUNT_DIVIDER) % displayStacks.size()));
    } else {
        final Image logo = findPaneOfTypeByID(DELIVERY_IMAGE, Image.class);
        logo.setVisible(true);
        logo.setImage(request.getDisplayIcon());
    }
    final ColonyView view = ColonyManager.getColonyView(colonyId);
    findPaneOfTypeByID(REQUESTER, Label.class).setLabelText(request.getRequester().getDisplayName(view.getRequestManager(), request.getToken()).getFormattedText());
    final Label targetLabel = findPaneOfTypeByID(LIST_ELEMENT_ID_REQUEST_LOCATION, Label.class);
    targetLabel.setLabelText(request.getRequester().getDeliveryLocation().toString());
    final ColonyView colony = ColonyManager.getColonyView(colonyId);
    if (colony == null) {
        Log.getLogger().warn("---Colony Null in WindowRequestDetail---");
        return;
    }
    try {
        final IRequestResolver resolver = colony.getRequestManager().getResolverForRequest(request.getToken());
        if (resolver == null) {
            Log.getLogger().warn("---IRequestResolver Null in WindowRequestDetail---");
            return;
        }
        findPaneOfTypeByID(RESOLVER, Label.class).setLabelText("Resolver: " + resolver.getDisplayName(view.getRequestManager(), request.getToken()).getFormattedText());
    } catch (@SuppressWarnings(EXCEPTION_HANDLERS_SHOULD_PRESERVE_THE_ORIGINAL_EXCEPTIONS) final IllegalArgumentException e) {
        /**
         * Do nothing we just need to know if it has a resolver or not.
         */
        Log.getLogger().warn("---IRequestResolver Null in WindowRequestDetail---", e);
    }
    box.setSize(box.getWidth(), y);
}
Also used : ColonyView(com.minecolonies.coremod.colony.ColonyView) IRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver) Box(com.minecolonies.blockout.views.Box) ItemStack(net.minecraft.item.ItemStack)

Aggregations

IRequestResolver (com.minecolonies.api.colony.requestsystem.resolver.IRequestResolver)7 ImmutableList (com.google.common.collect.ImmutableList)3 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)3 WrappedStaticStateRequestManager (com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)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 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 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 Box (com.minecolonies.blockout.views.Box)1 MineColonies (com.minecolonies.coremod.MineColonies)1 ColonyView (com.minecolonies.coremod.colony.ColonyView)1 IStandardRequestManager (com.minecolonies.coremod.colony.requestsystem.management.IStandardRequestManager)1