| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.gson.JsonArray;
|
| 5 | import com.google.gson.JsonElement;
|
| 6 | import com.google.gson.JsonObject;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 10 | import java.io.BufferedReader;
|
| 11 | import java.io.IOException;
|
| 12 | import java.io.Reader;
|
| 13 | import java.util.List;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.ChatFormatting;
|
| 17 | import net.minecraft.client.GameNarrator;
|
| 18 | import net.minecraft.client.Minecraft;
|
| 19 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 20 | import net.minecraft.client.gui.components.LogoRenderer;
|
| 21 | import net.minecraft.client.gui.render.TextureSetup;
|
| 22 | import net.minecraft.client.input.KeyEvent;
|
| 23 | import net.minecraft.client.renderer.RenderPipelines;
|
| 24 | import net.minecraft.client.renderer.blockentity.AbstractEndPortalRenderer;
|
| 25 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 26 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 27 | import net.minecraft.network.chat.CommonComponents;
|
| 28 | import net.minecraft.network.chat.Component;
|
| 29 | import net.minecraft.resources.Identifier;
|
| 30 | import net.minecraft.sounds.Music;
|
| 31 | import net.minecraft.sounds.Musics;
|
| 32 | import net.minecraft.util.FormattedCharSequence;
|
| 33 | import net.minecraft.util.GsonHelper;
|
| 34 | import net.minecraft.util.RandomSource;
|
| 35 | import org.apache.commons.lang3.StringUtils;
|
| 36 | import org.slf4j.Logger;
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public class WinScreen extends Screen {
|
| 40 | private static final Identifier VIGNETTE_LOCATION = Identifier.withDefaultNamespace("textures/misc/credits_vignette.png");
|
| 41 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 42 | private static final Component SECTION_HEADING = Component.literal("============").withStyle(ChatFormatting.WHITE);
|
| 43 | private static final String NAME_PREFIX = " ";
|
| 44 | private static final String OBFUSCATE_TOKEN = "" + ChatFormatting.WHITE + ChatFormatting.OBFUSCATED + ChatFormatting.GREEN + ChatFormatting.AQUA;
|
| 45 | private static final float SPEEDUP_FACTOR = 5.0F;
|
| 46 | private static final float SPEEDUP_FACTOR_FAST = 15.0F;
|
| 47 | private static final Identifier END_POEM_LOCATION = Identifier.withDefaultNamespace("texts/end.txt");
|
| 48 | private static final Identifier CREDITS_LOCATION = Identifier.withDefaultNamespace("texts/credits.json");
|
| 49 | private static final Identifier POSTCREDITS_LOCATION = Identifier.withDefaultNamespace("texts/postcredits.txt");
|
| 50 | private final boolean poem;
|
| 51 | private final Runnable onFinished;
|
| 52 | private float scroll;
|
| 53 | private List<FormattedCharSequence> lines;
|
| 54 | private List<Component> narratorComponents;
|
| 55 | private IntSet centeredLines;
|
| 56 | private int totalScrollLength;
|
| 57 | private boolean speedupActive;
|
| 58 | private final IntSet speedupModifiers = new IntOpenHashSet();
|
| 59 | private float scrollSpeed;
|
| 60 | private final float unmodifiedScrollSpeed;
|
| 61 | private int direction;
|
| 62 | private final LogoRenderer logoRenderer = new LogoRenderer(false);
|
| 63 |
|
| 64 | public WinScreen(final boolean poem, final Runnable onFinished) {
|
| 65 | super(GameNarrator.NO_TITLE);
|
| 66 | this.poem = poem;
|
| 67 | this.onFinished = onFinished;
|
| 68 | if (!poem) {
|
| 69 | this.unmodifiedScrollSpeed = 0.75F;
|
| 70 | } else {
|
| 71 | this.unmodifiedScrollSpeed = 0.5F;
|
| 72 | }
|
| 73 |
|
| 74 | this.direction = 1;
|
| 75 | this.scrollSpeed = this.unmodifiedScrollSpeed;
|
| 76 | }
|
| 77 |
|
| 78 | private float calculateScrollSpeed() {
|
| 79 | return this.speedupActive
|
| 80 | ? this.unmodifiedScrollSpeed * (5.0F + this.speedupModifiers.size() * 15.0F) * this.direction
|
| 81 | : this.unmodifiedScrollSpeed * this.direction;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void tick() {
|
| 86 | this.minecraft.getMusicManager().tick();
|
| 87 | this.minecraft.getSoundManager().tick(false);
|
| 88 | float maxScroll = this.totalScrollLength + this.height + this.height + 24;
|
| 89 | if (this.scroll > maxScroll) {
|
| 90 | this.respawn();
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean keyPressed(final KeyEvent event) {
|
| 96 | if (event.isUp()) {
|
| 97 | this.direction = -1;
|
| 98 | } else if (event.shortcutKey() == 1073742048 || event.shortcutKey() == 1073742052) {
|
| 99 | this.speedupModifiers.add(event.shortcutKey());
|
| 100 | } else if (event.shortcutKey() == 32) {
|
| 101 | this.speedupActive = true;
|
| 102 | }
|
| 103 |
|
| 104 | this.scrollSpeed = this.calculateScrollSpeed();
|
| 105 | return super.keyPressed(event);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public boolean keyReleased(final KeyEvent event) {
|
| 110 | if (event.isUp()) {
|
| 111 | this.direction = 1;
|
| 112 | }
|
| 113 |
|
| 114 | if (event.shortcutKey() == 32) {
|
| 115 | this.speedupActive = false;
|
| 116 | } else if (event.shortcutKey() == 1073742048 || event.shortcutKey() == 1073742052) {
|
| 117 | this.speedupModifiers.remove(event.shortcutKey());
|
| 118 | }
|
| 119 |
|
| 120 | this.scrollSpeed = this.calculateScrollSpeed();
|
| 121 | return super.keyReleased(event);
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void onClose() {
|
| 126 | this.respawn();
|
| 127 | }
|
| 128 |
|
| 129 | private void respawn() {
|
| 130 | this.onFinished.run();
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | protected void init() {
|
| 135 | if (this.lines == null) {
|
| 136 | this.lines = Lists.newArrayList();
|
| 137 | this.narratorComponents = Lists.newArrayList();
|
| 138 | this.centeredLines = new IntOpenHashSet();
|
| 139 | if (this.poem) {
|
| 140 | this.wrapCreditsIO(END_POEM_LOCATION, this::addPoemFile);
|
| 141 | }
|
| 142 |
|
| 143 | this.wrapCreditsIO(CREDITS_LOCATION, this::addCreditsFile);
|
| 144 | if (this.poem) {
|
| 145 | this.wrapCreditsIO(POSTCREDITS_LOCATION, this::addPoemFile);
|
| 146 | }
|
| 147 |
|
| 148 | this.totalScrollLength = this.lines.size() * 12;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public Component getNarrationMessage() {
|
| 154 | return CommonComponents.joinForNarration(this.narratorComponents.toArray(Component[]::new));
|
| 155 | }
|
| 156 |
|
| 157 | private void wrapCreditsIO(final Identifier file, final WinScreen.CreditsReader creditsReader) {
|
| 158 | try (Reader resource = this.minecraft.getResourceManager().openAsReader(file)) {
|
| 159 | creditsReader.read(resource);
|
| 160 | } catch (Exception e) {
|
| 161 | LOGGER.error("Couldn't load credits from file {}", file, e);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | private void addPoemFile(final Reader inputReader) throws IOException {
|
| 166 | BufferedReader reader = new BufferedReader(inputReader);
|
| 167 | RandomSource random = RandomSource.createThreadLocalInstance(8124371L);
|
| 168 |
|
| 169 | String line;
|
| 170 | while ((line = reader.readLine()) != null) {
|
| 171 | line = line.replaceAll("PLAYERNAME", this.minecraft.getUser().getName());
|
| 172 |
|
| 173 | int pos;
|
| 174 | while ((pos = line.indexOf(OBFUSCATE_TOKEN)) != -1) {
|
| 175 | String before = line.substring(0, pos);
|
| 176 | String after = line.substring(pos + OBFUSCATE_TOKEN.length());
|
| 177 | line = before + ChatFormatting.WHITE + ChatFormatting.OBFUSCATED + "XXXXXXXX".substring(0, random.nextInt(4) + 3) + after;
|
| 178 | }
|
| 179 |
|
| 180 | this.addPoemLines(line);
|
| 181 | this.addEmptyLine();
|
| 182 | }
|
| 183 |
|
| 184 | for (int i = 0; i < 8; i++) {
|
| 185 | this.addEmptyLine();
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | private void addCreditsFile(final Reader inputReader) {
|
| 190 | for (JsonElement sectionElement : GsonHelper.parseArray(inputReader)) {
|
| 191 | JsonObject section = sectionElement.getAsJsonObject();
|
| 192 | String sectionName = section.get("section").getAsString();
|
| 193 | this.addCreditsLine(SECTION_HEADING, true, false);
|
| 194 | this.addCreditsLine(Component.literal(sectionName).withStyle(ChatFormatting.YELLOW), true, true);
|
| 195 | this.addCreditsLine(SECTION_HEADING, true, false);
|
| 196 | this.addEmptyLine();
|
| 197 | this.addEmptyLine();
|
| 198 |
|
| 199 | for (JsonElement disciplineElement : section.getAsJsonArray("disciplines")) {
|
| 200 | JsonObject discipline = disciplineElement.getAsJsonObject();
|
| 201 | String disciplineName = discipline.get("discipline").getAsString();
|
| 202 | if (StringUtils.isNotEmpty(disciplineName)) {
|
| 203 | this.addCreditsLine(Component.literal(disciplineName).withStyle(ChatFormatting.YELLOW), true, true);
|
| 204 | this.addEmptyLine();
|
| 205 | this.addEmptyLine();
|
| 206 | }
|
| 207 |
|
| 208 | for (JsonElement titleElement : discipline.getAsJsonArray("titles")) {
|
| 209 | JsonObject title = titleElement.getAsJsonObject();
|
| 210 | String titleName = title.get("title").getAsString();
|
| 211 | JsonArray names = title.getAsJsonArray("names");
|
| 212 | this.addCreditsLine(Component.literal(titleName).withStyle(ChatFormatting.GRAY), false, true);
|
| 213 |
|
| 214 | for (JsonElement nameElement : names) {
|
| 215 | String name = nameElement.getAsString();
|
| 216 | this.addCreditsLine(Component.literal(" ").append(name).withStyle(ChatFormatting.WHITE), false, true);
|
| 217 | }
|
| 218 |
|
| 219 | this.addEmptyLine();
|
| 220 | this.addEmptyLine();
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | private void addEmptyLine() {
|
| 227 | this.lines.add(FormattedCharSequence.EMPTY);
|
| 228 | this.narratorComponents.add(CommonComponents.EMPTY);
|
| 229 | }
|
| 230 |
|
| 231 | private void addPoemLines(final String line) {
|
| 232 | Component component = Component.literal(line);
|
| 233 | this.lines.addAll(this.minecraft.font.split(component, 256));
|
| 234 | this.narratorComponents.add(component);
|
| 235 | }
|
| 236 |
|
| 237 | private void addCreditsLine(final Component line, final boolean centered, final boolean narrated) {
|
| 238 | if (centered) {
|
| 239 | this.centeredLines.add(this.lines.size());
|
| 240 | }
|
| 241 |
|
| 242 | this.lines.add(line.getVisualOrderText());
|
| 243 | if (narrated) {
|
| 244 | this.narratorComponents.add(line);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 250 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 251 | this.extractVignette(graphics);
|
| 252 | this.scroll = Math.max(0.0F, this.scroll + a * this.scrollSpeed);
|
| 253 | int logoX = this.width / 2 - 128;
|
| 254 | int logoY = this.height + 50;
|
| 255 | float yOffs = -this.scroll;
|
| 256 | graphics.pose().pushMatrix();
|
| 257 | graphics.pose().translate(0.0F, yOffs);
|
| 258 | graphics.nextStratum();
|
| 259 | this.logoRenderer.extractRenderState(graphics, this.width, 1.0F, logoY);
|
| 260 | int yPos = logoY + 100;
|
| 261 |
|
| 262 | for (int i = 0; i < this.lines.size(); i++) {
|
| 263 | if (i == this.lines.size() - 1) {
|
| 264 | float diff = yPos + yOffs - (this.height / 2 - 6);
|
| 265 | if (diff < 0.0F) {
|
| 266 | graphics.pose().translate(0.0F, -diff);
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | if (yPos + yOffs + 12.0F + 8.0F > 0.0F && yPos + yOffs < this.height) {
|
| 271 | FormattedCharSequence line = this.lines.get(i);
|
| 272 | if (this.centeredLines.contains(i)) {
|
| 273 | graphics.centeredText(this.font, line, logoX + 128, yPos, -1);
|
| 274 | } else {
|
| 275 | graphics.text(this.font, line, logoX, yPos, -1);
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | yPos += 12;
|
| 280 | }
|
| 281 |
|
| 282 | graphics.pose().popMatrix();
|
| 283 | }
|
| 284 |
|
| 285 | private void extractVignette(final GuiGraphicsExtractor graphics) {
|
| 286 | graphics.blit(RenderPipelines.VIGNETTE, VIGNETTE_LOCATION, 0, 0, 0.0F, 0.0F, this.width, this.height, this.width, this.height);
|
| 287 | }
|
| 288 |
|
| 289 | @Override
|
| 290 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 291 | if (this.poem) {
|
| 292 | TextureManager textureManager = Minecraft.getInstance().getTextureManager();
|
| 293 | AbstractTexture skyTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_SKY_LOCATION);
|
| 294 | AbstractTexture portalTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_PORTAL_LOCATION);
|
| 295 | TextureSetup textureSetup = TextureSetup.doubleTexture(
|
| 296 | skyTexture.getTextureView(), skyTexture.getSampler(), portalTexture.getTextureView(), portalTexture.getSampler()
|
| 297 | );
|
| 298 | graphics.fill(RenderPipelines.END_PORTAL, textureSetup, 0, 0, this.width, this.height);
|
| 299 | } else {
|
| 300 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 301 | }
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | protected void extractMenuBackground(final GuiGraphicsExtractor graphics, final int x, final int y, final int width, final int height) {
|
| 306 | float v = this.scroll * 0.5F;
|
| 307 | Screen.extractMenuBackgroundTexture(graphics, Screen.MENU_BACKGROUND, 0, 0, 0.0F, v, width, height);
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | public boolean isPauseScreen() {
|
| 312 | return !this.poem;
|
| 313 | }
|
| 314 |
|
| 315 | @Override
|
| 316 | public boolean isAllowedInPortal() {
|
| 317 | return true;
|
| 318 | }
|
| 319 |
|
| 320 | @Override
|
| 321 | public void removed() {
|
| 322 | this.minecraft.getMusicManager().stopPlaying(Musics.CREDITS);
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public Music getBackgroundMusic() {
|
| 327 | return Musics.CREDITS;
|
| 328 | }
|
| 329 |
|
| 330 | @FunctionalInterface
|
| 331 | @Environment(EnvType.CLIENT)
|
| 332 | private interface CreditsReader {
|
| 333 | void read(final Reader reader) throws IOException;
|
| 334 | }
|
| 335 | }
|
| 336 |
|