use of com.minecolonies.api.colony.requestsystem.token.IToken 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();
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class DeliveryRequestResolverFactory method deserialize.
@NotNull
@Override
public DeliveryRequestResolver deserialize(@NotNull final IFactoryController controller, @NotNull final NBTTagCompound nbt) {
final IToken token = controller.deserialize(nbt.getCompoundTag(NBT_TOKEN));
final ILocation location = controller.deserialize(nbt.getCompoundTag(NBT_LOCATION));
return new DeliveryRequestResolver(location, token);
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class StandardRetryingRequestResolverFactory method deserialize.
@NotNull
@Override
public StandardRetryingRequestResolver deserialize(@NotNull final IFactoryController controller, @NotNull final NBTTagCompound nbt) {
final Map<IToken<?>, Integer> assignments = NBTUtils.streamCompound(nbt.getTagList(NBT_TRIES, Constants.NBT.TAG_COMPOUND)).map(assignmentCompound -> {
IToken token = controller.deserialize(assignmentCompound.getCompoundTag(NBT_TOKEN));
Integer tries = assignmentCompound.getInteger(NBT_VALUE);
return new HashMap.SimpleEntry<>(token, tries);
}).collect(Collectors.toMap(HashMap.SimpleEntry::getKey, HashMap.SimpleEntry::getValue));
final Map<IToken<?>, Integer> delays = NBTUtils.streamCompound(nbt.getTagList(NBT_DELAYS, Constants.NBT.TAG_COMPOUND)).map(assignmentCompound -> {
IToken token = controller.deserialize(assignmentCompound.getCompoundTag(NBT_TOKEN));
Integer tries = assignmentCompound.getInteger(NBT_VALUE);
return new HashMap.SimpleEntry<>(token, tries);
}).collect(Collectors.toMap(HashMap.SimpleEntry::getKey, HashMap.SimpleEntry::getValue));
final IToken<?> token = controller.deserialize(nbt.getCompoundTag(NBT_TOKEN));
final ILocation location = controller.deserialize(nbt.getCompoundTag(NBT_LOCATION));
final StandardRetryingRequestResolver retryingRequestResolver = new StandardRetryingRequestResolver(token, location);
retryingRequestResolver.updateData(assignments, delays);
return retryingRequestResolver;
}
use of com.minecolonies.api.colony.requestsystem.token.IToken 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.token.IToken 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);
}
Aggregations