| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 5 | import java.util.List;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 10 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 11 | import net.minecraft.client.gui.TextAlignment;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.input.MouseButtonEvent;
|
| 14 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 15 | import net.minecraft.client.player.LocalPlayer;
|
| 16 | import net.minecraft.client.renderer.RenderPipelines;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.chat.Style;
|
| 20 | import net.minecraft.network.chat.ClickEvent.OpenUrl;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class DeathScreen extends Screen {
|
| 26 | private static final int TITLE_SCALE = 2;
|
| 27 | private static final Identifier DRAFT_REPORT_SPRITE = Identifier.withDefaultNamespace("icon/draft_report");
|
| 28 | private int delayTicker;
|
| 29 | private final @Nullable Component causeOfDeath;
|
| 30 | private final boolean hardcore;
|
| 31 | private final LocalPlayer player;
|
| 32 | private final Component deathScore;
|
| 33 | private final List<Button> exitButtons = Lists.newArrayList();
|
| 34 | private @Nullable Button exitToTitleButton;
|
| 35 |
|
| 36 | public DeathScreen(final @Nullable Component causeOfDeath, final boolean hardcore, final LocalPlayer player) {
|
| 37 | super(Component.translatable(hardcore ? "deathScreen.title.hardcore" : "deathScreen.title"));
|
| 38 | this.causeOfDeath = causeOfDeath;
|
| 39 | this.hardcore = hardcore;
|
| 40 | this.player = player;
|
| 41 | Component scoreValue = Component.literal(Integer.toString(player.getScore())).withStyle(ChatFormatting.YELLOW);
|
| 42 | this.deathScore = Component.translatable("deathScreen.score.value", scoreValue);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void init() {
|
| 47 | this.delayTicker = 0;
|
| 48 | this.exitButtons.clear();
|
| 49 | Component message = this.hardcore ? Component.translatable("deathScreen.spectate") : Component.translatable("deathScreen.respawn");
|
| 50 | this.exitButtons.add(this.addRenderableWidget(Button.builder(message, button -> {
|
| 51 | this.player.respawn();
|
| 52 | button.active = false;
|
| 53 | }).bounds(this.width / 2 - 100, this.height / 4 + 72, 200, 20).build()));
|
| 54 | this.exitToTitleButton = this.addRenderableWidget(
|
| 55 | Button.builder(
|
| 56 | Component.translatable("deathScreen.titleScreen"),
|
| 57 | button -> this.minecraft.getReportingContext().draftReportHandled(this.minecraft, this, this::handleExitToTitleScreen, true)
|
| 58 | )
|
| 59 | .bounds(this.width / 2 - 100, this.height / 4 + 96, 200, 20)
|
| 60 | .build()
|
| 61 | );
|
| 62 | this.exitButtons.add(this.exitToTitleButton);
|
| 63 | this.setButtonsActive(false);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public boolean shouldCloseOnEsc() {
|
| 68 | return false;
|
| 69 | }
|
| 70 |
|
| 71 | private void handleExitToTitleScreen() {
|
| 72 | if (this.hardcore) {
|
| 73 | this.exitToTitleScreen();
|
| 74 | } else {
|
| 75 | ConfirmScreen confirm = new DeathScreen.TitleConfirmScreen(
|
| 76 | result -> {
|
| 77 | if (result) {
|
| 78 | this.exitToTitleScreen();
|
| 79 | } else {
|
| 80 | this.player.respawn();
|
| 81 | this.minecraft.gui.setScreen(null);
|
| 82 | }
|
| 83 | },
|
| 84 | Component.translatable("deathScreen.quit.confirm"),
|
| 85 | CommonComponents.EMPTY,
|
| 86 | Component.translatable("deathScreen.titleScreen"),
|
| 87 | Component.translatable("deathScreen.respawn")
|
| 88 | );
|
| 89 | this.minecraft.gui.setScreen(confirm);
|
| 90 | confirm.setDelay(20);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | private void exitToTitleScreen() {
|
| 95 | if (this.minecraft.level != null) {
|
| 96 | this.minecraft.level.disconnect(ClientLevel.DEFAULT_QUIT_MESSAGE);
|
| 97 | }
|
| 98 |
|
| 99 | this.minecraft.disconnectWithSavingScreen();
|
| 100 | this.minecraft.gui.setScreen(new TitleScreen());
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 105 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 106 | this.visitText(graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR));
|
| 107 | if (this.exitToTitleButton != null && this.minecraft.getReportingContext().hasDraftReport()) {
|
| 108 | graphics.blitSprite(
|
| 109 | RenderPipelines.GUI_TEXTURED,
|
| 110 | DRAFT_REPORT_SPRITE,
|
| 111 | this.exitToTitleButton.getX() + this.exitToTitleButton.getWidth() - 17,
|
| 112 | this.exitToTitleButton.getY() + 3,
|
| 113 | 15,
|
| 114 | 15
|
| 115 | );
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | private void visitText(final ActiveTextCollector output) {
|
| 120 | ActiveTextCollector.Parameters normalParameters = output.defaultParameters();
|
| 121 | int middleLine = this.width / 2;
|
| 122 | output.defaultParameters(normalParameters.withScale(2.0F));
|
| 123 | output.accept(TextAlignment.CENTER, middleLine / 2, 30, this.title);
|
| 124 | output.defaultParameters(normalParameters);
|
| 125 | if (this.causeOfDeath != null) {
|
| 126 | output.accept(TextAlignment.CENTER, middleLine, 85, this.causeOfDeath);
|
| 127 | }
|
| 128 |
|
| 129 | output.accept(TextAlignment.CENTER, middleLine, 100, this.deathScore);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 134 | extractDeathBackground(graphics, this.width, this.height);
|
| 135 | }
|
| 136 |
|
| 137 | private static void extractDeathBackground(final GuiGraphicsExtractor graphics, final int width, final int height) {
|
| 138 | graphics.fillGradient(0, 0, width, height, 1615855616, -1602211792);
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 143 | ActiveTextCollector.ClickableStyleFinder finder = new ActiveTextCollector.ClickableStyleFinder(this.getFont(), (int)event.x(), (int)event.y());
|
| 144 | this.visitText(finder);
|
| 145 | Style clickedStyle = finder.result();
|
| 146 | return clickedStyle != null && clickedStyle.getClickEvent() instanceof OpenUrl openUrl
|
| 147 | ? clickUrlAction(this.minecraft, this, openUrl.uri())
|
| 148 | : super.mouseClicked(event, doubleClick);
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public boolean isPauseScreen() {
|
| 153 | return false;
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public boolean isAllowedInPortal() {
|
| 158 | return true;
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public void tick() {
|
| 163 | super.tick();
|
| 164 | this.delayTicker++;
|
| 165 | if (this.delayTicker == 20) {
|
| 166 | this.setButtonsActive(true);
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | private void setButtonsActive(final boolean isActive) {
|
| 171 | for (Button button : this.exitButtons) {
|
| 172 | button.active = isActive;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | @Environment(EnvType.CLIENT)
|
| 177 | public static class TitleConfirmScreen extends ConfirmScreen {
|
| 178 | public TitleConfirmScreen(final BooleanConsumer callback, final Component title, final Component message, final Component yesButton, final Component noButton) {
|
| 179 | super(callback, title, message, yesButton, noButton);
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 184 | DeathScreen.extractDeathBackground(graphics, this.width, this.height);
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|