[Tutorial] How to override default command

Discussion in 'Resources' started by BaumwolleHD, Aug 4, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    BaumwolleHD

    In this tutorial i will show you how to easily override commands that give you an error when tring it with the normal way (getCommand("plugins").setExecutor()).

    Long story short let me make an example:

    YourMainClass:

    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. System.out.print("Your plugin is starting");
    5. Bukkit.getPluginmanager().RegisterEnents(PlayerCommandPreprocessEventListener(),this)
    6.  
    7. }


    PlayerCommandPreprocessEventListener:

    Code:java
    1. package me.BaumwolleHD.com.events;
    2.  
    3. import me.BaumwolleHD.com.AzzuroNoteMain;
    4. import me.BaumwolleHD.com.ANServer.Server;
    5.  
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    11.  
    12. public class PlayerCommandPreprocessEventListener implements Listener {
    13.  
    14. @EventHandler(priority = EventPriority.LOWEST)
    15. public void onCommandPreProcess(PlayerCommandPreprocessEvent event){
    16. if(event.getMessage().toLowerCase().startsWith("/plugins")){
    17. event.setCancelled(true);
    18. event.getPlayer().sendMessage("This server uses no Plugins exept");
    19. } else if(event.getMessage().toLowerCase().startsWith("/stop")) {
    20. event.setCancelled(true);
    21. Bukkit.getServer().shutdown()
    22. }
    23. }
    24. }


    Plugin.yml:

    Code:
    name: AzzuroNote
    version: 2.0
    description: If you read this please go kill yourself
    author: BaumwolleHD
    website: http://www.azzuronote.de
    commands:
      stop:
        description: Stop the server
        usage: /<command>
      plugin:
        descrition: Get information about the used plugins
        usage: /<command>
    
    Explination:

    In the onCommand() method we register the events in the PlayerCommandPreprocessEventListener class.

    In the PlayerCommandPreprocessEventListener class we register the PlayercommandPreprocessEvent wich is called before bukkit looks at the command.

    In it we check the send message starts with "/plugin" or "/stop" and if does a text will be to the user of the command or if the cmomand user is an Operator the server will be shut down.

    This works with every command!

    I hope i helped you.
     
    Fabricio20 likes this.
  2. Offline

    Minnymin3

    You can do this by making a normal command with the same name as the default Bukkit command...
     
    CraftThatBlock and Garris0n like this.
  3. Offline

    BaumwolleHD

    Minnymin3 My bad but the normal version only works for most of the vanilla commands not for bukkit commands or stop i will edit
     
  4. Offline

    SourceForums

    BaumwolleHD
    You can't get strings from the command arguments. How will I do this?
     
  5. Offline

    Bart

    Take evt.getMessage(), split using a space as the delimiter, ignore the first part?

    Code:java
    1. String[] parts = evt.getMessage().split(" ");

    /test 1 2 3 4 5
    parts[0] = /test
    parts[1] = 1
    parts[2] = 2
    parts[3] = 3
    parts[4] = 4
    parts[5] = 5

    Not sure though.
     
  6. Offline

    SourceForums

    Bart
    Nope, it doesn't work unfortunately. :(
     
  7. Offline

    Bart

    To be honest you should be using onCommand and CommandExecutors, not PlayerCommandPreProcessEvent. I don't see any situation to use that event unless your plugin is restricting users from executing certain commands under certain conditions.

    I'm pretty sure @Minnymin3's method works fine.
     
    Garris0n likes this.
  8. Offline

    SourceForums

    Bart
    So, should I just do this like how I'd normally create a command? In that case, how will I cancel the original code?
     
  9. Offline

    Bart

    If you override the command as normal, e.g. getCommand("give") or getCommand("plugins"), I'm pretty sure it would work as normal and the normal code would not be executed.
     
    Garris0n likes this.
  10. Offline

    SourceForums

    Bart
    Wouldn't that just execute both original and new functions?
     
  11. Offline

    Bart

    No because you're overriding the default functionality. Try it yourself, it might not work.
     
  12. Offline

    SourceForums

    Bart
    Yes, I will test it, but just for future uses, can I set the priority of the override? Otherwise, wouldn't it crash with other plugins that override the same commands?
     
Thread Status:
Not open for further replies.

Share This Page