use of com.laytonsmith.core.constructs.CString in project CommandHelper by EngineHub.
the class CompilerObject method compile0.
void compile0() throws ConfigCompileException {
Token t = consume();
if (t.type == TType.NEWLINE) {
return;
}
if (t.type == TType.CONST_START) {
StringBuilder constName = new StringBuilder();
while ((t = consume()).type != TType.RCURLY_BRACKET) {
if (t.type != TType.BARE_STRING && t.type != TType.CONCAT) {
throw new ConfigCompileException("Constant names may only contain names and dots.", t.getTarget());
}
constName.append(t.val());
}
Construct constant = env.getConstant(constName.toString());
if (constant == null) {
throw new ConfigCompileException("Expected the constant ${" + constName.toString() + "} to be provided in the compilation options, but it wasn't.", t.getTarget());
}
t = new Token(TType.STRING, constant.val(), constant.getTarget());
}
if (t.type == TType.BARE_STRING && peek().type == TType.FUNC_START) {
consume();
CFunction f = new CFunction(t.val(), t.getTarget());
functionLines.add(peek().getTarget());
pushNode(f);
return;
}
if (t.type == TType.FUNC_END || t.type == TType.COMMA) {
if (autoConcatCounter > 0) {
autoConcatCounter--;
popNode(t.getTarget());
}
}
if (t.type == TType.COMMA) {
return;
}
if (t.type == TType.FUNC_END) {
// We're done with this child, so push it up
popNode(t.getTarget());
functionLines.pop();
return;
}
if (t.type == TType.LSQUARE_BRACKET) {
CFunction f = new CFunction("__cbracket__", Target.UNKNOWN);
pushNode(f);
bracketCounter++;
bracketLines.push(t.getTarget());
return;
}
if (t.type == TType.RSQUARE_BRACKET) {
if (bracketCounter == 0) {
throw new ConfigCompileException("Unexpected right bracket. (Did you have too many right square brackets (]) in your code?)", t.getTarget());
}
bracketCounter--;
bracketLines.pop();
popNode(t.getTarget());
return;
}
if (t.type == TType.LCURLY_BRACKET) {
CFunction f = new CFunction("__cbrace__", Target.UNKNOWN);
pushNode(f);
braceCounter++;
braceLines.push(t.getTarget());
return;
}
if (t.type == TType.RCURLY_BRACKET) {
if (braceCounter == 0) {
throw new ConfigCompileException("Unexpected right brace. (Did you have too many right braces (}) in your code?)", t.getTarget());
}
braceCounter--;
braceLines.pop();
popNode(t.getTarget());
return;
}
// If the next token ISN'T a ) , } ] we need to autoconcat this
if (peek().type != TType.FUNC_END && peek().type != TType.COMMA && peek().type != TType.RCURLY_BRACKET && peek().type != TType.RSQUARE_BRACKET) {
// ... unless we're already in an autoconcat
if (!(pointer.getData() instanceof CFunction && ((CFunction) pointer.getData()).val().equals("__autoconcat__"))) {
CFunction f = new CFunction("__autoconcat__", Target.UNKNOWN);
pushNode(f);
autoConcatCounter++;
}
}
if (t.type == TType.BARE_STRING && peek().type == TType.LABEL) {
consume();
pointer.addChild(new ParseTree(new CLabel(new CString(t.val(), t.getTarget())), stream.getFileOptions()));
return;
}
if (t.type.isIdentifier()) {
// If it's an atomic, put it in a construct and parse tree, then add it
pointer.addChild(new ParseTree(resolveIdentifier(t), stream.getFileOptions()));
return;
}
if (t.type.isSymbol()) {
pointer.addChild(new ParseTree(new CSymbol(t.val(), t.type, t.getTarget()), stream.getFileOptions()));
return;
}
// Now we have to check ahead for commas and other division parameters.
}
use of com.laytonsmith.core.constructs.CString in project CommandHelper by EngineHub.
the class BoundEvent method trigger.
/**
* When the event actually occurs, this should be run, after translating the original event object (of whatever type
* it may be) into a standard map, which contains the event object data. It is converted into a CArray here, and
* then the script is executed with the driver's execute function.
*
* @param activeEvent
*/
public void trigger(ActiveEvent activeEvent) throws EventException {
try {
// GenericTree<Construct> root = new GenericTree<Construct>();
// root.setRoot(tree);
Environment env = originalEnv.clone();
CArray ca = CArray.GetAssociativeArray(Target.UNKNOWN);
for (Map.Entry<String, Construct> entry : activeEvent.parsedEvent.entrySet()) {
ca.set(new CString(entry.getKey(), Target.UNKNOWN), entry.getValue(), Target.UNKNOWN);
}
env.getEnv(GlobalEnv.class).GetVarList().set(new IVariable(CArray.TYPE, eventObjName, ca, Target.UNKNOWN));
env.getEnv(GlobalEnv.class).SetEvent(activeEvent);
activeEvent.addHistory("Triggering bound event: " + this);
try {
ProfilePoint p = env.getEnv(GlobalEnv.class).GetProfiler().start("Executing event handler for " + this.getEventName() + " defined at " + this.getTarget(), LogLevel.ERROR);
try {
this.execute(env, activeEvent);
} finally {
p.stop();
}
} catch (ConfigRuntimeException e) {
// We don't know how to handle this, but we need to set the env,
// then pass it up the chain
e.setEnv(env);
throw e;
}
} catch (CloneNotSupportedException ex) {
Logger.getLogger(BoundEvent.class.getName()).log(Level.SEVERE, null, ex);
}
}
use of com.laytonsmith.core.constructs.CString in project CommandHelper by EngineHub.
the class ObjectGenerator method enchants.
public CArray enchants(Map<MCEnchantment, Integer> map, Target t) {
CArray ret = new CArray(t);
for (Map.Entry<MCEnchantment, Integer> entry : map.entrySet()) {
CArray eObj = CArray.GetAssociativeArray(t);
eObj.set("etype", new CString(entry.getKey().getName(), t), t);
eObj.set("elevel", new CInt(entry.getValue(), t), t);
ret.push(eObj, t);
}
return ret;
}
use of com.laytonsmith.core.constructs.CString in project CommandHelper by EngineHub.
the class ObjectGenerator method recipe.
public Construct recipe(MCRecipe r, Target t) {
if (r == null) {
return CNull.NULL;
}
CArray ret = CArray.GetAssociativeArray(t);
ret.set("type", new CString(r.getRecipeType().name(), t), t);
ret.set("result", item(r.getResult(), t), t);
if (r instanceof MCFurnaceRecipe) {
ret.set("input", item(((MCFurnaceRecipe) r).getInput(), t), t);
} else if (r instanceof MCShapelessRecipe) {
MCShapelessRecipe shapeless = (MCShapelessRecipe) r;
CArray il = new CArray(t);
for (MCItemStack i : shapeless.getIngredients()) {
il.push(item(i, t), t);
}
ret.set("ingredients", il, t);
if (shapeless.getKey() != null) {
ret.set("key", shapeless.getKey(), t);
}
} else if (r instanceof MCShapedRecipe) {
MCShapedRecipe shaped = (MCShapedRecipe) r;
CArray shape = new CArray(t);
for (String line : shaped.getShape()) {
shape.push(new CString(line, t), t);
}
CArray imap = CArray.GetAssociativeArray(t);
for (Map.Entry<Character, MCItemStack> entry : shaped.getIngredientMap().entrySet()) {
imap.set(entry.getKey().toString(), item(entry.getValue(), t), t);
}
ret.set("shape", shape, t);
ret.set("ingredients", imap, t);
if (shaped.getKey() != null) {
ret.set("key", shaped.getKey(), t);
}
}
return ret;
}
use of com.laytonsmith.core.constructs.CString in project CommandHelper by EngineHub.
the class ObjectGenerator method item.
/**
* Gets an MCItemStack from a given item "object". Supports both the old and new formats currently
*
* @param i
* @param t
* @return An abstract item stack
*/
public MCItemStack item(Construct i, Target t) {
if (i instanceof CNull) {
return EmptyItem();
}
if (!(i instanceof CArray)) {
throw new CREFormatException("Expected an array!", t);
}
CArray item = (CArray) i;
MCMaterial mat;
int data = 0;
int qty = 1;
if (item.containsKey("qty")) {
qty = Static.getInt32(item.get("qty", t), t);
if (qty <= 0) {
return EmptyItem();
}
}
if (item.containsKey("name")) {
mat = StaticLayer.GetConvertor().GetMaterial(item.get("name", t).val());
} else if (item.containsKey("type")) {
Construct type = item.get("type", t);
if (type instanceof CString) {
int seperatorIndex = type.val().indexOf(':');
if (seperatorIndex != -1) {
try {
data = Integer.parseInt(type.val().substring(seperatorIndex + 1));
} catch (NumberFormatException e) {
throw new CRERangeException("The item data \"" + type.val().substring(seperatorIndex + 1) + "\" is not a valid integer.", t);
}
type = new CString(type.val().substring(0, seperatorIndex), t);
}
}
mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(type, t));
} else {
throw new CREFormatException("Could not find item name!", t);
}
if (mat == null) {
throw new CRENotFoundException("A material could not be found based on the given name.", t);
}
if (mat.getType() == 0) {
return EmptyItem();
}
if (item.containsKey("data")) {
data = Static.getInt32(item.get("data", t), t);
}
MCItemMeta meta = null;
if (item.containsKey("meta")) {
meta = itemMeta(item.get("meta", t), mat, t);
}
// Create itemstack
MCItemStack ret = StaticLayer.GetItemStack(mat, data, qty);
if (meta != null) {
ret.setItemMeta(meta);
}
// Fallback to enchants in item array if not in meta
if (item.containsKey("enchants")) {
try {
Map<MCEnchantment, Integer> enchants = enchants((CArray) item.get("enchants", t), t);
for (Map.Entry<MCEnchantment, Integer> entry : enchants.entrySet()) {
ret.addUnsafeEnchantment(entry.getKey(), entry.getValue());
}
} catch (ClassCastException ex) {
throw new CREFormatException("Enchants must be an array of enchantment arrays.", t);
}
}
return ret;
}
Aggregations