Solved Custom /plugins command

Discussion in 'Spigot Plugin Development' started by antSTAR, Jan 25, 2020.

  1. Hello Spigot!

    I'm currently working on a custom core for my test server where I just practice to code and I'm trying to make a custom /plugins command due to I don't want the orignal text to show up when a player runs the /plugins message.

    So, what I'm trying to do is when you run /plugins a custom message will show up instead of spigots original message when you run the /plugins command. Is there any way to make this?

    Thanks.
     
  2. SteelPhoenix

    Moderator

    Just create it like you would any other command. It will override the default command.
     
  3. You can Listen for the PlayerCommandPreprocessEvent, check if the command is /plugins, cancel it if so and send your custom message to the player
     
  4. Yes, there is an event that you override the /plugins command.
    The event is called PlayerCommandPreprocessEvent.
    To override the command use it like this:

    Code (Java):
    @EventHandler(priority = EventPriority.LOWEST)
        public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
            String[] cmd = event.getMessage().split(" ");
            if(cmd[0].equals("/plugins")) {
                if(cmd.length > 1) {
                    event.setCancelled(true);
                    // Do stuff with args where cmd[1] is the new args[0] of commands
                } else {
                    event.setCancelled(true);
                    // Do stuff that runs only when player uses /plugins (without args)
                }
            }
        }
     
  5. Are you sure about this? Just tried this out and /plugins don't execute the code. If I enter /myplugin:plugins; that will only execute the custom command.
     
  6. Code (Java):

    package com.fighter.Listeners;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    public class CustomPluginsCommand implements Listener {
        @EventHandler
        public void onCustomPlugins(PlayerCommandPreprocessEvent event) {
            Player player = event.getPlayer();
            String message = event.getMessage();
           
            if (message.equals("/plugins")) {
                if (!player.hasPermission("customplugins.permission")) {
                    player.sendMessage(ChatColor.GREEN + "Plugins: 0");
                    event.setCancelled(true);
                } else {
                    return;
                }
            }
           
        }
    }
     
    EDIT: If doesn't work then sorry, NOT tested!
     
  7. Thanks everyone! I got it working with the PlayerCommandPreprocessEvent event :)
     
    • Like Like x 1