Replacing § with & problem (config)

Discussion in 'Plugin Development' started by Monkey_Swag, Jul 1, 2014.

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

    Monkey_Swag

    Hey... so I get this error that says that there is a problem with the string "prefix' in my config.yml. My best guess is that it's the amperisgn(&). I'm really bad at configs so yeah :/
    stacktrace:
    [​IMG]

    Code:
    Code:java
    1. public void onEnable(){
    2. getConfig().options().copyDefaults(true);
    3. saveConfig();
    4. }
    5. @Override
    6. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    7. if(!(sender instanceof Player)){
    8. sender.sendMessage("§4PLAYERS ONLY!");
    9. return true;
    10. }
    11. Player p = (Player) sender;
    12. if(cmd.getName().equalsIgnoreCase("broadcast") || cmd.getName().equalsIgnoreCase("bc")){
    13. if(!p.hasPermission("bc.use")){
    14. p.sendMessage("§cYou cannot use this command!");
    15. return true;
    16. }
    17. String msg = "";
    18. for (int i = 0; i < args.length; i++) {
    19. msg += args[i] + " ";
    20. }
    21. if(args.length > 0){
    22. Bukkit.broadcastMessage(getConfig().getString("prefix").replace("§", "&") + " " + msg.replace("&", "§"));
    23. return true;
    24. } else {
    25. p.sendMessage("§cUsage: /broadcast <message>");
    26. return true;
    27. }
    28. }
    29. return false;
    30. }
    31.  
    32. }
    33. [/i]
     
  2. Offline

    afistofirony

    Looks like the YAML parser can't parse the ampersand and left bracket correctly because it thinks they're configuration tokens. Try putting apostrophes around the text:

    Code:
    prefix: '&1[EncorePvP]'
     
  3. Offline

    ko47374737

    Try replaceAll();
     
  4. Offline

    afistofirony

    ko47374737 replaceAll() doesn't really do anything different from replace(). It just takes a regular expression. :p
     
  5. Offline

    ko47374737

    Code:java
    1. String n = getConfig().getString("prefix").replace("§", "&") + " " + msg.replace("&", "§")
    2. bukkit.broadcastMessage(n);
     
  6. ko47374737 That wasn't really the problem in the first place, replaceAll() wouldn't fix anything, you have the first replace statement the wrong way round, and replace() is a bad way to translate chatcolors - ChatColor has a method for this.
     
  7. Offline

    Traks

    afistofirony That is an incredibly big difference, regex adds so much more possibilities.
    AdamQpzm replaceAll("&(?=[a-fklmnor0-9])", "§")

    But yes, for code maintainability and readability, you should just use the translate method in the ChatColor enum.
     
Thread Status:
Not open for further replies.

Share This Page