Search in sources :

Example 1 with ProgramCardInstance

use of mcjty.rftoolscontrol.logic.grid.ProgramCardInstance in project RFToolsControl by McJty.

the class GuiProgrammer method loadProgram.

private void loadProgram(int slot) {
    ItemStack card = tileEntity.getStackInSlot(slot);
    if (card.isEmpty()) {
        return;
    }
    clearGrid(false);
    ProgramCardInstance instance = ProgramCardInstance.parseInstance(card);
    if (instance == null) {
        return;
    }
    loadProgram(instance);
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) ItemStack(net.minecraft.item.ItemStack)

Example 2 with ProgramCardInstance

use of mcjty.rftoolscontrol.logic.grid.ProgramCardInstance in project RFToolsControl by McJty.

the class GuiProgrammer method validateProgram.

private void validateProgram() {
    Panel panel = new Panel(mc, this).setLayout(new VerticalLayout()).setFilledBackground(0xff666666, 0xffaaaaaa).setFilledRectThickness(1);
    panel.setBounds(new Rectangle(60, 10, 200, 130));
    Window modalWindow = getWindowManager().createModalWindow(panel);
    WidgetList errorList = new WidgetList(mc, this);
    errorList.addSelectionEvent(new SelectionEvent() {

        @Override
        public void select(Widget parent, int index) {
        }

        @Override
        public void doubleClick(Widget parent, int index) {
            if (errorList.getSelected() != -1) {
                Widget<?> child = errorList.getChild(errorList.getSelected());
                GridPos pos = (GridPos) child.getUserObject();
                if (pos != null) {
                    window.setTextFocus(getHolder(pos.getX(), pos.getY()));
                }
            }
            getWindowManager().closeWindow(modalWindow);
        }
    });
    panel.addChild(errorList);
    panel.addChild(new Button(mc, this).addButtonEvent(w -> {
        getWindowManager().closeWindow(modalWindow);
    }).setText("Close"));
    ProgramCardInstance instance = makeGridInstance(false);
    List<Pair<GridPos, String>> errors = ProgramValidator.validate(instance);
    for (Pair<GridPos, String> entry : errors) {
        GridPos p = entry.getKey();
        errorList.addChild(new Label(mc, this).setColor(0xffff0000).setHorizontalAlignment(HorizontalAlignment.ALIGH_LEFT).setText(entry.getValue()).setUserObject(p));
    }
}
Also used : Window(mcjty.lib.gui.Window) Label(mcjty.lib.gui.widgets.Label) Panel(mcjty.lib.gui.widgets.Panel) ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) Button(mcjty.lib.gui.widgets.Button) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) SelectionEvent(mcjty.lib.gui.events.SelectionEvent) VerticalLayout(mcjty.lib.gui.layout.VerticalLayout) Pair(org.apache.commons.lang3.tuple.Pair)

Example 3 with ProgramCardInstance

use of mcjty.rftoolscontrol.logic.grid.ProgramCardInstance in project RFToolsControl by McJty.

the class ProgramCommand method saveProgram.

private void saveProgram(ICommandSender sender, String arg, ItemStack item) {
    ProgramCardInstance program = ProgramCardInstance.parseInstance(item);
    String json = program.writeToJson();
    // File file = new File("." + File.separator + "rftoolscontrol" + File.separator + arg);
    File file = new File(arg);
    if (file.exists()) {
        file.delete();
    }
    try (PrintWriter writer = new PrintWriter(file)) {
        writer.print(json);
    } catch (FileNotFoundException e) {
        ITextComponent component = new TextComponentString(TextFormatting.RED + "Error opening file for writing!");
        if (sender instanceof EntityPlayer) {
            ((EntityPlayer) sender).sendStatusMessage(component, false);
        } else {
            sender.sendMessage(component);
        }
        return;
    }
    ITextComponent component = new TextComponentString("Saved program!");
    if (sender instanceof EntityPlayer) {
        ((EntityPlayer) sender).sendStatusMessage(component, false);
    } else {
        sender.sendMessage(component);
    }
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) ITextComponent(net.minecraft.util.text.ITextComponent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextComponentString(net.minecraft.util.text.TextComponentString) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 4 with ProgramCardInstance

use of mcjty.rftoolscontrol.logic.grid.ProgramCardInstance in project RFToolsControl by McJty.

the class ProgramCommand method loadProgram.

private void loadProgram(ICommandSender sender, String arg, ItemStack item) {
    // File file = new File("." + File.separator + "rftoolscontrol" + File.separator + arg);
    File file = new File(arg);
    String json;
    try (FileInputStream stream = new FileInputStream(file)) {
        byte[] data = new byte[(int) file.length()];
        stream.read(data);
        json = new String(data, "UTF-8");
    } catch (IOException e) {
        ITextComponent component = new TextComponentString(TextFormatting.RED + "Error opening file for reading!");
        if (sender instanceof EntityPlayer) {
            ((EntityPlayer) sender).sendStatusMessage(component, false);
        } else {
            sender.sendMessage(component);
        }
        return;
    }
    ProgramCardInstance program = ProgramCardInstance.readFromJson(json);
    program.writeToNBT(item);
    RFToolsCtrlMessages.INSTANCE.sendToServer(new PacketItemNBTToServer(item.getTagCompound()));
    ITextComponent component = new TextComponentString("Loaded program!");
    if (sender instanceof EntityPlayer) {
        ((EntityPlayer) sender).sendStatusMessage(component, false);
    } else {
        sender.sendMessage(component);
    }
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) ITextComponent(net.minecraft.util.text.ITextComponent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextComponentString(net.minecraft.util.text.TextComponentString) PacketItemNBTToServer(mcjty.rftoolscontrol.network.PacketItemNBTToServer) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 5 with ProgramCardInstance

use of mcjty.rftoolscontrol.logic.grid.ProgramCardInstance in project RFToolsControl by McJty.

the class GuiProgrammer method makeGridInstance.

private ProgramCardInstance makeGridInstance(boolean selectionOnly) {
    ProgramCardInstance instance = ProgramCardInstance.newInstance();
    for (int x = 0; x < GRID_WIDTH; x++) {
        for (int y = 0; y < GRID_HEIGHT; y++) {
            IconHolder holder = getHolder(x, y);
            IIcon icon = holder.getIcon();
            if (icon != null) {
                if (selectionOnly && !icon.hasOverlay("S")) {
                    continue;
                }
                String operandId = icon.getID();
                GridInstance.Builder builder = GridInstance.builder(operandId);
                for (Connection connection : Connection.values()) {
                    if (icon.hasOverlay(connection.getId())) {
                        if (connection.isPrimary()) {
                            builder.primaryConnection(connection);
                        } else {
                            builder.secondaryConnection(connection);
                        }
                    }
                }
                Opcode opcode = Opcodes.OPCODES.get(operandId);
                Map<String, Object> data = icon.getData();
                for (ParameterDescription description : opcode.getParameters()) {
                    ParameterValue value = data == null ? null : (ParameterValue) data.get(description.getName());
                    if (value == null) {
                        value = ParameterValue.constant(null);
                    }
                    Parameter parameter = Parameter.builder().type(description.getType()).value(value).build();
                    builder.parameter(parameter);
                }
                instance.putGridInstance(x, y, builder.build());
            }
        }
    }
    return instance;
}
Also used : ParameterValue(mcjty.rftoolscontrol.api.parameters.ParameterValue) IIcon(mcjty.lib.gui.icons.IIcon) Connection(mcjty.rftoolscontrol.logic.Connection) Opcode(mcjty.rftoolscontrol.api.code.Opcode) ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Aggregations

ProgramCardInstance (mcjty.rftoolscontrol.logic.grid.ProgramCardInstance)8 GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 ItemStack (net.minecraft.item.ItemStack)2 ITextComponent (net.minecraft.util.text.ITextComponent)2 TextComponentString (net.minecraft.util.text.TextComponentString)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Clipboard (java.awt.datatransfer.Clipboard)1 StringSelection (java.awt.datatransfer.StringSelection)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 Window (mcjty.lib.gui.Window)1 SelectionEvent (mcjty.lib.gui.events.SelectionEvent)1 IIcon (mcjty.lib.gui.icons.IIcon)1 VerticalLayout (mcjty.lib.gui.layout.VerticalLayout)1 Button (mcjty.lib.gui.widgets.Button)1 Label (mcjty.lib.gui.widgets.Label)1 Panel (mcjty.lib.gui.widgets.Panel)1 Opcode (mcjty.rftoolscontrol.api.code.Opcode)1 Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)1