Search in sources :

Example 16 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class GuiFiller method preLoad.

protected void preLoad(BuildCraftJsonGui json) {
    TypedKeyMap<String, Object> properties = json.properties;
    FunctionContext context = json.context;
    properties.put("filler.inventory", new InventorySlotHolder(container, container.tile.invResources));
    properties.put("statement.container", container.tile);
    properties.put("controllable", container.tile);
    properties.put("controllable.sprite", SPRITE_CONTROL_MODE);
    context.put_o("controllable.mode", Mode.class, container.tile::getControlMode);
    context.put_b("filler.is_finished", container.tile::isFinished);
    context.put_b("filler.is_locked", container.tile::isLocked);
    context.put_l("filler.to_break", container.tile::getCountToBreak);
    context.put_l("filler.to_place", container.tile::getCountToPlace);
    properties.put("filler.possible", FillerStatementContext.CONTEXT_ALL);
    properties.put("filler.pattern", container.getPatternStatementClient());
    properties.put("filler.pattern.sprite", SPRITE_PATTERN);
    context.put_b("filler.invert", container::isInverted);
    properties.put("filler.invert", IButtonBehaviour.TOGGLE);
    properties.put("filler.invert", container.isInverted());
    properties.put("filler.invert", (IButtonClickEventListener) (b, k) -> container.sendInverted(b.isButtonActive()));
    context.put_b("filler.excavate", container.tile::canExcavate);
    properties.put("filler.excavate", IButtonBehaviour.TOGGLE);
    properties.put("filler.excavate", container.tile.canExcavate());
    properties.put("filler.excavate", (IButtonClickEventListener) (b, k) -> container.tile.sendCanExcavate(b.isButtonActive()));
}
Also used : Mode(buildcraft.api.tiles.IControllable.Mode) InventorySlotHolder(buildcraft.lib.gui.json.InventorySlotHolder) SpriteDelegate(buildcraft.lib.gui.json.SpriteDelegate) IFillerPattern(buildcraft.api.filler.IFillerPattern) FunctionContext(buildcraft.lib.expression.FunctionContext) FillerStatementContext(buildcraft.builders.filler.FillerStatementContext) IButtonBehaviour(buildcraft.lib.gui.button.IButtonBehaviour) GuiBC8(buildcraft.lib.gui.GuiBC8) ResourceLocation(net.minecraft.util.ResourceLocation) IButtonClickEventListener(buildcraft.lib.gui.button.IButtonClickEventListener) BuildCraftJsonGui(buildcraft.lib.gui.json.BuildCraftJsonGui) BCCoreSprites(buildcraft.core.BCCoreSprites) ContainerFiller(buildcraft.builders.container.ContainerFiller) TypedKeyMap(buildcraft.lib.misc.collect.TypedKeyMap) FunctionContext(buildcraft.lib.expression.FunctionContext) InventorySlotHolder(buildcraft.lib.gui.json.InventorySlotHolder)

Example 17 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class NodeCasting method castToDouble.

public static INodeFuncDouble castToDouble(INodeFunc func) throws InvalidExpressionException {
    if (func instanceof INodeFuncDouble) {
        return (INodeFuncDouble) func;
    }
    Class<?> type = NodeTypes.getType(func);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    INodeFunc caster = ctx.getFunction("(double)", Collections.singletonList(type));
    if (caster == null || NodeTypes.getType(caster) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    return (stack) -> (INodeDouble) caster.getNode(new NodeStack(func.getNode(stack)));
}
Also used : INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeTypes(buildcraft.lib.expression.api.NodeTypes) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeStack(buildcraft.lib.expression.NodeStack) Collections(java.util.Collections) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 18 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeStatementSource method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    String source = json.properties.get("source");
    StatementContext<?> ctxSource = gui.properties.get(source, StatementContext.class);
    String side = json.properties.get("side");
    String style = json.properties.get("style");
    if (style == null || "flat".equals(style)) {
        return new GuiElementStatementSource<>(gui, !"right".equals(side), ctxSource);
    // } else if ("ledger.left".equals(layout) || "ledger.right".equals(layout)) {
    // TODO!
    } else {
        throw new JsonSyntaxException("Unknown style '" + style + "'");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) GuiElementStatementSource(buildcraft.lib.gui.statement.GuiElementStatementSource) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 19 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementType method createContext.

public static FunctionContext createContext(JsonGuiElement json) {
    FunctionContext ctx = json.context;
    // if json overrides variables then its ok
    ctx = new FunctionContext(ctx);
    Set<String> args = new HashSet<>();
    // Put in args first
    for (String key : json.properties.keySet()) {
        if (key.startsWith("args.")) {
            String argName = key.substring("args.".length());
            args.add(argName);
            String value = json.properties.get(key);
            try {
                IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
                ctx.putVariable(argName, NodeTypes.createConstantNode(node));
            } catch (InvalidExpressionException e) {
                // Ignore the error
                BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
            }
        }
    }
    for (String key : json.properties.keySet()) {
        if (key.contains(".") || key.contains("[")) {
            continue;
        }
        String value = json.properties.get(key);
        try {
            IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
            ctx.putVariable(key, NodeTypes.createConstantNode(node));
        } catch (InvalidExpressionException e) {
        // Ignore the error
        // BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
        }
    }
    return ctx;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) HashSet(java.util.HashSet)

Example 20 with FunctionContext

use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.

the class ElementTypeHelp method deserialize0.

// Args:
// - pos[0], pos[1]: the area for help (where it will be drawn, relative to the root of the gui). Defaults
// to 0,0
// - size[0], size[1]: the size of the help area
// - area[0-3]: mapping for pos[0], pos[1], size[0], size[1]
// - colour: The colour for the help element (overlay)
// - title: The name of the help element
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    String title = json.properties.get("title");
    List<String> text = new ArrayList<>();
    if (json.properties.containsKey("text[0]")) {
        int i = 0;
        while (true) {
            String prop = json.properties.get("text[" + i + "]");
            if (prop == null) {
                break;
            }
            text.add(prop);
            i++;
        }
    } else {
        text.add(json.properties.getOrDefault("text", "ERROR: Help not given!"));
    }
    int colour = resolveEquationInt(json, "colour", ctx);
    ElementHelpInfo help = new ElementHelpInfo(title, colour, text.toArray(new String[0]));
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    return new DummyHelpElement(area, help);
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) DummyHelpElement(buildcraft.lib.gui.help.DummyHelpElement) ArrayList(java.util.ArrayList) ElementHelpInfo(buildcraft.lib.gui.help.ElementHelpInfo) FunctionContext(buildcraft.lib.expression.FunctionContext)

Aggregations

FunctionContext (buildcraft.lib.expression.FunctionContext)26 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)8 JsonSyntaxException (com.google.gson.JsonSyntaxException)8 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)7 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)6 NodeStack (buildcraft.lib.expression.NodeStack)5 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)5 ResourceLocation (net.minecraft.util.ResourceLocation)5 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeFunc (buildcraft.lib.expression.api.INodeFunc)4 INodeStack (buildcraft.lib.expression.api.INodeStack)4 IButtonBehaviour (buildcraft.lib.gui.button.IButtonBehaviour)4 IButtonClickEventListener (buildcraft.lib.gui.button.IButtonClickEventListener)4 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)4 Test (org.junit.Test)4 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 GuiBC8 (buildcraft.lib.gui.GuiBC8)3 ISimpleDrawable (buildcraft.lib.gui.ISimpleDrawable)3 BuildCraftJsonGui (buildcraft.lib.gui.json.BuildCraftJsonGui)3 TypedKeyMap (buildcraft.lib.misc.collect.TypedKeyMap)3