Search in sources :

Example 1 with RAWTYPES

use of com.minecolonies.api.util.constant.Suppression.RAWTYPES 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 2 with RAWTYPES

use of com.minecolonies.api.util.constant.Suppression.RAWTYPES in project minecolonies by Minecolonies.

the class AbstractEntityAIBasic method lookForRequests.

/**
 * Utility method to search for items currently needed.
 * Poll this until all items are there.
 */
@NotNull
private AIState lookForRequests() {
    if (!this.getOwnBuilding().hasWorkerOpenRequestsFiltered(worker.getCitizenData(), r -> !worker.getCitizenData().isRequestAsync(r.getToken())) && !getOwnBuilding().hasCitizenCompletedRequests(worker.getCitizenData())) {
        return IDLE;
    }
    if (!walkToBuilding() && getOwnBuilding().hasCitizenCompletedRequests(worker.getCitizenData())) {
        delay += DELAY_RECHECK;
        @SuppressWarnings(RAWTYPES) final ImmutableList<IRequest> completedRequests = getOwnBuilding().getCompletedRequests(worker.getCitizenData());
        completedRequests.stream().filter(r -> !(r.canBeDelivered())).forEach(r -> getOwnBuilding().markRequestAsAccepted(worker.getCitizenData(), r.getToken()));
        @SuppressWarnings(RAWTYPES) final IRequest firstDeliverableRequest = completedRequests.stream().filter(IRequest::canBeDelivered).findFirst().orElse(null);
        if (firstDeliverableRequest != null) {
            boolean async = false;
            if (worker.getCitizenData().isRequestAsync(firstDeliverableRequest.getToken())) {
                async = true;
                job.getAsyncRequests().remove(firstDeliverableRequest.getToken());
            }
            getOwnBuilding().markRequestAsAccepted(worker.getCitizenData(), firstDeliverableRequest.getToken());
            final ItemStack deliveredItemStack = firstDeliverableRequest.getDelivery();
            final int count = InventoryUtils.getItemCountInItemHandler(new InvWrapper(worker.getInventoryCitizen()), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true));
            if (count >= deliveredItemStack.getCount() && getTotalRequiredAmount(deliveredItemStack) <= count) {
                return NEEDS_ITEM;
            }
            // Takes one Stack from the hut if existent
            if (InventoryUtils.getItemCountInProvider(getOwnBuilding(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true)) >= deliveredItemStack.getCount() && InventoryUtils.transferXOfFirstSlotInProviderWithIntoNextFreeSlotInItemHandler(getOwnBuilding(), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(deliveredItemStack, stack, true, true), deliveredItemStack.getCount(), new InvWrapper(worker.getInventoryCitizen()))) {
                return NEEDS_ITEM;
            } else {
                // Lets try this again.
                if (async) {
                    worker.getCitizenData().createRequestAsync(firstDeliverableRequest.getRequest());
                } else {
                    worker.getCitizenData().createRequest(firstDeliverableRequest.getRequest());
                }
            }
        }
    }
    return NEEDS_ITEM;
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) InventoryCitizen(com.minecolonies.coremod.inventory.InventoryCitizen) Blocks(net.minecraft.init.Blocks) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) TypeToken(com.google.common.reflect.TypeToken) AbstractJob(com.minecolonies.coremod.colony.jobs.AbstractJob) TextComponentBase(net.minecraft.util.text.TextComponentBase) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) TileEntityChest(net.minecraft.tileentity.TileEntityChest) ArrayList(java.util.ArrayList) JobDeliveryman(com.minecolonies.coremod.colony.jobs.JobDeliveryman) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) AITarget(com.minecolonies.coremod.entity.ai.util.AITarget) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Block(net.minecraft.block.Block) RAWTYPES(com.minecolonies.api.util.constant.Suppression.RAWTYPES) Tool(com.minecolonies.api.colony.requestsystem.requestable.Tool) com.minecolonies.api.util(com.minecolonies.api.util) Stack(com.minecolonies.api.colony.requestsystem.requestable.Stack) Constants(com.minecolonies.api.util.constant.Constants) TranslationConstants(com.minecolonies.api.util.constant.TranslationConstants) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) IWalkToProxy(com.minecolonies.api.entity.ai.pathfinding.IWalkToProxy) EntityCitizenWalkToProxy(com.minecolonies.coremod.entity.pathfinding.EntityCitizenWalkToProxy) TOOL_LEVEL_WOOD_OR_GOLD(com.minecolonies.api.util.constant.ToolLevelConstants.TOOL_LEVEL_WOOD_OR_GOLD) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EnumFacing(net.minecraft.util.EnumFacing) ItemFood(net.minecraft.item.ItemFood) BlockPos(net.minecraft.util.math.BlockPos) SATURATION_DECREASE_FACTOR(com.minecolonies.api.util.constant.CitizenConstants.SATURATION_DECREASE_FACTOR) AIState(com.minecolonies.coremod.entity.ai.util.AIState) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TypeConstants(com.minecolonies.api.util.constant.TypeConstants) AbstractBuildingWorker(com.minecolonies.coremod.colony.buildings.AbstractBuildingWorker) WorkerUtil(com.minecolonies.coremod.util.WorkerUtil) TileEntity(net.minecraft.tileentity.TileEntity) ToolType(com.minecolonies.api.util.constant.ToolType) MAX_PRIO(com.minecolonies.coremod.colony.buildings.AbstractBuilding.MAX_PRIO) ItemStorage(com.minecolonies.api.crafting.ItemStorage) IToolType(com.minecolonies.api.util.constant.IToolType) NotNull(org.jetbrains.annotations.NotNull) HIGH_SATURATION(com.minecolonies.api.util.constant.CitizenConstants.HIGH_SATURATION) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with RAWTYPES

use of com.minecolonies.api.util.constant.Suppression.RAWTYPES in project minecolonies by Minecolonies.

the class WindowCitizen method getOpenRequestsOfCitizen.

@SuppressWarnings(RAWTYPES)
private ImmutableList<IRequest> getOpenRequestsOfCitizen() {
    final ArrayList<IRequest> requests = new ArrayList<>();
    if (citizen.getWorkBuilding() != null) {
        requests.addAll(getOpenRequestsOfCitizenFromBuilding(citizen.getWorkBuilding()));
    }
    if (citizen.getHomeBuilding() != null && !citizen.getHomeBuilding().equals(citizen.getWorkBuilding())) {
        requests.addAll(getOpenRequestsOfCitizenFromBuilding(citizen.getHomeBuilding()));
    }
    final BlockPos playerPos = Minecraft.getMinecraft().player.getPosition();
    requests.sort(Comparator.comparing((IRequest request) -> request.getRequester().getDeliveryLocation().getInDimensionLocation().getDistance(playerPos.getX(), playerPos.getY(), playerPos.getZ())).thenComparingInt(request -> request.getToken().hashCode()));
    return ImmutableList.copyOf(requests);
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) SwitchView(com.minecolonies.blockout.views.SwitchView) ExperienceUtils(com.minecolonies.coremod.util.ExperienceUtils) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ColonyView(com.minecolonies.coremod.colony.ColonyView) IRequestManager(com.minecolonies.api.colony.requestsystem.manager.IRequestManager) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) CitizenData(com.minecolonies.coremod.colony.CitizenData) Gui(net.minecraft.client.gui.Gui) ImmutableList(com.google.common.collect.ImmutableList) ColonyManager(com.minecolonies.coremod.colony.ColonyManager) Minecraft(net.minecraft.client.Minecraft) RAWTYPES(com.minecolonies.api.util.constant.Suppression.RAWTYPES) Constants(com.minecolonies.api.util.constant.Constants) com.minecolonies.blockout.controls(com.minecolonies.blockout.controls) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable) LanguageHandler(com.minecolonies.api.util.LanguageHandler) Predicate(java.util.function.Predicate) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) TransferItemsToCitizenRequestMessage(com.minecolonies.coremod.network.messages.TransferItemsToCitizenRequestMessage) BlockPos(net.minecraft.util.math.BlockPos) Pane(com.minecolonies.blockout.Pane) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) ScrollingList(com.minecolonies.blockout.views.ScrollingList) CitizenDataView(com.minecolonies.coremod.colony.CitizenDataView) Alignment(com.minecolonies.blockout.Alignment) UpdateRequestStateMessage(com.minecolonies.coremod.network.messages.UpdateRequestStateMessage) AbstractBuildingView(com.minecolonies.coremod.colony.buildings.views.AbstractBuildingView) GuiScreen(net.minecraft.client.gui.GuiScreen) List(java.util.List) FMLClientHandler(net.minecraftforge.fml.client.FMLClientHandler) MineColonies(com.minecolonies.coremod.MineColonies) InventoryUtils(com.minecolonies.api.util.InventoryUtils) View(com.minecolonies.blockout.views.View) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) Box(com.minecolonies.blockout.views.Box) OpenInventoryMessage(com.minecolonies.coremod.network.messages.OpenInventoryMessage) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)3 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)3 RAWTYPES (com.minecolonies.api.util.constant.Suppression.RAWTYPES)3 NotNull (org.jetbrains.annotations.NotNull)3 TypeToken (com.google.common.reflect.TypeToken)2 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)2 IDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)2 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)2 Constants (com.minecolonies.api.util.constant.Constants)2 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)2 MineColonies (com.minecolonies.coremod.MineColonies)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Predicate (java.util.function.Predicate)2 ItemStack (net.minecraft.item.ItemStack)2 BlockPos (net.minecraft.util.math.BlockPos)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 Lists (com.google.common.collect.Lists)1 AssigningStrategy (com.minecolonies.api.colony.requestsystem.manager.AssigningStrategy)1