1.16.5 § Character gets messed up when generating a new config

Discussion in 'Spigot Plugin Development' started by stelioshah, Oct 16, 2021.

  1. Hey, I am storing some colored names/lores in a config in my plugin. When regenerating the config the § character gets messed up (it gets replaced with some random letters) and the color code stops working. How do I avoid this? (I can manually replace the random letters back to §).
     
  2. Why not just store them using a '&' and then translate the color codes?
     
    • Friendly Friendly x 2
  3. Is translating going to be simple? I am storing ItemStacks in the config which I generate when some event happens, how am I supposed to translate the color codes for the ItemStacks. Would I need to rename them to their respective translated name any time I load them?
     
  4. This
    Code (Text):
    ChatColor.translateAlternateColorCodes('&', "&b&lCool Message here with fancy Colours");
     
    • Agree Agree x 1
  5. Code (Text):
    ItemStack anItemStack = getConfig().getItemStack("path");
         
    anItemStack.getItemMeta().setDisplayName(ChatColor.translateAlternateColorCodes('&', anItemStack.getItemMeta().getDisplayName()));
    That's the only way I see this working, disregarding the lore checking. Correct me if I am wrong.
     
  6. That is basically how to use it, yea. It just takes any string you have and returns a new string that replaces the given char with section signs(§)
     
    • Friendly Friendly x 1
  7. If you wana color code the lore of the item, I recomend you look into Streams (Specifically Mapping)
     
    • Informative Informative x 1
  8. Just create a method like this:
    Code (Java):

    private String formatColors(String arg) {
        return ChatColor.translateAlternateColorCodes('&', arg);
    }

    private List<String> formatColors(List<String> arg) {
        ArrayList<String> preList = new ArrayList<>();
        arg.stream().forEach(s -> preList.add(formatColors(s)));
        return preList;
    }
     
    P.S. Written without IDE, may have made a mistake in creating a stream.
    P.P.S. Use formatColors(Arrays.asList(.....)) for your lore.
     
    • Like Like x 1
  9. Why do that when you could...

    Code (Java):

    private String formatColors(String arg) {
        return ChatColor.translateAlternateColorCodes('&', arg);
    }

    private List<String> formatColors(List<String> arg) {
        return arg.stream().map(string -> formatColors(string)).collect(Collectors.toList());
    }
     
    :p
     
    • Like Like x 1
  10. like I said look insto streams, its alot cleaner.

    can be shortened to
    Code (Java):
    arg.stream().map(this::formatColors).collect(Collectors.toList());
     
    • Like Like x 2
  11. You two are right, but the point is the same: use ChatColor and a method :D
     
    • Agree Agree x 2
  12. true, I was just pointing out a cleaner way of doing stuff
     
  13. You only have to set encoding to UTF-8. Using ChatColor's method isn't a bad idea but you can avoid it and it's easier to just change the encoding.
     
  14. no its not, using the § character is super bad practice, not to mention its harder on users since that character isnt even on the keyboard.
     
    • Like Like x 1