Ejemplo n.º 1
0
  public void run(final List<Variable> vars, Environment myEnv, final MethodScriptComplete done) {
    // Some things, such as the label are determined at compile time
    this.CurrentEnv = myEnv;
    this.CurrentEnv.getEnv(GlobalEnv.class).SetLabel(this.label);
    MCCommandSender p = myEnv.getEnv(CommandHelperEnvironment.class).GetCommandSender();
    if (!hasBeenCompiled || compilerError) {
      Target target = Target.UNKNOWN;
      if (left.size() >= 1) {
        try {
          target = new Target(left.get(0).line_num, left.get(0).file, left.get(0).column);
        } catch (NullPointerException e) {
          // Oh well, we tried to get more information
        }
      }
      throw new ConfigRuntimeException(
          "Unable to run command, script not yet compiled, or a compiler error occured for that command."
              + " To see the compile error, run /reloadaliases",
          null,
          target);
    }
    if (p instanceof MCPlayer) {
      if (CurrentEnv.getEnv(GlobalEnv.class).GetLabel() != null) {
        PermissionsResolver perms = CurrentEnv.getEnv(GlobalEnv.class).GetPermissionsResolver();
        String[] groups = CurrentEnv.getEnv(GlobalEnv.class).GetLabel().split("/");
        for (String group : groups) {
          if (group.startsWith("-")
              && perms.inGroup(((MCPlayer) p).getName(), group.substring(1))) {
            // negative permission
            throw new ConfigRuntimeException(
                "You do not have permission to use that command",
                ExceptionType.InsufficientPermissionException,
                Target.UNKNOWN);
          } else if (perms.inGroup(((MCPlayer) p).getName(), group)) {
            // They do have permission.
            break;
          }
        }
      }
    }

    try {
      for (ParseTree rootNode : cright) {
        for (Construct tempNode : rootNode.getAllData()) {
          if (tempNode instanceof Variable) {
            if (left_vars == null) {
              throw new ConfigRuntimeException(
                  "$variables may not be used in this context. Only @variables may be.",
                  null,
                  tempNode.getTarget());
            }
            ((Variable) tempNode)
                .setVal(
                    new CString(
                        Static.resolveDollarVar(
                                left_vars.get(((Variable) tempNode).getName()), vars)
                            .toString(),
                        tempNode.getTarget()));
          }
        }

        MethodScriptCompiler.registerAutoIncludes(CurrentEnv, this);
        MethodScriptCompiler.execute(rootNode, CurrentEnv, done, this);
      }
    } catch (ConfigRuntimeException ex) {
      // We don't know how to handle this really, so let's pass it up the chain.
      throw ex;
    } catch (CancelCommandException e) {
      // p.sendMessage(e.getMessage());
      // The message in the exception is actually empty
    } catch (LoopBreakException e) {
      if (p != null) {
        p.sendMessage("The break() function must be used inside a for() or foreach() loop");
      }
      System.out.println("The break() function must be used inside a for() or foreach() loop");
    } catch (LoopContinueException e) {
      if (p != null) {
        p.sendMessage("The continue() function must be used inside a for() or foreach() loop");
      }
      System.out.println("The continue() function must be used inside a for() or foreach() loop");
    } catch (FunctionReturnException e) {
      if (myEnv.getEnv(CommandHelperEnvironment.class).GetEvent() != null) {
        // Oh, we're running in an event handler. Those know how to catch it too.
        throw e;
      }
      if (p != null) {
        p.sendMessage("The return() function must be used inside a procedure.");
      }
      System.out.println("The return() function must be used inside a procedure.");
    } catch (Throwable t) {
      System.out.println("An unexpected exception occured during the execution of a script.");
      t.printStackTrace();
      if (p != null) {
        p.sendMessage(
            "An unexpected exception occured during the execution of your script. Please check the console for more information.");
      }
    }
    if (done != null) {
      done.done(null);
    }
  }
Ejemplo n.º 2
0
  /**
   * This is the workhorse function. It takes a given command, then converts it into the actual
   * command(s). If the command maps to a defined alias, it will run the specified alias. It will
   * search through the global list of aliases, as well as the aliases defined for that specific
   * player. This function doesn't handle the /alias command however.
   *
   * @param command
   * @return
   */
  public boolean alias(String command, final MCCommandSender player, List<Script> playerCommands) {

    GlobalEnv gEnv;
    try {
      gEnv =
          new GlobalEnv(
              parent.executionQueue,
              parent.profiler,
              parent.persistenceNetwork,
              parent.permissionsResolver,
              MethodScriptFileLocations.getDefault().getConfigDirectory(),
              new Profiles(MethodScriptFileLocations.getDefault().getSQLProfilesFile()));
    } catch (IOException ex) {
      Logger.getLogger(AliasCore.class.getName()).log(Level.SEVERE, null, ex);
      return false;
    } catch (Profiles.InvalidProfileException ex) {
      throw ConfigRuntimeException.CreateUncatchableException(ex.getMessage(), Target.UNKNOWN);
    }
    CommandHelperEnvironment cEnv = new CommandHelperEnvironment();
    cEnv.SetCommandSender(player);
    Environment env = Environment.createEnvironment(gEnv, cEnv);

    if (player instanceof MCBlockCommandSender) {
      cEnv.SetBlockCommandSender((MCBlockCommandSender) player);
    }

    if (scripts == null) {
      throw ConfigRuntimeException.CreateUncatchableException(
          "Cannot run alias commands, no config file is loaded", Target.UNKNOWN);
    }

    boolean match = false;
    try { // catch RuntimeException
      // If player is null, we are running the test harness, so don't
      // actually add the player to the array.
      if (player != null
          && player instanceof MCPlayer
          && echoCommand.contains(((MCPlayer) player).getName())) {
        // we are running one of the expanded commands, so exit with false
        return false;
      }

      // Global aliases override personal ones, so check the list first
      for (Script s : scripts) {
        try {
          if (s.match(command)) {
            this.addPlayerReference(player);
            if (Prefs.ConsoleLogCommands() && s.doLog()) {
              StringBuilder b = new StringBuilder("CH: Running original command ");
              if (player instanceof MCPlayer) {
                b.append("on player ").append(((MCPlayer) player).getName());
              } else {
                b.append("from a MCCommandSender");
              }
              b.append(" ----> ").append(command);
              Static.getLogger().log(Level.INFO, b.toString());
            }
            try {
              env.getEnv(CommandHelperEnvironment.class).SetCommand(command);
              ProfilePoint alias =
                  env.getEnv(GlobalEnv.class)
                      .GetProfiler()
                      .start("Global Alias - \"" + command + "\"", LogLevel.ERROR);
              try {
                s.run(
                    s.getVariables(command),
                    env,
                    new MethodScriptComplete() {
                      @Override
                      public void done(String output) {
                        try {
                          if (output != null) {
                            if (!output.trim().isEmpty() && output.trim().startsWith("/")) {
                              if (Prefs.DebugMode()) {
                                if (player instanceof MCPlayer) {
                                  Static.getLogger()
                                      .log(
                                          Level.INFO,
                                          "[CommandHelper]: Executing command on "
                                              + ((MCPlayer) player).getName()
                                              + ": "
                                              + output.trim());
                                } else {
                                  Static.getLogger()
                                      .log(
                                          Level.INFO,
                                          "[CommandHelper]: Executing command from console equivalent: "
                                              + output.trim());
                                }
                              }

                              if (player instanceof MCPlayer) {
                                ((MCPlayer) player).chat(output.trim());
                              } else {
                                Static.getServer()
                                    .dispatchCommand(player, output.trim().substring(1));
                              }
                            }
                          }
                        } catch (Throwable e) {
                          System.err.println(e.getMessage());
                          player.sendMessage(MCChatColor.RED + e.getMessage());
                        } finally {
                          Static.getAliasCore().removePlayerReference(player);
                        }
                      }
                    });
              } finally {
                alias.stop();
              }
            } catch (ConfigRuntimeException ex) {
              ex.setEnv(env);
              ConfigRuntimeException.React(ex, env);
            } catch (Throwable e) {
              // This is not a simple user script error, this is a deeper problem, so we always
              // handle this.
              System.err.println(
                  "An unexpected exception occured: " + e.getClass().getSimpleName());
              player.sendMessage(
                  "An unexpected exception occured: "
                      + MCChatColor.RED
                      + e.getClass().getSimpleName());
              e.printStackTrace();
            } finally {
              Static.getAliasCore().removePlayerReference(player);
            }
            match = true;
            break;
          }
        } catch (Exception e) {
          System.err.println("An unexpected exception occured inside the command " + s.toString());
          e.printStackTrace();
        }
      }

      if (player instanceof MCPlayer) {
        if (match == false && playerCommands != null) {
          // if we are still looking, look in the aliases for this player
          for (Script ac : playerCommands) {
            // RunnableAlias b = ac.getRunnableAliases(command, player);
            try {

              ac.compile();

              if (ac.match(command)) {
                Static.getAliasCore().addPlayerReference(player);
                ProfilePoint alias =
                    env.getEnv(GlobalEnv.class)
                        .GetProfiler()
                        .start(
                            "User Alias (" + player.getName() + ") - \"" + command + "\"",
                            LogLevel.ERROR);
                try {
                  ac.run(
                      ac.getVariables(command),
                      env,
                      new MethodScriptComplete() {
                        @Override
                        public void done(String output) {
                          if (output != null) {
                            if (!output.trim().isEmpty() && output.trim().startsWith("/")) {
                              if (Prefs.DebugMode()) {
                                Static.getLogger()
                                    .log(
                                        Level.INFO,
                                        "[CommandHelper]: Executing command on "
                                            + ((MCPlayer) player).getName()
                                            + ": "
                                            + output.trim());
                              }
                              ((MCPlayer) player).chat(output.trim());
                            }
                          }
                          Static.getAliasCore().removePlayerReference(player);
                        }
                      });
                } finally {
                  alias.stop();
                }
                match = true;
                break;
              }
            } catch (ConfigRuntimeException e) {
              // Unlike system scripts, this should just report the problem to the player
              // env.getEnv(CommandHelperEnvironment.class).SetCommandSender(player);
              Static.getAliasCore().removePlayerReference(player);
              e.setEnv(env);
              ConfigRuntimeException.React(e, env);
              match = true;
            } catch (ConfigCompileException e) {
              // Something strange happened, and a bad alias was added
              // to the database. Our best course of action is to just
              // skip it.
            }
          }
        }
      }
    } catch (Throwable e) {
      // Not only did an error happen, an error happened in our error handler
      throw new InternalException(
          TermColors.RED
              + "An unexpected error occured in the CommandHelper plugin. "
              + "Further, this is likely an error with the error handler, so it may be caused by your script, "
              + "however, there is no more information at this point. Check your script, but also report this "
              + "as a bug in CommandHelper. Also, it's possible that some commands will no longer work. As a temporary "
              + "workaround, restart the server, and avoid doing whatever it is you did to make this happen.\nThe error is as follows: "
              + e.toString()
              + "\n"
              + TermColors.reset()
              + "Stack Trace:\n"
              + StringUtil.joinString(Arrays.asList(e.getStackTrace()), "\n", 0));
    }
    return match;
  }