Solved I need help "using hex code in plugin"

Discussion in 'Spigot Plugin Development' started by Baran123456, Jul 5, 2021.

  1. Code (Text):

    package me.loleman.chatbot.Utils;
    import me.clip.placeholderapi.PlaceholderAPI;
    import me.loleman.chatbot.ChatBot;
    import me.loleman.chatbot.Utils.Vars;
    import me.loleman.chatbot.files.ChatFile;
    import me.loleman.chatbot.files.ConfigFile;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
        public class Utils {
            public static String replaceVars(String text, Player player) {
                String message = Vars.PREFIX_CHAT + " " + text;
                if (player != null) {
                    message = message.replace("%player%", player.getName());
                }
                if (text == null || text.equalsIgnoreCase("null")) {
                    message = Vars.PREFIX_CHAT + " " + Vars.RESPONSE_DEFAULT;
                }
                if (ChatBot.isPAPI()) {
                    message = PlaceholderAPI.setPlaceholders((Player)player, (String)message);
                }
                return ChatColor.translateAlternateColorCodes((char)'&', (String)message);
               
            }
       
            public static void reload() {
                ChatFile.reload();
                ConfigFile.reload();
                Vars.reload();
            }
        }



    How can I convert this to hex code?
     
  2. use this code
    Code (Text):
    public final Pattern HEX_PATTERN = Pattern.compile("&(#[A-Fa-f0-9]{6})");
        public final char COLOR_CHAR = ChatColor.COLOR_CHAR;

        public String translateHexColorCodes(String message) {
            Matcher matcher = HEX_PATTERN.matcher(message);
            StringBuffer buffer = new StringBuffer(message.length() + 4 * 8);
            while (matcher.find()) {
                String group = matcher.group(1);
                matcher.appendReplacement(buffer, COLOR_CHAR + "x"
                        + COLOR_CHAR + group.charAt(0) + COLOR_CHAR + group.charAt(1)
                        + COLOR_CHAR + group.charAt(2) + COLOR_CHAR + group.charAt(3)
                        + COLOR_CHAR + group.charAt(4) + COLOR_CHAR + group.charAt(5)
                );
            }
            return matcher.appendTail(buffer).toString();
        }
     
  3. You want to convert your text to HEX code or convert stuff inside the text to HEX colors?
     
  4. inside the text to HEX colors
     
  5. Is it enough to add directly?
     
  6. Yes, is enough. Be sure you add it in AsyncPlayerChatEvent.
     
  7. Code (Text):

    package me.srpandastick.chatbot.Utils;
    import me.clip.placeholderapi.PlaceholderAPI;
    import me.srpandastick.chatbot.ChatBot;
    import me.srpandastick.chatbot.Utils.Vars;
    import me.srpandastick.chatbot.files.ChatFile;
    import me.srpandastick.chatbot.files.ConfigFile;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import java.util.regex.Pattern;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    public final Pattern HEX_PATTERN = Pattern.compile("&(#[A-Fa-f0-9]{6})");
    public final char COLOR_CHAR = ChatColor.COLOR_CHAR;
    public String translateHexColorCodes(String message) {
        Matcher matcher = HEX_PATTERN.matcher(message);
        StringBuffer buffer = new StringBuffer(message.length() + 4 * 8);
        while (matcher.find()) {
            String group = matcher.group(1);
            matcher.appendReplacement(buffer, COLOR_CHAR + "x"
                    + COLOR_CHAR + group.charAt(0) + COLOR_CHAR + group.charAt(1)
                    + COLOR_CHAR + group.charAt(2) + COLOR_CHAR + group.charAt(3)
                    + COLOR_CHAR + group.charAt(4) + COLOR_CHAR + group.charAt(5)
            );
        }
        return matcher.appendTail(buffer).toString();
    }
    public class Utils {
        public static String replaceVars(String text, Player player) {
            String message = Vars.PREFIX_CHAT + " " + text;
            if (player != null) {
                message = message.replace("%player%", player.getName());
            }
            if (text == null || text.equalsIgnoreCase("null")) {
                message = Vars.PREFIX_CHAT + " " + Vars.RESPONSE_DEFAULT;
            }
            if (ChatBot.isPAPI()) {
                message = PlaceholderAPI.setPlaceholders((Player)player, (String)message);
            }
            return ChatColor.translateAlternateColorCodes((char)'&', (String)message);
        }
        public static void reload() {
            ChatFile.reload();
            ConfigFile.reload();
            Vars.reload();
        }
    }


    i got an error

    "Could not load 'plugins\ChatBot3.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: main class `me.srpandastick.chatbot.ChatBot' does not extend JavaPlugin.......
     
  8. Code (Text):

    full error
     Legacy plugin ChatBot v1.1.2-SNAPSHOT does not specify an api-version.
    [10:44:10] [Server thread/ERROR]: Could not load 'plugins\ChatBot3.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: main class `me.srpandastick.chatbot.ChatBot' does not extend JavaPlugin
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:145) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:394) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:301) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at org.bukkit.craftbukkit.v1_17_R1.CraftServer.loadPlugins(CraftServer.java:384) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at net.minecraft.server.dedicated.DedicatedServer.init(DedicatedServer.java:232) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:987) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:307) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            at java.lang.Thread.run(Thread.java:831) [?:?]
    Caused by: java.lang.ClassCastException: class me.srpandastick.chatbot.ChatBot
            at java.lang.Class.asSubclass(Class.java:3853) ~[?:?]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:74) ~[spigot-1.17.jar:3168-Spigot-a483d2c-bc00005]
            ... 8 more
     
  9. Here's a function I wrote long ago and still use:
    Code (Java):

    import net.md_5.bungee.api.ChatColor;
    import java.util.regex.Pattern;
    ...
    public static String chatColors(String str) {
        Pattern unicode = Pattern.compile("\\\\u\\+[a-fA-F0-9]{4}");
        Matcher match = unicode.matcher(str);
        while (match.find()) {
            String code = str.substring(match.start(),match.end());
            str = str.replace(code,Character.toString((char) Integer.parseInt(code.replace("\\u+",""),16)));
            match = unicode.matcher(str);
        }
        Pattern pattern = Pattern.compile("&#[a-fA-F0-9]{6}");
        match = pattern.matcher(str);
        while (match.find()) {
            String color = str.substring(match.start(),match.end());
            str = str.replace(color,ChatColor.of(color.replace("&","")) + "");
            match = pattern.matcher(str);
        }
        return ChatColor.translateAlternateColorCodes('&',str);
    }
    This function does multiple things:
    1) Replaces the pattern "\u+???" with the appropriate Unicode value (for example: "\u+235F" will become "⍟").
    2) Replaces the pattern "&#??????" with the appropriate Hex color (for example: "&#b300b3" will become this color).
    3) Replaces all regular ChatColor stuff with the translation character being "&" (you can change the function if you want to receive a different one, up to you).
    Hope this helps!