| 1 | package net.minecraft.client.gui.screens.options;
|
| 2 |
|
| 3 | import com.mojang.authlib.services.FriendsService.ResultCode;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.OptionInstance;
|
| 10 | import net.minecraft.client.Options;
|
| 11 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.gui.components.CycleButton;
|
| 14 | import net.minecraft.client.gui.components.Tooltip;
|
| 15 | import net.minecraft.client.gui.screens.PrivacyConfirmLinkScreen;
|
| 16 | import net.minecraft.client.gui.screens.Screen;
|
| 17 | import net.minecraft.client.gui.screens.friends.FriendsListConfirmScreen;
|
| 18 | import net.minecraft.client.gui.screens.social.PlayerSocialManager;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.ClickEvent.OpenUrl;
|
| 21 | import net.minecraft.util.CommonLinks;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class OnlineOptionsScreen extends OptionsSubScreen {
|
| 26 | private static final Component TITLE = Component.translatable("options.online.title");
|
| 27 | private static final Component SERVERS_HEADER = Component.translatable("options.online.servers.header")
|
| 28 | .withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 29 | private static final Component REALMS_HEADER = Component.translatable("options.online.realms.header").withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 30 | private static final Component FRIENDS_HEADER = Component.translatable("options.online.friends.header")
|
| 31 | .withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 32 | private static final Component XBOX_SETTINGS = Component.translatable("options.online.xboxSettings");
|
| 33 | private static final Component FRIENDS_CONFIRM_TITLE = Component.translatable("options.friendsList.confirm.title").withStyle(ChatFormatting.UNDERLINE);
|
| 34 | private static final Component MICROSOFT_ACCOUNT_LINK = Component.translatable("options.friendsList.confirm.message.link")
|
| 35 | .withStyle(style -> style.withUnderlined(true).withColor(ChatFormatting.BLUE).withClickEvent(new OpenUrl(CommonLinks.PRIVACY_AND_ONLINE_SETTINGS)));
|
| 36 | private static final Component FRIENDS_CONFIRM_MESSAGE = Component.translatable("options.friendsList.confirm.message", MICROSOFT_ACCOUNT_LINK);
|
| 37 | private static final Component FRIENDS_CONFIRM_TURN_ON = Component.translatable("options.friendsList.confirm.turnOn");
|
| 38 | private static final Component FRIENDS_CONFIRM_TURN_OFF = Component.translatable("options.friendsList.confirm.turnOff");
|
| 39 | private static final Component FRIENDS_LIST_LABEL = Component.translatable("options.friendsList");
|
| 40 | private static final Component ALLOW_FRIEND_REQUESTS_LABEL = Component.translatable("options.allowFriendRequests");
|
| 41 | private static final Tooltip ALLOW_FRIEND_REQUESTS_TOOLTIP = Tooltip.create(Component.translatable("options.allowFriendRequests.tooltip"));
|
| 42 | private static final Component IN_GAME_NOTIFICATIONS_LABEL = Component.translatable("options.inGameNotification");
|
| 43 | private static final Tooltip IN_GAME_NOTIFICATIONS_TOOLTIP = Tooltip.create(Component.translatable("options.inGameNotification.tooltip"));
|
| 44 | private @Nullable CycleButton<Boolean> friendsListButton;
|
| 45 | private @Nullable CycleButton<Boolean> allowFriendRequestsButton;
|
| 46 | private @Nullable CycleButton<Boolean> inGameNotificationButton;
|
| 47 | private @Nullable AbstractWidget presenceWidget;
|
| 48 |
|
| 49 | public OnlineOptionsScreen(final Screen lastScreen, final Options options) {
|
| 50 | super(lastScreen, options, TITLE);
|
| 51 | }
|
| 52 |
|
| 53 | public static void confirmFriendsListEnabled(final Minecraft minecraft, final Runnable onEnabled, final @Nullable Screen lastScreen) {
|
| 54 | PlayerSocialManager playerSocialManager = minecraft.getPlayerSocialManager();
|
| 55 | if (playerSocialManager.isFriendListEnabled()) {
|
| 56 | onEnabled.run();
|
| 57 | } else {
|
| 58 | minecraft.gui.setScreen(new FriendsListConfirmScreen(accepted -> {
|
| 59 | if (accepted) {
|
| 60 | applyFriendSettings(minecraft, true, true, successful -> {
|
| 61 | if (successful) {
|
| 62 | onEnabled.run();
|
| 63 | }
|
| 64 | });
|
| 65 | } else {
|
| 66 | minecraft.gui.setScreen(lastScreen);
|
| 67 | }
|
| 68 | }, FRIENDS_CONFIRM_TITLE, FRIENDS_CONFIRM_MESSAGE, FRIENDS_CONFIRM_TURN_ON, FRIENDS_CONFIRM_TURN_OFF));
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | private static void applyFriendSettings(
|
| 73 | final Minecraft minecraft, final boolean friendsListEnabled, final boolean allowFriendRequests, final Consumer<Boolean> onResult
|
| 74 | ) {
|
| 75 | PlayerSocialManager playerSocialManager = minecraft.getPlayerSocialManager();
|
| 76 | playerSocialManager.updateFriendSettings(friendsListEnabled, allowFriendRequests).whenCompleteAsync((result, var5) -> {
|
| 77 | boolean success = result == ResultCode.SUCCESS;
|
| 78 | if (success) {
|
| 79 | playerSocialManager.setFriendListEnabled(friendsListEnabled);
|
| 80 | playerSocialManager.setAllowFriendRequests(allowFriendRequests);
|
| 81 | }
|
| 82 |
|
| 83 | onResult.accept(success);
|
| 84 | }, minecraft);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected void addOptions() {
|
| 89 | this.list.addHeader(FRIENDS_HEADER);
|
| 90 | PlayerSocialManager playerSocialManager = this.minecraft.getPlayerSocialManager();
|
| 91 | OptionInstance<Boolean> inGameNotificationOpt = this.options.inGameNotification();
|
| 92 | this.friendsListButton = CycleButton.onOffBuilder(playerSocialManager.isFriendListEnabled())
|
| 93 | .create(0, 0, 150, 20, FRIENDS_LIST_LABEL, (var3, newValue) -> this.onFriendsListToggled(newValue, playerSocialManager, inGameNotificationOpt));
|
| 94 | this.friendsListButton.active = !this.minecraft.isDemo() && !this.minecraft.isOfflineDeveloperMode();
|
| 95 | this.allowFriendRequestsButton = CycleButton.onOffBuilder(playerSocialManager.isAllowFriendRequests())
|
| 96 | .withTooltip(var0 -> ALLOW_FRIEND_REQUESTS_TOOLTIP)
|
| 97 | .create(
|
| 98 | 0,
|
| 99 | 0,
|
| 100 | 150,
|
| 101 | 20,
|
| 102 | ALLOW_FRIEND_REQUESTS_LABEL,
|
| 103 | (var2x, enabled) -> applyFriendSettings(
|
| 104 | this.minecraft, playerSocialManager.isFriendListEnabled(), enabled, var1x -> this.updateFriendListDependentButtons()
|
| 105 | )
|
| 106 | );
|
| 107 | this.list.addSmall(this.friendsListButton, this.allowFriendRequestsButton);
|
| 108 | this.inGameNotificationButton = CycleButton.onOffBuilder(inGameNotificationOpt.get())
|
| 109 | .withTooltip(var0 -> IN_GAME_NOTIFICATIONS_TOOLTIP)
|
| 110 | .create(0, 0, 150, 20, IN_GAME_NOTIFICATIONS_LABEL, (var1x, enabled) -> inGameNotificationOpt.set(enabled));
|
| 111 | this.presenceWidget = this.options.sharePresence().createButton(this.options);
|
| 112 | this.list.addSmall(this.inGameNotificationButton, this.presenceWidget);
|
| 113 | this.updateFriendListDependentButtons();
|
| 114 | this.list.addBig(Button.builder(XBOX_SETTINGS, var1x -> PrivacyConfirmLinkScreen.confirmLinkNow(this, CommonLinks.PRIVACY_AND_ONLINE_SETTINGS)).build());
|
| 115 | this.list.addHeader(SERVERS_HEADER);
|
| 116 | this.list.addBig(this.options.allowServerListing());
|
| 117 | this.list.addHeader(REALMS_HEADER);
|
| 118 | this.list.addBig(this.options.realmsNotifications());
|
| 119 | }
|
| 120 |
|
| 121 | private void onFriendsListToggled(final Boolean newValue, final PlayerSocialManager playerSocialManager, final OptionInstance<Boolean> inGameNotificationOpt) {
|
| 122 | if (newValue) {
|
| 123 | this.minecraft.gui.setScreen(new FriendsListConfirmScreen(accepted -> {
|
| 124 | this.minecraft.gui.setScreen(this);
|
| 125 | if (accepted && !this.minecraft.isOfflineDeveloperMode()) {
|
| 126 | playerSocialManager.setFriendListEnabled(true);
|
| 127 | playerSocialManager.setAllowFriendRequests(true);
|
| 128 | applyFriendSettings(this.minecraft, true, true, var1x -> this.updateFriendListDependentButtons());
|
| 129 | }
|
| 130 |
|
| 131 | this.updateFriendListDependentButtons();
|
| 132 | }, FRIENDS_CONFIRM_TITLE, FRIENDS_CONFIRM_MESSAGE, FRIENDS_CONFIRM_TURN_ON, FRIENDS_CONFIRM_TURN_OFF));
|
| 133 | } else {
|
| 134 | boolean friendListEnabled = playerSocialManager.isFriendListEnabled();
|
| 135 | boolean allowFriendRequests = playerSocialManager.isAllowFriendRequests();
|
| 136 | playerSocialManager.setFriendListEnabled(false);
|
| 137 | playerSocialManager.setAllowFriendRequests(false);
|
| 138 | inGameNotificationOpt.set(false);
|
| 139 | this.updateFriendListDependentButtons();
|
| 140 | applyFriendSettings(this.minecraft, false, false, result -> {
|
| 141 | if (!result) {
|
| 142 | playerSocialManager.setFriendListEnabled(friendListEnabled);
|
| 143 | playerSocialManager.setAllowFriendRequests(allowFriendRequests);
|
| 144 | }
|
| 145 |
|
| 146 | this.updateFriendListDependentButtons();
|
| 147 | });
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private void updateFriendListDependentButtons() {
|
| 152 | PlayerSocialManager playerSocialManager = this.minecraft.getPlayerSocialManager();
|
| 153 | boolean enabled = playerSocialManager.isFriendListEnabled();
|
| 154 | if (this.friendsListButton != null) {
|
| 155 | this.friendsListButton.setValue(enabled);
|
| 156 | }
|
| 157 |
|
| 158 | if (this.allowFriendRequestsButton != null) {
|
| 159 | this.allowFriendRequestsButton.setValue(playerSocialManager.isAllowFriendRequests());
|
| 160 | this.allowFriendRequestsButton.active = enabled;
|
| 161 | }
|
| 162 |
|
| 163 | if (this.inGameNotificationButton != null) {
|
| 164 | this.inGameNotificationButton.setValue(this.options.inGameNotification().get());
|
| 165 | this.inGameNotificationButton.active = enabled;
|
| 166 | }
|
| 167 |
|
| 168 | if (this.presenceWidget != null) {
|
| 169 | this.presenceWidget.active = enabled;
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 |
|