| 1 | package net.minecraft.client.gui.screens.reporting;
|
| 2 |
|
| 3 | import com.mojang.authlib.minecraft.report.AbuseReportLimits;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.TextAlignment;
|
| 11 | import net.minecraft.client.gui.components.Button;
|
| 12 | import net.minecraft.client.gui.components.MultiLineLabel;
|
| 13 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 14 | import net.minecraft.client.gui.navigation.ScreenDirection;
|
| 15 | import net.minecraft.client.gui.screens.Screen;
|
| 16 | import net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.DividerEntry;
|
| 17 | import net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Heading;
|
| 18 | import net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.MessageEntry;
|
| 19 | import net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.MessageHeadingEntry;
|
| 20 | import net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.PaddingEntry;
|
| 21 | import net.minecraft.client.input.KeyEvent;
|
| 22 | import net.minecraft.client.multiplayer.chat.ChatTrustLevel;
|
| 23 | import net.minecraft.client.multiplayer.chat.GuiMessageTag;
|
| 24 | import net.minecraft.client.multiplayer.chat.LoggedChatMessage;
|
| 25 | import net.minecraft.client.multiplayer.chat.report.ChatReport;
|
| 26 | import net.minecraft.client.multiplayer.chat.report.ReportingContext;
|
| 27 | import net.minecraft.network.chat.CommonComponents;
|
| 28 | import net.minecraft.network.chat.Component;
|
| 29 | import net.minecraft.resources.Identifier;
|
| 30 | import net.minecraft.util.Mth;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class ChatSelectionScreen extends Screen {
|
| 35 | private static final Identifier CHECKMARK_SPRITE = Identifier.withDefaultNamespace("icon/checkmark");
|
| 36 | private static final Component TITLE = Component.translatable("gui.chatSelection.title");
|
| 37 | private static final Component CONTEXT_INFO = Component.translatable("gui.chatSelection.context");
|
| 38 | private final @Nullable Screen lastScreen;
|
| 39 | private final ReportingContext reportingContext;
|
| 40 | private Button confirmSelectedButton;
|
| 41 | private MultiLineLabel contextInfoLabel;
|
| 42 | private ChatSelectionScreen.@Nullable ChatSelectionList chatSelectionList;
|
| 43 | private final ChatReport.Builder report;
|
| 44 | private final Consumer<ChatReport.Builder> onSelected;
|
| 45 | private ChatSelectionLogFiller chatLogFiller;
|
| 46 |
|
| 47 | public ChatSelectionScreen(
|
| 48 | final @Nullable Screen lastScreen, final ReportingContext reportingContext, final ChatReport.Builder report, final Consumer<ChatReport.Builder> onSelected
|
| 49 | ) {
|
| 50 | super(TITLE);
|
| 51 | this.lastScreen = lastScreen;
|
| 52 | this.reportingContext = reportingContext;
|
| 53 | this.report = report.copy();
|
| 54 | this.onSelected = onSelected;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void init() {
|
| 59 | this.chatLogFiller = new ChatSelectionLogFiller(this.reportingContext, this::canReport);
|
| 60 | this.contextInfoLabel = MultiLineLabel.create(this.font, CONTEXT_INFO, this.width - 16);
|
| 61 | this.chatSelectionList = this.addRenderableWidget(new ChatSelectionScreen.ChatSelectionList(this.minecraft, (this.contextInfoLabel.getLineCount() + 1) * 9));
|
| 62 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_BACK, b -> this.onClose()).bounds(this.width / 2 - 155, this.height - 32, 150, 20).build());
|
| 63 | this.confirmSelectedButton = this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, b -> {
|
| 64 | this.onSelected.accept(this.report);
|
| 65 | this.onClose();
|
| 66 | }).bounds(this.width / 2 - 155 + 160, this.height - 32, 150, 20).build());
|
| 67 | this.updateConfirmSelectedButton();
|
| 68 | this.extendLog();
|
| 69 | this.chatSelectionList.setScrollAmount(this.chatSelectionList.maxScrollAmount());
|
| 70 | }
|
| 71 |
|
| 72 | private boolean canReport(final LoggedChatMessage message) {
|
| 73 | return message.canReport(this.report.reportedProfileId());
|
| 74 | }
|
| 75 |
|
| 76 | private void extendLog() {
|
| 77 | int pageSize = this.chatSelectionList.getMaxVisibleEntries();
|
| 78 | this.chatLogFiller.fillNextPage(pageSize, this.chatSelectionList);
|
| 79 | }
|
| 80 |
|
| 81 | private void onReachedScrollTop() {
|
| 82 | this.extendLog();
|
| 83 | }
|
| 84 |
|
| 85 | private void updateConfirmSelectedButton() {
|
| 86 | this.confirmSelectedButton.active = !this.report.reportedMessages().isEmpty();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 91 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 92 | ActiveTextCollector textRenderer = graphics.textRenderer();
|
| 93 | graphics.centeredText(this.font, this.title, this.width / 2, 10, -1);
|
| 94 | AbuseReportLimits reportLimits = this.reportingContext.sender().reportLimits();
|
| 95 | int messageCount = this.report.reportedMessages().size();
|
| 96 | int maxMessageCount = reportLimits.maxReportedMessageCount();
|
| 97 | Component selectedText = Component.translatable("gui.chatSelection.selected", messageCount, maxMessageCount);
|
| 98 | graphics.centeredText(this.font, selectedText, this.width / 2, 26, -1);
|
| 99 | int topY = this.chatSelectionList.getFooterTop();
|
| 100 | this.contextInfoLabel.visitLines(TextAlignment.CENTER, this.width / 2, topY, 9, textRenderer);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void onClose() {
|
| 105 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public Component getNarrationMessage() {
|
| 110 | return CommonComponents.joinForNarration(super.getNarrationMessage(), CONTEXT_INFO);
|
| 111 | }
|
| 112 |
|
| 113 | @Environment(EnvType.CLIENT)
|
| 114 | public class ChatSelectionList
|
| 115 | extends ObjectSelectionList<net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry>
|
| 116 | implements ChatSelectionLogFiller.Output {
|
| 117 | public static final int ITEM_HEIGHT = 16;
|
| 118 | private @Nullable Heading previousHeading;
|
| 119 |
|
| 120 | public ChatSelectionList(final Minecraft minecraft, final int upperMargin) {
|
| 121 | super(minecraft, ChatSelectionScreen.this.width, ChatSelectionScreen.this.height - upperMargin - 80, 40, 16);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void setScrollAmount(final double scrollAmount) {
|
| 126 | double prevScrollAmount = this.scrollAmount();
|
| 127 | super.setScrollAmount(scrollAmount);
|
| 128 | if (this.maxScrollAmount() > 1.0E-5F && scrollAmount <= 1.0E-5F && !Mth.equal(scrollAmount, prevScrollAmount)) {
|
| 129 | ChatSelectionScreen.this.onReachedScrollTop();
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void acceptMessage(final int id, final LoggedChatMessage.Player message) {
|
| 135 | boolean canReport = message.canReport(ChatSelectionScreen.this.report.reportedProfileId());
|
| 136 | ChatTrustLevel trustLevel = message.trustLevel();
|
| 137 | GuiMessageTag tag = trustLevel.createTag(message.message());
|
| 138 | net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry entry = new MessageEntry(
|
| 139 | this, id, message.toContentComponent(), message.toNarrationComponent(), tag, canReport, true
|
| 140 | );
|
| 141 | this.addEntryToTop(entry);
|
| 142 | this.updateHeading(message, canReport);
|
| 143 | }
|
| 144 |
|
| 145 | private void updateHeading(final LoggedChatMessage.Player message, final boolean canReport) {
|
| 146 | net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry entry = new MessageHeadingEntry(
|
| 147 | this, message.profile(), message.toHeadingComponent(), canReport
|
| 148 | );
|
| 149 | this.addEntryToTop(entry);
|
| 150 | Heading heading = new Heading(message.profileId(), entry);
|
| 151 | if (this.previousHeading != null && this.previousHeading.canCombine(heading)) {
|
| 152 | this.removeEntryFromTop(this.previousHeading.entry());
|
| 153 | }
|
| 154 |
|
| 155 | this.previousHeading = heading;
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public void acceptDivider(final Component text) {
|
| 160 | this.addEntryToTop(new PaddingEntry());
|
| 161 | this.addEntryToTop(new DividerEntry(this, text));
|
| 162 | this.addEntryToTop(new PaddingEntry());
|
| 163 | this.previousHeading = null;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public int getRowWidth() {
|
| 168 | return Math.min(350, this.width - 50);
|
| 169 | }
|
| 170 |
|
| 171 | public int getMaxVisibleEntries() {
|
| 172 | return Mth.positiveCeilDiv(this.height, 16);
|
| 173 | }
|
| 174 |
|
| 175 | protected void extractItem(
|
| 176 | final GuiGraphicsExtractor graphics,
|
| 177 | final int mouseX,
|
| 178 | final int mouseY,
|
| 179 | final float a,
|
| 180 | final net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry entry
|
| 181 | ) {
|
| 182 | if (this.shouldHighlightEntry(entry)) {
|
| 183 | boolean selected = this.getSelected() == entry;
|
| 184 | int outlineColor = this.isFocused() && selected ? -1 : -8355712;
|
| 185 | this.extractSelection(graphics, entry, outlineColor);
|
| 186 | }
|
| 187 |
|
| 188 | entry.extractContent(graphics, mouseX, mouseY, this.getHovered() == entry, a);
|
| 189 | }
|
| 190 |
|
| 191 | private boolean shouldHighlightEntry(final net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry entry) {
|
| 192 | if (entry.canSelect()) {
|
| 193 | boolean entrySelected = this.getSelected() == entry;
|
| 194 | boolean nothingSelected = this.getSelected() == null;
|
| 195 | boolean entryHovered = this.getHovered() == entry;
|
| 196 | return entrySelected || nothingSelected && entryHovered && entry.canReport();
|
| 197 | } else {
|
| 198 | return false;
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | protected @Nullable Entry nextEntry(final ScreenDirection dir) {
|
| 203 | return this.nextEntry(dir, net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry::canSelect);
|
| 204 | }
|
| 205 |
|
| 206 | public void setSelected(final @Nullable Entry selected) {
|
| 207 | super.setSelected(selected);
|
| 208 | net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry entry = this.nextEntry(ScreenDirection.UP);
|
| 209 | if (entry == null) {
|
| 210 | ChatSelectionScreen.this.onReachedScrollTop();
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public boolean keyPressed(final KeyEvent event) {
|
| 216 | net.minecraft.client.gui.screens.reporting.ChatSelectionScreen.ChatSelectionList.Entry selected = this.getSelected();
|
| 217 | return selected != null && selected.keyPressed(event) ? true : super.keyPressed(event);
|
| 218 | }
|
| 219 |
|
| 220 | public int getFooterTop() {
|
| 221 | return this.getBottom() + 9;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|