[Tutorial/Recourse] onCommand efficentcy

Discussion in 'Resources' started by JPG2000, Nov 5, 2013.

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

    JPG2000

    PRE-NOTE: This is basically all for saving time. You could figure this out on your own, but this is a good example of a nice way to do this.

    Basically, in plugins that are using alot of onCommands (or commands in general), its usually nice to shorten down what the code does with methods. For most people (including me) every command I made, I did the usual:
    Code:java
    1. if (sender instanceof Player) {
    2. if (sender.hasPermission("perm")) {
    3. //code
    4. } else {
    5. sender.sendMessage("You dont have permission!");
    6. }
    7.  
    8. } else {
    9. sender.sendMessage("You can't be a player");
    10. }


    Well, it honestly got really old. So, I created some very simple methods to shorten things down.
    Please note, you have to edit this a little your self. I'll tell you when in bold comments.

    In the end:
    Code:java
    1. if (Helper.isPlayer(sender)) {
    2. if (Helper.isAuthorized("plugin.permission") {
    3. //Code
    4. }
    5. }

    It shortens down the code, and still sends messages etc.

    It also has a help message system, very nice.

    HelpMessage exampel:
    Code:java
    1. player.sendMessage(Helper.getHelpMessage("heal");
    2. //It would return the help message of heal



    NOTE: Add this class and then rename the package to your liking. Then edit the class - comment if you have any questions.
    Helper class:
    http://pastebin.com/cDi1Eq9y
     
  2. Shouldn't the help message example have 2 closing brackets at the end like this

    Code:java
    1. player.sendMessage(Helper.getHelpMessage("heal"));


    not like

    Code:java
    1. player.sendMessage(Helper.getHelpMessage("heal");
     
  3. Offline

    JPG2000

  4. Offline

    drtshock

    You can just define the permission and no permission message in the plugin.yml and you don't need to handle permissions in your commands at all ;)
     
  5. Offline

    JPG2000

    drtshock Yes but color isnt/can't included.
     
  6. Offline

    Garris0n

    Did you try putting a ยง(code) in it?
     
    JPG2000 likes this.
  7. Offline

    Cirno

    Just here to correct a typo in your title; it's "efficiency".
     
    DSH105 and drtshock like this.
Thread Status:
Not open for further replies.

Share This Page