Search in sources :

Example 1 with IRequest

use of com.minecolonies.api.colony.requestsystem.request.IRequest in project minecolonies by Minecolonies.

the class DeliveryRequestResolver method attemptResolve.

@Nullable
@Override
public List<IToken<?>> attemptResolve(@NotNull final IRequestManager manager, @NotNull final IRequest<? extends Delivery> request) {
    if (manager.getColony().getWorld().isRemote) {
        return null;
    }
    final Colony colony = (Colony) manager.getColony();
    // We can do an instant get here, since we are already filtering on anything that has no entity.
    final CitizenData freeDeliveryMan = colony.getCitizenManager().getCitizens().stream().filter(citizenData -> citizenData.getCitizenEntity().map(entityCitizen -> request.getRequest().getTarget().isReachableFromLocation(entityCitizen.getLocation())).orElse(false)).filter(c -> c.getJob() instanceof JobDeliveryman).min(Comparator.comparing((CitizenData c) -> ((JobDeliveryman) c.getJob()).getTaskQueue().size()).thenComparing(Comparator.comparing(c -> {
        BlockPos targetPos = request.getRequest().getTarget().getInDimensionLocation();
        // We can do an instant get here, since we are already filtering on anything that has no entity.
        BlockPos entityLocation = c.getCitizenEntity().get().getLocation().getInDimensionLocation();
        return BlockPosUtil.getDistanceSquared(targetPos, entityLocation);
    }))).orElse(null);
    if (freeDeliveryMan == null) {
        return null;
    }
    final JobDeliveryman job = (JobDeliveryman) freeDeliveryMan.getJob();
    job.addRequest(request.getToken());
    return Lists.newArrayList();
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) BlockPos(net.minecraft.util.math.BlockPos) TypeToken(com.google.common.reflect.TypeToken) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) Log(com.minecolonies.blockout.Log) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Nullable(org.jetbrains.annotations.Nullable) CitizenData(com.minecolonies.coremod.colony.CitizenData) List(java.util.List) Lists(com.google.common.collect.Lists) MineColonies(com.minecolonies.coremod.MineColonies) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) AbstractRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.core.AbstractRequestResolver) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData) BlockPos(net.minecraft.util.math.BlockPos) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with IRequest

use of com.minecolonies.api.colony.requestsystem.request.IRequest in project minecolonies by Minecolonies.

the class DeliveryRequestResolver method canResolve.

@Override
public boolean canResolve(@NotNull final IRequestManager manager, final IRequest<? extends Delivery> requestToCheck) {
    if (manager.getColony().getWorld().isRemote) {
        return false;
    }
    final Colony colony = (Colony) manager.getColony();
    final CitizenData freeDeliveryMan = colony.getCitizenManager().getCitizens().stream().filter(citizenData -> citizenData.getCitizenEntity().map(entityCitizen -> requestToCheck.getRequest().getTarget().isReachableFromLocation(entityCitizen.getLocation())).orElse(false)).filter(c -> c.getJob() instanceof JobDeliveryman).findFirst().orElse(null);
    return freeDeliveryMan != null;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) Colony(com.minecolonies.coremod.colony.Colony) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) BlockPos(net.minecraft.util.math.BlockPos) TypeToken(com.google.common.reflect.TypeToken) BlockPosUtil(com.minecolonies.api.util.BlockPosUtil) Log(com.minecolonies.blockout.Log) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Nullable(org.jetbrains.annotations.Nullable) CitizenData(com.minecolonies.coremod.colony.CitizenData) List(java.util.List) Lists(com.google.common.collect.Lists) MineColonies(com.minecolonies.coremod.MineColonies) Delivery(com.minecolonies.api.colony.requestsystem.requestable.Delivery) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) AbstractRequestResolver(com.minecolonies.coremod.colony.requestsystem.resolvers.core.AbstractRequestResolver) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 3 with IRequest

use of com.minecolonies.api.colony.requestsystem.request.IRequest 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 4 with IRequest

use of com.minecolonies.api.colony.requestsystem.request.IRequest in project minecolonies by Minecolonies.

the class RequestHandler method onRequestOverruled.

/**
 * Method used to handle requests that were overruled or cancelled.
 * Cancels all children first, handles the creation of clean up requests.
 *
 * @param manager The manager that got notified of the cancellation or overruling.
 * @param token   The token of the request that got cancelled or overruled
 */
@SuppressWarnings(UNCHECKED)
public static void onRequestOverruled(final IStandardRequestManager manager, final IToken<?> token) {
    @SuppressWarnings(RAWTYPES) final IRequest request = getRequest(manager, token);
    if (manager.getRequestResolverRequestAssignmentDataStore().getAssignmentForValue(token) == null) {
        manager.getRequestIdentitiesDataStore().getIdentities().remove(token);
        return;
    }
    // Lets cancel all our children first, else this would make a big fat mess.
    if (request.hasChildren()) {
        final ImmutableCollection<IToken<?>> currentChildren = request.getChildren();
        currentChildren.forEach(t -> onRequestCancelled(manager, t));
    }
    // Notify the resolver.
    ResolverHandler.getResolverForRequest(manager, token).onRequestBeingOverruled(manager, request);
    // This will notify everyone :D
    manager.updateRequestState(token, RequestState.COMPLETED);
}
Also used : IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest)

Example 5 with IRequest

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

Aggregations

IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)22 NotNull (org.jetbrains.annotations.NotNull)13 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)11 Nullable (org.jetbrains.annotations.Nullable)11 BlockPos (net.minecraft.util.math.BlockPos)10 List (java.util.List)9 ItemStack (net.minecraft.item.ItemStack)9 ImmutableList (com.google.common.collect.ImmutableList)8 TypeToken (com.google.common.reflect.TypeToken)7 Lists (com.google.common.collect.Lists)6 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)6 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)6 IDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)6 MineColonies (com.minecolonies.coremod.MineColonies)6 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)6 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)5 RAWTYPES (com.minecolonies.api.util.constant.Suppression.RAWTYPES)5 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)5 ArrayList (java.util.ArrayList)5 Delivery (com.minecolonies.api.colony.requestsystem.requestable.Delivery)4