Solved command won't bring args[0] to broadcast message

Discussion in 'Plugin Development' started by Sivert2246, Nov 29, 2015.

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

    Sivert2246

    Hi! I'm trying to make a broadcast plugin that send an server message with /bc
    I've tried everything I know to fix this, no errors are appearing,
    args[0] wont go to the broadcast message... anyone knows how to fix this?
    Picture of my code: Error1Broadcast.png
     
  2. Online

    timtower Administrator Administrator Moderator

    @Sivert2246 Your check for the cmd, a command will never have a space.
     
  3. Offline

    Sivert2246

    Like this?
    (Sorry If i don't quite understand I'm new to Bukkit coding & coding in general) 124.png
     
  4. Online

    timtower Administrator Administrator Moderator

    @Sivert2246 That wouldn't work either.
    The cmd is the value registered in the plugin.yml, no need to access the args in the first check.
     
  5. Offline

    Sivert2246

    So... Where should I put the args[0]?
     
  6. Online

    timtower Administrator Administrator Moderator

    @Sivert2246 Do you need it somewhere else besides the broadcast line?
    And please use [code] <<code here>>[/code] instead of screenshots.
     
  7. Offline

    Sivert2246

    ok, no I only need it in the broadcast line
     
  8. Offline

    mcdorli

    [ code=java ] <<code here>> [/ code ] I hate reading a gray text.
     
  9. Offline

    Sivert2246

    Code:
    
    [B]package[/B] mc.sivert2246.plugin.serverbroadcast;
    
    
    
    [B]import[/B] org.bukkit.Bukkit;
    
    [B]import[/B] org.bukkit.ChatColor;
    
    [B]import[/B] org.bukkit.command.Command;
    
    [B]import[/B] org.bukkit.entity.Player;
    
    [B]import[/B] org.bukkit.plugin.java.JavaPlugin;
    
    
    
    [B]public class [/B]Main [B]extends [/B]JavaPlugin
    
    {
    
    @Override
    
    [B]public void onEnable[/B]()
    
    {
    
    getConfig().options().copyDefaults([B]true[/B]);
    
    saveConfig();
    
    reloadConfig();
    
    }
    
    [B]public boolean onCommand[/B](Command sender,Command cmd,String label,String[] args)
    
    {
    
    Player [B]theSender[/B] =(Player) sender;
    
    {
    
    [B]if[/B](cmd.getName().equalsIgnoreCase("bc"+ args[0]))
    
    {
    
    [B]if[/B](sender [B]instanceof[/B]Player)
    
    {
    
    [B]if[/B](theSender.hasPermission("server.broadcast"))
    
    {
    
    Bukkit.[I]broadcastMessage[/I](ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG"+ args[0])));
    
    }
    
    [B]else[/B]
    
    {
    
    [U]theSender[/U].sendMessage(ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG")+ getConfig().getString("No-Permission")));
    
    }
    
    }
    
    [B]else[/B]
    
    {
    
    [B]if[/B](theSender.hasPermission("server.broadcast"))
    
    {
    
    Bukkit.[I]broadcastMessage[/I](args[0]);
    
    }
    
    [B]else[/B]
    
    {
    
    theSender.sendMessage(ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG")+ getConfig().getString("No-Permission")));
    
    }
    
    }
    
    }
    
    [B]returnfalse[/B];
    
    }
    
    }
    
    }
    
     
  10. Offline

    Zombie_Striker

    @Sivert2246
    1. OMG, Its the [B ]Virus!!
    2. Don't Blind cast.
    3. cmd.getName returns the COMMAND, not the args. There is no way a command will be equal to the command+something else. That makes no sense.
    4. Right, only check if the sender is actually a player AFTER you already created the instance.
    5. That formatting tho. For Java, keep the brackets on the same line as the if statements.
    6. "returnfalse", cause that by it self will surely throw no error; Its not like it's missing a space or semicolon or anything.
     
  11. Offline

    Sivert2246

    ok, i'm looking at my code now & fixing it

    Wait what? is my cast blinded?
    And I think I did what you told me, and still doesnt work :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2015
  12. Offline

    Zombie_Striker

    @Sivert2246
    Yes. You casted the sender to be a player before you even knew he was a player.

    Most likely, it should work. Can you post your updated code.
     
  13. Offline

    Sivert2246

    Working on updating it now...

    Like this? (Still doesn't work... :/ )
    Code:
    
    [B]package[/B] mc.sivert2246.plugin.serverbroadcast;
    
    
    
    [B]import[/B] org.bukkit.Bukkit;
    
    [B]import[/B] org.bukkit.ChatColor;
    
    [B]import[/B] org.bukkit.command.Command;
    
    [B]import[/B] org.bukkit.entity.Player;
    
    [B]import[/B] org.bukkit.plugin.java.JavaPlugin;
    
    
    
    [B]public class [/B]Main [B]extends [/B]JavaPlugin
    
    {
    
    @Override
    
    [B]public void onEnable[/B]()
    
    {
    
    getConfig().options().copyDefaults([B]true[/B]);
    
    saveConfig();
    
    reloadConfig();
    
    }
    
    [B]public boolean onCommand[/B](Command sender,Command cmd,String label,String[] args)
    
    {
    
    [B]if[/B](cmd.getAliases().equals("bc")){
    
    [B]if[/B](sender [B]instanceof [/B]Player){
    
    Player [B]theSender[/B] =(Player) sender;{
    
    [B]if[/B](theSender.hasPermission("server.broadcast")){
    
    Bukkit.[I]broadcastMessage[/I](ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG"+ args[0])));
    
    }
    
    [B]else[/B]{
    
    Bukkit.[I]broadcastMessage[/I](ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG"+ args[0])));
    
    }
    
    }
    
    }
    
    }
    
    [B]return false[/B];
    
    }
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2015
  14. Offline

    Zombie_Striker

    Why are you checking if the aliases are "bc". Why not check if the main command is by by using cmd.getName().equalsIgnoreCase("bc")
     
  15. Offline

    mcdorli

    Use cmd.getName().equalsIgnoreCase
     
  16. Offline

    Sivert2246

    oh, didnt see that, changed back to cmd.getName().equalsIgnoreCase("bc")
    Still doesn't work?
     
  17. Offline

    Xerox262

    Why do you even need to cast to player anyway? CommandSender has a hasPermission method.

    You never checked if there was an argument 0, you just kinda coded it assuming there will never not be one which would throw exceptions if there isn't.

    P.S. We need to see your config.yml since you're trying to get something from it that might not exist (You might be missing a space.).
     
  18. Offline

    Sivert2246

    config.yml:

    Code:
    #Hey! Welcomme to the config!
    
    #The plugin tag:
    Plugin-TAG: '&3[&bBroadcast&3] '
    
    #Not required permissions message
    No-Permission: ' &eYou dont have permission for this command!'
    plugin.yml:

    Code:
    name: ServerBroadcast
    main: mc.sivert2246.plugin.serverbroadcast.Main
    version: 1.0
    commands:
      bc:
      description: /bc Message to broadcast. (Support color codes.)
     
  19. Offline

    Mrs. bwfctower

    @Sivert2246 Well you should indent the description.
     
  20. Offline

    Sivert2246

    wait, how do you mean with indent it? like remove the description?
     
  21. Offline

    Xerox262

    Your problem is you're doing getConfig().getString("Plugin-TAG"+ args[0]) when you should be doing getConfig().getString("Plugin-TAG") + args[0]
     
  22. Offline

    tommyhoogstra

  23. Offline

    Sivert2246

    ok, changed it but the plugin won't give me anything after using the command :/
    Code:
    
    [B]package[/B] mc.sivert2246.plugin.serverbroadcast;
    
    
    
    [B]import[/B] org.bukkit.Bukkit;
    
    [B]import[/B] org.bukkit.ChatColor;
    
    [B]import[/B] org.bukkit.command.Command;
    
    [B]import[/B] org.bukkit.entity.Player;
    
    [B]import[/B] org.bukkit.plugin.java.JavaPlugin;
    
    
    
    [B]public class [/B]Main [B]extends [/B]JavaPlugin
    
    {
    
    @Override
    
    [B]public void onEnable[/B]()
    
    {
    
    getConfig().options().copyDefaults([B]true[/B]);
    
    saveConfig();
    
    reloadConfig();
    
    }
    
    [B]public boolean onCommand[/B](Command sender,Command cmd,String label,String[] args)
    
    {
    
    [B]if[/B](cmd.getName().equalsIgnoreCase("bc")){
    
    [B]if[/B](sender [B]instanceof[/B]Player){
    
    Player [B]theSender[/B] =(Player) sender;{
    
    [B]if[/B](theSender.hasPermission("server.broadcast")){
    
    Bukkit.[I]broadcastMessage[/I](ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG")+ args[0]));
    
    }
    
    [B]else[/B]{
    
    Bukkit.[I]broadcastMessage[/I](ChatColor.[I]translateAlternateColorCodes[/I]('&', getConfig().getString("Plugin-TAG")+ args[0]));
    
    }
    
    }
    
    }
    
    }
    
    [B]return false[/B];
    
    }
    
    }
    
     
  24. Offline

    Xerox262

    Any errors in console?
     
  25. Offline

    Sivert2246

    Nope, I don't get anything, just that I issued a server command :/
     
  26. Offline

    mcdorli

    What editor do you use BTW?
    How did nobody be aware of this. This isn't how this works:
    Code:
    //                            This V
    public boolean onCommand(Command sender, Command cmd, String label, String[] args) {
    
    This should be CommandSender sender
     
    Zombie_Striker likes this.
  27. Offline

    Xerox262

    It's got to do with the fact that there's god damn [B ] every where
     
    Zombie_Striker likes this.
  28. Offline

    mcdorli

    Why? Why is it there. He uses some sort of custom text color library?
     
  29. Offline

    Xerox262

    I removed it was part of intellij because I noticed that. You thinking he's using a word processor rather than a text editor?
     
  30. Offline

    mcdorli

    Something that keeps the bold text when you copy out it. Funny tough, that it resembles IntelliJ, just because it's black and not white like eclipse.
     
Thread Status:
Not open for further replies.

Share This Page