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()));
}
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)));
}
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 + "'");
}
}
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;
}
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);
}
Aggregations