| 1 | package net.minecraft.client.gui.screens.options;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.gui.components.LoadingDotsWidget;
|
| 11 | import net.minecraft.client.gui.screens.ConfirmScreen;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.client.gui.screens.worldselection.AbstractGameRulesScreen;
|
| 14 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 15 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
|
| 18 | import net.minecraft.network.protocol.game.ServerboundSetGameRulePacket;
|
| 19 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action;
|
| 20 | import net.minecraft.network.protocol.game.ServerboundSetGameRulePacket.Entry;
|
| 21 | import net.minecraft.resources.ResourceKey;
|
| 22 | import net.minecraft.world.level.gamerules.GameRule;
|
| 23 | import net.minecraft.world.level.gamerules.GameRuleMap;
|
| 24 | import net.minecraft.world.level.gamerules.GameRules;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class InWorldGameRulesScreen extends AbstractGameRulesScreen implements HasGamemasterPermissionReaction {
|
| 29 | private static final Component PENDING_TEXT = Component.translatable("editGamerule.inGame.downloadingGamerules");
|
| 30 | private final GameRuleMap initialValues = GameRuleMap.of();
|
| 31 | private final List<GameRule<?>> serverProvidedRules = new ArrayList<>();
|
| 32 | private final ClientPacketListener connection;
|
| 33 | private final Screen lastScreen;
|
| 34 | private @Nullable LoadingDotsWidget loadingDotsWidget;
|
| 35 | private boolean receivedServerValues = false;
|
| 36 |
|
| 37 | public InWorldGameRulesScreen(final ClientPacketListener connection, final Consumer<Optional<GameRules>> exitCallback, final Screen lastScreen) {
|
| 38 | super(new GameRules(connection.enabledFeatures()), exitCallback);
|
| 39 | this.connection = connection;
|
| 40 | this.lastScreen = lastScreen;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void initContent() {
|
| 45 | this.loadingDotsWidget = new LoadingDotsWidget(this.font, PENDING_TEXT);
|
| 46 | this.layout.addToContents(this.loadingDotsWidget);
|
| 47 | this.connection.send(new ServerboundClientCommandPacket(Action.REQUEST_GAMERULE_VALUES));
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected void onDone() {
|
| 52 | List<Entry> changedEntries = new ArrayList<>();
|
| 53 | this.initialValues.keySet().forEach(rule -> this.collectChangedGameRule((GameRule<?>)rule, changedEntries));
|
| 54 | if (!changedEntries.isEmpty()) {
|
| 55 | this.connection.send(new ServerboundSetGameRulePacket(changedEntries));
|
| 56 | }
|
| 57 |
|
| 58 | this.closeAndApplyChanges();
|
| 59 | }
|
| 60 |
|
| 61 | private <T> void collectChangedGameRule(final GameRule<T> rule, final List<Entry> entries) {
|
| 62 | if (this.hasGameRuleChanged(rule)) {
|
| 63 | T currentValue = this.gameRules.get(rule);
|
| 64 | BuiltInRegistries.GAME_RULE.getResourceKey(rule).ifPresent(key -> entries.add(new Entry((ResourceKey<GameRule<?>>)key, rule.serialize(currentValue))));
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public void onClose() {
|
| 70 | if (this.hasPendingChanges()) {
|
| 71 | this.minecraft.gui.setScreen(new ConfirmScreen(confirmed -> {
|
| 72 | if (confirmed) {
|
| 73 | this.closeAndDiscardChanges();
|
| 74 | } else {
|
| 75 | this.minecraft.gui.setScreen(this);
|
| 76 | }
|
| 77 | }, Component.translatable("editGamerule.inGame.discardChanges.title"), Component.translatable("editGamerule.inGame.discardChanges.message")));
|
| 78 | } else {
|
| 79 | this.closeAndDiscardChanges();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | private boolean hasPendingChanges() {
|
| 84 | return this.initialValues.keySet().stream().anyMatch(this::hasGameRuleChanged);
|
| 85 | }
|
| 86 |
|
| 87 | private <T> boolean hasGameRuleChanged(final GameRule<T> rule) {
|
| 88 | return !this.gameRules.get(rule).equals(this.initialValues.get(rule));
|
| 89 | }
|
| 90 |
|
| 91 | public void onGameRuleValuesUpdated(final Map<ResourceKey<GameRule<?>>, String> values) {
|
| 92 | if (!this.receivedServerValues) {
|
| 93 | this.receivedServerValues = true;
|
| 94 | values.forEach((key, valueStr) -> {
|
| 95 | GameRule<?> rule = BuiltInRegistries.GAME_RULE.getValue((ResourceKey<GameRule<?>>)key);
|
| 96 | if (rule != null) {
|
| 97 | this.serverProvidedRules.add(rule);
|
| 98 | this.initializeGameRuleValue(rule, valueStr);
|
| 99 | }
|
| 100 | });
|
| 101 | if (this.loadingDotsWidget != null) {
|
| 102 | this.removeWidget(this.loadingDotsWidget);
|
| 103 | }
|
| 104 |
|
| 105 | GameRules serverGameRules = new GameRules(this.serverProvidedRules);
|
| 106 | this.ruleList = this.layout.addToContents(new AbstractGameRulesScreen.RuleList(serverGameRules));
|
| 107 | this.addRenderableWidget(this.ruleList);
|
| 108 | this.repositionElements();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private <T> void initializeGameRuleValue(final GameRule<T> rule, final String valueStr) {
|
| 113 | rule.deserialize(valueStr).result().ifPresent(value -> {
|
| 114 | this.initialValues.set(rule, (T)value);
|
| 115 | this.gameRules.set(rule, (T)value, null);
|
| 116 | });
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public void onGamemasterPermissionChanged(final boolean hasGamemasterPermission) {
|
| 121 | if (!hasGamemasterPermission) {
|
| 122 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 123 | if (this.minecraft.gui.screen() instanceof HasGamemasterPermissionReaction screen) {
|
| 124 | screen.onGamemasterPermissionChanged(hasGamemasterPermission);
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 | }
|
| 129 |
|