Better way to catch CommandExceptions for every single command?

Discussion in 'Plugin Development' started by TheEnderCrafter9, Aug 28, 2016.

Thread Status:
Not open for further replies.
  1. I'm thinking of ways to catch CommandException without manually writing it over and over for each command that I have.

    Right now I think I should just do a PlayerCommandPreProcessEvent, cancel it, then make the player execute the command inside a try catch block.
     
  2. Offline

    HeartandSoul

    Do a try/catch, Obviously. Say I'm doing this:

    Code:
    public void addRecipes(){
        RecipeTest recipeTest = new RecipeTest();
        recipeTest.getRecipes();
    }
    But this does not work, it does not return an error for an unknown reason.
    OH WAIT! Java is here to save the day with try/catch!

    Code:
    public void addRecipes(){
        try{
        RecipeTest recipeTest = new RecipeTest();
        recipeTest.getRecipes();
       }catch (Exception e){
            e.printStackTrace();
        }
    }
    Aside from my sarcastic remarks, catch a command exception when you try it.
     
  3. This would been helpful on day one. However, I am asking how should I try catch automatically for each command.
     
  4. Offline

    Zombie_Striker

    @TheEnderCrafter9
    Then just encapsulate the whole onCommand, like so.
    Code:
    boolean onCommnand(.....){
    try{
    
      // PUT ALL COMMANDS HERE
    
    } catch(...){
    }
    }
    [edit] this will only work if all your commands are in one onCommand. If not, then you will need multiple try/catches.
     
  5. I tried that :p but I also want it to support other plugins. Right now I think the event is the way to go.
     
  6. If you want it to support other plugins, you may have to use their API's. Or at least whatever you can grab on to. I'm still a bit rusty after a 3 month break so I can't say much more. Also, I know all plugins do not have a API.
     
  7. @TheEnderCrafter9
    I have to ask though, why do you want to catch CommandExceptions? I mean.. they're already being caught.. by Bukkit. A CommandException should never occurr in properly written code, and is only there to alert the user that something has gone wrong.
     
    bwfcwalshy likes this.
  8. I know that Bukkit catches them, however, I was interested on how to do it. I rewrite my code to have as little as possible exceptions, but unfortunately, others don't. I was wonder how I could replace the message "An internal error occured".
     
  9. Online

    timtower Administrator Administrator Moderator

    @TheEnderCrafter9 If it is just changing that message: listen for packets and find that message, if you found it: change it.
     
  10. No, I also want to alert all the avaliable developers the command that failed and who executed it.
     
  11. Online

    timtower Administrator Administrator Moderator

    @TheEnderCrafter9 Then I suggest that you just hook into the logger and pull all errors from the log.
     
  12. The console is not aviable to all the developers, and while I as the owner of the server do that, I would need to repost every single error or log. I would find it easier if every dev just got notified.
     
  13. Online

    timtower Administrator Administrator Moderator

    @TheEnderCrafter9 That is why you hook into it with the plugin.
    You see that there is a plugin throwing an error, get a couple lines before, get the error.
    Send that to the developers.

    No need for the developers to have direct access.
     
  14. What if I wanted to blow up everyone on a command failure?
    :rolleyes:


    Show Spoiler

    My worlds would be long gone because of (cough cough other plugins)
     
  15. Online

    timtower Administrator Administrator Moderator

    @TheEnderCrafter9 Then you can still do that with the logger.
    You got me questioning your sanity though....
     
  16. Huh, what about summoning a wither, maybe named (translated) "&4Coding Police"?


    That way the devs would have to fix code
    :):):):):)
     
  17. Online

    timtower Administrator Administrator Moderator

    You probably want to keep it with messages, and I am pretty sure that you know how to summon a wither.
     
  18. True, now I just need to write an auto updater (or download a lib) and make an april fools copy :)
     
  19. Offline

    Zombie_Striker

  20. ArsenArsen likes this.
  21. Anyone have any ideas that are better then mine? Also, sudo ing the player causes the command to be thrown twice.
     
  22. @TheEnderCrafter9
    If I'm going to give you some honest advice, don't catch these exceptions, especially for other plugins! It completely disables the user to be able to make any bug reports to the plugins author.
     
    timtower and bwfcwalshy like this.
Thread Status:
Not open for further replies.

Share This Page