Search in sources :

Example 31 with CArray

use of com.laytonsmith.core.constructs.CArray 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 32 with CArray

use of com.laytonsmith.core.constructs.CArray 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 33 with CArray

use of com.laytonsmith.core.constructs.CArray 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 34 with CArray

use of com.laytonsmith.core.constructs.CArray 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)

Example 35 with CArray

use of com.laytonsmith.core.constructs.CArray in project CommandHelper by EngineHub.

the class InventoryManagement method GetInventory.

// @api
// public static class pinv_consolidate extends AbstractFunction {
// 
// public String getName() {
// return "pinv_consolidate";
// }
// 
// public Integer[] numArgs() {
// return new Integer[]{0, 1};
// }
// 
// public String docs() {
// return "void {[player]} Consolidates a player's inventory as much as possible."
// + " There is no guarantee anything will happen after this function"
// + " is called, and there is no way to specify details about how"
// + " consolidation occurs, however, the following heuristics are followed:"
// + " The hotbar items will not be moved from the hotbar, unless there are"
// + " two+ slots that have the same item. Items in the main inventory area"
// + " will be moved closer to the bottom of the main inventory. No empty slots"
// + " will be filled in the hotbar.";
// }
// 
// public Class<? extends CREThrowable>[] thrown() {
// return new Class[]{};
// }
// 
// public boolean isRestricted() {
// return true;
// }
// 
// public boolean preResolveVariables() {
// return true;
// }
// 
// public Boolean runAsync() {
// return false;
// }
// 
// public Construct exec(Target t, Environment environment, Construct... args) throws ConfigRuntimeException {
// MCPlayer p = environment.GetPlayer();
// if(args.length == 1){
// p = Static.GetPlayer(args[0], t);
// }
// //First, we need to address the hotbar
// for(int i = 0; i < 10; i++){
// //If the stack size is maxed out, we're done.
// }
// 
// return CVoid.VOID;
// }
// 
// public CHVersion since() {
// return CHVersion.V3_3_1;
// }
// }
private static MCInventory GetInventory(Construct specifier, MCWorld w, Target t) {
    MCInventory inv;
    if (specifier instanceof CArray) {
        MCLocation l = ObjectGenerator.GetGenerator().location(specifier, w, t);
        inv = StaticLayer.GetConvertor().GetLocationInventory(l);
    } else {
        MCEntity entity = Static.getEntity(specifier, t);
        inv = StaticLayer.GetConvertor().GetEntityInventory(entity);
    }
    if (inv == null) {
        throw new CREFormatException("The entity or location specified is not capable of having an inventory.", t);
    } else {
        return inv;
    }
}
Also used : MCLocation(com.laytonsmith.abstraction.MCLocation) MCInventory(com.laytonsmith.abstraction.MCInventory) MCEntity(com.laytonsmith.abstraction.MCEntity) CArray(com.laytonsmith.core.constructs.CArray) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException)

Aggregations

CArray (com.laytonsmith.core.constructs.CArray)50 CString (com.laytonsmith.core.constructs.CString)27 Construct (com.laytonsmith.core.constructs.Construct)23 CInt (com.laytonsmith.core.constructs.CInt)17 CNull (com.laytonsmith.core.constructs.CNull)12 CREFormatException (com.laytonsmith.core.exceptions.CRE.CREFormatException)12 HashMap (java.util.HashMap)9 CDouble (com.laytonsmith.core.constructs.CDouble)8 IVariable (com.laytonsmith.core.constructs.IVariable)8 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)6 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 CBoolean (com.laytonsmith.core.constructs.CBoolean)5 Target (com.laytonsmith.core.constructs.Target)5 CRECastException (com.laytonsmith.core.exceptions.CRE.CRECastException)5 FunctionReturnException (com.laytonsmith.core.exceptions.FunctionReturnException)5 MCEnchantment (com.laytonsmith.abstraction.MCEnchantment)4 MCItemMeta (com.laytonsmith.abstraction.MCItemMeta)4 MCItemStack (com.laytonsmith.abstraction.MCItemStack)4