| 1 | package net.minecraft.client.gui.screens.reporting;
|
| 2 |
|
| 3 | import com.mojang.authlib.minecraft.report.AbuseReportLimits;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import java.util.concurrent.CancellationException;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.Optionull;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.gui.components.Checkbox;
|
| 14 | import net.minecraft.client.gui.components.MultiLineEditBox;
|
| 15 | import net.minecraft.client.gui.components.StringWidget;
|
| 16 | import net.minecraft.client.gui.components.Tooltip;
|
| 17 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 18 | import net.minecraft.client.gui.layouts.Layout;
|
| 19 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 20 | import net.minecraft.client.gui.screens.GenericWaitingScreen;
|
| 21 | import net.minecraft.client.gui.screens.Screen;
|
| 22 | import net.minecraft.client.gui.screens.multiplayer.WarningScreen;
|
| 23 | import net.minecraft.client.multiplayer.chat.report.Report;
|
| 24 | import net.minecraft.client.multiplayer.chat.report.ReportingContext;
|
| 25 | import net.minecraft.network.chat.CommonComponents;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.network.chat.ThrowingComponent;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public abstract class AbstractReportScreen<B extends Report.Builder<?>> extends Screen {
|
| 32 | private static final Component REPORT_SENT_MESSAGE = Component.translatable("gui.abuseReport.report_sent_msg");
|
| 33 | private static final Component REPORT_SENDING_TITLE = Component.translatable("gui.abuseReport.sending.title").withStyle(ChatFormatting.BOLD);
|
| 34 | private static final Component REPORT_SENT_TITLE = Component.translatable("gui.abuseReport.sent.title").withStyle(ChatFormatting.BOLD);
|
| 35 | private static final Component REPORT_ERROR_TITLE = Component.translatable("gui.abuseReport.error.title").withStyle(ChatFormatting.BOLD);
|
| 36 | private static final Component REPORT_SEND_GENERIC_ERROR = Component.translatable("gui.abuseReport.send.generic_error");
|
| 37 | protected static final Component SEND_REPORT = Component.translatable("gui.abuseReport.send");
|
| 38 | protected static final Component OBSERVED_WHAT_LABEL = Component.translatable("gui.abuseReport.observed_what");
|
| 39 | protected static final Component SELECT_REASON = Component.translatable("gui.abuseReport.select_reason");
|
| 40 | private static final Component DESCRIBE_PLACEHOLDER = Component.translatable("gui.abuseReport.describe");
|
| 41 | protected static final Component MORE_COMMENTS_LABEL = Component.translatable("gui.abuseReport.more_comments");
|
| 42 | private static final Component MORE_COMMENTS_NARRATION = Component.translatable("gui.abuseReport.comments");
|
| 43 | private static final Component ATTESTATION_CHECKBOX = Component.translatable("gui.abuseReport.attestation").withColor(-2039584);
|
| 44 | protected static final int BUTTON_WIDTH = 120;
|
| 45 | protected static final int MARGIN = 20;
|
| 46 | protected static final int SCREEN_WIDTH = 280;
|
| 47 | protected static final int SPACING = 8;
|
| 48 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 49 | protected final Screen lastScreen;
|
| 50 | protected final ReportingContext reportingContext;
|
| 51 | protected final LinearLayout layout = LinearLayout.vertical().spacing(8);
|
| 52 | protected B reportBuilder;
|
| 53 | private Checkbox attestation;
|
| 54 | protected Button sendButton;
|
| 55 |
|
| 56 | protected AbstractReportScreen(final Component title, final Screen lastScreen, final ReportingContext reportingContext, final B reportBuilder) {
|
| 57 | super(title);
|
| 58 | this.lastScreen = lastScreen;
|
| 59 | this.reportingContext = reportingContext;
|
| 60 | this.reportBuilder = reportBuilder;
|
| 61 | }
|
| 62 |
|
| 63 | protected MultiLineEditBox createCommentBox(final int width, final int height, final Consumer<String> valueListener) {
|
| 64 | AbuseReportLimits reportLimits = this.reportingContext.sender().reportLimits();
|
| 65 | MultiLineEditBox commentBox = MultiLineEditBox.builder().setPlaceholder(DESCRIBE_PLACEHOLDER).build(this.font, width, height, MORE_COMMENTS_NARRATION);
|
| 66 | commentBox.setValue(this.reportBuilder.comments());
|
| 67 | commentBox.setCharacterLimit(reportLimits.maxOpinionCommentsLength());
|
| 68 | commentBox.setValueListener(valueListener);
|
| 69 | return commentBox;
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void init() {
|
| 74 | this.layout.defaultCellSetting().alignHorizontallyCenter();
|
| 75 | this.createHeader();
|
| 76 | this.addContent();
|
| 77 | this.createFooter();
|
| 78 | this.onReportChanged();
|
| 79 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 80 | this.repositionElements();
|
| 81 | }
|
| 82 |
|
| 83 | protected void createHeader() {
|
| 84 | this.layout.addChild(new StringWidget(this.title, this.font));
|
| 85 | }
|
| 86 |
|
| 87 | protected abstract void addContent();
|
| 88 |
|
| 89 | protected void createFooter() {
|
| 90 | this.attestation = this.layout
|
| 91 | .addChild(
|
| 92 | Checkbox.builder(ATTESTATION_CHECKBOX, this.font)
|
| 93 | .selected(this.reportBuilder.attested())
|
| 94 | .maxWidth(280)
|
| 95 | .tooltip(Tooltip.create(ATTESTATION_CHECKBOX))
|
| 96 | .onValueChange((checkbox, value) -> {
|
| 97 | this.reportBuilder.setAttested(value);
|
| 98 | this.onReportChanged();
|
| 99 | })
|
| 100 | .build()
|
| 101 | );
|
| 102 | LinearLayout buttonsLayout = this.layout.addChild(LinearLayout.horizontal().spacing(8));
|
| 103 | buttonsLayout.addChild(Button.builder(CommonComponents.GUI_BACK, b -> this.onClose()).width(120).build());
|
| 104 | this.sendButton = buttonsLayout.addChild(Button.builder(SEND_REPORT, b -> this.sendReport()).width(120).build());
|
| 105 | }
|
| 106 |
|
| 107 | protected void onReportChanged() {
|
| 108 | Report.CannotBuildReason cannotBuildReason = this.reportBuilder.checkBuildable();
|
| 109 | this.sendButton.active = cannotBuildReason == null && this.attestation.selected();
|
| 110 | this.sendButton.setTooltip(Optionull.map(cannotBuildReason, Report.CannotBuildReason::tooltip));
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected void repositionElements() {
|
| 115 | this.layout.arrangeElements();
|
| 116 | FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
| 117 | }
|
| 118 |
|
| 119 | protected void sendReport() {
|
| 120 | this.reportBuilder.build(this.reportingContext).ifLeft(result -> {
|
| 121 | CompletableFuture<?> sendFuture = this.reportingContext.sender().send(result.id(), result.reportType(), result.report());
|
| 122 | this.minecraft.gui.setScreen(GenericWaitingScreen.createWaiting(REPORT_SENDING_TITLE, CommonComponents.GUI_CANCEL, () -> {
|
| 123 | this.minecraft.gui.setScreen(this);
|
| 124 | sendFuture.cancel(true);
|
| 125 | }));
|
| 126 | sendFuture.handleAsync((ok, throwable) -> {
|
| 127 | if (throwable == null) {
|
| 128 | this.onReportSendSuccess();
|
| 129 | } else {
|
| 130 | if (throwable instanceof CancellationException) {
|
| 131 | return null;
|
| 132 | }
|
| 133 |
|
| 134 | this.onReportSendError(throwable);
|
| 135 | }
|
| 136 |
|
| 137 | return null;
|
| 138 | }, this.minecraft);
|
| 139 | }).ifRight(reason -> this.displayReportSendError(reason.message()));
|
| 140 | }
|
| 141 |
|
| 142 | private void onReportSendSuccess() {
|
| 143 | this.clearDraft();
|
| 144 | this.minecraft
|
| 145 | .gui
|
| 146 | .setScreen(GenericWaitingScreen.createCompleted(REPORT_SENT_TITLE, REPORT_SENT_MESSAGE, CommonComponents.GUI_DONE, () -> this.minecraft.gui.setScreen(null)));
|
| 147 | }
|
| 148 |
|
| 149 | private void onReportSendError(final Throwable throwable) {
|
| 150 | LOGGER.error("Encountered error while sending abuse report", throwable);
|
| 151 | Component message;
|
| 152 | if (throwable.getCause() instanceof ThrowingComponent error) {
|
| 153 | message = error.getComponent();
|
| 154 | } else {
|
| 155 | message = REPORT_SEND_GENERIC_ERROR;
|
| 156 | }
|
| 157 |
|
| 158 | this.displayReportSendError(message);
|
| 159 | }
|
| 160 |
|
| 161 | private void displayReportSendError(final Component message) {
|
| 162 | Component styledMessage = message.copy().withStyle(ChatFormatting.RED);
|
| 163 | this.minecraft
|
| 164 | .gui
|
| 165 | .setScreen(GenericWaitingScreen.createCompleted(REPORT_ERROR_TITLE, styledMessage, CommonComponents.GUI_BACK, () -> this.minecraft.gui.setScreen(this)));
|
| 166 | }
|
| 167 |
|
| 168 | private void saveDraft() {
|
| 169 | if (this.reportBuilder.hasContent()) {
|
| 170 | this.reportingContext.setReportDraft(this.reportBuilder.report().copy());
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | private void clearDraft() {
|
| 175 | this.reportingContext.setReportDraft(null);
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public void onClose() {
|
| 180 | if (this.reportBuilder.hasContent()) {
|
| 181 | this.minecraft.gui.setScreen(new AbstractReportScreen.DiscardReportWarningScreen());
|
| 182 | } else {
|
| 183 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public void removed() {
|
| 189 | this.saveDraft();
|
| 190 | super.removed();
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | private class DiscardReportWarningScreen extends WarningScreen {
|
| 195 | private static final Component TITLE = Component.translatable("gui.abuseReport.discard.title").withStyle(ChatFormatting.BOLD);
|
| 196 | private static final Component MESSAGE = Component.translatable("gui.abuseReport.discard.content");
|
| 197 | private static final Component RETURN = Component.translatable("gui.abuseReport.discard.return");
|
| 198 | private static final Component DRAFT = Component.translatable("gui.abuseReport.discard.draft");
|
| 199 | private static final Component DISCARD = Component.translatable("gui.abuseReport.discard.discard");
|
| 200 |
|
| 201 | protected DiscardReportWarningScreen() {
|
| 202 | super(TITLE, MESSAGE, MESSAGE);
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | protected Layout addFooterButtons() {
|
| 207 | LinearLayout footer = LinearLayout.vertical().spacing(8);
|
| 208 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 209 | LinearLayout firstFooterRow = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 210 | firstFooterRow.addChild(Button.builder(RETURN, button -> this.onClose()).build());
|
| 211 | firstFooterRow.addChild(Button.builder(DRAFT, button -> {
|
| 212 | AbstractReportScreen.this.saveDraft();
|
| 213 | this.minecraft.gui.setScreen(AbstractReportScreen.this.lastScreen);
|
| 214 | }).build());
|
| 215 | footer.addChild(Button.builder(DISCARD, button -> {
|
| 216 | AbstractReportScreen.this.clearDraft();
|
| 217 | this.minecraft.gui.setScreen(AbstractReportScreen.this.lastScreen);
|
| 218 | }).build());
|
| 219 | return footer;
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public void onClose() {
|
| 224 | this.minecraft.gui.setScreen(AbstractReportScreen.this);
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public boolean shouldCloseOnEsc() {
|
| 229 | return false;
|
| 230 | }
|
| 231 | }
|
| 232 | }
|
| 233 |
|