I am trying to make a /msg command, it just doesn't work ;p

Discussion in 'Spigot Plugin Development' started by poncethecat, May 24, 2015.

  1. gone
     
    #1 poncethecat, May 24, 2015
    Last edited: Jan 11, 2018
  2. Why does it say you're 44? >.>

    It doesn't work because Minecraft chat limit isn't 100. I could be wrong. :confused:

    P.S You should never use capitalization in your package name.
     
    • Winner Winner x 1
  3. Any syntax/runtime errors or is it logic?
     
  4. Why are you looping all the players? Why not do something like: /msg <player> <message> - then get args[0] (because array logic), and then get the player from that string.
     
    • Like Like x 1
  5. Player p = Bukkit.getServer().getPlayer(arg[0]);
     
  6. foncused

    Moderator Patron

    Use .equals() or .equalsIgnoreCase() instead of == when comparing strings.
     
  7. Then you've got some errors in console?
     
  8. 99 cents says yes.
     
  9. try this for a broadcast plugin > CBA TO TURN INTO A MSG PLUGIN LOL :p

    Code (Java):
                        String nothin = "";
                        for (String word : args) {
                            nothin += " " + word; //nothin is now equal to " " + word
                        }
                        nothin = nothin.substring(1);
                        for(Player server : Bukkit.getOnlinePlayers()){
                            server.sendMessage(nothin)
                            return true;
                        }
     
  10. foncused

    Moderator Patron

    You could post the error...
     
  11. When you get an internal error message in-game, you get an error message in-console as well.
     
    • Agree Agree x 2
  12. Is there anyone online? Ad there's no point in looping though all players just do

    Player target = Bukkit.getPlayer(args[0]);

    and then
    if (target == null) {

    }
    //Continue with other shiz
     
  13. If you're still looking, when I get home I'll send you my '/msg'
     
  14. Did you check if there were any more args before you broadcasted them.
     
  15. Serializator

    Supporter

    1. See if args[0] isn't null.
    2. Use .equals(); or .equalsIgnoreCase(); instead of ==.
    3. Use a StringBuilder instead of using args[1-100].
    4. Why use .toString(); if player.getName(); already returns a String.
    5. Don't cast before checking if it is an instanceof.
    6. Why check if the the sender is a player?

    I don't want to spoon feed you, but I think I do at the moment, but I hope you learn from the tips I gave you.

    Code (Text):

    public boolean onCommand(CommandSender cs, Command cmd, String ls, String[] args) {
       if(cmd.getName().equalsIgnoreCase("msg")) {
         if(args.length >= 2) {
           Player player = Bukkit.getServer().getPlayer(args[0]);

           if((player != null) && player.isOnline()) {
             StringBuilder message = new StringBuilder();

             for(int i = 2; i < args.length; i++) {
               message.append(args[i] + " ");
             }

             player.sendMessage(cs.getName() + " -> " + player.getName() + ChatColor.translateAlternateColorCodes('&', message.toString()));
           } else {
             cs.sendMessage(ChatColor.RED + "You need to send the message to an online player.");
           }
         } else {
           cs.sendMessage(ChatColor.RED + "You need a minimum of two arguments.")
         }

         return true;
       }

       return false;
    }
    Disclaimer: Not 100% this works, wrote it without API/IDE.
     
    • Like Like x 2
    • Agree Agree x 2
  16. The solution to this problem: Learn Java. Then learn the Spigot API.
     
    • Like Like x 1
  17. @Serializator That looks almost like mine, you beat me.

    @BetaNyan Keep your two cents to yourself. He is trying to learn, he just got stuck. Keep your smart ass comments to yourself.
     
    • Agree Agree x 3
    • Like Like x 1
  18. It is because you tried to access an argument that didn't exist in the Array. (In this case typing /msg without any arguments)
    You can check size of the array using:
    Code (Text):

    if(args.length < 1)
          return;
     
     
  19. clip

    Benefactor

    String msg = StringUtils.join(args, " ", 1, args.length)

    Much easier way to join your args.
     
  20. I agree with @BetaNyan it is obvious he copied a broadcast plugin