| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.player;
|
| 7 |
|
| 8 | import com.mojang.brigadier.StringReader;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
|
| 10 | import meteordevelopment.meteorclient.MeteorClient;
|
| 11 | import meteordevelopment.meteorclient.mixininterface.IChatHud;
|
| 12 | import meteordevelopment.meteorclient.pathing.BaritoneUtils;
|
| 13 | import meteordevelopment.meteorclient.systems.config.Config;
|
| 14 | import meteordevelopment.meteorclient.utils.PostInit;
|
| 15 | import meteordevelopment.meteorclient.utils.misc.text.MeteorClickEvent;
|
| 16 | import net.minecraft.ChatFormatting;
|
| 17 | import net.minecraft.network.chat.*;
|
| 18 | import net.minecraft.world.phys.Vec3;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | import java.util.function.Supplier;
|
| 22 |
|
| 23 | import static meteordevelopment.meteorclient.MeteorClient.mc;
|
| 24 |
|
| 25 | public class ChatUtils {
|
| 26 | private static final Object2ObjectLinkedOpenHashMap<String, Supplier<Component>> customPrefixes = new Object2ObjectLinkedOpenHashMap<>();
|
| 27 | private static String forcedPrefixClassName;
|
| 28 |
|
| 29 | private static Component PREFIX;
|
| 30 |
|
| 31 | private ChatUtils() {
|
| 32 | }
|
| 33 |
|
| 34 | @PostInit
|
| 35 | public static void init() {
|
| 36 | PREFIX = Component.empty()
|
| 37 | .setStyle(Style.EMPTY.applyFormats(ChatFormatting.GRAY))
|
| 38 | .append("[")
|
| 39 | .append(Component.literal("Meteor").setStyle(Style.EMPTY.withColor(TextColor.fromRgb(MeteorClient.ADDON.color.getPacked()))))
|
| 40 | .append("] ");
|
| 41 | }
|
| 42 |
|
| 43 | public static Component getMeteorPrefix() {
|
| 44 | return PREFIX;
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * Registers a custom prefix to be used when calling from a class in the specified package. When null is returned from the supplier the default Meteor prefix is used.
|
| 49 | */
|
| 50 | @SuppressWarnings("unused")
|
| 51 | public static void registerCustomPrefix(String packageName, Supplier<Component> supplier) {
|
| 52 | customPrefixes.put(packageName, supplier);
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * The package name must match exactly to the one provided through {@link #registerCustomPrefix(String, Supplier)}.
|
| 57 | */
|
| 58 | @SuppressWarnings("unused")
|
| 59 | public static void unregisterCustomPrefix(String packageName) {
|
| 60 | customPrefixes.remove(packageName);
|
| 61 | }
|
| 62 |
|
| 63 | public static void forceNextPrefixClass(Class<?> klass) {
|
| 64 | forcedPrefixClassName = klass.getName();
|
| 65 | }
|
| 66 |
|
| 67 | // Player
|
| 68 |
|
| 69 | /**
|
| 70 | * Sends the message as if the user typed it into chat and adds it to the chat history.
|
| 71 | */
|
| 72 | public static void sendPlayerMsg(String message) {
|
| 73 | sendPlayerMsg(message, true);
|
| 74 | }
|
| 75 |
|
| 76 | /**
|
| 77 | * Sends the message as if the user typed it into chat.
|
| 78 | */
|
| 79 | public static void sendPlayerMsg(String message, boolean addToHistory) {
|
| 80 | if (addToHistory) mc.gui.hud.getChat().addRecentChat(message);
|
| 81 |
|
| 82 | if (message.startsWith("/")) mc.player.connection.sendCommand(message.substring(1));
|
| 83 | else mc.player.connection.sendChat(message);
|
| 84 | }
|
| 85 |
|
| 86 | // Default
|
| 87 |
|
| 88 | public static void info(String message, Object... args) {
|
| 89 | sendMsg(ChatFormatting.GRAY, message, args);
|
| 90 | }
|
| 91 |
|
| 92 | public static void infoPrefix(String prefix, String message, Object... args) {
|
| 93 | sendMsg(0, prefix, ChatFormatting.LIGHT_PURPLE, ChatFormatting.GRAY, message, args);
|
| 94 | }
|
| 95 |
|
| 96 | // Warning
|
| 97 |
|
| 98 | public static void warning(String message, Object... args) {
|
| 99 | sendMsg(ChatFormatting.YELLOW, message, args);
|
| 100 | }
|
| 101 |
|
| 102 | public static void warningPrefix(String prefix, String message, Object... args) {
|
| 103 | sendMsg(0, prefix, ChatFormatting.LIGHT_PURPLE, ChatFormatting.YELLOW, message, args);
|
| 104 | }
|
| 105 |
|
| 106 | // Error
|
| 107 |
|
| 108 | public static void error(String message, Object... args) {
|
| 109 | sendMsg(ChatFormatting.RED, message, args);
|
| 110 | }
|
| 111 |
|
| 112 | public static void errorPrefix(String prefix, String message, Object... args) {
|
| 113 | sendMsg(0, prefix, ChatFormatting.LIGHT_PURPLE, ChatFormatting.RED, message, args);
|
| 114 | }
|
| 115 |
|
| 116 | // Misc
|
| 117 |
|
| 118 | public static void sendMsg(Component message) {
|
| 119 | sendMsg(null, message);
|
| 120 | }
|
| 121 |
|
| 122 | public static void sendMsg(String prefix, Component message) {
|
| 123 | sendMsg(0, prefix, ChatFormatting.LIGHT_PURPLE, message);
|
| 124 | }
|
| 125 |
|
| 126 | public static void sendMsg(ChatFormatting color, String message, Object... args) {
|
| 127 | sendMsg(0, null, null, color, message, args);
|
| 128 | }
|
| 129 |
|
| 130 | public static void sendMsg(int id, ChatFormatting color, String message, Object... args) {
|
| 131 | sendMsg(id, null, null, color, message, args);
|
| 132 | }
|
| 133 |
|
| 134 | public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable ChatFormatting prefixColor, ChatFormatting messageColor, String messageContent, Object... args) {
|
| 135 | MutableComponent message = formatMsg(String.format(messageContent, args), messageColor);
|
| 136 | sendMsg(id, prefixTitle, prefixColor, message);
|
| 137 | }
|
| 138 |
|
| 139 | public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable ChatFormatting prefixColor, String messageContent, ChatFormatting messageColor) {
|
| 140 | MutableComponent message = formatMsg(messageContent, messageColor);
|
| 141 | sendMsg(id, prefixTitle, prefixColor, message);
|
| 142 | }
|
| 143 |
|
| 144 | public static void sendMsg(int id, @Nullable String prefixTitle, @Nullable ChatFormatting prefixColor, Component msg) {
|
| 145 | if (mc.level == null) return;
|
| 146 |
|
| 147 | MutableComponent message = Component.empty();
|
| 148 | message.append(getPrefix());
|
| 149 | if (prefixTitle != null) message.append(getCustomPrefix(prefixTitle, prefixColor));
|
| 150 | message.append(msg);
|
| 151 |
|
| 152 | if (!Config.get().deleteChatFeedback.get()) id = 0;
|
| 153 |
|
| 154 | final int finalId = id; // Intellij copes about using non-final args in lambdas
|
| 155 | mc.execute(() -> ((IChatHud) mc.gui.hud.getChat()).meteor$add(message, finalId));
|
| 156 | }
|
| 157 |
|
| 158 | private static MutableComponent getCustomPrefix(String prefixTitle, ChatFormatting prefixColor) {
|
| 159 | MutableComponent prefix = Component.empty();
|
| 160 | prefix.setStyle(prefix.getStyle().applyFormats(ChatFormatting.GRAY));
|
| 161 |
|
| 162 | prefix.append("[");
|
| 163 |
|
| 164 | MutableComponent moduleTitle = Component.literal(prefixTitle);
|
| 165 | moduleTitle.setStyle(moduleTitle.getStyle().applyFormats(prefixColor));
|
| 166 | prefix.append(moduleTitle);
|
| 167 |
|
| 168 | prefix.append("] ");
|
| 169 |
|
| 170 | return prefix;
|
| 171 | }
|
| 172 |
|
| 173 | private static Component getPrefix() {
|
| 174 | if (customPrefixes.isEmpty()) {
|
| 175 | forcedPrefixClassName = null;
|
| 176 | return PREFIX;
|
| 177 | }
|
| 178 |
|
| 179 | boolean foundChatUtils = false;
|
| 180 | String className = null;
|
| 181 |
|
| 182 | if (forcedPrefixClassName != null) {
|
| 183 | className = forcedPrefixClassName;
|
| 184 | forcedPrefixClassName = null;
|
| 185 | } else {
|
| 186 | for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
|
| 187 | if (foundChatUtils) {
|
| 188 | if (!element.getClassName().equals(ChatUtils.class.getName())) {
|
| 189 | className = element.getClassName();
|
| 190 | break;
|
| 191 | }
|
| 192 | } else {
|
| 193 | if (element.getClassName().equals(ChatUtils.class.getName())) foundChatUtils = true;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | if (className == null) return PREFIX;
|
| 199 |
|
| 200 | for (var entry : customPrefixes.object2ObjectEntrySet()) {
|
| 201 | if (className.startsWith(entry.getKey())) {
|
| 202 | Component prefix = entry.getValue().get();
|
| 203 | return prefix != null ? prefix : PREFIX;
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | return PREFIX;
|
| 208 | }
|
| 209 |
|
| 210 | private static MutableComponent formatMsg(String message, ChatFormatting defaultColor) {
|
| 211 | StringReader reader = new StringReader(message);
|
| 212 | MutableComponent text = Component.empty();
|
| 213 | Style style = Style.EMPTY.applyFormats(defaultColor);
|
| 214 | StringBuilder result = new StringBuilder();
|
| 215 | boolean formatting = false;
|
| 216 | while (reader.canRead()) {
|
| 217 | char c = reader.read();
|
| 218 | if (c == '(') {
|
| 219 | text.append(Component.literal(result.toString()).setStyle(style));
|
| 220 | result.setLength(0);
|
| 221 | result.append(c);
|
| 222 | formatting = true;
|
| 223 | } else {
|
| 224 | result.append(c);
|
| 225 |
|
| 226 | if (formatting && c == ')') {
|
| 227 | switch (result.toString()) {
|
| 228 | case "(default)" -> {
|
| 229 | style = style.applyFormats(defaultColor);
|
| 230 | result.setLength(0);
|
| 231 | }
|
| 232 | case "(highlight)" -> {
|
| 233 | style = style.applyFormats(ChatFormatting.WHITE);
|
| 234 | result.setLength(0);
|
| 235 | }
|
| 236 | case "(underline)" -> {
|
| 237 | style = style.applyFormats(ChatFormatting.UNDERLINE);
|
| 238 | result.setLength(0);
|
| 239 | }
|
| 240 | case "(bold)" -> {
|
| 241 | style = style.applyFormats(ChatFormatting.BOLD);
|
| 242 | result.setLength(0);
|
| 243 | }
|
| 244 | }
|
| 245 | formatting = false;
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | if (!result.isEmpty()) text.append(Component.literal(result.toString()).setStyle(style));
|
| 251 |
|
| 252 | return text;
|
| 253 | }
|
| 254 |
|
| 255 | public static MutableComponent formatCoords(Vec3 pos) {
|
| 256 | String coordsString = String.format("(highlight)(underline)%.0f, %.0f, %.0f(default)", pos.x, pos.y, pos.z);
|
| 257 | MutableComponent coordsText = formatMsg(coordsString, ChatFormatting.GRAY);
|
| 258 |
|
| 259 | if (BaritoneUtils.IS_AVAILABLE) {
|
| 260 | Style style = coordsText.getStyle().applyFormats(ChatFormatting.BOLD)
|
| 261 | .withHoverEvent(new HoverEvent.ShowText(
|
| 262 | Component.literal("Set as Baritone goal")
|
| 263 | ))
|
| 264 | .withClickEvent(new MeteorClickEvent(
|
| 265 | String.format("%sgoto %d %d %d", BaritoneUtils.getPrefix(), (int) pos.x, (int) pos.y, (int) pos.z)
|
| 266 | ));
|
| 267 |
|
| 268 | coordsText.setStyle(style);
|
| 269 | }
|
| 270 |
|
| 271 | return coordsText;
|
| 272 | }
|
| 273 | }
|
| 274 |
|