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);
}
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;
}
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);
}
}
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();
}
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);
}
Aggregations