Search in sources :

Example 21 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class TileEntityFormulaicAssemblicator method getInitialInventory.

@Nonnull
@Override
protected IInventorySlotHolder getInitialInventory() {
    craftingGridSlots = new ArrayList<>();
    inputSlots = new ArrayList<>();
    outputSlots = new ArrayList<>();
    InventorySlotHelper builder = InventorySlotHelper.forSideWithConfig(this::getDirection, this::getConfig);
    builder.addSlot(formulaSlot = FormulaInventorySlot.at(this, 6, 26));
    for (int slotY = 0; slotY < 2; slotY++) {
        for (int slotX = 0; slotX < 9; slotX++) {
            int index = slotY * 9 + slotX;
            InputInventorySlot inputSlot = InputInventorySlot.at(stack -> {
                // Is item valid
                if (formula == null) {
                    return true;
                }
                IntList indices = formula.getIngredientIndices(level, stack);
                if (!indices.isEmpty()) {
                    HashedItem stockItem = stockControlMap[index];
                    if (!stockControl || stockItem == null) {
                        return true;
                    }
                    return ItemHandlerHelper.canItemStacksStack(stockItem.getStack(), stack);
                }
                return false;
            }, item -> true, this, 8 + slotX * 18, 98 + slotY * 18);
            builder.addSlot(inputSlot);
            inputSlots.add(inputSlot);
        }
    }
    for (int slotY = 0; slotY < 3; slotY++) {
        for (int slotX = 0; slotX < 3; slotX++) {
            IInventorySlot craftingSlot = FormulaicCraftingSlot.at(this::getAutoMode, this, 26 + slotX * 18, 17 + slotY * 18);
            builder.addSlot(craftingSlot);
            craftingGridSlots.add(craftingSlot);
        }
    }
    for (int slotY = 0; slotY < 3; slotY++) {
        for (int slotX = 0; slotX < 2; slotX++) {
            OutputInventorySlot outputSlot = OutputInventorySlot.at(this, 116 + slotX * 18, 17 + slotY * 18);
            builder.addSlot(outputSlot);
            outputSlots.add(outputSlot);
        }
    }
    // Add the energy slot after adding the other slots so that it has the lowest priority in shift clicking
    builder.addSlot(energySlot = EnergyInventorySlot.fillOrConvert(energyContainer, this::getLevel, this, 152, 76));
    return builder.build();
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) InputInventorySlot(mekanism.common.inventory.slot.InputInventorySlot) HashedItem(mekanism.common.lib.inventory.HashedItem) InventorySlotHelper(mekanism.common.capabilities.holder.slot.InventorySlotHelper) OutputInventorySlot(mekanism.common.inventory.slot.OutputInventorySlot) IntList(it.unimi.dsi.fastutil.ints.IntList) Nonnull(javax.annotation.Nonnull)

Example 22 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class ItemConfigurator method useOn.

@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
    PlayerEntity player = context.getPlayer();
    World world = context.getLevel();
    if (!world.isClientSide && player != null) {
        BlockPos pos = context.getClickedPos();
        Direction side = context.getClickedFace();
        ItemStack stack = context.getItemInHand();
        TileEntity tile = WorldUtils.getTileEntity(world, pos);
        ConfiguratorMode mode = getMode(stack);
        if (mode.isConfigurating()) {
            // Configurate
            TransmissionType transmissionType = Objects.requireNonNull(mode.getTransmission(), "Configurating state requires transmission type");
            if (tile instanceof ISideConfiguration && ((ISideConfiguration) tile).getConfig().supports(transmissionType)) {
                ISideConfiguration config = (ISideConfiguration) tile;
                ConfigInfo info = config.getConfig().getConfig(transmissionType);
                if (info != null) {
                    RelativeSide relativeSide = RelativeSide.fromDirections(config.getDirection(), side);
                    DataType dataType = info.getDataType(relativeSide);
                    if (!player.isShiftKeyDown()) {
                        player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_VIEW_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
                    } else if (SecurityUtils.canAccess(player, tile)) {
                        if (!player.isCreative()) {
                            IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
                            FloatingLong energyPerConfigure = MekanismConfig.gear.configuratorEnergyPerConfigure.get();
                            if (energyContainer == null || energyContainer.extract(energyPerConfigure, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerConfigure)) {
                                return ActionResultType.FAIL;
                            }
                            energyContainer.extract(energyPerConfigure, Action.EXECUTE, AutomationType.MANUAL);
                        }
                        DataType old = dataType;
                        dataType = info.incrementDataType(relativeSide);
                        if (dataType != old) {
                            player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_TOGGLE_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
                            config.getConfig().sideChanged(transmissionType, relativeSide);
                        }
                    } else {
                        SecurityUtils.displayNoAccess(player);
                    }
                }
                return ActionResultType.SUCCESS;
            }
            if (SecurityUtils.canAccess(player, tile)) {
                Optional<IConfigurable> capability = CapabilityUtils.getCapability(tile, Capabilities.CONFIGURABLE_CAPABILITY, side).resolve();
                if (capability.isPresent()) {
                    IConfigurable config = capability.get();
                    if (player.isShiftKeyDown()) {
                        return config.onSneakRightClick(player, side);
                    }
                    return config.onRightClick(player, side);
                }
            } else {
                SecurityUtils.displayNoAccess(player);
                return ActionResultType.SUCCESS;
            }
        } else if (mode == ConfiguratorMode.EMPTY) {
            // Empty
            if (tile instanceof IMekanismInventory) {
                IMekanismInventory inv = (IMekanismInventory) tile;
                if (inv.hasInventory()) {
                    if (SecurityUtils.canAccess(player, tile)) {
                        boolean creative = player.isCreative();
                        IEnergyContainer energyContainer = creative ? null : StorageUtils.getEnergyContainer(stack, 0);
                        if (!creative && energyContainer == null) {
                            return ActionResultType.FAIL;
                        }
                        // TODO: Switch this to items being handled by TileEntityMekanism, energy handled here (via lambdas?)
                        FloatingLong energyPerItemDump = MekanismConfig.gear.configuratorEnergyPerItem.get();
                        for (IInventorySlot inventorySlot : inv.getInventorySlots(null)) {
                            if (!inventorySlot.isEmpty()) {
                                if (!creative) {
                                    if (energyContainer.extract(energyPerItemDump, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerItemDump)) {
                                        break;
                                    }
                                    energyContainer.extract(energyPerItemDump, Action.EXECUTE, AutomationType.MANUAL);
                                }
                                Block.popResource(world, pos, inventorySlot.getStack().copy());
                                inventorySlot.setEmpty();
                            }
                        }
                        return ActionResultType.SUCCESS;
                    } else {
                        SecurityUtils.displayNoAccess(player);
                        return ActionResultType.FAIL;
                    }
                }
            }
        } else if (mode == ConfiguratorMode.ROTATE) {
            // Rotate
            if (tile instanceof TileEntityMekanism) {
                if (SecurityUtils.canAccess(player, tile)) {
                    TileEntityMekanism tileMekanism = (TileEntityMekanism) tile;
                    if (Attribute.get(tileMekanism.getBlockType(), AttributeStateFacing.class).canRotate()) {
                        if (!player.isShiftKeyDown()) {
                            tileMekanism.setFacing(side);
                        } else if (player.isShiftKeyDown()) {
                            tileMekanism.setFacing(side.getOpposite());
                        }
                    }
                } else {
                    SecurityUtils.displayNoAccess(player);
                }
            }
            return ActionResultType.SUCCESS;
        } else if (mode == ConfiguratorMode.WRENCH) {
            // Wrench
            return ActionResultType.PASS;
        }
    }
    return ActionResultType.PASS;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) World(net.minecraft.world.World) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) Direction(net.minecraft.util.Direction) IConfigurable(mekanism.api.IConfigurable) ConfiguratorMode(mekanism.common.item.ItemConfigurator.ConfiguratorMode) AttributeStateFacing(mekanism.common.block.attribute.AttributeStateFacing) PlayerEntity(net.minecraft.entity.player.PlayerEntity) TileEntity(net.minecraft.tileentity.TileEntity) FloatingLong(mekanism.api.math.FloatingLong) IEnergyContainer(mekanism.api.energy.IEnergyContainer) TransmissionType(mekanism.common.lib.transmitter.TransmissionType) RelativeSide(mekanism.api.RelativeSide) DataType(mekanism.common.tile.component.config.DataType) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration) Nonnull(javax.annotation.Nonnull)

Aggregations

IInventorySlot (mekanism.api.inventory.IInventorySlot)22 ItemStack (net.minecraft.item.ItemStack)12 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)5 SyncableItemStack (mekanism.common.inventory.container.sync.SyncableItemStack)5 HashedItem (mekanism.common.lib.inventory.HashedItem)5 ConfigInfo (mekanism.common.tile.component.config.ConfigInfo)4 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 IntList (it.unimi.dsi.fastutil.ints.IntList)2 IntOpenHashSet (it.unimi.dsi.fastutil.ints.IntOpenHashSet)2 IntSet (it.unimi.dsi.fastutil.ints.IntSet)2 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)2 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)2 HashMap (java.util.HashMap)2 NBTConstants (mekanism.api.NBTConstants)2 IMekanismInventory (mekanism.api.inventory.IMekanismInventory)2 FloatingLong (mekanism.api.math.FloatingLong)2 ComputerMethod (mekanism.common.integration.computer.annotation.ComputerMethod)2 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)2