Chat format help

Discussion in 'Plugin Development' started by tomisanhues3, Jun 12, 2015.

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

    tomisanhues3

    Hello, I need some help with my plugin, Im very new to java so I would appreciate an example code on this, thanks in advance

    1. need to make it were the chat reads all the message, not just the args[1]
    2. want the chat format to be@ [player who sent it] + >> [Complete message the sender sent]@

    Code:
    package me.tomisanhues3.essentials;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Essentials extends JavaPlugin {
    
      
      
        // Disable
        @Override
        public void onDisable() {
            getLogger().info("essentials has been disabled!");
    
        }
    
        // enable
        @Override
        public void onEnable() {
            getLogger().info("essentials has been invoked!");
        }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (cmd.getName().equals("msg")) {
                if (args.length >= 2) {
                    Player player = Bukkit.getServer().getPlayer(args[0]);
                    if (player != null && player.isOnline()) {
                        {
                        // get target player -> send target player message
                        player.sendMessage(ChatColor.RED + args[1] );
    
                    }
    
                }
            }
            return false;
    }
            return false;
    }
    }
    
     
  2. Offline

    xDeeKay

    @tomisanhues3 Use something like this to build the strings:
    Code:
    String message = StringUtils.join(args, ' ', 1, args.length);
    This will join all the args from args[1] onward.
     
  3. Offline

    LeePMC

    or if that doesn't work (TBH @xDeeKay i have never seen yours used before, i might try it now) but if xDeeKays version doesn't work, you can always fall back to
    Code:
    String joinMessage;
                      
                        StringBuilder sb = new StringBuilder();
                        for (int i = 1; i < args.length; i++) {
                          
                                if (i != 0)
                                    sb.append(' ');
                                sb.append(args[i]);
                              
                            }
                      
                            joinMessage = sb.toString();
    which will do the same thing just in a different way
     
  4. Offline

    xDeeKay

    @LeePMC I used to use the method you mentioned, but I got annoyed with the space it leaves at the end =p
     
  5. Offline

    LeePMC

    or if that doesn't work (TBH @xDeeKay i have never seen yours used before, i might try it now) but if xDeeKays version doesn't work, you can always fall back to
    Code:
    String joinMessage;
                       
                        StringBuilder sb = new StringBuilder();
                        for (int i = 2; i < args.length; i++) {
                           
                                if (i != 0)
                                    sb.append(' ');
                                sb.append(args[i]);
                               
                            }
                       
                            joinMessage = sb.toString();
    which will do the same thing just in a different way
     
  6. @LeePMC
    You made a little mistake.
    i is never 0. So it will always run the code to append ' '. Fixed code would be:
    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for (int i = 2; i < args.length; i++) {
    3. if (i != 2) sb.append(" ");
    4. sb.append(args);
    5. }
    6. String message = sb.toString();
     
  7. Offline

    tomisanhues3

    Thank you everymuch all for helping me, learned quite a bit out of this post, also, how can I change the chat format for the sender and for the reciever
    like essentials MSG system
     
  8. Offline

    LeePMC

    @FisheyLP oh... I never realised xD the one I posted works perfectly for me without the String no errors or anything, probably just in case you use it where a string is not allowed :p

    EDIT: that !=0 was my bad, I took it from my plugin which was originally checking for i=0 and not 2
     
Thread Status:
Not open for further replies.

Share This Page