Search in sources :

Example 6 with IDeliverable

use of com.minecolonies.api.colony.requestsystem.requestable.IDeliverable in project minecolonies by Minecolonies.

the class WindowCitizen method fulfill.

/**
 * On Button click transfert Items and fullfil.
 *
 * @param button the clicked button.
 */
private void fulfill(@NotNull final Button button) {
    final int row = resourceList.getListElementIndexByPane(button);
    if (getOpenRequestTreeOfCitizen().size() > row && row >= 0) {
        @NotNull final IRequest tRequest = getOpenRequestTreeOfCitizen().get(row).getRequest();
        if (!(tRequest.getRequest() instanceof IDeliverable)) {
            return;
        }
        @NotNull final IRequest<? extends IDeliverable> request = (IRequest<? extends IDeliverable>) tRequest;
        final Predicate<ItemStack> requestPredicate = stack -> request.getRequest().matches(stack);
        final int amount = request.getRequest().getCount();
        final int count = InventoryUtils.getItemCountInItemHandler(new InvWrapper(inventory), requestPredicate);
        if (!isCreative && count <= 0) {
            return;
        }
        // The itemStack size should not be greater than itemStack.getMaxStackSize, We send 1 instead
        // and use quantity for the size
        @NotNull final ItemStack itemStack;
        if (isCreative) {
            itemStack = request.getDisplayStacks().stream().findFirst().orElse(ItemStack.EMPTY);
        } else {
            itemStack = inventory.getStackInSlot(InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(inventory), requestPredicate));
        }
        MineColonies.getNetwork().sendToServer(new TransferItemsToCitizenRequestMessage(citizen, itemStack, isCreative ? amount : Math.min(amount, count), citizen.getColonyId()));
        MineColonies.getNetwork().sendToServer(new UpdateRequestStateMessage(citizen.getColonyId(), request.getToken(), RequestState.OVERRULED, itemStack));
    }
    button.disable();
}
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) UpdateRequestStateMessage(com.minecolonies.coremod.network.messages.UpdateRequestStateMessage) TransferItemsToCitizenRequestMessage(com.minecolonies.coremod.network.messages.TransferItemsToCitizenRequestMessage) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull) IDeliverable(com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)

Aggregations

IDeliverable (com.minecolonies.api.colony.requestsystem.requestable.IDeliverable)6 ItemStack (net.minecraft.item.ItemStack)6 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)5 BlockPos (net.minecraft.util.math.BlockPos)5 NotNull (org.jetbrains.annotations.NotNull)5 ImmutableList (com.google.common.collect.ImmutableList)4 TypeToken (com.google.common.reflect.TypeToken)4 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)4 List (java.util.List)4 Nullable (org.jetbrains.annotations.Nullable)4 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)3 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)3 Colony (com.minecolonies.coremod.colony.Colony)3 TileEntity (net.minecraft.tileentity.TileEntity)3 TileEntityChest (net.minecraft.tileentity.TileEntityChest)3 EnumFacing (net.minecraft.util.EnumFacing)3 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)3 Lists (com.google.common.collect.Lists)2 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)2 Stack (com.minecolonies.api.colony.requestsystem.requestable.Stack)2