Search in sources :

Example 21 with Construct

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

the class RandomTests method testClone.

@Test
public void testClone() throws CloneNotSupportedException {
    CArray c1 = C.Array(C.Void(), C.Void()).clone();
    CBoolean c2 = C.Boolean(true).clone();
    CDouble c4 = C.Double(1).clone();
    CFunction c5 = new CFunction("", Target.UNKNOWN).clone();
    CInt c6 = C.Int(1).clone();
    CNull c7 = C.Null().clone();
    CString c8 = C.String("").clone();
    Construct c9 = C.Void().clone();
    Command c10 = new Command("/c", Target.UNKNOWN).clone();
    IVariable c12 = new IVariable(Auto.TYPE, "@name", C.Null(), Target.UNKNOWN).clone();
    Variable c13 = new Variable("$name", "", false, false, Target.UNKNOWN);
}
Also used : CInt(com.laytonsmith.core.constructs.CInt) IVariable(com.laytonsmith.core.constructs.IVariable) Variable(com.laytonsmith.core.constructs.Variable) Command(com.laytonsmith.core.constructs.Command) CBoolean(com.laytonsmith.core.constructs.CBoolean) IVariable(com.laytonsmith.core.constructs.IVariable) CArray(com.laytonsmith.core.constructs.CArray) CDouble(com.laytonsmith.core.constructs.CDouble) CFunction(com.laytonsmith.core.constructs.CFunction) Construct(com.laytonsmith.core.constructs.Construct) CNull(com.laytonsmith.core.constructs.CNull) CString(com.laytonsmith.core.constructs.CString) Test(org.junit.Test)

Example 22 with Construct

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

the class RandomTests method testVoidAndReturnedVoidAreTheExactSame.

@Test
public void testVoidAndReturnedVoidAreTheExactSame() throws Exception {
    Environment env = Static.GenerateStandaloneEnvironment(false);
    Construct returnedVoid = new ArrayHandling.array_insert().exec(Target.UNKNOWN, env, C.Array(), C.String(""), C.Int(0));
    Construct voidKeyword = Static.resolveConstruct("void", Target.UNKNOWN);
    assertTrue(returnedVoid == voidKeyword);
}
Also used : ArrayHandling(com.laytonsmith.core.functions.ArrayHandling) Environment(com.laytonsmith.core.environments.Environment) Construct(com.laytonsmith.core.constructs.Construct) Test(org.junit.Test)

Example 23 with Construct

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

the class BukkitMCCommand method handleTabComplete.

// I may be able to move these to c.l.c.f.Commands.java
@Override
public List<String> handleTabComplete(MCCommandSender sender, String alias, String[] args) {
    if (Commands.onTabComplete.containsKey(cmd.getName().toLowerCase())) {
        Target t = Target.UNKNOWN;
        CArray cargs = new CArray(t);
        for (String arg : args) {
            cargs.push(new CString(arg, t), t);
        }
        CClosure closure = Commands.onTabComplete.get(cmd.getName().toLowerCase());
        try {
            closure.execute(new CString(alias, t), new CString(sender.getName(), t), cargs, // reserved for an obgen style command array
            new CArray(t));
        } catch (FunctionReturnException e) {
            Construct fret = e.getReturn();
            if (fret instanceof CArray) {
                List<String> ret = new ArrayList<>();
                if (((CArray) fret).inAssociativeMode()) {
                    for (Construct key : ((CArray) fret).keySet()) {
                        ret.add(((CArray) fret).get(key, Target.UNKNOWN).val());
                    }
                } else {
                    for (Construct value : ((CArray) fret).asList()) {
                        ret.add(value.val());
                    }
                }
                return ret;
            }
        } catch (ConfigRuntimeException cre) {
            ConfigRuntimeException.HandleUncaughtException(cre, closure.getEnv());
            return new ArrayList<>();
        }
    }
    BukkitMCCommandTabCompleteEvent event = new BukkitMCCommandTabCompleteEvent(sender, cmd, alias, args);
    EventUtils.TriggerListener(Driver.TAB_COMPLETE, "tab_complete_command", event);
    return event.getCompletions();
}
Also used : BukkitMCCommandTabCompleteEvent(com.laytonsmith.abstraction.bukkit.events.BukkitMiscEvents.BukkitMCCommandTabCompleteEvent) Target(com.laytonsmith.core.constructs.Target) CClosure(com.laytonsmith.core.constructs.CClosure) CArray(com.laytonsmith.core.constructs.CArray) Construct(com.laytonsmith.core.constructs.Construct) ArrayList(java.util.ArrayList) List(java.util.List) CString(com.laytonsmith.core.constructs.CString) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) ConfigRuntimeException(com.laytonsmith.core.exceptions.ConfigRuntimeException) CString(com.laytonsmith.core.constructs.CString)

Example 24 with Construct

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

the class AbstractEvent method execute.

/**
 * This function is run when the actual event occurs.
 *
 * @param tree The compiled parse tree
 * @param b The bound event
 * @param env The operating environment
 * @param activeEvent The active event being executed
 */
@Override
public final void execute(ParseTree tree, BoundEvent b, Environment env, BoundEvent.ActiveEvent activeEvent) throws ConfigRuntimeException {
    preExecution(env, activeEvent);
    // Various events have a player to put into the env.
    // Do this after preExcecution() in case the particular event needs to inject the player first.
    Construct c = activeEvent.getParsedEvent().get("player");
    if (c != null) {
        try {
            MCPlayer p = Static.GetPlayer(c, Target.UNKNOWN);
            env.getEnv(CommandHelperEnvironment.class).SetPlayer(p);
        } catch (CREPlayerOfflineException e) {
            // Set env CommandSender to prevent incorrect inherited player from being used in a player event.
            if (env.getEnv(CommandHelperEnvironment.class).GetPlayer() != null) {
                env.getEnv(CommandHelperEnvironment.class).SetCommandSender(Static.getServer().getConsole());
            }
        }
    }
    ProfilePoint event = null;
    if (env.getEnv(GlobalEnv.class).GetProfiler() != null) {
        event = env.getEnv(GlobalEnv.class).GetProfiler().start("Event " + b.getEventName() + " (defined at " + b.getTarget().toString() + ")", LogLevel.ERROR);
    }
    try {
        try {
            // Get the label from the bind time environment, and put it in the current environment.
            String label = b.getEnvironment().getEnv(GlobalEnv.class).GetLabel();
            if (label == null) {
                // Set the permission to global if it's null, since that means
                // it wasn't set, and so we aren't in a secured environment anyways.
                label = Static.GLOBAL_PERMISSION;
            }
            env.getEnv(GlobalEnv.class).SetLabel(label);
            MethodScriptCompiler.execute(tree, env, null, null);
        } catch (CancelCommandException ex) {
            if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
                StreamUtils.GetSystemOut().println(ex.getMessage());
            }
        } catch (FunctionReturnException ex) {
        // We simply allow this to end the event execution
        } catch (ProgramFlowManipulationException ex) {
            ConfigRuntimeException.HandleUncaughtException(new CREFormatException("Unexpected control flow operation used.", ex.getTarget()), env);
        }
    } finally {
        if (event != null) {
            event.stop();
        }
        // Finally, among other things, we need to clean-up injected players and entities
        postExecution(env, activeEvent);
    }
}
Also used : CREPlayerOfflineException(com.laytonsmith.core.exceptions.CRE.CREPlayerOfflineException) CancelCommandException(com.laytonsmith.core.exceptions.CancelCommandException) MCPlayer(com.laytonsmith.abstraction.MCPlayer) Construct(com.laytonsmith.core.constructs.Construct) CommandHelperEnvironment(com.laytonsmith.core.environments.CommandHelperEnvironment) ProgramFlowManipulationException(com.laytonsmith.core.exceptions.ProgramFlowManipulationException) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) FunctionReturnException(com.laytonsmith.core.exceptions.FunctionReturnException) CREFormatException(com.laytonsmith.core.exceptions.CRE.CREFormatException) ProfilePoint(com.laytonsmith.core.profiler.ProfilePoint)

Example 25 with Construct

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

the class NewScript method toString.

@Override
public String toString() {
    StringBuilder b = new StringBuilder(label != null ? label + ":" : "");
    boolean first = true;
    for (Construct c : signature) {
        if (!first) {
            b.append(" ");
        }
        first = false;
        if (c instanceof Variable) {
            Variable var = (Variable) c;
            if (var.isOptional() && !var.getDefault().trim().isEmpty()) {
                b.append("[").append(var.getVariableName()).append("='").append(var.getDefault()).append("']");
            } else if (var.isOptional()) {
                b.append("[").append(var.getVariableName()).append("]");
            } else {
                b.append(var.getVariableName());
            }
        } else {
            b.append(c.val());
        }
    }
    return b.toString();
}
Also used : Variable(com.laytonsmith.core.constructs.Variable) Construct(com.laytonsmith.core.constructs.Construct)

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