// @EventHandler(event = PreCommandEvent.class, order = Order.EARLY)
  public void onPlayerCommandPreprocess(PreCommandEvent event) {

    String[] split = event.getMessage().split(" ");

    if (split.length > 0) {
      split[0] = "/" + split[0];
      split = plugin.getWorldEdit().commandDetection(split);
      event.setMessage(StringUtil.joinString(split, " "));
    }
  }
  // Set a root-level comment.
  public void setComment(String key, String... comment) {
    if (comment != null && comment.length > 0) {
      for (int i = 0; i < comment.length; i++) {
        if (!comment[i].matches("^" + COMMENT_CHAR + " ?")) {
          comment[i] = COMMENT_CHAR + " " + comment[i];
        }
      }

      String s = StringUtil.joinString(comment, LINE_BREAK);
      comments.put(key, s);
    } else {
      comments.remove(key);
    }
  }
Beispiel #3
0
  /**
   * Called when a player attempts to use a command
   *
   * @param event Relevant event details
   */
  @EventHandler(priority = EventPriority.LOW, event = PlayerCommandPreprocessEvent.class)
  public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
    if (event.isCancelled()) {
      return;
    }

    String[] split = event.getMessage().split(" ");

    if (split.length > 0) {
      split = plugin.getWorldEdit().commandDetection(split);
      split[0] = "/" + split[0];
    }

    event.setMessage(StringUtil.joinString(split, " "));
  }
  /**
   * Get the usage string for a nested command.
   *
   * @param args
   * @param level
   * @param method
   * @param player
   * @return
   * @throws CommandException
   */
  protected String getNestedUsage(String[] args, int level, Method method, T player)
      throws CommandException {

    StringBuilder command = new StringBuilder();

    command.append("/");

    for (int i = 0; i <= level; ++i) {
      command.append(args[i] + " ");
    }

    Map<String, Method> map = commands.get(method);
    boolean found = false;

    command.append("<");

    Set<String> allowedCommands = new HashSet<String>();

    for (Map.Entry<String, Method> entry : map.entrySet()) {
      Method childMethod = entry.getValue();
      found = true;

      if (hasPermission(childMethod, player)) {
        Command childCmd = childMethod.getAnnotation(Command.class);

        allowedCommands.add(childCmd.aliases()[0]);
      }
    }

    if (allowedCommands.size() > 0) {
      command.append(StringUtil.joinString(allowedCommands, "|", 0));
    } else {
      if (!found) {
        command.append("?");
      } else {
        // command.append("action");
        throw new CommandPermissionsException();
      }
    }

    command.append(">");

    return command.toString();
  }
 public String docs() {
   return "string {x, z, [world] | locationArray} Returns the biome type of this block column. The location array's"
       + " y value is ignored. The value returned"
       + " may be one of the following: "
       + StringUtil.joinString(MCBiomeType.values(), ", ", 0);
 }
 public String docs() {
   return "void {x, z, [world], biome | locationArray, biome} Sets the biome of the specified block column."
       + " The location array's y value is ignored."
       + " Biome may be one of the following: "
       + StringUtil.joinString(MCBiomeType.values(), ", ", 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;
  }
 public void setPermissions(String[] permissions) {
   this.permissions = permissions;
   if (permissions != null) {
     super.setPermission(StringUtil.joinString(permissions, ";"));
   }
 }