Search in sources :

Example 11 with Parameter

use of mcjty.rftoolscontrol.api.parameters.Parameter in project RFToolsControl by McJty.

the class VariableScreenModule method getData.

@Override
public ModuleDataVariable getData(IScreenDataHelper h, World worldObj, long millis) {
    World world = DimensionManager.getWorld(dim);
    if (world == null) {
        return null;
    }
    if (!WorldTools.chunkLoaded(world, coordinate)) {
        return null;
    }
    Block block = world.getBlockState(coordinate).getBlock();
    if (block != ModBlocks.processorBlock) {
        return null;
    }
    if (varIdx < 0 || varIdx >= ProcessorTileEntity.MAXVARS) {
        return null;
    }
    TileEntity te = world.getTileEntity(coordinate);
    if (te instanceof ProcessorTileEntity) {
        ProcessorTileEntity processor = (ProcessorTileEntity) te;
        Parameter parameter = processor.getParameter(varIdx);
        return new ModuleDataVariable(parameter);
    }
    return null;
}
Also used : ProcessorTileEntity(mcjty.rftoolscontrol.blocks.processor.ProcessorTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ProcessorTileEntity(mcjty.rftoolscontrol.blocks.processor.ProcessorTileEntity) Block(net.minecraft.block.Block) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ModuleDataVariable(mcjty.rftoolscontrol.rftoolssupport.ModuleDataVariable) World(net.minecraft.world.World)

Example 12 with Parameter

use of mcjty.rftoolscontrol.api.parameters.Parameter in project RFToolsControl by McJty.

the class GridInstance method readFromJson.

public static GridInstance readFromJson(JsonElement element) {
    JsonObject gridObject = element.getAsJsonObject();
    String id = gridObject.get("id").getAsString();
    GridInstance.Builder builder = GridInstance.builder(id);
    if (gridObject.has("primary")) {
        String primary = gridObject.get("primary").getAsString();
        builder.primaryConnection(Connection.getConnection(primary));
    }
    if (gridObject.has("secondary")) {
        String secondary = gridObject.get("secondary").getAsString();
        builder.secondaryConnection(Connection.getConnection(secondary));
    }
    Opcode opcode = Opcodes.OPCODES.get(id);
    if (opcode == null) {
        // Sanity check in case an opcode got removed
        return null;
    }
    List<ParameterDescription> parameters = opcode.getParameters();
    JsonArray parameterArray = gridObject.get("parameters").getAsJsonArray();
    for (int i = 0; i < parameterArray.size(); i++) {
        JsonObject parObject = parameterArray.get(i).getAsJsonObject();
        Parameter parameter = ParameterTools.readFromJson(parObject);
        if (parameter.getParameterType() != parameters.get(i).getType()) {
            // Sanity check
            builder.parameter(Parameter.builder().type(parameters.get(i).getType()).value(ParameterValue.constant(null)).build());
        } else {
            builder.parameter(parameter);
        }
    }
    return builder.build();
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Opcode(mcjty.rftoolscontrol.api.code.Opcode) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Example 13 with Parameter

use of mcjty.rftoolscontrol.api.parameters.Parameter in project RFToolsControl by McJty.

the class GridInstance method writeToNBT.

public NBTTagCompound writeToNBT(int x, int y) {
    NBTTagCompound tag = new NBTTagCompound();
    tag.setInteger("x", x);
    tag.setInteger("y", y);
    tag.setString("id", getId());
    if (primaryConnection != null) {
        tag.setString("prim", primaryConnection.getId());
    }
    if (secondaryConnection != null) {
        tag.setString("sec", secondaryConnection.getId());
    }
    NBTTagList parList = new NBTTagList();
    for (Parameter parameter : getParameters()) {
        NBTTagCompound nbt = ParameterTools.writeToNBT(parameter);
        parList.appendTag(nbt);
    }
    tag.setTag("pars", parList);
    return tag;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter)

Example 14 with Parameter

use of mcjty.rftoolscontrol.api.parameters.Parameter in project RFToolsControl by McJty.

the class GridInstance method readFromNBT.

public static GridInstance readFromNBT(NBTTagCompound tag) {
    String opcodeid = tag.getString("id");
    GridInstance.Builder builder = GridInstance.builder(opcodeid);
    if (tag.hasKey("prim")) {
        builder.primaryConnection(Connection.getConnection(tag.getString("prim")));
    }
    if (tag.hasKey("sec")) {
        builder.secondaryConnection(Connection.getConnection(tag.getString("sec")));
    }
    Opcode opcode = Opcodes.OPCODES.get(opcodeid);
    if (opcode == null) {
        // Sanity check in case an opcode got removed
        return null;
    }
    List<ParameterDescription> parameters = opcode.getParameters();
    NBTTagList parList = tag.getTagList("pars", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < parList.tagCount(); i++) {
        NBTTagCompound parTag = (NBTTagCompound) parList.get(i);
        Parameter parameter = ParameterTools.readFromNBT(parTag);
        if (parameter.getParameterType() != parameters.get(i).getType()) {
            // Sanity check
            builder.parameter(Parameter.builder().type(parameters.get(i).getType()).value(ParameterValue.constant(null)).build());
        } else {
            builder.parameter(parameter);
        }
    }
    return builder.build();
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Opcode(mcjty.rftoolscontrol.api.code.Opcode) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Example 15 with Parameter

use of mcjty.rftoolscontrol.api.parameters.Parameter in project RFToolsControl by McJty.

the class CompiledCard method compile.

public static CompiledCard compile(ProgramCardInstance cardInstance) {
    if (cardInstance == null) {
        return null;
    }
    CompiledCard card = new CompiledCard();
    Map<GridPos, GridInstance> gridInstances = cardInstance.getGridInstances();
    // First find the indices of all compiled grid instances
    Map<GridPos, Integer> posToIndex = new HashMap<>();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        posToIndex.put(location, posToIndex.size());
    }
    // Index of a dummy stop opcode that we can use to go too in case there is no real connection
    int stopIdx = posToIndex.size();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        GridInstance grid = entry.getValue();
        String id = grid.getId();
        Opcode opcode = Opcodes.OPCODES.get(id);
        GridPos primaryOutput = grid.getPrimaryConnection() != null ? grid.getPrimaryConnection().offset(location) : null;
        GridPos secondaryOutput = grid.getSecondaryConnection() != null ? grid.getSecondaryConnection().offset(location) : null;
        CompiledOpcode.Builder opcodeBuilder = CompiledOpcode.builder().opcode(opcode);
        opcodeBuilder.grid(location.getX(), location.getY());
        if (primaryOutput != null && posToIndex.containsKey(primaryOutput)) {
            opcodeBuilder.primaryIndex(posToIndex.get(primaryOutput));
        } else {
            opcodeBuilder.primaryIndex(stopIdx);
        }
        if (secondaryOutput != null && posToIndex.containsKey(secondaryOutput)) {
            opcodeBuilder.secondaryIndex(posToIndex.get(secondaryOutput));
        } else {
            opcodeBuilder.secondaryIndex(stopIdx);
        }
        List<ParameterDescription> parameters = opcode.getParameters();
        boolean single = false;
        for (int i = 0; i < grid.getParameters().size(); i++) {
            Parameter parameter = grid.getParameters().get(i);
            if (i < parameters.size() && "single".equals(parameters.get(i).getName())) {
                single = TypeConverters.convertToBool(parameter);
            }
            opcodeBuilder.parameter(parameter);
        }
        if (opcode.isEvent()) {
            card.events.putIfAbsent(opcode, new ArrayList<>());
            card.events.get(opcode).add(new CompiledEvent(card.opcodes.size(), single));
        }
        card.opcodes.add(opcodeBuilder.build());
    }
    card.opcodes.add(CompiledOpcode.builder().opcode(Opcodes.DO_STOP_OR_RESUME).build());
    for (Opcode opcode : Opcodes.OPCODES.values()) {
        if (!card.events.containsKey(opcode)) {
            card.events.put(opcode, Collections.emptyList());
        }
    }
    return card;
}
Also used : Opcode(mcjty.rftoolscontrol.api.code.Opcode) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Aggregations

Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)15 Opcode (mcjty.rftoolscontrol.api.code.Opcode)7 ParameterDescription (mcjty.rftoolscontrol.api.parameters.ParameterDescription)6 GridInstance (mcjty.rftoolscontrol.logic.grid.GridInstance)5 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)4 IIcon (mcjty.lib.gui.icons.IIcon)3 GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 Window (mcjty.lib.gui.Window)2 PositionalLayout (mcjty.lib.gui.layout.PositionalLayout)2 VerticalLayout (mcjty.lib.gui.layout.VerticalLayout)2 Button (mcjty.lib.gui.widgets.Button)2 Label (mcjty.lib.gui.widgets.Label)2 Panel (mcjty.lib.gui.widgets.Panel)2 ParameterType (mcjty.rftoolscontrol.api.parameters.ParameterType)2 ParameterValue (mcjty.rftoolscontrol.api.parameters.ParameterValue)2 Connection (mcjty.rftoolscontrol.logic.Connection)2 ParameterEditor (mcjty.rftoolscontrol.logic.editors.ParameterEditor)2 ProgramCardInstance (mcjty.rftoolscontrol.logic.grid.ProgramCardInstance)2