Search in sources :

Example 26 with Construct

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);
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) Test(org.junit.Test) StaticTest(com.laytonsmith.testing.StaticTest)

Example 27 with Construct

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;
}
Also used : CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) MCCommandSender(com.laytonsmith.abstraction.MCCommandSender) CNull(com.laytonsmith.core.constructs.CNull)

Example 28 with Construct

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;
}
Also used : IVariable(com.laytonsmith.core.constructs.IVariable) IVariableList(com.laytonsmith.core.constructs.IVariableList) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree)

Example 29 with Construct

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;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) ArrayList(java.util.ArrayList) Target(com.laytonsmith.core.constructs.Target) Construct(com.laytonsmith.core.constructs.Construct) File(java.io.File)

Example 30 with Construct

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;
}
Also used : CArray(com.laytonsmith.core.constructs.CArray) MCItemMeta(com.laytonsmith.abstraction.MCItemMeta) Construct(com.laytonsmith.core.constructs.Construct) CNull(com.laytonsmith.core.constructs.CNull)

Aggregations

Construct (com.laytonsmith.core.constructs.Construct)42 CString (com.laytonsmith.core.constructs.CString)25 CArray (com.laytonsmith.core.constructs.CArray)23 IVariable (com.laytonsmith.core.constructs.IVariable)14 CInt (com.laytonsmith.core.constructs.CInt)11 CNull (com.laytonsmith.core.constructs.CNull)11 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)10 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)9 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 Variable (com.laytonsmith.core.constructs.Variable)8 CDouble (com.laytonsmith.core.constructs.CDouble)7 CommandHelperEnvironment (com.laytonsmith.core.environments.CommandHelperEnvironment)7 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)7 CBoolean (com.laytonsmith.core.constructs.CBoolean)6 Target (com.laytonsmith.core.constructs.Target)6 Environment (com.laytonsmith.core.environments.Environment)6 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)6 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)6