| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.blaze3d.Blaze3D;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.realmsclient.util.JsonUtils;
|
| 8 | import java.net.URI;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.List;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.gui.components.Button;
|
| 17 | import net.minecraft.client.gui.components.PopupScreen;
|
| 18 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 19 | import net.minecraft.client.gui.screens.Screen;
|
| 20 | import net.minecraft.network.chat.CommonComponents;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import net.minecraft.util.LenientJsonParser;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 | import org.slf4j.Logger;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class RealmsNotification {
|
| 29 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 30 | private static final String NOTIFICATION_UUID = "notificationUuid";
|
| 31 | private static final String DISMISSABLE = "dismissable";
|
| 32 | private static final String SEEN = "seen";
|
| 33 | private static final String TYPE = "type";
|
| 34 | private static final String VISIT_URL = "visitUrl";
|
| 35 | private static final String INFO_POPUP = "infoPopup";
|
| 36 | private static final Component BUTTON_TEXT_FALLBACK = Component.translatable("mco.notification.visitUrl.buttonText.default");
|
| 37 | private final UUID uuid;
|
| 38 | private final boolean dismissable;
|
| 39 | private final boolean seen;
|
| 40 | private final String type;
|
| 41 |
|
| 42 | private RealmsNotification(final UUID uuid, final boolean dismissable, final boolean seen, final String type) {
|
| 43 | this.uuid = uuid;
|
| 44 | this.dismissable = dismissable;
|
| 45 | this.seen = seen;
|
| 46 | this.type = type;
|
| 47 | }
|
| 48 |
|
| 49 | public boolean seen() {
|
| 50 | return this.seen;
|
| 51 | }
|
| 52 |
|
| 53 | public boolean dismissable() {
|
| 54 | return this.dismissable;
|
| 55 | }
|
| 56 |
|
| 57 | public UUID uuid() {
|
| 58 | return this.uuid;
|
| 59 | }
|
| 60 |
|
| 61 | public static List<RealmsNotification> parseList(final String json) {
|
| 62 | List<RealmsNotification> result = new ArrayList<>();
|
| 63 |
|
| 64 | try {
|
| 65 | for (JsonElement element : LenientJsonParser.parse(json).getAsJsonObject().get("notifications").getAsJsonArray()) {
|
| 66 | result.add(parse(element.getAsJsonObject()));
|
| 67 | }
|
| 68 | } catch (Exception e) {
|
| 69 | LOGGER.error("Could not parse list of RealmsNotifications", e);
|
| 70 | }
|
| 71 |
|
| 72 | return result;
|
| 73 | }
|
| 74 |
|
| 75 | private static RealmsNotification parse(final JsonObject jsonObject) {
|
| 76 | UUID uuid = JsonUtils.getUuidOr("notificationUuid", jsonObject, null);
|
| 77 | if (uuid == null) {
|
| 78 | throw new IllegalStateException("Missing required property notificationUuid");
|
| 79 | }
|
| 80 |
|
| 81 | boolean dismissable = JsonUtils.getBooleanOr("dismissable", jsonObject, true);
|
| 82 | boolean seen = JsonUtils.getBooleanOr("seen", jsonObject, false);
|
| 83 | String type = JsonUtils.getRequiredString("type", jsonObject);
|
| 84 | RealmsNotification base = new RealmsNotification(uuid, dismissable, seen, type);
|
| 85 |
|
| 86 | return switch (type) {
|
| 87 | case "visitUrl" -> RealmsNotification.VisitUrl.parse(base, jsonObject);
|
| 88 | case "infoPopup" -> RealmsNotification.InfoPopup.parse(base, jsonObject);
|
| 89 | default -> base;
|
| 90 | };
|
| 91 | }
|
| 92 |
|
| 93 | @Environment(EnvType.CLIENT)
|
| 94 | public static class InfoPopup extends RealmsNotification {
|
| 95 | private static final String TITLE = "title";
|
| 96 | private static final String MESSAGE = "message";
|
| 97 | private static final String IMAGE = "image";
|
| 98 | private static final String URL_BUTTON = "urlButton";
|
| 99 | private final RealmsText title;
|
| 100 | private final RealmsText message;
|
| 101 | private final Identifier image;
|
| 102 | private final RealmsNotification.@Nullable UrlButton urlButton;
|
| 103 |
|
| 104 | private InfoPopup(
|
| 105 | final RealmsNotification base,
|
| 106 | final RealmsText title,
|
| 107 | final RealmsText message,
|
| 108 | final Identifier image,
|
| 109 | final RealmsNotification.@Nullable UrlButton urlButton
|
| 110 | ) {
|
| 111 | super(base.uuid, base.dismissable, base.seen, base.type);
|
| 112 | this.title = title;
|
| 113 | this.message = message;
|
| 114 | this.image = image;
|
| 115 | this.urlButton = urlButton;
|
| 116 | }
|
| 117 |
|
| 118 | public static RealmsNotification.InfoPopup parse(final RealmsNotification base, final JsonObject object) {
|
| 119 | RealmsText title = JsonUtils.getRequired("title", object, RealmsText::parse);
|
| 120 | RealmsText message = JsonUtils.getRequired("message", object, RealmsText::parse);
|
| 121 | Identifier image = Identifier.parse(JsonUtils.getRequiredString("image", object));
|
| 122 | RealmsNotification.UrlButton urlButton = JsonUtils.getOptional("urlButton", object, RealmsNotification.UrlButton::parse);
|
| 123 | return new RealmsNotification.InfoPopup(base, title, message, image, urlButton);
|
| 124 | }
|
| 125 |
|
| 126 | public @Nullable PopupScreen buildScreen(final Screen parentScreen, final Consumer<UUID> dismiss) {
|
| 127 | Component title = this.title.createComponent();
|
| 128 | if (title == null) {
|
| 129 | RealmsNotification.LOGGER.warn("Realms info popup had title with no available translation: {}", this.title);
|
| 130 | return null;
|
| 131 | }
|
| 132 |
|
| 133 | PopupScreen.Builder builder = new PopupScreen.Builder(parentScreen, title)
|
| 134 | .setImage(this.image)
|
| 135 | .addMessage(this.message.createComponent(CommonComponents.EMPTY));
|
| 136 | if (this.urlButton != null) {
|
| 137 | builder.addButton(this.urlButton.urlText.createComponent(RealmsNotification.BUTTON_TEXT_FALLBACK), popup -> {
|
| 138 | Minecraft minecraft = Minecraft.getInstance();
|
| 139 | minecraft.gui.setScreen(new ConfirmLinkScreen(result -> {
|
| 140 | if (result) {
|
| 141 | Blaze3D.openUri(this.urlButton.url);
|
| 142 | minecraft.gui.setScreen(parentScreen);
|
| 143 | } else {
|
| 144 | minecraft.gui.setScreen(popup);
|
| 145 | }
|
| 146 | }, this.urlButton.url, true));
|
| 147 | dismiss.accept(this.uuid());
|
| 148 | });
|
| 149 | }
|
| 150 |
|
| 151 | builder.addButton(CommonComponents.GUI_OK, popup -> {
|
| 152 | popup.onClose();
|
| 153 | dismiss.accept(this.uuid());
|
| 154 | });
|
| 155 | builder.onClose(() -> dismiss.accept(this.uuid()));
|
| 156 | return builder.build();
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | @Environment(EnvType.CLIENT)
|
| 161 | private record UrlButton(URI url, RealmsText urlText) {
|
| 162 | private static final String URL = "url";
|
| 163 | private static final String URL_TEXT = "urlText";
|
| 164 |
|
| 165 | public static RealmsNotification.UrlButton parse(final JsonObject jsonObject) {
|
| 166 | URI url = URI.create(JsonUtils.getRequiredString("url", jsonObject));
|
| 167 | RealmsText urlText = JsonUtils.getRequired("urlText", jsonObject, RealmsText::parse);
|
| 168 | return new RealmsNotification.UrlButton(url, urlText);
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Environment(EnvType.CLIENT)
|
| 173 | public static class VisitUrl extends RealmsNotification {
|
| 174 | private static final String URL = "url";
|
| 175 | private static final String BUTTON_TEXT = "buttonText";
|
| 176 | private static final String MESSAGE = "message";
|
| 177 | private final URI url;
|
| 178 | private final RealmsText buttonText;
|
| 179 | private final RealmsText message;
|
| 180 |
|
| 181 | private VisitUrl(final RealmsNotification base, final URI url, final RealmsText buttonText, final RealmsText message) {
|
| 182 | super(base.uuid, base.dismissable, base.seen, base.type);
|
| 183 | this.url = url;
|
| 184 | this.buttonText = buttonText;
|
| 185 | this.message = message;
|
| 186 | }
|
| 187 |
|
| 188 | public static RealmsNotification.VisitUrl parse(final RealmsNotification base, final JsonObject jsonObject) {
|
| 189 | URI url = JsonUtils.getRequiredUri("url", jsonObject);
|
| 190 | RealmsText buttonText = JsonUtils.getRequired("buttonText", jsonObject, RealmsText::parse);
|
| 191 | RealmsText message = JsonUtils.getRequired("message", jsonObject, RealmsText::parse);
|
| 192 | return new RealmsNotification.VisitUrl(base, url, buttonText, message);
|
| 193 | }
|
| 194 |
|
| 195 | public Component getMessage() {
|
| 196 | return this.message.createComponent(Component.translatable("mco.notification.visitUrl.message.default"));
|
| 197 | }
|
| 198 |
|
| 199 | public Button buildOpenLinkButton(final Screen parentScreen) {
|
| 200 | Component buttonLabel = this.buttonText.createComponent(RealmsNotification.BUTTON_TEXT_FALLBACK);
|
| 201 | return Button.builder(buttonLabel, ConfirmLinkScreen.confirmLink(parentScreen, this.url)).build();
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 |
|