Broadcasting

Discussion in 'Plugin Development' started by xX_Blazer_Xx, Feb 6, 2016.

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

    xX_Blazer_Xx

    Hey guys! I've come over this problem that is so simple but i simply cant get over with. I'm making a plugin full of a bunch of useful plugins and one is this :
    (i dont know how to index code, sorry, please tell me if you can. ! also have a bunch of comments to help me out if i forget. ignore them)

    Code:
    public boolean onCommand2(CommandSender sender, Command cmd, 
                String label, String[]args){
    if(cmd.getName()/*if command done*/.equalsIgnoreCase("brod")/*Is the command hello*/){
       
        if(args[1]=="prefix"){
           
           
            Bukkit.broadcastMessage(ChatColor.BLUE+"["+ChatColor.GOLD+ChatColor.BOLD+"Broadcast"
            +ChatColor.BLUE+"]"+ChatColor.GREEN+args[0]);
            }
        else if(args[1]==null){
            Bukkit.broadcastMessage(args[0]);
           
           
           
        }
        else{
            sender.sendMessage(ChatColor.RED+"Usage: "+ChatColor.WHITE+"/brod <Message> (<prefix>: for prefix<null>: for nothing)");
           
           
           
           
        }
    return true;
       
       }
    return false;   
           
        }
    



    Any help is awesome! thanks
     
  2. Offline

    JoaoBM

    @xX_Blazer_Xx Check if there are args before getting them. Put that last else statement if the args are == 0
     
  3. Code:
    if cmd name equals("broad")
     if args length == 0
      sendMessage "Wrong usage";
     else
      String args = /* join args */
      Bukkit broadcast args;
    
     
  4. Offline

    xX_Blazer_Xx

    Thankyou Guys! Ill try it out now. @JoaBM Im gonna assume you are talking about the length of the args.

    Got rid of the else at the end and dd the if on top to check whether the args had anything and the rest as shown. Still shows the usage from the plugin yml. and if removed, nothing. I tried doing a get.Logger().info(args.length+args[1]+args[0]+"test") but even that doesnt end up in the console. It is at the top so it should run right? Or an error comes before the code is run at the very top if the first if statment,

    Any help?

    OK! it seems that the oncommand2 wont work so i pasted the cmmand in a working one and did this
    Code:
    if(args.length==0){
                        sender.sendMessage(ChatColor.RED+"Usage: "+ChatColor.WHITE+"/brod <Message> (<prefix>: for prefix<null>: for nothing)");
                      
                    }
                    else if(args.length==1){
                        Bukkit.broadcastMessage(args[0]);
                      
                    }
                    else if(args.length==2){
                        System.out.println(args.length+" "+args[1]+args[0]);
                        getLogger().info(args.length+" "+args[1]+args[0]);
                        getLogger().info("testtt");
                      
                    if(args[1]=="prefix"){
                      
                      
                      
                        Bukkit.broadcastMessage(ChatColor.BLUE+"["+ChatColor.GOLD+ChatColor.BOLD+"Broadcast"
                        +ChatColor.BLUE+"]"+ChatColor.GREEN+args[0]);
                        }else{
                            sender.sendMessage(ChatColor.RED+"Usage: "+ChatColor.WHITE+"/brod <Message> (<prefix>: for prefix<null>: for nothing)");
                        }
                  
                    return true;
                  
                  
                  
                }
                    }
              
                else{
                    sender.sendMessage(ChatColor.BOLD+"Hello "+ChatColor.ITALIC+ChatColor.GREEN+"Console!");
                  
                  
                  
                }
              
              
              
              
            }
          
            return false;
          
        }
    
    ]/code]
    
    
    It works now! f just one arg it broadcasts,just that if i add prefix it takes it as a error. Another thing is no matter if the command was done sussessfully, it still prints the usage from plugin.yml
     
    Last edited by a moderator: Feb 7, 2016
  5. Offline

    Caedus

    don't compare strings with ==, use .equals instead:

    Code:
    if(args[1].equals("prefix"))
    Use code tags on your recent post there too, please
     
  6. First off, use [ syntax=java ] [/ syntax ] to show us code :)
    Also, don't use onCommand2, just use onCommand, it's just unnecessary.
     
    xX_Blazer_Xx likes this.
  7. Offline

    xX_Blazer_Xx

    Thankyou for all the replies, ive got it to work,i did multiple oncommandsinstead of the 2 if statments instead. Thankyou all and @_zircon_ ill use that in the future.
     
  8. Lolz these comments
    Code:
    if(cmd.getName()/*if command done*/.equalsIgnoreCase("brod")/*Is the command hello*/){
    
    :)
     
  9. Offline

    WolfMage1

    If it works for him it works :p
     
    Lionhard likes this.
  10. Offline

    JoaoBM

    @xX_Blazer_Xx Also one thing I you could do is to use multiple else if checking different cmd.getName to create different commands in one method.
     
Thread Status:
Not open for further replies.

Share This Page