Search in sources :

Example 11 with DataType

use of mekanism.common.tile.component.config.DataType in project Mekanism by mekanism.

the class TileComponentEjector method outputItems.

/**
 * @apiNote Ensure that it can eject before calling this method.
 */
private void outputItems(ConfigInfo info) {
    for (DataType dataType : info.getSupportedDataTypes()) {
        if (!dataType.canOutput()) {
            continue;
        }
        ISlotInfo slotInfo = info.getSlotInfo(dataType);
        if (slotInfo instanceof InventorySlotInfo) {
            // Validate the slot info is of the correct type
            Set<Direction> outputs = info.getSidesForData(dataType);
            if (!outputs.isEmpty()) {
                EjectTransitRequest ejectMap = InventoryUtils.getEjectItemMap(new EjectTransitRequest(tile, outputs.iterator().next()), ((InventorySlotInfo) slotInfo).getSlots());
                if (!ejectMap.isEmpty()) {
                    for (Direction side : outputs) {
                        TileEntity target = WorldUtils.getTileEntity(tile.getLevel(), tile.getBlockPos().relative(side));
                        if (target != null) {
                            // Update the side so that if/when the response uses it, it makes sure it is grabbing from the correct side
                            ejectMap.side = side;
                            // If the spot is not loaded just skip trying to eject to it
                            TransitResponse response;
                            if (target instanceof TileEntityLogisticalTransporterBase) {
                                response = ((TileEntityLogisticalTransporterBase) target).getTransmitter().insert(tile, ejectMap, outputColor, true, 0);
                            } else {
                                response = ejectMap.addToInventory(target, side, 0, false);
                            }
                            if (!response.isEmpty()) {
                                // use the items returned by the TransitResponse; will be visible next loop
                                response.useAll();
                                if (ejectMap.isEmpty()) {
                                    // If we are out of items to eject, break
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    tickDelay = 10;
}
Also used : ISlotInfo(mekanism.common.tile.component.config.slot.ISlotInfo) TileEntity(net.minecraft.tileentity.TileEntity) TransitResponse(mekanism.common.lib.inventory.TransitRequest.TransitResponse) DataType(mekanism.common.tile.component.config.DataType) InventorySlotInfo(mekanism.common.tile.component.config.slot.InventorySlotInfo) Direction(net.minecraft.util.Direction) TileEntityLogisticalTransporterBase(mekanism.common.tile.transmitter.TileEntityLogisticalTransporterBase)

Example 12 with DataType

use of mekanism.common.tile.component.config.DataType in project Mekanism by mekanism.

the class GuiMekanism method addSlots.

protected void addSlots() {
    int size = menu.slots.size();
    for (int i = 0; i < size; i++) {
        Slot slot = menu.slots.get(i);
        if (slot instanceof InventoryContainerSlot) {
            InventoryContainerSlot containerSlot = (InventoryContainerSlot) slot;
            ContainerSlotType slotType = containerSlot.getSlotType();
            DataType dataType = findDataType(containerSlot);
            // Shift the slots by one as the elements include the border of the slot
            SlotType type;
            if (dataType != null) {
                type = SlotType.get(dataType);
            } else if (slotType == ContainerSlotType.INPUT || slotType == ContainerSlotType.OUTPUT || slotType == ContainerSlotType.EXTRA) {
                type = SlotType.NORMAL;
            } else if (slotType == ContainerSlotType.POWER) {
                type = SlotType.POWER;
            } else if (slotType == ContainerSlotType.NORMAL || slotType == ContainerSlotType.VALIDITY) {
                type = SlotType.NORMAL;
            } else {
                // slotType == ContainerSlotType.IGNORED: don't do anything
                continue;
            }
            GuiSlot guiSlot = new GuiSlot(type, this, slot.x - 1, slot.y - 1);
            SlotOverlay slotOverlay = containerSlot.getSlotOverlay();
            if (slotOverlay != null) {
                guiSlot.with(slotOverlay);
            }
            if (slotType == ContainerSlotType.VALIDITY) {
                int index = i;
                guiSlot.validity(() -> checkValidity(index));
            }
            addButton(guiSlot);
        } else {
            addButton(new GuiSlot(SlotType.NORMAL, this, slot.x - 1, slot.y - 1));
        }
    }
}
Also used : SlotOverlay(mekanism.common.inventory.container.slot.SlotOverlay) ContainerSlotType(mekanism.common.inventory.container.slot.ContainerSlotType) GuiSlot(mekanism.client.gui.element.slot.GuiSlot) IVirtualSlot(mekanism.common.inventory.container.slot.IVirtualSlot) Slot(net.minecraft.inventory.container.Slot) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) GuiVirtualSlot(mekanism.client.gui.element.slot.GuiVirtualSlot) DataType(mekanism.common.tile.component.config.DataType) GuiSlot(mekanism.client.gui.element.slot.GuiSlot) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) SlotType(mekanism.client.gui.element.slot.SlotType) ContainerSlotType(mekanism.common.inventory.container.slot.ContainerSlotType)

Example 13 with DataType

use of mekanism.common.tile.component.config.DataType in project Mekanism by mekanism.

the class GuiMekanismTile method getFromSlot.

private DataType getFromSlot(Slot slot) {
    if (slot.index < tile.getSlots() && slot instanceof InventoryContainerSlot) {
        ISideConfiguration config = (ISideConfiguration) tile;
        ConfigInfo info = config.getConfig().getConfig(TransmissionType.ITEM);
        if (info != null) {
            Set<DataType> supportedDataTypes = info.getSupportedDataTypes();
            IInventorySlot inventorySlot = ((InventoryContainerSlot) slot).getInventorySlot();
            for (DataType type : supportedDataTypes) {
                ISlotInfo slotInfo = info.getSlotInfo(type);
                if (slotInfo instanceof InventorySlotInfo && ((InventorySlotInfo) slotInfo).hasSlot(inventorySlot)) {
                    return type;
                }
            }
        }
    }
    return null;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) ISlotInfo(mekanism.common.tile.component.config.slot.ISlotInfo) DataType(mekanism.common.tile.component.config.DataType) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) InventorySlotInfo(mekanism.common.tile.component.config.slot.InventorySlotInfo) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration)

Example 14 with DataType

use of mekanism.common.tile.component.config.DataType in project Mekanism by mekanism.

the class PacketConfigurationUpdate method handle.

@Override
public void handle(NetworkEvent.Context context) {
    PlayerEntity player = context.getSender();
    if (player == null) {
        return;
    }
    TileEntity tile = WorldUtils.getTileEntity(player.level, pos);
    if (tile instanceof ISideConfiguration) {
        ISideConfiguration config = (ISideConfiguration) tile;
        if (packetType == ConfigurationPacket.EJECT) {
            ConfigInfo info = config.getConfig().getConfig(transmission);
            if (info != null) {
                info.setEjecting(!info.isEjecting());
                WorldUtils.saveChunk(tile);
            }
        } else if (packetType == ConfigurationPacket.CLEAR_ALL) {
            TileComponentConfig configComponent = config.getConfig();
            ConfigInfo info = configComponent.getConfig(transmission);
            if (info != null) {
                for (RelativeSide side : EnumUtils.SIDES) {
                    if (info.isSideEnabled(side) && info.getDataType(side) != DataType.NONE) {
                        info.setDataType(DataType.NONE, side);
                        configComponent.sideChanged(transmission, side);
                    }
                }
            }
        } else if (packetType == ConfigurationPacket.SIDE_DATA) {
            TileComponentConfig configComponent = config.getConfig();
            ConfigInfo info = configComponent.getConfig(transmission);
            if (info != null) {
                DataType type = info.getDataType(inputSide);
                boolean changed = false;
                if (clickType == 0) {
                    changed = type != info.incrementDataType(inputSide);
                } else if (clickType == 1) {
                    changed = type != info.decrementDataType(inputSide);
                } else if (clickType == 2 && type != DataType.NONE) {
                    // We only need to update it if we are changing it to none
                    changed = true;
                    info.setDataType(DataType.NONE, inputSide);
                }
                if (changed) {
                    configComponent.sideChanged(transmission, inputSide);
                }
            }
        } else if (packetType == ConfigurationPacket.EJECT_COLOR) {
            TileComponentEjector ejector = config.getEjector();
            if (clickType == 0) {
                ejector.setOutputColor(TransporterUtils.increment(ejector.getOutputColor()));
            } else if (clickType == 1) {
                ejector.setOutputColor(TransporterUtils.decrement(ejector.getOutputColor()));
            } else if (clickType == 2) {
                ejector.setOutputColor(null);
            }
        } else if (packetType == ConfigurationPacket.INPUT_COLOR) {
            TileComponentEjector ejector = config.getEjector();
            if (clickType == 0) {
                ejector.setInputColor(inputSide, TransporterUtils.increment(ejector.getInputColor(inputSide)));
            } else if (clickType == 1) {
                ejector.setInputColor(inputSide, TransporterUtils.decrement(ejector.getInputColor(inputSide)));
            } else if (clickType == 2) {
                ejector.setInputColor(inputSide, null);
            }
        } else if (packetType == ConfigurationPacket.STRICT_INPUT) {
            TileComponentEjector ejector = config.getEjector();
            ejector.setStrictInput(!ejector.hasStrictInput());
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileComponentEjector(mekanism.common.tile.component.TileComponentEjector) RelativeSide(mekanism.api.RelativeSide) DataType(mekanism.common.tile.component.config.DataType) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) TileComponentConfig(mekanism.common.tile.component.TileComponentConfig) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration)

Example 15 with DataType

use of mekanism.common.tile.component.config.DataType 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

DataType (mekanism.common.tile.component.config.DataType)15 ConfigInfo (mekanism.common.tile.component.config.ConfigInfo)9 RelativeSide (mekanism.api.RelativeSide)6 ISideConfiguration (mekanism.common.tile.interfaces.ISideConfiguration)6 TransmissionType (mekanism.common.lib.transmitter.TransmissionType)5 TileEntity (net.minecraft.tileentity.TileEntity)5 Direction (net.minecraft.util.Direction)5 IInventorySlot (mekanism.api.inventory.IInventorySlot)4 EnumColor (mekanism.api.text.EnumColor)4 ItemStack (net.minecraft.item.ItemStack)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)4 ItemConfigurator (mekanism.common.item.ItemConfigurator)3 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)3 TileComponentConfig (mekanism.common.tile.component.TileComponentConfig)3 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 Nullable (javax.annotation.Nullable)2 IEnergyContainer (mekanism.api.energy.IEnergyContainer)2 IExtendedFluidTank (mekanism.api.fluid.IExtendedFluidTank)2 InventoryContainerSlot (mekanism.common.inventory.container.slot.InventoryContainerSlot)2