does anybody know the code for sub commands

Discussion in 'Plugin Development' started by GalaxyPrisonMc, Jun 30, 2014.

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

    GalaxyPrisonMc

  2. Offline

    adam753

    "sub commands" are just args. So if you type "/power join red", then it'll execute a "power" command with args[0]="join" and args[1] = "red". It's as simple as that; how you write the code to handle it is down to you.
     
  3. Offline

    GalaxyPrisonMc

    adam753 thank you so much do you think you code show me i'm kinda new and trying to learn i would do it my self if i knew how to i usually like to try and do stuff on my own so i learn it but i honestly don't know how to right it

    fixed

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    1Rogue


    If you feel like dealing with giant if-else trees, yes.
     
  5. Offline

    GalaxyPrisonMc

    1Rogue is there a better way to do it if so i'm learning java and bukkit so i'm open to suggestions

    can somebody please help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    Garris0n

    What exactly do you need? Your question has been answered.

    Also, you should at least have a basic understanding of Java before using the Bukkit API.
     
    adam753 likes this.
  7. Offline

    adam753

    GalaxyPrisonMc Sure, that's one way of doing it. There's no "right" way.
     
  8. Offline

    SuperOriginal

    GalaxyPrisonMc That is a way of doing it, just what 1Rouge was talking about was that instead of using multiple if() statements for different things, you can just use one if statement using "&&" which means "and." So instead of doing if(label = power) and if(args[0] equals join) and if(args[1] equals red), you can just do if(label = power and args[0] equals join and args[1] equals red). So it would look like this in code:
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command command,
    3. String label, String[] args) {
    4. if (label.equalsIgnoreCase("power") && args[0].equalsIgnoreCase("join") && args[1].equalsIgnoreCase("red")) {
    5. }
    6. return true;
    7. }
    8. }
     
  9. SuperOriginal
    I wouldn't do that either. In my opinion the best way to do this is to do this is
    Code:java
    1. if(!statement) {
    2. ...
    3. // return
    4. }
    5.  
    6. //this will be reached only if the statement above is true
     
  10. Offline

    Syd

    if-else trees for sub commands should be considered very ugly and basic. They work, but you won't be happy with it.

    I developed a small Framework for myself, that allows me easily to create as many subcommands as I want (and sub sub commands). Also, every SubCommand is an own class, making the code easier readable.

    It saved my many hours of work and debugging and I would recommend every dev to use such a system as well. (There are more, propably better, systems out there - maybe even in the Resource section. ;))

    TL;DR:
    Avoid if-else trees, develop/use a framework that makes command management way easier.

    PS: for reference my code: https://github.com/SydMontague/CLCore/tree/master/src/main/java/de/craftlancer/core/command
    It's poorly documented, and might not suite your needs - but it could help you (and others) to understand my way of handling subcommands. ;)
     
Thread Status:
Not open for further replies.

Share This Page