| 1 | package com.mojang.realmsclient.gui;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 4 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 5 | import com.mojang.realmsclient.dto.RealmsSlot;
|
| 6 | import com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen;
|
| 7 | import com.mojang.realmsclient.util.RealmsTextureManager;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.gui.components.StringWidget;
|
| 14 | import net.minecraft.client.gui.components.Tooltip;
|
| 15 | import net.minecraft.client.renderer.RenderPipelines;
|
| 16 | import net.minecraft.network.chat.CommonComponents;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.network.chat.MutableComponent;
|
| 19 | import net.minecraft.resources.Identifier;
|
| 20 | import net.minecraft.util.ARGB;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class RealmsWorldSlotButton extends Button {
|
| 25 | private static final Identifier SLOT_FRAME_SPRITE = Identifier.withDefaultNamespace("widget/slot_frame");
|
| 26 | public static final Identifier EMPTY_SLOT_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/empty_frame.png");
|
| 27 | public static final Identifier DEFAULT_WORLD_SLOT_1 = Identifier.withDefaultNamespace("textures/gui/title/background/panorama_0.png");
|
| 28 | public static final Identifier DEFAULT_WORLD_SLOT_2 = Identifier.withDefaultNamespace("textures/gui/title/background/panorama_2.png");
|
| 29 | public static final Identifier DEFAULT_WORLD_SLOT_3 = Identifier.withDefaultNamespace("textures/gui/title/background/panorama_3.png");
|
| 30 | private static final Component SWITCH_TO_MINIGAME_SLOT_TOOLTIP = Component.translatable("mco.configure.world.slot.tooltip.minigame");
|
| 31 | private static final Component SWITCH_TO_WORLD_SLOT_TOOLTIP = Component.translatable("mco.configure.world.slot.tooltip");
|
| 32 | private static final Component MINIGAME = Component.translatable("mco.worldSlot.minigame");
|
| 33 | private final RealmsConfigureWorldScreen configureWorldScreen;
|
| 34 | private final int slotIndex;
|
| 35 | private final StringWidget slotNameWidget;
|
| 36 | private RealmsWorldSlotButton.State state;
|
| 37 |
|
| 38 | public RealmsWorldSlotButton(
|
| 39 | final RealmsConfigureWorldScreen configureWorldScreen,
|
| 40 | final int x,
|
| 41 | final int y,
|
| 42 | final int width,
|
| 43 | final int height,
|
| 44 | final int slotIndex,
|
| 45 | final RealmsServer serverData,
|
| 46 | final Button.OnPress onPress
|
| 47 | ) {
|
| 48 | super(x, y, width, height, CommonComponents.EMPTY, onPress, DEFAULT_NARRATION);
|
| 49 | this.configureWorldScreen = configureWorldScreen;
|
| 50 | this.slotIndex = slotIndex;
|
| 51 | this.state = this.setServerData(serverData);
|
| 52 | this.slotNameWidget = new StringWidget(Component.literal(this.state.slotName), Minecraft.getInstance().font);
|
| 53 | }
|
| 54 |
|
| 55 | public RealmsWorldSlotButton.State getState() {
|
| 56 | return this.state;
|
| 57 | }
|
| 58 |
|
| 59 | public RealmsWorldSlotButton.State setServerData(final RealmsServer serverData) {
|
| 60 | this.state = new RealmsWorldSlotButton.State(serverData, this.slotIndex);
|
| 61 | this.setTooltipAndNarration(this.state, serverData.minigameName);
|
| 62 | return this.state;
|
| 63 | }
|
| 64 |
|
| 65 | private void setTooltipAndNarration(final RealmsWorldSlotButton.State state, final @Nullable String minigameName) {
|
| 66 | Component tooltipComponent = switch (state.action) {
|
| 67 | case SWITCH_SLOT -> state.minigame ? SWITCH_TO_MINIGAME_SLOT_TOOLTIP : SWITCH_TO_WORLD_SLOT_TOOLTIP;
|
| 68 | default -> null;
|
| 69 | };
|
| 70 | if (tooltipComponent != null) {
|
| 71 | this.setTooltip(Tooltip.create(tooltipComponent));
|
| 72 | }
|
| 73 |
|
| 74 | MutableComponent slotContents = Component.literal(state.slotName);
|
| 75 | if (state.minigame && minigameName != null) {
|
| 76 | slotContents = slotContents.append(CommonComponents.SPACE).append(minigameName);
|
| 77 | }
|
| 78 |
|
| 79 | this.setMessage(slotContents);
|
| 80 | }
|
| 81 |
|
| 82 | private static RealmsWorldSlotButton.Action getAction(final boolean activeSlot, final boolean empty, final boolean expired) {
|
| 83 | return activeSlot || empty && expired ? RealmsWorldSlotButton.Action.NOTHING : RealmsWorldSlotButton.Action.SWITCH_SLOT;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public boolean isActive() {
|
| 88 | return this.state.action != RealmsWorldSlotButton.Action.NOTHING && super.isActive();
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 93 | int x = this.getX();
|
| 94 | int y = this.getY();
|
| 95 | boolean hoveredOrFocused = this.isHoveredOrFocused();
|
| 96 | Identifier texture;
|
| 97 | if (this.state.minigame) {
|
| 98 | texture = RealmsTextureManager.worldTemplate(String.valueOf(this.state.imageId), this.state.image);
|
| 99 | } else if (this.state.empty) {
|
| 100 | texture = EMPTY_SLOT_LOCATION;
|
| 101 | } else if (this.state.image != null && this.state.imageId != -1L) {
|
| 102 | texture = RealmsTextureManager.worldTemplate(String.valueOf(this.state.imageId), this.state.image);
|
| 103 | } else if (this.slotIndex == 1) {
|
| 104 | texture = DEFAULT_WORLD_SLOT_1;
|
| 105 | } else if (this.slotIndex == 2) {
|
| 106 | texture = DEFAULT_WORLD_SLOT_2;
|
| 107 | } else if (this.slotIndex == 3) {
|
| 108 | texture = DEFAULT_WORLD_SLOT_3;
|
| 109 | } else {
|
| 110 | texture = EMPTY_SLOT_LOCATION;
|
| 111 | }
|
| 112 |
|
| 113 | int color = -1;
|
| 114 | if (!this.state.activeSlot) {
|
| 115 | color = ARGB.colorFromFloat(1.0F, 0.56F, 0.56F, 0.56F);
|
| 116 | }
|
| 117 |
|
| 118 | graphics.blit(RenderPipelines.GUI_TEXTURED, texture, x + 1, y + 1, 0.0F, 0.0F, this.width - 2, this.height - 2, 74, 74, 74, 74, color);
|
| 119 | if (hoveredOrFocused && this.state.action != RealmsWorldSlotButton.Action.NOTHING) {
|
| 120 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, this.width, this.height);
|
| 121 | } else if (this.state.activeSlot) {
|
| 122 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, this.width, this.height, ARGB.colorFromFloat(1.0F, 0.8F, 0.8F, 0.8F));
|
| 123 | } else {
|
| 124 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, this.width, this.height, ARGB.colorFromFloat(1.0F, 0.56F, 0.56F, 0.56F));
|
| 125 | }
|
| 126 |
|
| 127 | if (this.state.hardcore) {
|
| 128 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, RealmsMainScreen.HARDCORE_MODE_SPRITE, x + 3, y + 4, 9, 8);
|
| 129 | }
|
| 130 |
|
| 131 | this.slotNameWidget.setMaxWidth(this.getWidth() - (this.state.activeSlot ? 2 : 0), StringWidget.TextOverflow.SCROLLING);
|
| 132 | this.slotNameWidget.setPosition(this.getX() + this.getWidth() / 2 - this.slotNameWidget.getWidth() / 2, y + this.height - 14);
|
| 133 | this.slotNameWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 134 | if (this.state.activeSlot) {
|
| 135 | graphics.centeredText(
|
| 136 | Minecraft.getInstance().font,
|
| 137 | RealmsMainScreen.getVersionComponent(this.state.slotVersion, this.state.compatibility.isCompatible()),
|
| 138 | x + this.width / 2,
|
| 139 | y + this.height + 2,
|
| 140 | -1
|
| 141 | );
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | protected void extractTooltipForNextRenderPass(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 147 | if (Minecraft.getInstance().gui.screen() == this.configureWorldScreen) {
|
| 148 | super.extractTooltipForNextRenderPass(graphics, mouseX, mouseY);
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | public void updateSlotState(final RealmsServer serverData) {
|
| 153 | this.state = this.setServerData(serverData);
|
| 154 | this.slotNameWidget.setMessage(Component.literal(this.state.slotName));
|
| 155 | }
|
| 156 |
|
| 157 | @Environment(EnvType.CLIENT)
|
| 158 | public enum Action {
|
| 159 | NOTHING,
|
| 160 | SWITCH_SLOT;
|
| 161 | }
|
| 162 |
|
| 163 | @Environment(EnvType.CLIENT)
|
| 164 | public static class State {
|
| 165 | private final String slotName;
|
| 166 | private final String slotVersion;
|
| 167 | private final RealmsServer.Compatibility compatibility;
|
| 168 | private final long imageId;
|
| 169 | private final @Nullable String image;
|
| 170 | public final boolean empty;
|
| 171 | public final boolean minigame;
|
| 172 | public final RealmsWorldSlotButton.Action action;
|
| 173 | public final boolean hardcore;
|
| 174 | public final boolean activeSlot;
|
| 175 |
|
| 176 | public State(final RealmsServer serverData, final int slotIndex) {
|
| 177 | this.minigame = slotIndex == 4;
|
| 178 | if (this.minigame) {
|
| 179 | this.slotName = RealmsWorldSlotButton.MINIGAME.getString();
|
| 180 | this.imageId = serverData.minigameId;
|
| 181 | this.image = serverData.minigameImage;
|
| 182 | this.empty = serverData.minigameId == -1;
|
| 183 | this.slotVersion = "";
|
| 184 | this.compatibility = RealmsServer.Compatibility.UNVERIFIABLE;
|
| 185 | this.hardcore = false;
|
| 186 | this.activeSlot = serverData.isMinigameActive();
|
| 187 | } else {
|
| 188 | RealmsSlot slot = serverData.slots.get(slotIndex);
|
| 189 | this.slotName = slot.options.getSlotName(slotIndex);
|
| 190 | this.imageId = slot.options.templateId;
|
| 191 | this.image = slot.options.templateImage;
|
| 192 | this.empty = slot.options.empty;
|
| 193 | this.slotVersion = slot.options.version;
|
| 194 | this.compatibility = slot.options.compatibility;
|
| 195 | this.hardcore = slot.isHardcore();
|
| 196 | this.activeSlot = serverData.activeSlot == slotIndex && !serverData.isMinigameActive();
|
| 197 | }
|
| 198 |
|
| 199 | this.action = RealmsWorldSlotButton.getAction(this.activeSlot, this.empty, serverData.expired);
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|