| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.ChatFormatting;
|
| 6 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.gui.components.ChatComponent;
|
| 9 | import net.minecraft.client.gui.components.CommandSuggestions;
|
| 10 | import net.minecraft.client.gui.components.EditBox;
|
| 11 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 12 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 13 | import net.minecraft.client.gui.screens.multiplayer.RestrictionsScreen;
|
| 14 | import net.minecraft.client.input.KeyEvent;
|
| 15 | import net.minecraft.client.input.MouseButtonEvent;
|
| 16 | import net.minecraft.client.multiplayer.chat.ChatAbilities;
|
| 17 | import net.minecraft.client.multiplayer.chat.ChatListener;
|
| 18 | import net.minecraft.network.chat.ClickEvent;
|
| 19 | import net.minecraft.network.chat.CommonComponents;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.chat.MutableComponent;
|
| 22 | import net.minecraft.network.chat.Style;
|
| 23 | import net.minecraft.network.chat.ClickEvent.Custom;
|
| 24 | import net.minecraft.util.FormattedCharSequence;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.StringUtil;
|
| 27 | import org.apache.commons.lang3.StringUtils;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class ChatScreen extends Screen {
|
| 32 | public static final double MOUSE_SCROLL_SPEED = 7.0;
|
| 33 | private static final Component USAGE_TEXT = Component.translatable("chat_screen.usage");
|
| 34 | private static final Component RESTRICTED_NARRATION_TEXT = Component.translatable("chat_screen.restricted.narration");
|
| 35 | public static final int USAGE_BACKGROUND_COLOR = -805306368;
|
| 36 | private final boolean closeOnSubmit;
|
| 37 | private String historyBuffer = "";
|
| 38 | private int historyPos = -1;
|
| 39 | protected EditBox input;
|
| 40 | protected String initial;
|
| 41 | protected boolean isDraft;
|
| 42 | private ChatComponent.DisplayMode displayMode = ChatComponent.DisplayMode.FOREGROUND;
|
| 43 | protected ChatScreen.ExitReason exitReason = ChatScreen.ExitReason.INTERRUPTED;
|
| 44 | private CommandSuggestions commandSuggestions;
|
| 45 |
|
| 46 | public ChatScreen(final String initial, final boolean isDraft) {
|
| 47 | this(initial, isDraft, true);
|
| 48 | }
|
| 49 |
|
| 50 | public ChatScreen(final String initial, final boolean isDraft, final boolean closeOnSubmit) {
|
| 51 | super(Component.translatable("chat_screen.title"));
|
| 52 | this.closeOnSubmit = closeOnSubmit;
|
| 53 | this.initial = initial;
|
| 54 | this.isDraft = isDraft;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void init() {
|
| 59 | this.historyPos = this.minecraft.gui.hud.getChat().getRecentChat().size();
|
| 60 | this.input = new EditBox(this.minecraft.fontFilterFishy, 4, this.height - 12, this.width - 4, 12, Component.translatable("chat.editBox")) {
|
| 61 | @Override
|
| 62 | protected MutableComponent createNarrationMessage() {
|
| 63 | return super.createNarrationMessage().append(ChatScreen.this.commandSuggestions.getNarrationMessage());
|
| 64 | }
|
| 65 | };
|
| 66 | this.input.setMaxLength(256);
|
| 67 | this.input.setBordered(false);
|
| 68 | this.input.setValue(this.initial);
|
| 69 | this.input.setResponder(this::onEdited);
|
| 70 | this.input.addFormatter(this::formatChat);
|
| 71 | this.input.setCanLoseFocus(false);
|
| 72 | this.addRenderableWidget(this.input);
|
| 73 | this.commandSuggestions = new CommandSuggestions(this.minecraft, this, this.input, this.font, false, false, 1, 10, true, -805306368);
|
| 74 | this.commandSuggestions.setAllowHiding(false);
|
| 75 | this.commandSuggestions.setAllowSuggestions(false);
|
| 76 | ChatAbilities chatAbilities = this.minecraft.player.chatAbilities();
|
| 77 | this.displayMode = chatAbilities.hasAnyRestrictions() ? ChatComponent.DisplayMode.FOREGROUND_RESTRICTED : ChatComponent.DisplayMode.FOREGROUND;
|
| 78 | this.commandSuggestions.setRestrictions(chatAbilities.canSendMessages(), chatAbilities.canSendCommands());
|
| 79 | this.commandSuggestions.updateCommandInfo();
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | protected void setInitialFocus() {
|
| 84 | this.setInitialFocus(this.input);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void resize(final int width, final int height) {
|
| 89 | this.initial = this.input.getValue();
|
| 90 | this.init(width, height);
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void onClose() {
|
| 95 | this.exitReason = ChatScreen.ExitReason.INTENTIONAL;
|
| 96 | super.onClose();
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void removed() {
|
| 101 | this.minecraft.gui.hud.getChat().resetChatScroll();
|
| 102 | this.initial = this.input.getValue();
|
| 103 | if (this.shouldDiscardDraft() || StringUtils.isBlank(this.initial)) {
|
| 104 | this.minecraft.gui.hud.getChat().discardDraft();
|
| 105 | } else if (!this.isDraft) {
|
| 106 | this.minecraft.gui.hud.getChat().saveAsDraft(this.initial);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | protected boolean shouldDiscardDraft() {
|
| 111 | return this.exitReason != ChatScreen.ExitReason.INTERRUPTED
|
| 112 | && (this.exitReason != ChatScreen.ExitReason.INTENTIONAL || !this.minecraft.options.saveChatDrafts().get());
|
| 113 | }
|
| 114 |
|
| 115 | private void onEdited(final String value) {
|
| 116 | this.commandSuggestions.setAllowSuggestions(true);
|
| 117 | this.commandSuggestions.updateCommandInfo();
|
| 118 | this.isDraft = false;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public boolean keyPressed(final KeyEvent event) {
|
| 123 | if (this.commandSuggestions.keyPressed(event)) {
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | if (this.isDraft && event.shortcutKey() == 8) {
|
| 128 | this.input.setValue("");
|
| 129 | this.isDraft = false;
|
| 130 | return true;
|
| 131 | }
|
| 132 |
|
| 133 | if (super.keyPressed(event)) {
|
| 134 | return true;
|
| 135 | }
|
| 136 |
|
| 137 | if (event.isConfirmation()) {
|
| 138 | if (!this.commandSuggestions.hasAllowedInput()) {
|
| 139 | return true;
|
| 140 | }
|
| 141 |
|
| 142 | this.handleChatInput(this.input.getValue(), true);
|
| 143 | if (this.closeOnSubmit) {
|
| 144 | this.exitReason = ChatScreen.ExitReason.DONE;
|
| 145 | this.minecraft.gui.setScreen(null);
|
| 146 | } else {
|
| 147 | this.input.setValue("");
|
| 148 | this.minecraft.gui.hud.getChat().resetChatScroll();
|
| 149 | }
|
| 150 |
|
| 151 | return true;
|
| 152 | } else {
|
| 153 | switch (event.shortcutKey()) {
|
| 154 | case 1073741899:
|
| 155 | this.minecraft.gui.hud.getChat().scrollChat(this.minecraft.gui.hud.getChat().getLinesPerPage() - 1);
|
| 156 | break;
|
| 157 | case 1073741900:
|
| 158 | case 1073741901:
|
| 159 | case 1073741903:
|
| 160 | case 1073741904:
|
| 161 | default:
|
| 162 | return false;
|
| 163 | case 1073741902:
|
| 164 | this.minecraft.gui.hud.getChat().scrollChat(-this.minecraft.gui.hud.getChat().getLinesPerPage() + 1);
|
| 165 | break;
|
| 166 | case 1073741905:
|
| 167 | this.moveInHistory(1);
|
| 168 | break;
|
| 169 | case 1073741906:
|
| 170 | this.moveInHistory(-1);
|
| 171 | }
|
| 172 |
|
| 173 | return true;
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public boolean mouseScrolled(final double x, final double y, final double scrollX, double scrollY) {
|
| 179 | scrollY = Mth.clamp(scrollY, -1.0, 1.0);
|
| 180 | if (this.commandSuggestions.mouseScrolled(scrollY)) {
|
| 181 | return true;
|
| 182 | }
|
| 183 |
|
| 184 | if (!this.minecraft.hasShiftDown()) {
|
| 185 | scrollY *= 7.0;
|
| 186 | }
|
| 187 |
|
| 188 | this.minecraft.gui.hud.getChat().scrollChat((int)scrollY);
|
| 189 | return true;
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 194 | if (this.commandSuggestions.mouseClicked(event)) {
|
| 195 | return true;
|
| 196 | }
|
| 197 |
|
| 198 | if (event.button() == 1) {
|
| 199 | int screenHeight = this.minecraft.getWindow().getGuiScaledHeight();
|
| 200 | ActiveTextCollector.ClickableStyleFinder finder = new ActiveTextCollector.ClickableStyleFinder(this.getFont(), (int)event.x(), (int)event.y())
|
| 201 | .includeInsertions(this.insertionClickMode());
|
| 202 | this.minecraft.gui.hud.getChat().captureClickableText(finder, screenHeight, this.minecraft.gui.hud.getGuiTicks(), this.displayMode);
|
| 203 | Style clicked = finder.result();
|
| 204 | if (clicked != null && this.handleComponentClicked(clicked, this.insertionClickMode())) {
|
| 205 | this.initial = this.input.getValue();
|
| 206 | return true;
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | return super.mouseClicked(event, doubleClick);
|
| 211 | }
|
| 212 |
|
| 213 | private boolean insertionClickMode() {
|
| 214 | return this.minecraft.hasShiftDown();
|
| 215 | }
|
| 216 |
|
| 217 | private boolean handleComponentClicked(final Style clicked, final boolean allowInsertions) {
|
| 218 | ClickEvent event = clicked.getClickEvent();
|
| 219 | if (allowInsertions) {
|
| 220 | if (clicked.getInsertion() != null) {
|
| 221 | this.insertText(clicked.getInsertion(), false);
|
| 222 | }
|
| 223 | } else if (event != null) {
|
| 224 | switch (event) {
|
| 225 | case Custom customEvent when customEvent.id().equals(ChatComponent.QUEUE_EXPAND_ID):
|
| 226 | ChatListener chatListener = this.minecraft.gui.chatListener();
|
| 227 | if (chatListener.queueSize() != 0L) {
|
| 228 | chatListener.acceptNextDelayedMessage();
|
| 229 | }
|
| 230 | break;
|
| 231 | case Custom customEvent when customEvent.id().equals(ChatComponent.GO_TO_RESTRICTIONS_SCREEN):
|
| 232 | this.minecraft.gui.setScreen(new RestrictionsScreen(this, this.minecraft.player.chatAbilities()));
|
| 233 | break;
|
| 234 | default:
|
| 235 | defaultHandleGameClickEvent(event, this.minecraft, this);
|
| 236 | }
|
| 237 |
|
| 238 | return true;
|
| 239 | }
|
| 240 |
|
| 241 | return false;
|
| 242 | }
|
| 243 |
|
| 244 | @Override
|
| 245 | public void insertText(final String text, final boolean replace) {
|
| 246 | if (replace) {
|
| 247 | this.input.setValue(text);
|
| 248 | } else {
|
| 249 | this.input.insertText(text);
|
| 250 | }
|
| 251 | }
|
| 252 |
|
| 253 | public void moveInHistory(final int dir) {
|
| 254 | int newPos = this.historyPos + dir;
|
| 255 | int max = this.minecraft.gui.hud.getChat().getRecentChat().size();
|
| 256 | newPos = Mth.clamp(newPos, 0, max);
|
| 257 | if (newPos != this.historyPos) {
|
| 258 | if (newPos == max) {
|
| 259 | this.historyPos = max;
|
| 260 | this.input.setValue(this.historyBuffer);
|
| 261 | } else {
|
| 262 | if (this.historyPos == max) {
|
| 263 | this.historyBuffer = this.input.getValue();
|
| 264 | }
|
| 265 |
|
| 266 | this.input.setValue(this.minecraft.gui.hud.getChat().getRecentChat().get(newPos));
|
| 267 | this.commandSuggestions.setAllowSuggestions(false);
|
| 268 | this.historyPos = newPos;
|
| 269 | }
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | private @Nullable FormattedCharSequence formatChat(final String text, final int offset) {
|
| 274 | return this.isDraft ? FormattedCharSequence.forward(text, Style.EMPTY.withColor(ChatFormatting.GRAY).withItalic(true)) : null;
|
| 275 | }
|
| 276 |
|
| 277 | @Override
|
| 278 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 279 | graphics.fill(2, this.height - 14, this.width - 2, this.height - 2, this.minecraft.options.getBackgroundColor(Integer.MIN_VALUE));
|
| 280 | this.minecraft
|
| 281 | .gui
|
| 282 | .hud
|
| 283 | .getChat()
|
| 284 | .extractRenderState(graphics, this.font, this.minecraft.gui.hud.getGuiTicks(), mouseX, mouseY, this.displayMode, this.insertionClickMode());
|
| 285 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 286 | this.commandSuggestions.extractRenderState(graphics, mouseX, mouseY);
|
| 287 | }
|
| 288 |
|
| 289 | @Override
|
| 290 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public boolean isPauseScreen() {
|
| 295 | return false;
|
| 296 | }
|
| 297 |
|
| 298 | @Override
|
| 299 | public boolean isAllowedInPortal() {
|
| 300 | return true;
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | protected void updateNarrationState(final NarrationElementOutput output) {
|
| 305 | output.add(NarratedElementType.TITLE, this.getTitle());
|
| 306 | if (this.displayMode.showRestrictedPrompt) {
|
| 307 | output.add(NarratedElementType.USAGE, CommonComponents.joinForNarration(USAGE_TEXT, RESTRICTED_NARRATION_TEXT));
|
| 308 | } else {
|
| 309 | output.add(NarratedElementType.USAGE, USAGE_TEXT);
|
| 310 | }
|
| 311 |
|
| 312 | String value = this.input.getValue();
|
| 313 | if (!value.isEmpty()) {
|
| 314 | output.nest().add(NarratedElementType.TITLE, Component.translatable("chat_screen.message", value));
|
| 315 | }
|
| 316 | }
|
| 317 |
|
| 318 | public void handleChatInput(String msg, final boolean addToRecent) {
|
| 319 | msg = this.normalizeChatMessage(msg);
|
| 320 | if (!msg.isEmpty()) {
|
| 321 | if (addToRecent) {
|
| 322 | this.minecraft.gui.hud.getChat().addRecentChat(msg);
|
| 323 | }
|
| 324 |
|
| 325 | if (msg.startsWith("/")) {
|
| 326 | this.minecraft.player.connection.sendCommand(msg.substring(1));
|
| 327 | } else {
|
| 328 | this.minecraft.player.connection.sendChat(msg);
|
| 329 | }
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | public String normalizeChatMessage(final String message) {
|
| 334 | return StringUtil.trimChatMessage(StringUtils.normalizeSpace(message.trim()));
|
| 335 | }
|
| 336 |
|
| 337 | @FunctionalInterface
|
| 338 | @Environment(EnvType.CLIENT)
|
| 339 | public interface ChatConstructor<T extends ChatScreen> {
|
| 340 | T create(String initial, boolean isDraft);
|
| 341 | }
|
| 342 |
|
| 343 | @Environment(EnvType.CLIENT)
|
| 344 | protected enum ExitReason {
|
| 345 | INTENTIONAL,
|
| 346 | INTERRUPTED,
|
| 347 | DONE;
|
| 348 | }
|
| 349 | }
|
| 350 |
|