Solved How to detect a plugin command event

Discussion in 'Spigot Plugin Development' started by Froyzz, May 26, 2016.

  1. Hi guys !
    I want to detect a command send by my other plugin, i tried the event "ServerCommandEvent" but it doesn't works.
    The code with the command:
    Code (Text):
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");
    my detection event:
    Code (Text):
        @EventHandler
        public void onBossCommandSummon(ServerCommandEvent event) throws IOException{
            CommandSender s = event.getSender();
            String c = event.getCommand();
            Bukkit.broadcastMessage(" who is the sender: try :The sender is : " + s.getName() + " ");
                Bukkit.broadcastMessage("first step");
                if (c.contains("mm mobs spawn Herobrine")){  
                    Bukkit.broadcastMessage("2nde step");
                    Config.setValeur(true);          
                }
                }
            }
    May you help me please, i don't find an other event for that :/
     
  2. I can't :(, the command is sending by the console, not by a player.
    Code (Text):
          @EventHandler
            public void onmort(EntityDeathEvent event){
                Entity p = event.getEntity();
                if (p instanceof PigZombie){
                    if (p.isCustomNameVisible()){
                        if (p.getWorld().getName().equals("world")){
                            Random r = new Random();
                            int alĂ©atoire = r.nextInt(4);                      
                            switch(alĂ©atoire){
                            case 0:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");
                                String m = Bukkit.getConsoleSender().getName();
                                Bukkit.broadcastMessage(" " + m + " ");
                            break;

                            case 1:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3778");
                            break;
                           
                            case 2:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3778");
                            break;
                           
                            case 3:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                               
                            break;
                           
                            case 4:
                                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5289,81,3775");
                               
                            break;
                           
                            default:;
                            break;    
     
  3. I wouldn't recommend doing it that way. A better way would be to create your own Event, call it when your command is executed, and then listen for that event in your plugin. The not-recommended way would be to listen to the PlayerCommandPreProcessEvent, which is called before the command is executed (meaning you have to check whether or not it was successful within your command and your listener). HIGHLY recommend using the method I suggested first.
     
  4. I can't, cause i can't detect if my command is executed or not,
    the way with
    Code (Text):
     Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mm mobs spawn Herobrine 1 world,5286,81,3775");
    doesn't works, i think the command is send by the plugin and not by the console, so i can't detect this command.
    I can't create an event too because the command is sending an other plugin, the detection event is in an other plugin.
    Could I not rather send a command directly from the console ?
     
  5. Yes, because you're executing the command as the console it won't call the PlayerCommandPreProcessEvent. Use the method I suggested above, that's your best option.
     
  6. i can't create an event cause the plugin detection is not the same with the plugin who send the
    command.
    "Yes, because you're executing the command as the console it won't call the PlayerCommandPreProcessEvent. Use the method I suggested above, that's your best option."
    No, my event is ServerCommandEvent, not PlayerCommandPreProcessEvent, that's not the same.
     
    #7 Froyzz, May 26, 2016
    Last edited: May 26, 2016
  7. From the Java docs: "Example of incorrect uses are: Using this event to run command logic".
    The way you're trying to accomplish your goal doesn't fit with the standards defined by the API you're using.
    If you want our help, I suggest you conform to those standards and try implementing methods that fit within them.
     
    • Funny Funny x 2
  8. Well, thanks :)