use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager 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);
}
use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager 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.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.
the class RequestHandler method onRequestCancelled.
/**
* 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 onRequestCancelled(final IStandardRequestManager manager, final IToken<?> token) {
@SuppressWarnings(RAWTYPES) final IRequest request = RequestHandler.getRequest(manager, token);
if (request == null) {
return;
}
request.setState(new WrappedStaticStateRequestManager(manager), RequestState.CANCELLED);
processInternalCancellation(manager, token);
// Notify the requester.
final IRequester requester = request.getRequester();
requester.onRequestCancelled(manager, token);
cleanRequestData(manager, token);
}
use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.
the class StandardRequestManager method updateRequestState.
/**
* Method to update the state of a given request.
*
* @param token The token that represents a given request to update.
* @param state The new state of that request.
* @throws IllegalArgumentException when the token is unknown to this manager.
*/
@Override
public void updateRequestState(@NotNull final IToken<?> token, @NotNull final RequestState state) {
final IRequest<?> request = RequestHandler.getRequest(this, token);
LogHandler.log("Updating request state from:" + token + ". With original state: " + request.getState() + " to : " + state);
request.setState(new WrappedStaticStateRequestManager(this), state);
if (colony != null) {
colony.markDirty();
}
switch(request.getState()) {
case COMPLETED:
LogHandler.log("Request completed: " + token + ". Notifying parent and requester...");
RequestHandler.onRequestSuccessful(this, token);
return;
case OVERRULED:
LogHandler.log("Request overruled: " + token + ". Notifying parent, children and requester...");
RequestHandler.onRequestOverruled(this, token);
break;
case CANCELLED:
LogHandler.log("Request cancelled: " + token + ". Notifying parent, children and requester...");
RequestHandler.onRequestCancelled(this, token);
return;
case RECEIVED:
LogHandler.log("Request received: " + token + ". Removing from system...");
RequestHandler.cleanRequestData(this, token);
return;
default:
}
}
use of com.minecolonies.coremod.colony.requestsystem.management.manager.wrapped.WrappedStaticStateRequestManager in project minecolonies by Minecolonies.
the class ResolverHandler method addRequestToResolver.
/**
* Method used to add a request to a resolver.
* <p>
* <p>
* Is only used internally.
* The method modifies the resolverRequestMap that is used to track which resolver handles which request.
* </p>
*
* @param manager The manager to modify
* @param resolver The resolver to add the request to.
* @param request The request to add to the resolver.
*/
public static void addRequestToResolver(final IStandardRequestManager manager, final IRequestResolver<?> resolver, final IRequest<?> request) {
if (!manager.getRequestResolverRequestAssignmentDataStore().getAssignments().containsKey(resolver.getRequesterId())) {
manager.getRequestResolverRequestAssignmentDataStore().getAssignments().put(resolver.getRequesterId(), new HashSet<>());
}
LogHandler.log("Adding request: " + request + " to resolver: " + resolver);
manager.getRequestResolverRequestAssignmentDataStore().getAssignments().get(resolver.getRequesterId()).add(request.getToken());
request.setState(new WrappedStaticStateRequestManager(manager), RequestState.ASSIGNED);
}
Aggregations