2 commands with the same name just 2 after the second one?

Discussion in 'Plugin Development' started by julllleee, Feb 21, 2012.

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

    julllleee

    Hi, Im making a help plugin to my own server. It's my first plugin and i need help with that when i say /help 2 i shows the command for /help? Don't know if i need to upload something but if i need to just say it ;) thanks in advance!
     
  2. Offline

    Firefly

    The "2" part of the command is is not the command's name but rather an argument. To catch that the player typed the "2" you need to do this:

    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
     
    //The "2" part is located in the 0 index of the String Array. We need to surround this in a Try/Catch block since the user could put in a string for this index of the String Array.
     
    try {
    int helpMenu = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {
    player.sendMessage("Your argument must be a number!");
    }
     
    if (helpMenu == 2) {
    //Do whatever here
    }
     
    return true;
    }
    Granted, I skipped a bit of code, but you should get the general idea from this.

    Hope this helps!

    -Firefly
     
  3. Offline

    julllleee

    Hmm thanks but i don't know where to put it? im not so good with java/bukkitplugins
    But this is my code
    Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package plugin.julle.help;
     
    import java.io.File;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    /**
    *
    * @author Julle
    */
    public class Help extends JavaPlugin {
        @Override
        public void onEnable(){
           
     
    }  @Override
      public void onDisable(){
           
    }  @Override
      public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    if (command.getName().equalsIgnoreCase("help")){
    if (sender instanceof Player) {
    Player player = (Player) sender; 
    player.sendMessage(this.getConfig().getString("1"));
    player.sendMessage(this.getConfig().getString("2"));
    player.sendMessage(this.getConfig().getString("3"));
    player.sendMessage(this.getConfig().getString("4"));
    player.sendMessage(this.getConfig().getString("5"));
    player.sendMessage(this.getConfig().getString("6"));
    player.sendMessage(this.getConfig().getString("7"));
    player.sendMessage(this.getConfig().getString("8"));
    player.sendMessage(this.getConfig().getString("9"));
    player.sendMessage(this.getConfig().getString("10"));
    }
    }
    else if (command.getName().equalsIgnoreCase("help2")){
    if (sender instanceof Player) {
    Player player = (Player) sender; 
    player.sendMessage(this.getConfig().getString("11"));
    player.sendMessage(this.getConfig().getString("12"));
    player.sendMessage(this.getConfig().getString("13"));
    player.sendMessage(this.getConfig().getString("14"));
    player.sendMessage(this.getConfig().getString("15"));
    player.sendMessage(this.getConfig().getString("16"));
    player.sendMessage(this.getConfig().getString("17"));
    player.sendMessage(this.getConfig().getString("18"));
    player.sendMessage(this.getConfig().getString("19"));
    player.sendMessage(this.getConfig().getString("20"));
     
    }
    }  return true;
        }
     
    }
    
    And this is my plugin.yml
    Code:
    name: Help
    main: plugin.julle.help.Help
    version: 1.1
    commands:
        help:
          aliases: ['help','hjälp']
        help2:
          aliases: ['help 2','hjälp 2']
     
  4. Offline

    Firefly

    if you are using help2 and not help 2 you don't need to do what I did. But you should put the onCommand method in between onEnable and onDisable
     
  5. Offline

    dillyg10

    Code:
    //in your onCommand...
    if (label.equalsIgnoreCase("help") {
    if (args.length>=1) {
    if args[0].equalsIgnoreCase("1") {
    //display what you want /help 1 to display
    }
    }
    else {
    //display here anything that you want the user to see when they type /help
    }
    }
    
     
  6. Offline

    Firefly

    You can't just do: if args[0].equalsIgnoreCase("1") since 1 is not a string but an integer and you need to Parse it first.
     
  7. Offline

    dillyg10

    Uhmm, no 1 is not an integer if you put it around "1"..

    if you want the plugin to do /help 1 then you do args[0].equalsIgnoreCase["1"]
     
  8. Offline

    julllleee

    Hmm, can't get it to work, i don't know so much about java and plugins. Started making plugins today. My friend helped me to build the first code, but he had no time to help me make the rests. Thats why i seem so stupid :p
    Code:
    package plugin.julle.help;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Help extends JavaPlugin {
        @Override
        public void onEnable(){
           
    }  @Override
      public void onDisable(){
       
    }  @Override
      public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    Player player = (Player) sender;   
    if (label.equalsIgnoreCase("help") {
    if (args.length>=1) {
    if args[0].equalsIgnoreCase("1") {
    player.sendMessage(this.getConfig().getString("1"));
    }
    }
    else {
    player.sendMessage(this.getConfig().getString("1"));
    }
    }
    return true;
    }}
    
    get 2 Errors on this code, Tried to fix them but can't get them to work :/
    And Firefly i tried to add your code but it just gives me a lot of errors :(
    thanks in advance!
     
  9. Offline

    dillyg10

    What are the errors, and what is in your config.yml?
     
  10. Offline

    julllleee

    on if (label.equalsIgnoreCase("help") { it says
    "cannot find symbol
    symbol: method
    equalsignoreCase(java.lang.string)
    location class plugin.julle.help.label

    ')' expected

    surround with ..."

    and on if args[0].equalsIgnoreCase("1") {
    " '(' expected
    ')' expected
    sorround with ...

    my config.yml looks like this

    Code:
    #ifall linjen är blank skriv in §1 annars får man error
    #Färger §11§22§33§44§55§66§77§88§99§aa§bb§cc§dd§ee§ff
    '1': §4Hjälp sida 1
    '2': Färger §11§22§33§44§55§66§77§88§99§aa§bb§cc§dd§ee§ff
    '3': text här
    '4': text här
    '5': text här
    '6': text här
    '7': text här
    '8': text här
    '9': text här
    '10': §9skriv /help2 eller /hjälp2 för att komma till nästa sida
    #sida 2
    '11': §4 Hjälp sida 2
    '12': text här
    '13': §1
    '14': §1
    '15': §1
    '16': §1
    '17': §1
    '18': §1
    '19': §1
    '20': §1
    #sida 3
    '21': §1
    '22': §1
    '23': §1
    '24': §1
    '25': §1
    '26': §1
    '27': §1
    '28': §1
    '29': §1
    '30': §1
    
    thanks
     
  11. Offline

    dillyg10

    ah, lol thoes are easy errors..
    {sidenote, what language are you speaking}
    just add () on the if statments for exapmle
    if(args[0].equalsIgnoreCase("1"))
    add () there :)
     
  12. Offline

    Firefly

    As I said, I left out a lot of code, you need to fill in the basic stuff. I gave you the very basics of what you need for functionality.

    This should be your full code in all its functionality (onCommand method). If you get errors, please post what they are.

    PHP:
      @Override
      
    public boolean onCommand(CommandSender senderCommand commandString commandLabelString[] args) {
    Player player = (Playersender;
    if (
    label.equalsIgnoreCase("help") {
    if (
    args.length>=1) {
     
    try {
      
    int helpMenu Integer.ParseInt(arg[0]);
    } catch (
    NumberFormatException e) {
      
    player.sendMessage("Argument must be an integer");
    }
    if (
    helpMenu == 1) {
      
    player.sendMessage(this.getConfig().getString("1"));
    }
    }
    }
    return 
    true;
    }
     
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  13. Offline

    julllleee

    Okay but i don't know what the basic code i need to add :/
    [​IMG]
    Im getting this error?
    Im speaking swedish :) the plugin is for my server with only swedish playing ppl
    thanks!
     
  14. Offline

    dillyg10

    Change label to commandLabel
     
  15. Offline

    julllleee

    [​IMG]
     
  16. Offline

    dillyg10

    Ok, don't take this in a rude way, but what Firefly was doing was giving you an outline of what you need, not the specific code, to fix these erros, change label to CommandLabel, change arg to args, and i think once you fix the args error you should be good
     
  17. Offline

    Firefly

    Try fixing these lines and post errors (if any)

    PHP:
    if (commandLabel.equalsIgnoreCase("help"))
     
     
    int helpMenu Integer.ParseInt(args[0]));
     
  18. Offline

    julllleee

    Ye this code worked. But when i said /help ingame or /help 1 or /help 2 it just shows my the default help page? the one bukkit have made?
    Code:
    package plugin.julle.help;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Help extends JavaPlugin {
        @Override
        public void onEnable(){
           
    }  @Override
      public void onDisable(){
       
    }  @Override
      public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    Player player = (Player) sender;   
    if (commandLabel.equalsIgnoreCase("help")) {
    if (args.length>=1) {
    if (args[0].equalsIgnoreCase("1")) {
    player.sendMessage(this.getConfig().getString("1"));
    }
    }
    else {
    player.sendMessage(this.getConfig().getString("1"));
    }
    }
    return true;
    }}
    
     
  19. Offline

    Firefly

    You can't override default commands (or at least I don't know how.)
     
  20. Offline

    dillyg10

    Change the command to something else and see if it works.
     
  21. Offline

    Firefly

    What dillyg10 said. You would have to change your commands to /myHelp or something like that, it needs to be different than the default commands.
     
  22. Offline

    julllleee

    hmm, when i used my orginal code the i could override the default command.


    change your code firefly, and got this :S just more errors. I think this will be my only plugin i make. I just fails to make them :/ I just create errors
    [​IMG]
     
  23. Offline

    Firefly

    You need to add a return statement for the onCommand method, then you need to remove a ")" from the end of ParseInt(args[0])); You also need to make sure all of your brackets are properly placed and indented. Stop trying to just copy and paste code. I give you code to guide you, not for you to copy/paste. The code I am writing is directly in here and not through a JDE, so therefore I cannot catch all the errors before I give you the code. Give the tips your JDE is telling you and work to fix those errors.
     
  24. Offline

    dillyg10

    Ok, you need a return statment before the bracke that is highlighted, you put too many parenthesies on helpMenu, and that should fix everything else. Don't give up on developing plugins! You just need to learn some more java, easily done through the internet or books :)
     
  25. Offline

    julllleee

    Ok, tried that but i just gets more errors! wtf

    i don't know what a return statement is, and when i fixed the parseint i get some other error :S Lol i have no desire to contiune developing, im not able to make a fucking help plugin!?! Ye im copying your text because i don't know what to do at all so i just take a change that the code will work.
    I don't have the skills to make plugins so i think im abandon plugin dev...
     
  26. Offline

    dillyg10

    Chill.. do you have a skype, I'll help you if you do :D
     
  27. Offline

    julllleee

    Ye, but can't stay up any longer. Need to sleep, school tomorrow.
    But is there any point of that? Because I will just spam you With questions and then its like you made the plugin.
     
  28. Offline

    Firefly

    You should consider reading the Bukkit Plugin Tutorial (on the wiki)... it contains tons of useful info and that's where I learned most of what I know about plugins/Java.
     
  29. Offline

    dillyg10

    I don't rlly care if you spam me with questions, lol I know I learned programming by spamming questions :D We are all noobs at one point.
     
  30. Offline

    julllleee

    Okay send a pm with your Skype name. Side note where are you from? [Your time zone]
    Ye will do that.

    Thanks
     
Thread Status:
Not open for further replies.

Share This Page