Beispiel #1
0
  /**
   * Used by the {@link CmdRegistration} to register the command.
   *
   * <p><b>Do not call this to register a command!</b>
   *
   * <p>You'll have to use {@link CmdRegistration#register(Plugin, BaseCmd)} to register a command
   * properly! Also don't forget to call {@link CmdRegistration#unregister(Plugin)} in onDisable()
   *
   * @param plugin The plugin that registered the command.
   * @throws CmdAlreadyRegisteredException When the command is already registered.
   */
  public void register(Plugin plugin) throws CmdAlreadyRegisteredException {
    setUsage(getUsage(plugin.getServer().getConsoleSender(), getBaseCmd().getName()));

    if (this.plugin != null) {
      throw new CmdAlreadyRegisteredException(plugin, this);
    }
    this.plugin = plugin;

    try {
      Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
      f.setAccessible(true);
      CommandMap commandMap = (CommandMap) f.get(Bukkit.getServer());

      commandMap.register(plugin.getName(), this);
    } catch (NoSuchFieldException | IllegalAccessException e) {
      e.printStackTrace();
    }
  }