| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.ListIterator;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.function.Predicate;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.ChatFormatting;
|
| 15 | import net.minecraft.Optionull;
|
| 16 | import net.minecraft.client.CommandHistory;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 19 | import net.minecraft.client.gui.Font;
|
| 20 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 21 | import net.minecraft.client.gui.TextAlignment;
|
| 22 | import net.minecraft.client.gui.screens.ChatScreen;
|
| 23 | import net.minecraft.client.gui.screens.Screen;
|
| 24 | import net.minecraft.client.multiplayer.chat.GuiMessage;
|
| 25 | import net.minecraft.client.multiplayer.chat.GuiMessageSource;
|
| 26 | import net.minecraft.client.multiplayer.chat.GuiMessageTag;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.network.chat.ComponentUtils;
|
| 29 | import net.minecraft.network.chat.MessageSignature;
|
| 30 | import net.minecraft.network.chat.Style;
|
| 31 | import net.minecraft.network.chat.ClickEvent.Custom;
|
| 32 | import net.minecraft.network.chat.HoverEvent.ShowText;
|
| 33 | import net.minecraft.resources.Identifier;
|
| 34 | import net.minecraft.util.ARGB;
|
| 35 | import net.minecraft.util.ArrayListDeque;
|
| 36 | import net.minecraft.util.FormattedCharSequence;
|
| 37 | import net.minecraft.util.Mth;
|
| 38 | import net.minecraft.util.profiling.Profiler;
|
| 39 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 40 | import org.joml.Matrix3x2f;
|
| 41 | import org.joml.Vector2f;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 | import org.slf4j.Logger;
|
| 44 |
|
| 45 | @Environment(EnvType.CLIENT)
|
| 46 | public class ChatComponent {
|
| 47 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 48 | private static final int MAX_CHAT_HISTORY = 100;
|
| 49 | private static final int MESSAGE_INDENT = 4;
|
| 50 | private static final int BOTTOM_MARGIN = 40;
|
| 51 | private static final int TOOLTIP_MAX_WIDTH = 210;
|
| 52 | private static final int TIME_BEFORE_MESSAGE_DELETION = 60;
|
| 53 | private static final Component DELETED_CHAT_MESSAGE = Component.translatable("chat.deleted_marker").withStyle(ChatFormatting.GRAY, ChatFormatting.ITALIC);
|
| 54 | public static final int MESSAGE_BOTTOM_TO_MESSAGE_TOP = 8;
|
| 55 | public static final Identifier QUEUE_EXPAND_ID = Identifier.withDefaultNamespace("internal/expand_chat_queue");
|
| 56 | private static final Style QUEUE_EXPAND_TEXT_STYLE = Style.EMPTY
|
| 57 | .withClickEvent(new Custom(QUEUE_EXPAND_ID, Optional.empty()))
|
| 58 | .withHoverEvent(new ShowText(Component.translatable("chat.queue.tooltip")));
|
| 59 | public static final Identifier GO_TO_RESTRICTIONS_SCREEN = Identifier.withDefaultNamespace("internal/go_to_restrictions_screen");
|
| 60 | private static final Component RESTRICTED_CHAT_MESSAGE = Component.translatable("chat_screen.restricted")
|
| 61 | .withStyle(Style.EMPTY.withColor(ChatFormatting.RED).withUnderlined(true).withClickEvent(new Custom(GO_TO_RESTRICTIONS_SCREEN, Optional.empty())));
|
| 62 | private static final Component RESTRICTED_CHAT_MESSAGE_WITH_HOVER = ComponentUtils.mergeStyles(
|
| 63 | RESTRICTED_CHAT_MESSAGE, Style.EMPTY.withHoverEvent(new ShowText(Component.translatable("chat_screen.restricted")))
|
| 64 | );
|
| 65 | private final Minecraft minecraft;
|
| 66 | private final CommandHistory commandHistory;
|
| 67 | private final ArrayListDeque<String> recentChat = new ArrayListDeque<>(100);
|
| 68 | private final List<GuiMessage> allMessages = Lists.newArrayList();
|
| 69 | private final List<GuiMessage.Line> trimmedMessages = Lists.newArrayList();
|
| 70 | private int chatScrollbarPos;
|
| 71 | private boolean newMessageSinceScroll;
|
| 72 | private ChatComponent.@Nullable Draft latestDraft;
|
| 73 | private @Nullable ChatScreen preservedScreen;
|
| 74 | private final List<ChatComponent.DelayedMessageDeletion> messageDeletionQueue = new ArrayList<>();
|
| 75 | private Predicate<GuiMessage> visibleMessageFilter = var0 -> true;
|
| 76 |
|
| 77 | public ChatComponent(final Minecraft minecraft) {
|
| 78 | this.minecraft = minecraft;
|
| 79 | this.commandHistory = new CommandHistory(minecraft.gameDirectory.toPath());
|
| 80 | this.recentChat.addAll(this.commandHistory.history());
|
| 81 | }
|
| 82 |
|
| 83 | public void tick() {
|
| 84 | if (!this.messageDeletionQueue.isEmpty()) {
|
| 85 | this.processMessageDeletionQueue();
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | public void setVisibleMessageFilter(final Predicate<GuiMessage> visibleMessageFilter) {
|
| 90 | this.visibleMessageFilter = visibleMessageFilter;
|
| 91 | this.refreshTrimmedMessages();
|
| 92 | }
|
| 93 |
|
| 94 | private int forEachLine(final ChatComponent.AlphaCalculator alphaCalculator, final ChatComponent.LineConsumer lineConsumer) {
|
| 95 | int perPage = this.getLinesPerPage();
|
| 96 | int count = 0;
|
| 97 |
|
| 98 | for (int i = Math.min(this.trimmedMessages.size() - this.chatScrollbarPos, perPage) - 1; i >= 0; i--) {
|
| 99 | int messageIndex = i + this.chatScrollbarPos;
|
| 100 | GuiMessage.Line message = this.trimmedMessages.get(messageIndex);
|
| 101 | float alpha = alphaCalculator.calculate(message);
|
| 102 | if (alpha > 1.0E-5F) {
|
| 103 | count++;
|
| 104 | lineConsumer.accept(message, i, alpha);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | return count;
|
| 109 | }
|
| 110 |
|
| 111 | public void extractRenderState(
|
| 112 | final GuiGraphicsExtractor graphics,
|
| 113 | final Font font,
|
| 114 | final int ticks,
|
| 115 | final int mouseX,
|
| 116 | final int mouseY,
|
| 117 | final ChatComponent.DisplayMode displayMode,
|
| 118 | final boolean changeCursorOnInsertions
|
| 119 | ) {
|
| 120 | graphics.pose().pushMatrix();
|
| 121 | this.extractRenderState(
|
| 122 | displayMode.foreground
|
| 123 | ? new ChatComponent.DrawingFocusedGraphicsAccess(graphics, font, mouseX, mouseY, changeCursorOnInsertions)
|
| 124 | : new ChatComponent.DrawingBackgroundGraphicsAccess(graphics),
|
| 125 | graphics.guiHeight(),
|
| 126 | ticks,
|
| 127 | displayMode
|
| 128 | );
|
| 129 | graphics.pose().popMatrix();
|
| 130 | }
|
| 131 |
|
| 132 | public void captureClickableText(
|
| 133 | final ActiveTextCollector activeTextCollector, final int screenHeight, final int ticks, final ChatComponent.DisplayMode displayMode
|
| 134 | ) {
|
| 135 | this.extractRenderState(new ChatComponent.ClickableTextOnlyGraphicsAccess(activeTextCollector), screenHeight, ticks, displayMode);
|
| 136 | }
|
| 137 |
|
| 138 | private void extractRenderState(
|
| 139 | final ChatComponent.ChatGraphicsAccess graphics, final int screenHeight, final int ticks, final ChatComponent.DisplayMode displayMode
|
| 140 | ) {
|
| 141 | boolean isForeground = displayMode.foreground;
|
| 142 | boolean isRestricted = displayMode.showRestrictedPrompt;
|
| 143 | int total = this.trimmedMessages.size();
|
| 144 | if (total > 0 || isRestricted) {
|
| 145 | ProfilerFiller profiler = Profiler.get();
|
| 146 | profiler.push("chat");
|
| 147 | float scale = (float)this.getScale();
|
| 148 | int maxWidth = Mth.ceil(this.getWidth() / scale);
|
| 149 | final int chatBottom = Mth.floor((screenHeight - 40) / scale);
|
| 150 | final float textOpacity = this.minecraft.options.chatOpacity().get().floatValue() * 0.9F + 0.1F;
|
| 151 | float backgroundOpacity = this.minecraft.options.textBackgroundOpacity().get().floatValue();
|
| 152 | final int messageHeight = 9;
|
| 153 | int messageBottomToMessageTop = 8;
|
| 154 | double chatLineSpacing = this.minecraft.options.chatLineSpacing().get();
|
| 155 | final int entryHeight = (int)(messageHeight * (chatLineSpacing + 1.0));
|
| 156 | final int entryBottomToMessageY = (int)Math.round(8.0 * (chatLineSpacing + 1.0) - 4.0 * chatLineSpacing);
|
| 157 | long queueSize = this.minecraft.gui.chatListener().queueSize();
|
| 158 | ChatComponent.AlphaCalculator alphaCalculator = isForeground ? ChatComponent.AlphaCalculator.FULLY_VISIBLE : ChatComponent.AlphaCalculator.timeBased(ticks);
|
| 159 | graphics.updatePose(pose -> {
|
| 160 | pose.scale(scale, scale);
|
| 161 | pose.translate(4.0F, 0.0F);
|
| 162 | });
|
| 163 | int count = this.forEachLine(alphaCalculator, (var5x, lineIndex, alphax) -> {
|
| 164 | int entryBottom = chatBottom - lineIndex * entryHeight;
|
| 165 | int entryTop = entryBottom - entryHeight;
|
| 166 | graphics.fill(-4, entryTop, maxWidth + 4 + 4, entryBottom, ARGB.black(alphax * backgroundOpacity));
|
| 167 | });
|
| 168 | int lineAboveMessagesY = chatBottom - (count + 1) * entryHeight;
|
| 169 | if (queueSize > 0L) {
|
| 170 | graphics.fill(-2, chatBottom, maxWidth + 4, chatBottom + messageHeight, ARGB.black(backgroundOpacity));
|
| 171 | }
|
| 172 |
|
| 173 | if (isRestricted) {
|
| 174 | graphics.fill(-2, lineAboveMessagesY, maxWidth + 4 + 4, lineAboveMessagesY + entryHeight, ARGB.black(backgroundOpacity));
|
| 175 | }
|
| 176 |
|
| 177 | this.forEachLine(alphaCalculator, new ChatComponent.LineConsumer() {
|
| 178 | private boolean hoveredOverCurrentMessage;
|
| 179 |
|
| 180 | @Override
|
| 181 | public void accept(final GuiMessage.Line line, final int lineIndex, final float alpha) {
|
| 182 | int entryBottom = chatBottom - lineIndex * entryHeight;
|
| 183 | int entryTop = entryBottom - entryHeight;
|
| 184 | int textTop = entryBottom - entryBottomToMessageY;
|
| 185 | boolean hoveredOverCurrentLine = graphics.handleMessage(textTop, alpha * textOpacity, line.content());
|
| 186 | this.hoveredOverCurrentMessage |= hoveredOverCurrentLine;
|
| 187 | boolean forceIconRendering;
|
| 188 | if (line.endOfEntry()) {
|
| 189 | forceIconRendering = this.hoveredOverCurrentMessage;
|
| 190 | this.hoveredOverCurrentMessage = false;
|
| 191 | } else {
|
| 192 | forceIconRendering = false;
|
| 193 | }
|
| 194 |
|
| 195 | GuiMessageTag tag = line.tag();
|
| 196 | if (tag != null) {
|
| 197 | graphics.handleTag(-4, entryTop, -2, entryBottom, alpha * textOpacity, tag);
|
| 198 | if (tag.icon() != null) {
|
| 199 | int iconLeft = line.getTagIconLeft(ChatComponent.this.minecraft.font);
|
| 200 | int textBottom = textTop + messageHeight;
|
| 201 | graphics.handleTagIcon(iconLeft, textBottom, forceIconRendering, tag, tag.icon());
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | });
|
| 206 | if (queueSize > 0L) {
|
| 207 | int queueLineBottom = chatBottom + messageHeight;
|
| 208 | Component queueMessage = Component.translatable("chat.queue", queueSize).setStyle(QUEUE_EXPAND_TEXT_STYLE);
|
| 209 | graphics.handleMessage(queueLineBottom - 8, 0.5F * textOpacity, queueMessage.getVisualOrderText());
|
| 210 | }
|
| 211 |
|
| 212 | if (isRestricted) {
|
| 213 | int restrictedMessageWidth = this.minecraft.font.width(RESTRICTED_CHAT_MESSAGE);
|
| 214 | FormattedCharSequence restrictedMessage = restrictedMessageWidth > maxWidth
|
| 215 | ? ComponentRenderUtils.clipText(RESTRICTED_CHAT_MESSAGE_WITH_HOVER, this.minecraft.font, maxWidth)
|
| 216 | : RESTRICTED_CHAT_MESSAGE.getVisualOrderText();
|
| 217 | graphics.handleMessage(lineAboveMessagesY + entryHeight - entryBottomToMessageY - 1, textOpacity, restrictedMessage);
|
| 218 | }
|
| 219 |
|
| 220 | if (total > 0 && isForeground) {
|
| 221 | int chatHeight = count * entryHeight;
|
| 222 | int virtualHeight = total * entryHeight;
|
| 223 | int y = this.chatScrollbarPos * chatHeight / total - chatBottom;
|
| 224 | int height = chatHeight * chatHeight / virtualHeight;
|
| 225 | if (virtualHeight != chatHeight) {
|
| 226 | int alpha = y > 0 ? 170 : 96;
|
| 227 | int color = this.newMessageSinceScroll ? 13382451 : 3355562;
|
| 228 | int scrollBarStartX = maxWidth + 4;
|
| 229 | graphics.fill(scrollBarStartX, -y, scrollBarStartX + 2, -y - height, ARGB.color(alpha, color));
|
| 230 | graphics.fill(scrollBarStartX + 2, -y, scrollBarStartX + 1, -y - height, ARGB.color(alpha, 13421772));
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | profiler.pop();
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | public void clearMessages(final boolean history) {
|
| 239 | this.minecraft.gui.chatListener().flushQueue();
|
| 240 | this.messageDeletionQueue.clear();
|
| 241 | this.trimmedMessages.clear();
|
| 242 | this.allMessages.clear();
|
| 243 | if (history) {
|
| 244 | this.recentChat.clear();
|
| 245 | this.recentChat.addAll(this.commandHistory.history());
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | public void addClientSystemMessage(final Component message) {
|
| 250 | this.addMessage(message, null, GuiMessageSource.SYSTEM_CLIENT, GuiMessageTag.systemSinglePlayer());
|
| 251 | }
|
| 252 |
|
| 253 | public void addServerSystemMessage(final Component message) {
|
| 254 | this.addMessage(message, null, GuiMessageSource.SYSTEM_SERVER, GuiMessageTag.systemSinglePlayer());
|
| 255 | }
|
| 256 |
|
| 257 | public void addPlayerMessage(final Component message, final @Nullable MessageSignature signature, final @Nullable GuiMessageTag tag) {
|
| 258 | this.addMessage(message, signature, GuiMessageSource.PLAYER, tag);
|
| 259 | }
|
| 260 |
|
| 261 | private void addMessage(final Component contents, final @Nullable MessageSignature signature, final GuiMessageSource source, final @Nullable GuiMessageTag tag) {
|
| 262 | GuiMessage message = new GuiMessage(this.minecraft.gui.hud.getGuiTicks(), contents, signature, source, tag);
|
| 263 | if (this.visibleMessageFilter.test(message)) {
|
| 264 | this.logChatMessage(message);
|
| 265 | this.addMessageToDisplayQueue(message);
|
| 266 | this.addMessageToQueue(message);
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | private void logChatMessage(final GuiMessage message) {
|
| 271 | String messageString = message.content().getString().replaceAll("\r", "\\\\r").replaceAll("\n", "\\\\n");
|
| 272 | String logTag = Optionull.map(message.tag(), GuiMessageTag::logTag);
|
| 273 | if (logTag != null) {
|
| 274 | LOGGER.info("[{}] [CHAT] {}", logTag, messageString);
|
| 275 | } else {
|
| 276 | LOGGER.info("[CHAT] {}", messageString);
|
| 277 | }
|
| 278 | }
|
| 279 |
|
| 280 | private void addMessageToDisplayQueue(final GuiMessage message) {
|
| 281 | int maxWidth = Mth.floor(this.getWidth() / this.getScale());
|
| 282 | List<FormattedCharSequence> lines = message.splitLines(this.minecraft.font, maxWidth);
|
| 283 | boolean chatting = this.isChatFocused();
|
| 284 |
|
| 285 | for (int i = 0; i < lines.size(); i++) {
|
| 286 | FormattedCharSequence line = lines.get(i);
|
| 287 | if (chatting && this.chatScrollbarPos > 0) {
|
| 288 | this.newMessageSinceScroll = true;
|
| 289 | this.scrollChat(1);
|
| 290 | }
|
| 291 |
|
| 292 | boolean endOfEntry = i == lines.size() - 1;
|
| 293 | this.trimmedMessages.addFirst(new GuiMessage.Line(message, line, endOfEntry));
|
| 294 | }
|
| 295 |
|
| 296 | while (this.trimmedMessages.size() > 100) {
|
| 297 | this.trimmedMessages.removeLast();
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | private void addMessageToQueue(final GuiMessage message) {
|
| 302 | this.allMessages.addFirst(message);
|
| 303 |
|
| 304 | while (this.allMessages.size() > 100) {
|
| 305 | this.allMessages.removeLast();
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | private void processMessageDeletionQueue() {
|
| 310 | int time = this.minecraft.gui.hud.getGuiTicks();
|
| 311 | this.messageDeletionQueue.removeIf(entry -> time >= entry.deletableAfter() ? this.deleteMessageOrDelay(entry.signature()) == null : false);
|
| 312 | }
|
| 313 |
|
| 314 | public void deleteMessage(final MessageSignature signature) {
|
| 315 | ChatComponent.DelayedMessageDeletion delayedMessage = this.deleteMessageOrDelay(signature);
|
| 316 | if (delayedMessage != null) {
|
| 317 | this.messageDeletionQueue.add(delayedMessage);
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | private ChatComponent.@Nullable DelayedMessageDeletion deleteMessageOrDelay(final MessageSignature signature) {
|
| 322 | int time = this.minecraft.gui.hud.getGuiTicks();
|
| 323 | ListIterator<GuiMessage> iterator = this.allMessages.listIterator();
|
| 324 |
|
| 325 | while (iterator.hasNext()) {
|
| 326 | GuiMessage message = iterator.next();
|
| 327 | if (signature.equals(message.signature())) {
|
| 328 | int deletableAfter = message.addedTime() + 60;
|
| 329 | if (time >= deletableAfter) {
|
| 330 | iterator.set(createDeletedMarker(message));
|
| 331 | this.refreshTrimmedMessages();
|
| 332 | return null;
|
| 333 | }
|
| 334 |
|
| 335 | return new ChatComponent.DelayedMessageDeletion(signature, deletableAfter);
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | return null;
|
| 340 | }
|
| 341 |
|
| 342 | private static GuiMessage createDeletedMarker(final GuiMessage message) {
|
| 343 | return new GuiMessage(message.addedTime(), DELETED_CHAT_MESSAGE, null, GuiMessageSource.SYSTEM_SERVER, GuiMessageTag.system());
|
| 344 | }
|
| 345 |
|
| 346 | public void rescaleChat() {
|
| 347 | this.resetChatScroll();
|
| 348 | this.refreshTrimmedMessages();
|
| 349 | }
|
| 350 |
|
| 351 | private void refreshTrimmedMessages() {
|
| 352 | this.trimmedMessages.clear();
|
| 353 |
|
| 354 | for (GuiMessage message : Lists.reverse(this.allMessages)) {
|
| 355 | if (this.visibleMessageFilter.test(message)) {
|
| 356 | this.addMessageToDisplayQueue(message);
|
| 357 | }
|
| 358 | }
|
| 359 | }
|
| 360 |
|
| 361 | public ArrayListDeque<String> getRecentChat() {
|
| 362 | return this.recentChat;
|
| 363 | }
|
| 364 |
|
| 365 | public void addRecentChat(final String message) {
|
| 366 | if (!message.equals(this.recentChat.peekLast())) {
|
| 367 | if (this.recentChat.size() >= 100) {
|
| 368 | this.recentChat.removeFirst();
|
| 369 | }
|
| 370 |
|
| 371 | this.recentChat.addLast(message);
|
| 372 | }
|
| 373 |
|
| 374 | if (message.startsWith("/")) {
|
| 375 | this.commandHistory.addCommand(message);
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | public void resetChatScroll() {
|
| 380 | this.chatScrollbarPos = 0;
|
| 381 | this.newMessageSinceScroll = false;
|
| 382 | }
|
| 383 |
|
| 384 | public void scrollChat(final int dir) {
|
| 385 | this.chatScrollbarPos += dir;
|
| 386 | int max = this.trimmedMessages.size();
|
| 387 | if (this.chatScrollbarPos > max - this.getLinesPerPage()) {
|
| 388 | this.chatScrollbarPos = max - this.getLinesPerPage();
|
| 389 | }
|
| 390 |
|
| 391 | if (this.chatScrollbarPos <= 0) {
|
| 392 | this.chatScrollbarPos = 0;
|
| 393 | this.newMessageSinceScroll = false;
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | public boolean isChatFocused() {
|
| 398 | return this.minecraft.gui.screen() instanceof ChatScreen;
|
| 399 | }
|
| 400 |
|
| 401 | private int getWidth() {
|
| 402 | return getWidth(this.minecraft.options.chatWidth().get());
|
| 403 | }
|
| 404 |
|
| 405 | private int getHeight() {
|
| 406 | return getHeight(this.isChatFocused() ? this.minecraft.options.chatHeightFocused().get() : this.minecraft.options.chatHeightUnfocused().get());
|
| 407 | }
|
| 408 |
|
| 409 | private double getScale() {
|
| 410 | return this.minecraft.options.chatScale().get();
|
| 411 | }
|
| 412 |
|
| 413 | public static int getWidth(final double pct) {
|
| 414 | int max = 320;
|
| 415 | int min = 40;
|
| 416 | return Mth.floor(pct * 280.0 + 40.0);
|
| 417 | }
|
| 418 |
|
| 419 | public static int getHeight(final double pct) {
|
| 420 | int max = 180;
|
| 421 | int min = 20;
|
| 422 | return Mth.floor(pct * 160.0 + 20.0);
|
| 423 | }
|
| 424 |
|
| 425 | public static double defaultUnfocusedPct() {
|
| 426 | int max = 180;
|
| 427 | int min = 20;
|
| 428 | return 70.0 / (getHeight(1.0) - 20);
|
| 429 | }
|
| 430 |
|
| 431 | public int getLinesPerPage() {
|
| 432 | return this.getHeight() / this.getLineHeight();
|
| 433 | }
|
| 434 |
|
| 435 | private int getLineHeight() {
|
| 436 | return (int)(9.0 * (this.minecraft.options.chatLineSpacing().get() + 1.0));
|
| 437 | }
|
| 438 |
|
| 439 | public void saveAsDraft(final String text) {
|
| 440 | boolean isCommand = text.startsWith("/");
|
| 441 | this.latestDraft = new ChatComponent.Draft(text, isCommand ? ChatComponent.ChatMethod.COMMAND : ChatComponent.ChatMethod.MESSAGE);
|
| 442 | }
|
| 443 |
|
| 444 | public void discardDraft() {
|
| 445 | this.latestDraft = null;
|
| 446 | }
|
| 447 |
|
| 448 | public <T extends ChatScreen> T createScreen(final ChatComponent.ChatMethod chatMethod, final ChatScreen.ChatConstructor<T> chat) {
|
| 449 | return this.latestDraft != null && chatMethod.isDraftRestorable(this.latestDraft)
|
| 450 | ? chat.create(this.latestDraft.text(), true)
|
| 451 | : chat.create(chatMethod.prefix(), false);
|
| 452 | }
|
| 453 |
|
| 454 | public void openScreen(final ChatComponent.ChatMethod chatMethod, final ChatScreen.ChatConstructor<?> chat) {
|
| 455 | this.minecraft.gui.setScreen(this.createScreen(chatMethod, (ChatScreen.ChatConstructor<Screen>)chat));
|
| 456 | }
|
| 457 |
|
| 458 | public void preserveCurrentChatScreen() {
|
| 459 | if (this.minecraft.gui.screen() instanceof ChatScreen chatScreen) {
|
| 460 | this.preservedScreen = chatScreen;
|
| 461 | }
|
| 462 | }
|
| 463 |
|
| 464 | public @Nullable ChatScreen restoreChatScreen() {
|
| 465 | ChatScreen restoredScreen = this.preservedScreen;
|
| 466 | this.preservedScreen = null;
|
| 467 | return restoredScreen;
|
| 468 | }
|
| 469 |
|
| 470 | public ChatComponent.State storeState() {
|
| 471 | return new ChatComponent.State(List.copyOf(this.allMessages), List.copyOf(this.recentChat), List.copyOf(this.messageDeletionQueue));
|
| 472 | }
|
| 473 |
|
| 474 | public void restoreState(final ChatComponent.State state) {
|
| 475 | this.recentChat.clear();
|
| 476 | this.recentChat.addAll(state.history);
|
| 477 | this.messageDeletionQueue.clear();
|
| 478 | this.messageDeletionQueue.addAll(state.delayedMessageDeletions);
|
| 479 | this.allMessages.clear();
|
| 480 | this.allMessages.addAll(state.messages);
|
| 481 | this.refreshTrimmedMessages();
|
| 482 | }
|
| 483 |
|
| 484 | @FunctionalInterface
|
| 485 | @Environment(EnvType.CLIENT)
|
| 486 | private interface AlphaCalculator {
|
| 487 | ChatComponent.AlphaCalculator FULLY_VISIBLE = var0 -> 1.0F;
|
| 488 |
|
| 489 | static ChatComponent.AlphaCalculator timeBased(final int currentTickTime) {
|
| 490 | return message -> {
|
| 491 | int tickDelta = currentTickTime - message.addedTime();
|
| 492 | double t = tickDelta / 200.0;
|
| 493 | t = 1.0 - t;
|
| 494 | t *= 10.0;
|
| 495 | t = Mth.clamp(t, 0.0, 1.0);
|
| 496 | t *= t;
|
| 497 | return (float)t;
|
| 498 | };
|
| 499 | }
|
| 500 |
|
| 501 | float calculate(GuiMessage.Line message);
|
| 502 | }
|
| 503 |
|
| 504 | @Environment(EnvType.CLIENT)
|
| 505 | public interface ChatGraphicsAccess {
|
| 506 | void updatePose(final Consumer<Matrix3x2f> updater);
|
| 507 |
|
| 508 | void fill(int x0, int y0, int x1, int y1, int color);
|
| 509 |
|
| 510 | boolean handleMessage(int textTop, float opacity, FormattedCharSequence message);
|
| 511 |
|
| 512 | void handleTag(int x0, int y0, int x1, int y1, float opacity, GuiMessageTag tag);
|
| 513 |
|
| 514 | void handleTagIcon(int left, int bottom, boolean forceVisible, GuiMessageTag tag, GuiMessageTag.Icon icon);
|
| 515 | }
|
| 516 |
|
| 517 | @Environment(EnvType.CLIENT)
|
| 518 | public enum ChatMethod {
|
| 519 | MESSAGE("MESSAGE", 0, ""),
|
| 520 | COMMAND("COMMAND", 1, "/");
|
| 521 |
|
| 522 | private final String prefix;
|
| 523 |
|
| 524 | ChatMethod(final String prefix) {
|
| 525 | this.prefix = prefix;
|
| 526 | }
|
| 527 |
|
| 528 | public String prefix() {
|
| 529 | return this.prefix;
|
| 530 | }
|
| 531 |
|
| 532 | public abstract boolean isDraftRestorable(ChatComponent.Draft draft);
|
| 533 | }
|
| 534 |
|
| 535 | @Environment(EnvType.CLIENT)
|
| 536 | private static class ClickableTextOnlyGraphicsAccess implements ChatComponent.ChatGraphicsAccess {
|
| 537 | private final ActiveTextCollector output;
|
| 538 |
|
| 539 | public ClickableTextOnlyGraphicsAccess(final ActiveTextCollector output) {
|
| 540 | this.output = output;
|
| 541 | }
|
| 542 |
|
| 543 | @Override
|
| 544 | public void updatePose(final Consumer<Matrix3x2f> updater) {
|
| 545 | ActiveTextCollector.Parameters defaultParameters = this.output.defaultParameters();
|
| 546 | Matrix3x2f newPose = new Matrix3x2f(defaultParameters.pose());
|
| 547 | updater.accept(newPose);
|
| 548 | this.output.defaultParameters(defaultParameters.withPose(newPose));
|
| 549 | }
|
| 550 |
|
| 551 | @Override
|
| 552 | public void fill(final int x0, final int y0, final int x1, final int y1, final int color) {
|
| 553 | }
|
| 554 |
|
| 555 | @Override
|
| 556 | public boolean handleMessage(final int textTop, final float opacity, final FormattedCharSequence message) {
|
| 557 | this.output.accept(TextAlignment.LEFT, 0, textTop, message);
|
| 558 | return false;
|
| 559 | }
|
| 560 |
|
| 561 | @Override
|
| 562 | public void handleTag(final int x0, final int y0, final int x1, final int y1, final float opacity, final GuiMessageTag tag) {
|
| 563 | }
|
| 564 |
|
| 565 | @Override
|
| 566 | public void handleTagIcon(final int left, final int bottom, final boolean forceVisible, final GuiMessageTag tag, final GuiMessageTag.Icon icon) {
|
| 567 | }
|
| 568 | }
|
| 569 |
|
| 570 | @Environment(EnvType.CLIENT)
|
| 571 | private record DelayedMessageDeletion(MessageSignature signature, int deletableAfter) {
|
| 572 | }
|
| 573 |
|
| 574 | @Environment(EnvType.CLIENT)
|
| 575 | public enum DisplayMode {
|
| 576 | BACKGROUND(false, false),
|
| 577 | FOREGROUND(true, false),
|
| 578 | FOREGROUND_RESTRICTED(true, true);
|
| 579 |
|
| 580 | public final boolean foreground;
|
| 581 | public final boolean showRestrictedPrompt;
|
| 582 |
|
| 583 | DisplayMode(final boolean foreground, final boolean showRestrictedPrompt) {
|
| 584 | this.foreground = foreground;
|
| 585 | this.showRestrictedPrompt = showRestrictedPrompt;
|
| 586 | }
|
| 587 | }
|
| 588 |
|
| 589 | @Environment(EnvType.CLIENT)
|
| 590 | public record Draft(String text, ChatComponent.ChatMethod chatMethod) {
|
| 591 | }
|
| 592 |
|
| 593 | @Environment(EnvType.CLIENT)
|
| 594 | private static class DrawingBackgroundGraphicsAccess implements ChatComponent.ChatGraphicsAccess {
|
| 595 | private final GuiGraphicsExtractor graphics;
|
| 596 | private final ActiveTextCollector textRenderer;
|
| 597 | private ActiveTextCollector.Parameters parameters;
|
| 598 |
|
| 599 | public DrawingBackgroundGraphicsAccess(final GuiGraphicsExtractor graphics) {
|
| 600 | this.graphics = graphics;
|
| 601 | this.textRenderer = graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.NONE, null);
|
| 602 | this.parameters = this.textRenderer.defaultParameters();
|
| 603 | }
|
| 604 |
|
| 605 | @Override
|
| 606 | public void updatePose(final Consumer<Matrix3x2f> updater) {
|
| 607 | updater.accept(this.graphics.pose());
|
| 608 | this.parameters = this.parameters.withPose(new Matrix3x2f(this.graphics.pose()));
|
| 609 | }
|
| 610 |
|
| 611 | @Override
|
| 612 | public void fill(final int x0, final int y0, final int x1, final int y1, final int color) {
|
| 613 | this.graphics.fill(x0, y0, x1, y1, color);
|
| 614 | }
|
| 615 |
|
| 616 | @Override
|
| 617 | public boolean handleMessage(final int textTop, final float opacity, final FormattedCharSequence message) {
|
| 618 | this.textRenderer.accept(TextAlignment.LEFT, 0, textTop, this.parameters.withOpacity(opacity), message);
|
| 619 | return false;
|
| 620 | }
|
| 621 |
|
| 622 | @Override
|
| 623 | public void handleTag(final int x0, final int y0, final int x1, final int y1, final float opacity, final GuiMessageTag tag) {
|
| 624 | int indicatorColor = ARGB.color(opacity, tag.indicatorColor());
|
| 625 | this.graphics.fill(x0, y0, x1, y1, indicatorColor);
|
| 626 | }
|
| 627 |
|
| 628 | @Override
|
| 629 | public void handleTagIcon(final int left, final int bottom, final boolean forceVisible, final GuiMessageTag tag, final GuiMessageTag.Icon icon) {
|
| 630 | }
|
| 631 | }
|
| 632 |
|
| 633 | @Environment(EnvType.CLIENT)
|
| 634 | private static class DrawingFocusedGraphicsAccess implements ChatComponent.ChatGraphicsAccess, Consumer<Style> {
|
| 635 | private final GuiGraphicsExtractor graphics;
|
| 636 | private final Font font;
|
| 637 | private final ActiveTextCollector textRenderer;
|
| 638 | private ActiveTextCollector.Parameters parameters;
|
| 639 | private final int globalMouseX;
|
| 640 | private final int globalMouseY;
|
| 641 | private final Vector2f localMousePos = new Vector2f();
|
| 642 | private @Nullable Style hoveredStyle;
|
| 643 | private final boolean changeCursorOnInsertions;
|
| 644 |
|
| 645 | public DrawingFocusedGraphicsAccess(
|
| 646 | final GuiGraphicsExtractor graphics, final Font font, final int mouseX, final int mouseY, final boolean changeCursorOnInsertions
|
| 647 | ) {
|
| 648 | this.graphics = graphics;
|
| 649 | this.font = font;
|
| 650 | this.textRenderer = graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR, this);
|
| 651 | this.globalMouseX = mouseX;
|
| 652 | this.globalMouseY = mouseY;
|
| 653 | this.changeCursorOnInsertions = changeCursorOnInsertions;
|
| 654 | this.parameters = this.textRenderer.defaultParameters();
|
| 655 | this.updateLocalMousePos();
|
| 656 | }
|
| 657 |
|
| 658 | private void updateLocalMousePos() {
|
| 659 | this.graphics.pose().invert(new Matrix3x2f()).transformPosition(this.globalMouseX, this.globalMouseY, this.localMousePos);
|
| 660 | }
|
| 661 |
|
| 662 | @Override
|
| 663 | public void updatePose(final Consumer<Matrix3x2f> updater) {
|
| 664 | updater.accept(this.graphics.pose());
|
| 665 | this.parameters = this.parameters.withPose(new Matrix3x2f(this.graphics.pose()));
|
| 666 | this.updateLocalMousePos();
|
| 667 | }
|
| 668 |
|
| 669 | @Override
|
| 670 | public void fill(final int x0, final int y0, final int x1, final int y1, final int color) {
|
| 671 | this.graphics.fill(x0, y0, x1, y1, color);
|
| 672 | }
|
| 673 |
|
| 674 | public void accept(final Style style) {
|
| 675 | this.hoveredStyle = style;
|
| 676 | }
|
| 677 |
|
| 678 | @Override
|
| 679 | public boolean handleMessage(final int textTop, final float opacity, final FormattedCharSequence message) {
|
| 680 | this.hoveredStyle = null;
|
| 681 | this.textRenderer.accept(TextAlignment.LEFT, 0, textTop, this.parameters.withOpacity(opacity), message);
|
| 682 | if (this.changeCursorOnInsertions && this.hoveredStyle != null && this.hoveredStyle.getInsertion() != null) {
|
| 683 | this.graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 684 | }
|
| 685 |
|
| 686 | return this.hoveredStyle != null;
|
| 687 | }
|
| 688 |
|
| 689 | private boolean isMouseOver(final int left, final int top, final int right, final int bottom) {
|
| 690 | return ActiveTextCollector.isPointInRectangle(this.localMousePos.x, this.localMousePos.y, left, top, right, bottom);
|
| 691 | }
|
| 692 |
|
| 693 | @Override
|
| 694 | public void handleTag(final int x0, final int y0, final int x1, final int y1, final float opacity, final GuiMessageTag tag) {
|
| 695 | int indicatorColor = ARGB.color(opacity, tag.indicatorColor());
|
| 696 | this.graphics.fill(x0, y0, x1, y1, indicatorColor);
|
| 697 | if (this.isMouseOver(x0, y0, x1, y1)) {
|
| 698 | this.showTooltip(tag);
|
| 699 | }
|
| 700 | }
|
| 701 |
|
| 702 | @Override
|
| 703 | public void handleTagIcon(final int left, final int bottom, final boolean forceVisible, final GuiMessageTag tag, final GuiMessageTag.Icon icon) {
|
| 704 | int top = bottom - icon.height - 1;
|
| 705 | int right = left + icon.width;
|
| 706 | boolean isMouseOver = this.isMouseOver(left, top, right, bottom);
|
| 707 | if (isMouseOver) {
|
| 708 | this.showTooltip(tag);
|
| 709 | }
|
| 710 |
|
| 711 | if (forceVisible || isMouseOver) {
|
| 712 | icon.extractRenderState(this.graphics, left, top);
|
| 713 | }
|
| 714 | }
|
| 715 |
|
| 716 | private void showTooltip(final GuiMessageTag tag) {
|
| 717 | if (tag.text() != null) {
|
| 718 | this.graphics.setTooltipForNextFrame(this.font, this.font.split(tag.text(), 210), this.globalMouseX, this.globalMouseY);
|
| 719 | }
|
| 720 | }
|
| 721 | }
|
| 722 |
|
| 723 | @FunctionalInterface
|
| 724 | @Environment(EnvType.CLIENT)
|
| 725 | private interface LineConsumer {
|
| 726 | void accept(GuiMessage.Line line, int lineIndex, float alpha);
|
| 727 | }
|
| 728 |
|
| 729 | @Environment(EnvType.CLIENT)
|
| 730 | public static class State {
|
| 731 | private final List<GuiMessage> messages;
|
| 732 | private final List<String> history;
|
| 733 | private final List<ChatComponent.DelayedMessageDeletion> delayedMessageDeletions;
|
| 734 |
|
| 735 | public State(final List<GuiMessage> messages, final List<String> history, final List<ChatComponent.DelayedMessageDeletion> delayedMessageDeletions) {
|
| 736 | this.messages = messages;
|
| 737 | this.history = history;
|
| 738 | this.delayedMessageDeletions = delayedMessageDeletions;
|
| 739 | }
|
| 740 | }
|
| 741 | }
|
| 742 |
|