Solved Colour formatting strings and Player send message

Discussion in 'Plugin Development' started by Nutowen, Aug 25, 2016.

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

    Nutowen

    I have this: I want it so you can do &a for color codes in game. If it is possible or something like that so it can send you like a blue message

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(command.getName().equalsIgnoreCase("nopmsg")){
    if(args.length == 1){
    sender.sendMessage(args);
    }
    }
    return false;
    }
     
  2. Offline

    Zombie_Striker

    @Nutowen
    To add color codes for message, use ChatColor.translateAlternateColorCodes('&', THE STRING);
     
    SethxGaming likes this.
  3. Offline

    SethxGaming

    Yes it is possible. there are several ways of doing this.

    First Method: Use §[ColorNumber] Example: §6This is Gold

    Second Method:
    ChatColor.[CHATCOLOR] Example: sender.sendMessage(ChatColor.ORANGE + "This is Gold");

    If you want players to be able to change the message ingame, or through the config then you need to do this:
    ChatColor.translateAlternativeColorCodes('&', getConfig().getString("String");
    This will make it when the message is modified in the config, color is controlled with &[ColorCode] Example: String: '&6This is Gold'

    Also:
    if you want a player name in a config file
    ChatColor.translateAlternativeColorCodes('&', getConfig().getString("String").replace("%player%", sender.getName());
    Now use:
    String: '&9%player% did something!'
    In Config
     
  4. Offline

    Nutowen

    I have another problem. I want it so it does it to a spcific player
    /nopmsg <name> hi

    then it sends that person hi

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

    if(command.getName().equalsIgnoreCase("nopmsg")){
    if(args.length == 1){
    Player target = Bukkit.getServer().getPlayer(args[0]);
    }
    if(args.length == 2){
    target.sendMessage(args[1]); target is error because target can't be resolved
    }
    }
    return false;
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 25, 2016
  5. Offline

    SethxGaming

    THIS WILL SEND THE TARGET A MESSAGE WITH THEIR NAME!

    Player target = Bukkit.getServer().getPlayer(args[0]);
    if(command.getName().equalsIgnoreCase("nopmsg")){
    if(args.length == 0){
    target.sendMessage(target.getName());
    }
    }
    return false;
    }

    THIS WILL SEND THE TARGET A MESSAGE ARGS 1!

    Player target = Bukkit.getServer().getPlayer(args[0]);
    if(command.getName().equalsIgnoreCase("nopmsg")){

    if(args.length < 1){
    sender.sendMessage("not enough args")
    return true;
    }

    if(target == null){
    sender.sendMessage("Target not online!");
    return true;
    }else{
    String message = "";
    for(int i = 1; i != args.length; i++)
    message += args + " ";
    message = message.trim();

    sender.sendMessage(sender.getName() + " > " + target.getName() + ": " + message);
    target.sendMessage(target.getName() + " > " + sender.getName() + ": " + message);


    }
    }
    return false;
    }

    Sorry for Mistakes, wrote off of the top of my head.

     
  6. Offline

    Nutowen

    We
    When I do /nopmsg thegaminglord124 hi
    This happens in chat repeated twice:

    thegaminglord124 > thegaminglord124: [
    LJava.lang.String;@fbf47ed
     
  7. Offline

    SethxGaming

    Thats because your targeting yourself
     
  8. Offline

    Nutowen

    thegaminglord124 > DessieYT: LJava.lang.String;@b28b810

    When I click on a chest from SimpleCrates with the command on:

    thegaminglord124 > CONSOLE:[LJava.lang.String;@4fe2af1d
     
  9. Offline

    Zombie_Striker

    Don't use the paragraph symbol.

    This was caused because target was not able to be found. You cannot reference objects that were not created.
    There is no need to reverse the sender and target names. That just makes this even more confusing.
    That is not his issue.
    This is most likely because you have either imported a another String class,
    or because there is an issue with the built string. Remove any "String" imports and add "toString()" at the end of "message" to fix this issue.
     
  10. Offline

    Nutowen

    import java.util.List;

    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.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
  11. Offline

    Zombie_Striker

    @Nutowen
    Okay. Have you added the "toString()"?
     
  12. Offline

    Nutowen

    sender.sendMessage(sender.getName() + " > " + target.getName() + ": " + message.toString());
    target.sendMessage(target.getName() + " > " + sender.getName() + ": " + message.toString());
     
Thread Status:
Not open for further replies.

Share This Page