Search in sources :

Example 1 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean in project BuildCraft by BuildCraft.

the class ElementTypeSlot method deserialize0.

// pos: the position of the slot
// slot: The slot to be moved
// index: If the slot was an InventorySlotHolder then this is the index of the slot in the list
// visible: If false then the slot won't be visible
@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    String slotName = json.properties.get("slot");
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    Slot slot = gui.properties.get(slotName, Slot.class);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    if (slot != null) {
        return new GuiElementSlotMover(gui, pos, visible, slot);
    }
    InventorySlotHolder holder = gui.properties.get(slotName, InventorySlotHolder.class);
    if (holder == null) {
        throw new JsonSyntaxException("Unknown slot '" + slotName + "'");
    }
    int index = resolveEquationInt(json, "index", ctx);
    if (index < 0 || index >= holder.slots.length) {
        throw new JsonSyntaxException("Invalid slot index! (" + index + ", min = 0, max = " + (holder.slots.length - 1) + ")");
    }
    return new GuiElementSlotMover(gui, pos, visible, holder.slots[index]);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Slot(net.minecraft.inventory.Slot) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition) GuiElementSlotMover(buildcraft.lib.gui.elem.GuiElementSlotMover)

Example 2 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean in project BuildCraft by BuildCraft.

the class ElementTypeToolTip 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]
// - text: The text to display in the tooltip
// - expression: The expression to display in the tooltip
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    List<String> text = new ArrayList<>();
    String key = "text";
    boolean isExpression = false;
    if (json.properties.containsKey("expression") || json.properties.containsKey("expression[0]")) {
        key = "expression";
        isExpression = true;
    }
    if (json.properties.containsKey(key + "[0]")) {
        int i = 0;
        while (true) {
            String prop = json.properties.get(key + "[" + i + "]");
            if (prop == null) {
                break;
            }
            text.add(prop);
            i++;
        }
    } else {
        text.add(json.properties.getOrDefault(key, "ERROR: Text not given!"));
    }
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    ITooltipElement source;
    if (isExpression) {
        List<INodeObject<String>> nodes = new ArrayList<>(text.size());
        try {
            for (String s : text) {
                nodes.add(GenericExpressionCompiler.compileExpressionString(s, ctx));
            }
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException(e);
        }
        source = (list) -> {
            if (visible.evaluate()) {
                String[] arr = new String[nodes.size()];
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = nodes.get(i).evaluate();
                }
                list.add(ToolTip.createLocalized(arr));
            }
        };
    } else {
        ToolTip tooltip = ToolTip.createLocalized(text.toArray(new String[0]));
        source = (list) -> {
            if (visible.evaluate()) {
                list.add(tooltip);
            }
        };
    }
    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 GuiElementToolTip(gui, area, source);
}
Also used : GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) ToolTip(buildcraft.lib.gui.elem.ToolTip) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ITooltipElement(buildcraft.lib.gui.ITooltipElement) ArrayList(java.util.ArrayList) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 3 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean in project BuildCraft by BuildCraft.

the class JsonModelRule method deserialize.

public static JsonModelRule deserialize(JsonElement json, FunctionContext fnCtx, ResourceLoaderContext ctx) {
    if (!json.isJsonObject()) {
        throw new JsonSyntaxException("Expected an object, got " + json);
    }
    JsonObject obj = json.getAsJsonObject();
    String when = JsonUtils.getString(obj, "when");
    INodeBoolean nodeWhen = JsonVariableModelPart.convertStringToBooleanNode(when, fnCtx);
    String type = JsonUtils.getString(obj, "type");
    if (type.startsWith("builtin:")) {
        String builtin = type.substring("builtin:".length());
        if ("rotate_facing".equals(builtin)) {
            fnCtx = new FunctionContext(fnCtx, ExpressionCompat.ENUM_FACING);
            String from = JsonUtils.getString(obj, "from");
            INodeObject<EnumFacing> nodeFrom = JsonVariableModelPart.convertStringToObjectNode(from, fnCtx, EnumFacing.class);
            String to = JsonUtils.getString(obj, "to");
            INodeObject<EnumFacing> nodeTo = JsonVariableModelPart.convertStringToObjectNode(to, fnCtx, EnumFacing.class);
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotateFacing.DEFAULT_ORIGIN;
            }
            return new RuleRotateFacing(nodeWhen, nodeFrom, nodeTo, origin);
        } else if ("rotate".equals(builtin)) {
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotate.DEFAULT_ORIGIN;
            }
            INodeDouble[] angles = JsonVariableModelPart.readVariablePosition(obj, "angle", fnCtx);
            return new RuleRotate(nodeWhen, origin, angles);
        } else if ("scale".equals(builtin)) {
            INodeDouble[] origin;
            if (obj.has("origin")) {
                origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
            } else {
                origin = RuleRotate.DEFAULT_ORIGIN;
            }
            INodeDouble[] scales = JsonVariableModelPart.readVariablePosition(obj, "scale", fnCtx);
            return new RuleScale(nodeWhen, origin, scales);
        } else {
            throw new JsonSyntaxException("Unknown built in rule type '" + builtin + "'");
        }
    } else {
        throw new JsonSyntaxException("Unknown rule type '" + type + "'");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) EnumFacing(net.minecraft.util.EnumFacing) JsonObject(com.google.gson.JsonObject) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 4 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean in project BuildCraft by BuildCraft.

the class NodeConditionalBoolean method inline.

@Override
public INodeBoolean inline() {
    INodeBoolean c = condition.inline();
    INodeBoolean t = ifTrue.inline();
    INodeBoolean f = ifFalse.inline();
    if (c instanceof NodeConstantBoolean) {
        return ((NodeConstantBoolean) c).value ? t : f;
    } else if (c != condition || t != ifTrue || f != ifFalse) {
        return new NodeConditionalBoolean(c, t, f);
    } else {
        return this;
    }
}
Also used : INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) NodeConstantBoolean(buildcraft.lib.expression.node.value.NodeConstantBoolean)

Example 5 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean in project BuildCraft by BuildCraft.

the class ExpressionTester method bakeAndCallBoolean.

private static void bakeAndCallBoolean(String function, boolean expected, FunctionContext ctx) {
    ExpressionDebugManager.debugPrintln("Testing \"" + function + "\", expecting " + expected);
    INodeBoolean node = bakeFunctionBoolean(function, ctx);
    ExpressionDebugManager.debugPrintln("To " + node);
    boolean got = node.evaluate();
    Assert.assertEquals(expected, got);
}
Also used : INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)

Aggregations

INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)11 FunctionContext (buildcraft.lib.expression.FunctionContext)6 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)4 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)3 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)3 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)3 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ISimpleDrawable (buildcraft.lib.gui.ISimpleDrawable)2 GuiElementDrawable (buildcraft.lib.gui.elem.GuiElementDrawable)2 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)2 ISprite (buildcraft.api.core.render.ISprite)1 SpriteRaw (buildcraft.lib.client.sprite.SpriteRaw)1 SubSpriteChanging (buildcraft.lib.client.sprite.SubSpriteChanging)1 IVariableNode (buildcraft.lib.expression.api.IVariableNode)1 NodeConditionalBoolean (buildcraft.lib.expression.node.condition.NodeConditionalBoolean)1 NodeConditionalDouble (buildcraft.lib.expression.node.condition.NodeConditionalDouble)1 NodeConditionalLong (buildcraft.lib.expression.node.condition.NodeConditionalLong)1 NodeConditionalObject (buildcraft.lib.expression.node.condition.NodeConditionalObject)1