1.8.8 Reactive unknown-command message

Discussion in 'Spigot Plugin Development' started by illuminator3, Feb 4, 2020.

  1. Changing the unknown-command message is pretty easy:
    Code (Java):
    SpigotConfig.unknownCommandMessage = "command not found";
    But how would I add the executed command at the end?
     
  2. not possible with default implementation. if you wanted to add this, youd have to override SimpleCommandMap#dispatch(CommandSender, String)
    from SimpleCommandMap:
    Code (Java):
      public boolean dispatch(@NotNull CommandSender sender, @NotNull String commandLine) throws CommandException {
        String[] args = commandLine.split(" ");
        if (args.length == 0)
          return false;
        String sentCommandLabel = args[0].toLowerCase(Locale.ENGLISH);
        Command target = getCommand(sentCommandLabel);
        if (target == null)
          return false;
        try {
          target.timings.startTiming();
          target.execute(sender, sentCommandLabel, Arrays.<String>copyOfRange(args, 1, args.length));
          target.timings.stopTiming();
        } catch (CommandException ex) {
          target.timings.stopTiming();
          throw ex;
        } catch (Throwable ex) {
          target.timings.stopTiming();
          throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
        }
        return true;
      }
    when this returns false, the unknown command message gets printed.
    from CraftServer (which would be hard to override)
    Code (Java):
      public boolean dispatchCommand(CommandSender sender, String commandLine) {
        Validate.notNull(sender, "Sender cannot be null");
        Validate.notNull(commandLine, "CommandLine cannot be null");
        AsyncCatcher.catchOp("command dispatch");
        if (this.commandMap.dispatch(sender, commandLine))
          return true;
        if (!SpigotConfig.unknownCommandMessage.isEmpty())
          sender.sendMessage(SpigotConfig.unknownCommandMessage);
        return false;
      }
    as you can see the functionality isnt there.

    handle the unknown command in SimpleCommandMap#dispatch(CommandSender, String) but return false anyway and set the unknownCommandMessage to empty so it doesnt get printed on top of your custo message