| 1 | package net.minecraft.client.gui.screens.multiplayer;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.FocusableTextWidget;
|
| 10 | import net.minecraft.client.gui.components.ScrollableLayout;
|
| 11 | import net.minecraft.client.gui.components.StringWidget;
|
| 12 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 13 | import net.minecraft.client.gui.layouts.LayoutSettings;
|
| 14 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 15 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 16 | import net.minecraft.client.gui.screens.Screen;
|
| 17 | import net.minecraft.client.multiplayer.chat.ChatAbilities;
|
| 18 | import net.minecraft.network.chat.CommonComponents;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.ComponentUtils;
|
| 21 | import net.minecraft.network.chat.Style;
|
| 22 | import net.minecraft.server.permissions.Permission;
|
| 23 | import net.minecraft.server.permissions.Permissions;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class RestrictionsScreen extends Screen {
|
| 28 | private static final Component TITLE = Component.translatable("restrictions_screen.title");
|
| 29 | private final Screen previousScreen;
|
| 30 | private final ChatAbilities chatAbilities;
|
| 31 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 32 | private @Nullable ScrollableLayout bodyScroll;
|
| 33 |
|
| 34 | public RestrictionsScreen(final Screen previousScreen, final ChatAbilities chatAbilities) {
|
| 35 | super(TITLE);
|
| 36 | this.previousScreen = previousScreen;
|
| 37 | this.chatAbilities = chatAbilities;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void init() {
|
| 42 | this.layout.addToHeader(new StringWidget(TITLE, this.font), LayoutSettings::alignHorizontallyCenter);
|
| 43 | LinearLayout body = LinearLayout.vertical();
|
| 44 | body.defaultCellSetting().alignHorizontallyCenter();
|
| 45 | int textBoxWidth = 250;
|
| 46 | this.chatAbilities
|
| 47 | .restrictions()
|
| 48 | .forEach(
|
| 49 | restriction -> {
|
| 50 | body.addChild(
|
| 51 | FocusableTextWidget.builder(ComponentUtils.mergeStyles(restriction.display(), Style.EMPTY.withBold(true).withColor(ChatFormatting.RED)), this.font)
|
| 52 | .maxWidth(250)
|
| 53 | .alwaysShowBorder(false)
|
| 54 | .backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
|
| 55 | .build()
|
| 56 | .setCentered(true)
|
| 57 | );
|
| 58 | restriction.action()
|
| 59 | .ifPresent(
|
| 60 | action -> body.addChild(Button.builder(action.title(), var2x -> action.runnable().accept(this.minecraft, this.previousScreen)).width(200).build())
|
| 61 | );
|
| 62 | }
|
| 63 | );
|
| 64 | if (this.chatAbilities.hasAnyRestrictions()) {
|
| 65 | body.addChild(new SpacerElement(10, 10));
|
| 66 | }
|
| 67 |
|
| 68 | List<Component> permissionEntries = new ArrayList<>();
|
| 69 | permissionEntries.add(createPermissionStatus(this.chatAbilities, Permissions.CHAT_SEND_MESSAGES, "send_messages"));
|
| 70 | permissionEntries.add(createPermissionStatus(this.chatAbilities, Permissions.CHAT_SEND_COMMANDS, "send_commands"));
|
| 71 | permissionEntries.add(createPermissionStatus(this.chatAbilities, Permissions.CHAT_RECEIVE_SYSTEM_MESSAGES, "receive_system_messages"));
|
| 72 | permissionEntries.add(createPermissionStatus(this.chatAbilities, Permissions.CHAT_RECEIVE_PLAYER_MESSAGES, "receive_player_messages"));
|
| 73 | Component message = CommonComponents.joinLines(permissionEntries);
|
| 74 | body.addChild(
|
| 75 | FocusableTextWidget.builder(message, this.font)
|
| 76 | .maxWidth(250)
|
| 77 | .alwaysShowBorder(false)
|
| 78 | .backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
|
| 79 | .build()
|
| 80 | .setCentered(true)
|
| 81 | );
|
| 82 | this.bodyScroll = new ScrollableLayout(this.minecraft, body, this.layout.getContentHeight());
|
| 83 | this.layout.addToContents(this.bodyScroll);
|
| 84 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 85 | footer.addChild(Button.builder(CommonComponents.GUI_DONE, var1x -> this.onClose()).width(200).build());
|
| 86 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 87 | this.repositionElements();
|
| 88 | }
|
| 89 |
|
| 90 | private static Component createPermissionStatus(final ChatAbilities abilities, final Permission permission, final String permissionId) {
|
| 91 | boolean isAllowed = abilities.permissions().hasPermission(permission);
|
| 92 | String prefix = "restrictions_screen.permission." + permissionId;
|
| 93 | return isAllowed
|
| 94 | ? Component.translatable(prefix + ".allowed").withStyle(ChatFormatting.GREEN)
|
| 95 | : Component.translatable(prefix + ".denied").withStyle(ChatFormatting.RED);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void onClose() {
|
| 100 | this.minecraft.gui.setScreen(this.previousScreen);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | protected void repositionElements() {
|
| 105 | this.bodyScroll.arrangeElements();
|
| 106 | this.bodyScroll.setMaxHeight(this.layout.getContentHeight());
|
| 107 | this.layout.arrangeElements();
|
| 108 | }
|
| 109 | }
|
| 110 |
|