| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.realmsclient.client.RealmsClient;
|
| 7 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 8 | import com.mojang.realmsclient.dto.WorldTemplate;
|
| 9 | import com.mojang.realmsclient.dto.WorldTemplatePaginatedList;
|
| 10 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 11 | import com.mojang.realmsclient.util.RealmsTextureManager;
|
| 12 | import com.mojang.realmsclient.util.TextRenderingUtils;
|
| 13 | import java.net.URI;
|
| 14 | import java.util.Collections;
|
| 15 | import java.util.List;
|
| 16 | import java.util.function.Consumer;
|
| 17 | import java.util.stream.Collectors;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.client.Minecraft;
|
| 21 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 22 | import net.minecraft.client.gui.components.Button;
|
| 23 | import net.minecraft.client.gui.components.ImageButton;
|
| 24 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 25 | import net.minecraft.client.gui.components.StringWidget;
|
| 26 | import net.minecraft.client.gui.components.Tooltip;
|
| 27 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 28 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 29 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 30 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 31 | import net.minecraft.client.input.MouseButtonEvent;
|
| 32 | import net.minecraft.client.renderer.RenderPipelines;
|
| 33 | import net.minecraft.client.resources.language.I18n;
|
| 34 | import net.minecraft.network.chat.CommonComponents;
|
| 35 | import net.minecraft.network.chat.Component;
|
| 36 | import net.minecraft.realms.RealmsScreen;
|
| 37 | import net.minecraft.resources.Identifier;
|
| 38 | import net.minecraft.util.CommonLinks;
|
| 39 | import net.minecraft.util.Util;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 | import org.slf4j.Logger;
|
| 42 |
|
| 43 | @Environment(EnvType.CLIENT)
|
| 44 | public class RealmsSelectWorldTemplateScreen extends RealmsScreen {
|
| 45 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 46 | private static final Identifier SLOT_FRAME_SPRITE = Identifier.withDefaultNamespace("widget/slot_frame");
|
| 47 | private static final Component SELECT_BUTTON_NAME = Component.translatable("mco.template.button.select");
|
| 48 | private static final Component TRAILER_BUTTON_NAME = Component.translatable("mco.template.button.trailer");
|
| 49 | private static final Component PUBLISHER_BUTTON_NAME = Component.translatable("mco.template.button.publisher");
|
| 50 | private static final int BUTTON_WIDTH = 100;
|
| 51 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 52 | private final Consumer<WorldTemplate> callback;
|
| 53 | private RealmsSelectWorldTemplateScreen.WorldTemplateList worldTemplateList;
|
| 54 | private final RealmsServer.WorldType worldType;
|
| 55 | private final List<Component> subtitle;
|
| 56 | private Button selectButton;
|
| 57 | private Button trailerButton;
|
| 58 | private Button publisherButton;
|
| 59 | private @Nullable WorldTemplate selectedTemplate = null;
|
| 60 | private @Nullable URI currentLink;
|
| 61 | private @Nullable List<TextRenderingUtils.Line> noTemplatesMessage;
|
| 62 |
|
| 63 | public RealmsSelectWorldTemplateScreen(
|
| 64 | final Component title,
|
| 65 | final Consumer<WorldTemplate> callback,
|
| 66 | final RealmsServer.WorldType worldType,
|
| 67 | final @Nullable WorldTemplatePaginatedList alreadyFetched
|
| 68 | ) {
|
| 69 | this(title, callback, worldType, alreadyFetched, List.of());
|
| 70 | }
|
| 71 |
|
| 72 | public RealmsSelectWorldTemplateScreen(
|
| 73 | final Component title,
|
| 74 | final Consumer<WorldTemplate> callback,
|
| 75 | final RealmsServer.WorldType worldType,
|
| 76 | final @Nullable WorldTemplatePaginatedList alreadyFetched,
|
| 77 | final List<Component> subtitle
|
| 78 | ) {
|
| 79 | super(title);
|
| 80 | this.callback = callback;
|
| 81 | this.worldType = worldType;
|
| 82 | if (alreadyFetched == null) {
|
| 83 | this.worldTemplateList = new RealmsSelectWorldTemplateScreen.WorldTemplateList();
|
| 84 | this.fetchTemplatesAsync(new WorldTemplatePaginatedList(10));
|
| 85 | } else {
|
| 86 | this.worldTemplateList = new RealmsSelectWorldTemplateScreen.WorldTemplateList(Lists.newArrayList(alreadyFetched.templates()));
|
| 87 | this.fetchTemplatesAsync(alreadyFetched);
|
| 88 | }
|
| 89 |
|
| 90 | this.subtitle = subtitle;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void init() {
|
| 95 | this.layout.setHeaderHeight(33 + this.subtitle.size() * (9 + 4));
|
| 96 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(4));
|
| 97 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 98 | header.addChild(new StringWidget(this.title, this.font));
|
| 99 | this.subtitle.forEach(warning -> header.addChild(new StringWidget(warning, this.font)));
|
| 100 | this.worldTemplateList = this.layout.addToContents(new RealmsSelectWorldTemplateScreen.WorldTemplateList(this.worldTemplateList.getTemplates()));
|
| 101 | LinearLayout bottomButtons = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 102 | bottomButtons.defaultCellSetting().alignHorizontallyCenter();
|
| 103 | this.trailerButton = bottomButtons.addChild(Button.builder(TRAILER_BUTTON_NAME, button -> this.onTrailer()).width(100).build());
|
| 104 | this.selectButton = bottomButtons.addChild(Button.builder(SELECT_BUTTON_NAME, button -> this.selectTemplate()).width(100).build());
|
| 105 | bottomButtons.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).width(100).build());
|
| 106 | this.publisherButton = bottomButtons.addChild(Button.builder(PUBLISHER_BUTTON_NAME, button -> this.onPublish()).width(100).build());
|
| 107 | this.updateButtonStates();
|
| 108 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 109 | this.repositionElements();
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected void repositionElements() {
|
| 114 | this.worldTemplateList.updateSize(this.width, this.layout);
|
| 115 | this.layout.arrangeElements();
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public Component getNarrationMessage() {
|
| 120 | List<Component> parts = Lists.newArrayListWithCapacity(2);
|
| 121 | parts.add(this.title);
|
| 122 | parts.addAll(this.subtitle);
|
| 123 | return CommonComponents.joinLines(parts);
|
| 124 | }
|
| 125 |
|
| 126 | private void updateButtonStates() {
|
| 127 | this.publisherButton.visible = this.selectedTemplate != null && this.selectedTemplate.link() != null;
|
| 128 | this.trailerButton.visible = this.selectedTemplate != null && this.selectedTemplate.trailer() != null;
|
| 129 | this.selectButton.active = this.selectedTemplate != null;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public void onClose() {
|
| 134 | this.callback.accept(null);
|
| 135 | }
|
| 136 |
|
| 137 | private void selectTemplate() {
|
| 138 | if (this.selectedTemplate != null) {
|
| 139 | this.callback.accept(this.selectedTemplate);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private void onTrailer() {
|
| 144 | if (this.selectedTemplate != null && this.selectedTemplate.trailer() != null) {
|
| 145 | ConfirmLinkScreen.confirmLinkNow(this, this.selectedTemplate.trailer());
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | private void onPublish() {
|
| 150 | if (this.selectedTemplate != null && this.selectedTemplate.link() != null) {
|
| 151 | ConfirmLinkScreen.confirmLinkNow(this, this.selectedTemplate.link());
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | private void fetchTemplatesAsync(final WorldTemplatePaginatedList startPage) {
|
| 156 | Util.nonCriticalIoPool()
|
| 157 | .execute(
|
| 158 | () -> {
|
| 159 | WorldTemplatePaginatedList page = startPage;
|
| 160 | RealmsClient client = RealmsClient.getOrCreate();
|
| 161 |
|
| 162 | while (page != null) {
|
| 163 | Either<WorldTemplatePaginatedList, Exception> result = this.fetchTemplates(page, client);
|
| 164 | page = this.minecraft
|
| 165 | .<WorldTemplatePaginatedList>submit(
|
| 166 | () -> {
|
| 167 | if (result.right().isPresent()) {
|
| 168 | LOGGER.error("Couldn't fetch templates", result.right().get());
|
| 169 | if (this.worldTemplateList.isEmpty()) {
|
| 170 | this.noTemplatesMessage = TextRenderingUtils.decompose(I18n.get("mco.template.select.failure"));
|
| 171 | }
|
| 172 |
|
| 173 | return null;
|
| 174 | } else {
|
| 175 | WorldTemplatePaginatedList currentPage = result.left().get();
|
| 176 |
|
| 177 | for (WorldTemplate template : currentPage.templates()) {
|
| 178 | this.worldTemplateList.addEntry(template);
|
| 179 | }
|
| 180 |
|
| 181 | if (currentPage.templates().isEmpty()) {
|
| 182 | if (this.worldTemplateList.isEmpty()) {
|
| 183 | String withoutLink = I18n.get("mco.template.select.none", "%link");
|
| 184 | TextRenderingUtils.LineSegment link = TextRenderingUtils.LineSegment.link(
|
| 185 | I18n.get("mco.template.select.none.linkTitle"), CommonLinks.REALMS_CONTENT_CREATION
|
| 186 | );
|
| 187 | this.noTemplatesMessage = TextRenderingUtils.decompose(withoutLink, link);
|
| 188 | }
|
| 189 |
|
| 190 | return null;
|
| 191 | } else {
|
| 192 | return currentPage;
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 | )
|
| 197 | .join();
|
| 198 | }
|
| 199 | }
|
| 200 | );
|
| 201 | }
|
| 202 |
|
| 203 | private Either<WorldTemplatePaginatedList, Exception> fetchTemplates(final WorldTemplatePaginatedList paginatedList, final RealmsClient client) {
|
| 204 | try {
|
| 205 | return Either.left(client.fetchWorldTemplates(paginatedList.page() + 1, paginatedList.size(), this.worldType));
|
| 206 | } catch (RealmsServiceException e) {
|
| 207 | return Either.right(e);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 213 | super.extractRenderState(graphics, xm, ym, a);
|
| 214 | this.currentLink = null;
|
| 215 | if (this.noTemplatesMessage != null) {
|
| 216 | this.extractMultilineMessage(graphics, xm, ym, this.noTemplatesMessage);
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | private void extractMultilineMessage(final GuiGraphicsExtractor graphics, final int xm, final int ym, final List<TextRenderingUtils.Line> noTemplatesMessage) {
|
| 221 | for (int i = 0; i < noTemplatesMessage.size(); i++) {
|
| 222 | TextRenderingUtils.Line line = noTemplatesMessage.get(i);
|
| 223 | int lineY = row(4 + i);
|
| 224 | int lineWidth = line.segments.stream().mapToInt(s -> this.font.width(s.renderedText())).sum();
|
| 225 | int startX = this.width / 2 - lineWidth / 2;
|
| 226 |
|
| 227 | for (TextRenderingUtils.LineSegment segment : line.segments) {
|
| 228 | int color = segment.isLink() ? -13408581 : -1;
|
| 229 | String text = segment.renderedText();
|
| 230 | graphics.text(this.font, text, startX, lineY, color);
|
| 231 | int endX = startX + this.font.width(text);
|
| 232 | if (segment.isLink() && xm > startX && xm < endX && ym > lineY - 3 && ym < lineY + 8) {
|
| 233 | URI linkUrl = segment.getLinkUrl();
|
| 234 | graphics.setTooltipForNextFrame(Component.literal(linkUrl.toString()), xm, ym);
|
| 235 | this.currentLink = linkUrl;
|
| 236 | }
|
| 237 |
|
| 238 | startX = endX;
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | @Environment(EnvType.CLIENT)
|
| 244 | private class Entry extends ObjectSelectionList.Entry<RealmsSelectWorldTemplateScreen.Entry> {
|
| 245 | private static final WidgetSprites WEBSITE_LINK_SPRITES = new WidgetSprites(
|
| 246 | Identifier.withDefaultNamespace("icon/link"), Identifier.withDefaultNamespace("icon/link_highlighted")
|
| 247 | );
|
| 248 | private static final WidgetSprites TRAILER_LINK_SPRITES = new WidgetSprites(
|
| 249 | Identifier.withDefaultNamespace("icon/video_link"), Identifier.withDefaultNamespace("icon/video_link_highlighted")
|
| 250 | );
|
| 251 | private static final Component PUBLISHER_LINK_TOOLTIP = Component.translatable("mco.template.info.tooltip");
|
| 252 | private static final Component TRAILER_LINK_TOOLTIP = Component.translatable("mco.template.trailer.tooltip");
|
| 253 | public final WorldTemplate template;
|
| 254 | private @Nullable ImageButton websiteButton;
|
| 255 | private @Nullable ImageButton trailerButton;
|
| 256 |
|
| 257 | public Entry(final WorldTemplate template) {
|
| 258 | this.template = template;
|
| 259 | if (template.link() != null) {
|
| 260 | this.websiteButton = new ImageButton(
|
| 261 | 15, 15, WEBSITE_LINK_SPRITES, ConfirmLinkScreen.confirmLink(RealmsSelectWorldTemplateScreen.this, template.link()), PUBLISHER_LINK_TOOLTIP
|
| 262 | );
|
| 263 | this.websiteButton.setTooltip(Tooltip.create(PUBLISHER_LINK_TOOLTIP));
|
| 264 | }
|
| 265 |
|
| 266 | if (template.trailer() != null) {
|
| 267 | this.trailerButton = new ImageButton(
|
| 268 | 15, 15, TRAILER_LINK_SPRITES, ConfirmLinkScreen.confirmLink(RealmsSelectWorldTemplateScreen.this, template.trailer()), TRAILER_LINK_TOOLTIP
|
| 269 | );
|
| 270 | this.trailerButton.setTooltip(Tooltip.create(TRAILER_LINK_TOOLTIP));
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | @Override
|
| 275 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 276 | RealmsSelectWorldTemplateScreen.this.selectedTemplate = this.template;
|
| 277 | RealmsSelectWorldTemplateScreen.this.updateButtonStates();
|
| 278 | if (doubleClick && this.isFocused()) {
|
| 279 | RealmsSelectWorldTemplateScreen.this.callback.accept(this.template);
|
| 280 | }
|
| 281 |
|
| 282 | if (this.websiteButton != null) {
|
| 283 | this.websiteButton.mouseClicked(event, doubleClick);
|
| 284 | }
|
| 285 |
|
| 286 | if (this.trailerButton != null) {
|
| 287 | this.trailerButton.mouseClicked(event, doubleClick);
|
| 288 | }
|
| 289 |
|
| 290 | return super.mouseClicked(event, doubleClick);
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 295 | graphics.blit(
|
| 296 | RenderPipelines.GUI_TEXTURED,
|
| 297 | RealmsTextureManager.worldTemplate(this.template.id(), this.template.image()),
|
| 298 | this.getContentX() + 1,
|
| 299 | this.getContentY() + 1 + 1,
|
| 300 | 0.0F,
|
| 301 | 0.0F,
|
| 302 | 38,
|
| 303 | 38,
|
| 304 | 38,
|
| 305 | 38
|
| 306 | );
|
| 307 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, RealmsSelectWorldTemplateScreen.SLOT_FRAME_SPRITE, this.getContentX(), this.getContentY() + 1, 40, 40);
|
| 308 | int padding = 5;
|
| 309 | int versionTextWidth = RealmsSelectWorldTemplateScreen.this.font.width(this.template.version());
|
| 310 | if (this.websiteButton != null) {
|
| 311 | this.websiteButton.setPosition(this.getContentRight() - versionTextWidth - this.websiteButton.getWidth() - 10, this.getContentY());
|
| 312 | this.websiteButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 313 | }
|
| 314 |
|
| 315 | if (this.trailerButton != null) {
|
| 316 | this.trailerButton.setPosition(this.getContentRight() - versionTextWidth - this.trailerButton.getWidth() * 2 - 15, this.getContentY());
|
| 317 | this.trailerButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 318 | }
|
| 319 |
|
| 320 | int textX = this.getContentX() + 45 + 20;
|
| 321 | int textY = this.getContentY() + 5;
|
| 322 | graphics.text(RealmsSelectWorldTemplateScreen.this.font, this.template.name(), textX, textY, -1);
|
| 323 | graphics.text(RealmsSelectWorldTemplateScreen.this.font, this.template.version(), this.getContentRight() - versionTextWidth - 5, textY, -6250336);
|
| 324 | graphics.text(RealmsSelectWorldTemplateScreen.this.font, this.template.author(), textX, textY + 9 + 5, -6250336);
|
| 325 | if (!this.template.recommendedPlayers().isBlank()) {
|
| 326 | graphics.text(RealmsSelectWorldTemplateScreen.this.font, this.template.recommendedPlayers(), textX, this.getContentBottom() - 9 / 2 - 5, -8355712);
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public Component getNarration() {
|
| 332 | Component entryName = CommonComponents.joinLines(
|
| 333 | Component.literal(this.template.name()),
|
| 334 | Component.translatable("mco.template.select.narrate.authors", this.template.author()),
|
| 335 | Component.literal(this.template.recommendedPlayers()),
|
| 336 | Component.translatable("mco.template.select.narrate.version", this.template.version())
|
| 337 | );
|
| 338 | return Component.translatable("narrator.select", entryName);
|
| 339 | }
|
| 340 | }
|
| 341 |
|
| 342 | @Environment(EnvType.CLIENT)
|
| 343 | private class WorldTemplateList extends ObjectSelectionList<RealmsSelectWorldTemplateScreen.Entry> {
|
| 344 | public WorldTemplateList() {
|
| 345 | this(Collections.emptyList());
|
| 346 | }
|
| 347 |
|
| 348 | public WorldTemplateList(final Iterable<WorldTemplate> templates) {
|
| 349 | super(
|
| 350 | Minecraft.getInstance(),
|
| 351 | RealmsSelectWorldTemplateScreen.this.width,
|
| 352 | RealmsSelectWorldTemplateScreen.this.layout.getContentHeight(),
|
| 353 | RealmsSelectWorldTemplateScreen.this.layout.getHeaderHeight(),
|
| 354 | 46
|
| 355 | );
|
| 356 | templates.forEach(this::addEntry);
|
| 357 | }
|
| 358 |
|
| 359 | public void addEntry(final WorldTemplate template) {
|
| 360 | this.addEntry(RealmsSelectWorldTemplateScreen.this.new Entry(template));
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 365 | if (RealmsSelectWorldTemplateScreen.this.currentLink != null) {
|
| 366 | ConfirmLinkScreen.confirmLinkNow(RealmsSelectWorldTemplateScreen.this, RealmsSelectWorldTemplateScreen.this.currentLink);
|
| 367 | return true;
|
| 368 | } else {
|
| 369 | return super.mouseClicked(event, doubleClick);
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | public void setSelected(final RealmsSelectWorldTemplateScreen.@Nullable Entry selected) {
|
| 374 | super.setSelected(selected);
|
| 375 | RealmsSelectWorldTemplateScreen.this.selectedTemplate = selected == null ? null : selected.template;
|
| 376 | RealmsSelectWorldTemplateScreen.this.updateButtonStates();
|
| 377 | }
|
| 378 |
|
| 379 | @Override
|
| 380 | public int getRowWidth() {
|
| 381 | return 300;
|
| 382 | }
|
| 383 |
|
| 384 | public boolean isEmpty() {
|
| 385 | return this.getItemCount() == 0;
|
| 386 | }
|
| 387 |
|
| 388 | public List<WorldTemplate> getTemplates() {
|
| 389 | return this.children().stream().map(c -> c.template).collect(Collectors.toList());
|
| 390 | }
|
| 391 | }
|
| 392 | }
|
| 393 |
|