| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.UUID;
|
| 6 | import java.util.Map.Entry;
|
| 7 | import java.util.function.Supplier;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.SharedConstants;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.Options;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.gui.components.Button;
|
| 15 | import net.minecraft.client.gui.components.CommonButtons;
|
| 16 | import net.minecraft.client.gui.components.FriendsButton;
|
| 17 | import net.minecraft.client.gui.components.SpriteIconButton;
|
| 18 | import net.minecraft.client.gui.components.StringWidget;
|
| 19 | import net.minecraft.client.gui.components.Tooltip;
|
| 20 | import net.minecraft.client.gui.components.toasts.NowPlayingToast;
|
| 21 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 22 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 23 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 24 | import net.minecraft.client.gui.screens.achievement.StatsScreen;
|
| 25 | import net.minecraft.client.gui.screens.advancements.AdvancementsScreen;
|
| 26 | import net.minecraft.client.gui.screens.friends.FriendsOverlayScreen;
|
| 27 | import net.minecraft.client.gui.screens.options.OnlineOptionsScreen;
|
| 28 | import net.minecraft.client.gui.screens.options.OptionsScreen;
|
| 29 | import net.minecraft.client.gui.screens.social.SocialInteractionsScreen;
|
| 30 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 31 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 32 | import net.minecraft.client.renderer.RenderPipelines;
|
| 33 | import net.minecraft.client.server.IntegratedServer;
|
| 34 | import net.minecraft.core.Holder;
|
| 35 | import net.minecraft.core.HolderSet;
|
| 36 | import net.minecraft.core.Registry;
|
| 37 | import net.minecraft.core.registries.Registries;
|
| 38 | import net.minecraft.network.chat.CommonComponents;
|
| 39 | import net.minecraft.network.chat.Component;
|
| 40 | import net.minecraft.resources.Identifier;
|
| 41 | import net.minecraft.server.ServerLinks;
|
| 42 | import net.minecraft.server.dialog.Dialog;
|
| 43 | import net.minecraft.server.dialog.Dialogs;
|
| 44 | import net.minecraft.sounds.SoundSource;
|
| 45 | import net.minecraft.tags.DialogTags;
|
| 46 | import net.minecraft.util.CommonLinks;
|
| 47 | import org.jspecify.annotations.Nullable;
|
| 48 |
|
| 49 | @Environment(EnvType.CLIENT)
|
| 50 | public class PauseScreen extends Screen {
|
| 51 | private static final Identifier DRAFT_REPORT_SPRITE = Identifier.withDefaultNamespace("icon/draft_report");
|
| 52 | private static final int COLUMNS = 2;
|
| 53 | private static final int MENU_PADDING_TOP = 50;
|
| 54 | private static final int BUTTON_PADDING = 4;
|
| 55 | private static final int BUTTON_WIDTH_FULL = 204;
|
| 56 | private static final int BUTTON_WIDTH_HALF = 98;
|
| 57 | private static final Component RETURN_TO_GAME = Component.translatable("menu.returnToGame");
|
| 58 | private static final Component ADVANCEMENTS = Component.translatable("gui.advancements");
|
| 59 | private static final Component STATS = Component.translatable("gui.stats");
|
| 60 | private static final Component SEND_FEEDBACK = Component.translatable("menu.sendFeedback");
|
| 61 | private static final Component REPORT_BUGS = Component.translatable("menu.reportBugs");
|
| 62 | private static final Component OPTIONS = Component.translatable("menu.options");
|
| 63 | private static final Component WORLD_OPTIONS = Component.translatable("options.worldOptions.button");
|
| 64 | private static final Component PLAYER_REPORTING = Component.translatable("menu.playerReporting");
|
| 65 | private static final Component GAME = Component.translatable("menu.game");
|
| 66 | private static final Component PAUSED = Component.translatable("menu.paused");
|
| 67 | private static final Tooltip CUSTOM_OPTIONS_TOOLTIP = Tooltip.create(Component.translatable("menu.custom_options.tooltip"));
|
| 68 | private static final Tooltip NO_PLAYERS_TO_REPORT_TOOLTIP = Tooltip.create(Component.translatable("menu.playerReporting.no_players"));
|
| 69 | private final Runnable friendListUpdateListener = this::onFriendListUpdate;
|
| 70 | private @Nullable FriendsButton friends;
|
| 71 | private final boolean showPauseMenu;
|
| 72 | private @Nullable Button disconnectButton;
|
| 73 |
|
| 74 | public PauseScreen(final boolean showPauseMenu) {
|
| 75 | super(showPauseMenu ? GAME : PAUSED);
|
| 76 | this.showPauseMenu = showPauseMenu;
|
| 77 | }
|
| 78 |
|
| 79 | public boolean showsPauseMenu() {
|
| 80 | return this.showPauseMenu;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void added() {
|
| 85 | super.added();
|
| 86 | if (this.showPauseMenu && this.minecraft.getPlayerSocialManager().isFriendListEnabled()) {
|
| 87 | this.minecraft.getPlayerSocialManager().addFriendListUpdateListener(this.friendListUpdateListener);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected void init() {
|
| 93 | if (this.showPauseMenu) {
|
| 94 | this.createPauseMenu();
|
| 95 | }
|
| 96 |
|
| 97 | int textWidth = this.font.width(this.title);
|
| 98 | this.addRenderableWidget(new StringWidget(this.width / 2 - textWidth / 2, this.showPauseMenu ? 40 : 10, textWidth, 9, this.title, this.font));
|
| 99 | }
|
| 100 |
|
| 101 | private void createPauseMenu() {
|
| 102 | GridLayout gridLayout = new GridLayout();
|
| 103 | gridLayout.defaultCellSetting().padding(4, 4, 4, 0);
|
| 104 | GridLayout.RowHelper helper = gridLayout.createRowHelper(2);
|
| 105 | helper.addChild(Button.builder(RETURN_TO_GAME, button -> {
|
| 106 | this.minecraft.gui.setScreen(null);
|
| 107 | this.minecraft.mouseHandler.grabMouse();
|
| 108 | }).width(204).build(), 2, gridLayout.newCellSettings().paddingTop(50));
|
| 109 | helper.addChild(this.openScreenButton(ADVANCEMENTS, () -> new AdvancementsScreen(this.minecraft.player.connection.getAdvancements(), this)));
|
| 110 | helper.addChild(this.openScreenButton(STATS, () -> new StatsScreen(this, this.minecraft.player.getStats())));
|
| 111 | LinearLayout iconButtonRow = LinearLayout.horizontal().spacing(4);
|
| 112 | SpriteIconButton reportBugsButton = SpriteIconButton.builder(REPORT_BUGS, ConfirmLinkScreen.confirmLink(this, CommonLinks.SNAPSHOT_BUGS_FEEDBACK), true)
|
| 113 | .width(20)
|
| 114 | .sprite(Identifier.withDefaultNamespace("pause_menu/bug"), 15, 15)
|
| 115 | .withTootip()
|
| 116 | .build();
|
| 117 | reportBugsButton.active = !SharedConstants.getCurrentVersion().dataVersion().isSideSeries();
|
| 118 | iconButtonRow.addChild(reportBugsButton);
|
| 119 | SpriteIconButton feedbackButton = SpriteIconButton.builder(
|
| 120 | SEND_FEEDBACK,
|
| 121 | ConfirmLinkScreen.confirmLink(this, SharedConstants.getCurrentVersion().stable() ? CommonLinks.RELEASE_FEEDBACK : CommonLinks.SNAPSHOT_FEEDBACK),
|
| 122 | true
|
| 123 | )
|
| 124 | .width(20)
|
| 125 | .sprite(Identifier.withDefaultNamespace("pause_menu/social_interactions"), 15, 15)
|
| 126 | .withTootip()
|
| 127 | .build();
|
| 128 | iconButtonRow.addChild(feedbackButton);
|
| 129 | this.friends = CommonButtons.friends(
|
| 130 | 20,
|
| 131 | var1x -> OnlineOptionsScreen.confirmFriendsListEnabled(this.minecraft, () -> this.minecraft.gui.setScreen(new FriendsOverlayScreen(this)), this),
|
| 132 | !this.minecraft.isDemo() && !this.minecraft.isOfflineDeveloperMode()
|
| 133 | );
|
| 134 | iconButtonRow.addChild(this.friends);
|
| 135 | SpriteIconButton playerReportingButton = SpriteIconButton.builder(
|
| 136 | PLAYER_REPORTING, var1x -> this.minecraft.gui.setScreen(new SocialInteractionsScreen(this)), true
|
| 137 | )
|
| 138 | .width(20)
|
| 139 | .sprite(Identifier.withDefaultNamespace("pause_menu/player_reporting"), 15, 15)
|
| 140 | .withTootip()
|
| 141 | .build();
|
| 142 | iconButtonRow.addChild(playerReportingButton);
|
| 143 | IntegratedServer integratedServer = this.minecraft.getSingleplayerServer();
|
| 144 | if (integratedServer != null && this.minecraft.player != null) {
|
| 145 | List<Entry<UUID, PlayerInfo>> list = this.minecraft
|
| 146 | .player
|
| 147 | .connection
|
| 148 | .getSeenPlayers()
|
| 149 | .entrySet()
|
| 150 | .stream()
|
| 151 | .filter(entry -> entry.getKey() != this.minecraft.player.getUUID())
|
| 152 | .toList();
|
| 153 | if (list.isEmpty()) {
|
| 154 | playerReportingButton.active = false;
|
| 155 | playerReportingButton.setTooltip(NO_PLAYERS_TO_REPORT_TOOLTIP);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | helper.addChild(iconButtonRow, 2, gridLayout.newCellSettings().alignHorizontallyCenter());
|
| 160 | Optional<? extends Holder<Dialog>> additions = this.getCustomAdditions();
|
| 161 | additions.ifPresent(dialogHolder -> this.addCustomDialogButtons(this.minecraft, (Holder<Dialog>)dialogHolder, helper));
|
| 162 | if (this.minecraft.level != null) {
|
| 163 | helper.addChild(this.openScreenButton(OPTIONS, () -> new OptionsScreen(this, this.minecraft.options)));
|
| 164 | helper.addChild(this.openScreenButton(WORLD_OPTIONS, () -> new WorldOptionsScreen(this, this.minecraft.level)));
|
| 165 | }
|
| 166 |
|
| 167 | this.disconnectButton = helper.addChild(
|
| 168 | Button.builder(
|
| 169 | CommonComponents.disconnectButtonLabel(this.minecraft.isLocalServer()),
|
| 170 | button -> {
|
| 171 | button.active = false;
|
| 172 | this.minecraft
|
| 173 | .getReportingContext()
|
| 174 | .draftReportHandled(this.minecraft, this, () -> this.minecraft.disconnectFromWorld(ClientLevel.DEFAULT_QUIT_MESSAGE), true);
|
| 175 | }
|
| 176 | )
|
| 177 | .width(204)
|
| 178 | .build(),
|
| 179 | 2
|
| 180 | );
|
| 181 | gridLayout.arrangeElements();
|
| 182 | FrameLayout.alignInRectangle(gridLayout, 0, 0, this.width, this.height, 0.5F, 0.25F);
|
| 183 | gridLayout.visitWidgets(this::addRenderableWidget);
|
| 184 | }
|
| 185 |
|
| 186 | private Optional<? extends Holder<Dialog>> getCustomAdditions() {
|
| 187 | Registry<Dialog> dialogRegistry = this.minecraft.player.connection.registryAccess().lookupOrThrow(Registries.DIALOG);
|
| 188 | Optional<? extends HolderSet<Dialog>> maybeCustomAdditions = dialogRegistry.get(DialogTags.PAUSE_SCREEN_ADDITIONS);
|
| 189 | if (maybeCustomAdditions.isPresent()) {
|
| 190 | HolderSet<Dialog> customAdditions = (HolderSet<Dialog>)maybeCustomAdditions.get();
|
| 191 | if (customAdditions.size() > 0) {
|
| 192 | if (customAdditions.size() == 1) {
|
| 193 | return Optional.of(customAdditions.get(0));
|
| 194 | }
|
| 195 |
|
| 196 | return dialogRegistry.get(Dialogs.CUSTOM_OPTIONS);
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | ServerLinks serverLinks = this.minecraft.player.connection.serverLinks();
|
| 201 | return !serverLinks.isEmpty() ? dialogRegistry.get(Dialogs.SERVER_LINKS) : Optional.empty();
|
| 202 | }
|
| 203 |
|
| 204 | private void addCustomDialogButtons(final Minecraft minecraft, final Holder<Dialog> dialog, final GridLayout.RowHelper helper) {
|
| 205 | helper.addChild(
|
| 206 | Button.builder(dialog.value().common().computeExternalTitle(), button -> minecraft.player.connection.showDialog(dialog, this))
|
| 207 | .width(204)
|
| 208 | .tooltip(CUSTOM_OPTIONS_TOOLTIP)
|
| 209 | .build(),
|
| 210 | 2
|
| 211 | );
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public void tick() {
|
| 216 | if (this.rendersNowPlayingToast()) {
|
| 217 | NowPlayingToast.tickMusicNotes();
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 223 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 224 | if (this.rendersNowPlayingToast()) {
|
| 225 | NowPlayingToast.extractToast(graphics, this.font);
|
| 226 | }
|
| 227 |
|
| 228 | if (this.showPauseMenu && this.minecraft.getReportingContext().hasDraftReport() && this.disconnectButton != null) {
|
| 229 | graphics.blitSprite(
|
| 230 | RenderPipelines.GUI_TEXTURED,
|
| 231 | DRAFT_REPORT_SPRITE,
|
| 232 | this.disconnectButton.getX() + this.disconnectButton.getWidth() - 17,
|
| 233 | this.disconnectButton.getY() + 3,
|
| 234 | 15,
|
| 235 | 15
|
| 236 | );
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 242 | if (this.showPauseMenu) {
|
| 243 | if (this.isTopmostScreen()) {
|
| 244 | this.extractBlurredBackground(graphics);
|
| 245 | }
|
| 246 |
|
| 247 | this.extractMenuBackground(graphics);
|
| 248 | this.minecraft.gui.hud.extractDeferredSubtitles();
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public void removed() {
|
| 254 | this.minecraft.getPlayerSocialManager().removeFriendListUpdateListener(this.friendListUpdateListener);
|
| 255 | super.removed();
|
| 256 | }
|
| 257 |
|
| 258 | public boolean rendersNowPlayingToast() {
|
| 259 | Options options = this.minecraft.options;
|
| 260 | return options.musicToast().get().renderInPauseScreen() && options.getFinalSoundSourceVolume(SoundSource.MUSIC) > 0.0F && this.showPauseMenu;
|
| 261 | }
|
| 262 |
|
| 263 | private boolean isTopmostScreen() {
|
| 264 | return this.minecraft.gui.screen() == this;
|
| 265 | }
|
| 266 |
|
| 267 | private Button openScreenButton(final Component message, final Supplier<Screen> newScreen) {
|
| 268 | return Button.builder(message, var2 -> this.minecraft.gui.setScreen(newScreen.get())).width(98).build();
|
| 269 | }
|
| 270 |
|
| 271 | private void onFriendListUpdate() {
|
| 272 | if (this.friends != null) {
|
| 273 | this.friends.refreshIncomingRequestCount();
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 |
|