Preventing use of magic but allowing use of colour codes.

Discussion in 'Spigot Plugin Development' started by NaJated, May 10, 2015.

Thread Status:
Not open for further replies.
  1. hey all, how would I stop someone from doing MAGIC e.g. &k in an arg. (Doing a nickname plugin) but allow reg colour codes e.g. &a (green)
     
  2. ChatColor.translateAltColorCodes( '&', "&kMessage".replace( "&k", "" ) )
     
  3. Doesn't work.

    Tried
    player.sendMessage("New nickname: " + ChatColor.translateAlternateColorCodes('&', args[0]).replace("&k", ""));
    and
    player.sendMessage("New nickname: " + ChatColor.translateAlternateColorCodes('&', args[0]).replace("&k", " "));
     
  4. Look at the arrangment and where .replace is. It would be args[0].replace( ... )
     
    • Like Like x 1
  5. It needs to be
    Code (Text):
    player.sendMessage("New nickname: " + ChatColor.translateAlternateColorCodes('&k', args[0].replace("&k", "")));
    Note the replace method is inside the translateAlternateColorCodes().

    Edit: Godammit, ninj'd by @_Cory_
     
    • Like Like x 1
  6. <3
     
  7. If you put &&kk in the nickname, your solution will not block the use of &k.
     
    • Like Like x 1
    • Creative Creative x 1
  8. Use this:
    Code (Text):

    while (text.contains("&k")) {
                text = text.replace("&k", "");
            }
     
  9. That is the same as
    Code (Java):
    text = text.replace("&k", "");
    String#replace will already replace all occurrences in the String.
     
  10. samczsun

    Supporter

    Not unless stripping an occurrence creates a new occurrence
     
    • Agree Agree x 2
  11. You could try this:

    Code (Text):
    ChatColor.translateAlternateColorCodes('&', input).replace(ChatColor.MAGIC, "");
     
  12. samczsun

    Supporter

    One way is to simply not parse &k, with regex like &[a-f0-9] for color codes only
     
  13. Haven't learnt Regex.
     
  14. samczsun

    Supporter

    Well... I've just provided the one you need.
     
  15. If you put &&kk just a String#replace dont will solve the problem
     
  16. Don't know the Object or whatever Regex is.
     
  17. Look into the Pattern and Matcher classes if you want to handle it efficiently. If you don't require efficiency and want simple code, look into String#replaceAll.
     
Thread Status:
Not open for further replies.