use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class ArrayHandlingTest method testArraySize.
@Test(timeout = 10000)
public void testArraySize() throws Exception, CancelCommandException {
ArrayHandling.array_size a = new ArrayHandling.array_size();
CArray arr = commonArray;
Construct ret = a.exec(Target.UNKNOWN, env, arr);
assertReturn(ret, C.Int);
assertCEquals(C.onstruct(3), ret);
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class ConfigRuntimeException method GetReaction.
/**
* If a exception bubbles all the way up to the top, this should be called first, to see what reaction the plugin
* should take. Generally speaking, you'll want to use {@link #HandleUncaughtException} instead of this, though if
* you need to take custom action, you can determine the user's preferred reaction with this method.
*
* @param e
* @return
*/
public static Reaction GetReaction(ConfigRuntimeException e, Environment env) {
// If there is an exception handler, call it to see what it says.
Reaction reaction = Reaction.REPORT;
if (env.getEnv(GlobalEnv.class).GetExceptionHandler() != null) {
CClosure c = env.getEnv(GlobalEnv.class).GetExceptionHandler();
CArray ex = ObjectGenerator.GetGenerator().exception(e, env, Target.UNKNOWN);
if (e.getEnv() != null) {
MCCommandSender sender = e.getEnv().getEnv(CommandHelperEnvironment.class).GetCommandSender();
c.getEnv().getEnv(CommandHelperEnvironment.class).SetCommandSender(sender);
}
Construct ret = CNull.NULL;
try {
c.execute(new Construct[] { ex });
} catch (FunctionReturnException retException) {
ret = retException.getReturn();
}
if (ret instanceof CNull || Prefs.ScreamErrors()) {
reaction = Reaction.REPORT;
} else {
if (Static.getBoolean(ret, Target.UNKNOWN)) {
reaction = Reaction.IGNORE;
} else {
reaction = Reaction.FATAL;
}
}
}
return reaction;
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class CompositeFunction method exec.
@Override
public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
ParseTree tree;
if (!cachedScripts.containsKey(this.getClass())) {
try {
String script = script();
tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(script, null, true)).getChildAt(0);
} catch (ConfigCompileException | ConfigCompileGroupException ex) {
// This is really bad.
throw new Error(ex);
}
if (cacheCompile()) {
cachedScripts.put(this.getClass(), tree);
}
} else {
tree = cachedScripts.get(this.getClass());
}
GlobalEnv env = environment.getEnv(GlobalEnv.class);
IVariableList oldVariables = env.GetVarList();
IVariableList newVariables = new IVariableList();
newVariables.set(new IVariable(CClassType.get("array"), "@arguments", new CArray(t, args.length, args), t));
env.SetVarList(newVariables);
Construct ret = CVoid.VOID;
try {
env.GetScript().eval(tree, environment);
} catch (FunctionReturnException ex) {
ret = ex.getReturn();
}
env.SetVarList(oldVariables);
return ret;
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class AbstractCREException method getFromCArray.
@SuppressWarnings({ "ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown" })
public static AbstractCREException getFromCArray(CArray exception, Target t) throws ClassNotFoundException {
String classType = exception.get("classType", t).val();
Class<? extends Mixed> clzz = NativeTypeList.getNativeClass(classType);
Throwable cause = null;
if (exception.get("causedBy", t) instanceof CArray) {
// It has a cause
cause = new CRECausedByWrapper((CArray) exception.get("causedBy", t));
}
String message = exception.get("message", t).val();
List<StackTraceElement> st = new ArrayList<>();
for (Construct consStElement : Static.getArray(exception.get("stackTrace", t), t).asList()) {
CArray stElement = Static.getArray(consStElement, t);
int line = Static.getInt32(stElement.get("line", t), t);
File f = new File(stElement.get("file", t).val());
//
int col = 0;
st.add(new StackTraceElement(stElement.get("id", t).val(), new Target(line, f, col)));
}
// Now we have parsed everything into POJOs
Class[] types = new Class[] { String.class, Target.class, Throwable.class };
Object[] args = new Object[] { message, t, cause };
AbstractCREException ex = (AbstractCREException) ReflectionUtils.newInstance(clzz, types, args);
ex.stackTrace = st;
return ex;
}
use of com.laytonsmith.core.constructs.Construct in project CommandHelper by EngineHub.
the class InventoryManagement method IsMatch.
/**
* Gets whether or not the two items are a match based on the given item array map.
*
* @param map The original item array map of what to compare
* @param is The MCItemStack to compare all other items with, converted from the item array map
* @param iis The current MCItemStack we're comparing
* @param t
* @return Whether or not the items are a match
*/
private static boolean IsMatch(CArray map, MCItemStack is, MCItemStack iis, Target t) {
if (!is.getType().equals(iis.getType())) {
return false;
}
if ((map == null || map.containsKey("data")) && is.getDurability() != iis.getDurability()) {
return false;
}
if (map != null && map.containsKey("meta")) {
Construct c = map.get("meta", t);
if (c instanceof CNull) {
if (iis.hasItemMeta()) {
return false;
}
} else {
if (!iis.hasItemMeta()) {
return false;
}
CArray metamap = (CArray) c;
MCItemMeta im = is.getItemMeta();
MCItemMeta iim = iis.getItemMeta();
if (metamap.containsKey("display")) {
if (im.hasDisplayName()) {
if (!iim.hasDisplayName() || !im.getDisplayName().equals(iim.getDisplayName())) {
return false;
}
} else if (iim.hasDisplayName()) {
return false;
}
}
if (metamap.containsKey("lore") && im.hasLore() && !im.getLore().equals(iim.getLore())) {
return false;
}
if (metamap.containsKey("enchants") && !im.getEnchants().equals(iim.getEnchants())) {
return false;
}
}
}
return true;
}
Aggregations