Registering commands in spigot

Discussion in 'Programming' started by Johan256, Jun 26, 2016.

  1. So I keep seeing this method of registering commands

    getCommand(command).setExecutor(new CommandClass());

    However, isn't this method of registering commands problematic? Every time when someone types in that command, the program runs an instance of that class and if that class uses a variable (for some reason) wouldn't that create a memory issue?

    Is there another way of registering a command from different classes?
  2. You shouldn't store variables inside the executor that is supposed to be unique to that player running the command. If you have to do that, either have local variables or create an inner private static class within the CommandExecutor that is stored in a HashMap of players and that class. However, just make sure to remove them when they leave otherwise you will wind up with a memory leak.
  3. No, what
    Code (Text):
    setExecutor(new CommandClass());
    does is it makes a new instance of CommandClass and sets that as the command executor, it will always use that one instance whenever the command is executed rather than make a new instance every time.