| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.client.RealmsError;
|
| 4 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.TextAlignment;
|
| 10 | import net.minecraft.client.gui.components.Button;
|
| 11 | import net.minecraft.client.gui.components.MultiLineLabel;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.ComponentUtils;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import net.minecraft.realms.RealmsScreen;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class RealmsGenericErrorScreen extends RealmsScreen {
|
| 21 | private static final Component GENERIC_TITLE = Component.translatable("mco.errorMessage.generic");
|
| 22 | private final Screen nextScreen;
|
| 23 | private final Component detail;
|
| 24 | private MultiLineLabel splitDetail = MultiLineLabel.EMPTY;
|
| 25 |
|
| 26 | public RealmsGenericErrorScreen(final RealmsServiceException realmsServiceException, final Screen nextScreen) {
|
| 27 | this(RealmsGenericErrorScreen.ErrorMessage.forServiceError(realmsServiceException), nextScreen);
|
| 28 | }
|
| 29 |
|
| 30 | public RealmsGenericErrorScreen(final Component message, final Screen nextScreen) {
|
| 31 | this(new RealmsGenericErrorScreen.ErrorMessage(GENERIC_TITLE, message), nextScreen);
|
| 32 | }
|
| 33 |
|
| 34 | public RealmsGenericErrorScreen(final Component title, final Component message, final Screen nextScreen) {
|
| 35 | this(new RealmsGenericErrorScreen.ErrorMessage(title, message), nextScreen);
|
| 36 | }
|
| 37 |
|
| 38 | private RealmsGenericErrorScreen(final RealmsGenericErrorScreen.ErrorMessage message, final Screen nextScreen) {
|
| 39 | super(message.title);
|
| 40 | this.nextScreen = nextScreen;
|
| 41 | this.detail = ComponentUtils.mergeStyles(message.detail, Style.EMPTY.withColor(-2142128));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void init() {
|
| 46 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_OK, button -> this.onClose()).bounds(this.width / 2 - 100, this.height - 52, 200, 20).build());
|
| 47 | this.splitDetail = MultiLineLabel.create(this.font, this.detail, this.width * 3 / 4);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void onClose() {
|
| 52 | this.minecraft.gui.setScreen(this.nextScreen);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Component getNarrationMessage() {
|
| 57 | return CommonComponents.joinForNarration(super.getNarrationMessage(), this.detail);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 62 | super.extractRenderState(graphics, xm, ym, a);
|
| 63 | graphics.centeredText(this.font, this.title, this.width / 2, 80, -1);
|
| 64 | ActiveTextCollector textRenderer = graphics.textRenderer();
|
| 65 | this.splitDetail.visitLines(TextAlignment.CENTER, this.width / 2, 100, 9, textRenderer);
|
| 66 | }
|
| 67 |
|
| 68 | @Environment(EnvType.CLIENT)
|
| 69 | private record ErrorMessage(Component title, Component detail) {
|
| 70 | private static RealmsGenericErrorScreen.ErrorMessage forServiceError(final RealmsServiceException realmsServiceException) {
|
| 71 | RealmsError errorDetails = realmsServiceException.realmsError;
|
| 72 | return new RealmsGenericErrorScreen.ErrorMessage(
|
| 73 | Component.translatable("mco.errorMessage.realmsService.realmsError", errorDetails.errorCode()), errorDetails.errorMessage()
|
| 74 | );
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|