AbstractGameRulesScreen .java

net.minecraft.client.gui.screens.worldselection.AbstractGameRulesScreen (class): members: createAndConfigureSearchBox, closeAndDiscardChanges, RuleEntry.

net.minecraft.client.gui.screens.worldselectionclass325 linesclientextends Screen12.5 KB

API Surface (16)

void createAndConfigureSearchBox(final LinearLayout headerLayout)
void init()
void repositionElements()
void setInitialFocus()
void closeAndDiscardChanges()
void closeAndApplyChanges()
void filterGameRules(final String filter)
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
List<? extends GuiEventListener> children()
List<? extends NarratableEntry> narratables()
List<? extends GuiEventListener> children()
List<? extends NarratableEntry> narratables()
void extractLabel(final GuiGraphicsExtractor graphics, final int rowTop, final int rowLeft)
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)

Fields & Constants (10)

TITLE
SEARCH_HINT
SEARCH_BOX_HEIGHT
invalidEntries
exitCallback
layout
gameRules
searchBox
ruleList
doneButton

String literals (2)

"editGamerule.title"
"gui.game_rule.search"

Full Source

AbstractGameRulesScreen.java325 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.worldselection;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import com.google.common.collect.Sets;
6
import com.mojang.serialization.DataResult;
7
import java.util.Comparator;
8
import java.util.List;
9
import java.util.Locale;
10
import java.util.Map;
11
import java.util.Optional;
12
import java.util.Set;
13
import java.util.function.Consumer;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.ChatFormatting;
17
import net.minecraft.client.Minecraft;
18
import net.minecraft.client.gui.GuiGraphicsExtractor;
19
import net.minecraft.client.gui.components.AbstractWidget;
20
import net.minecraft.client.gui.components.Button;
21
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
22
import net.minecraft.client.gui.components.CycleButton;
23
import net.minecraft.client.gui.components.EditBox;
24
import net.minecraft.client.gui.components.FocusableTextWidget;
25
import net.minecraft.client.gui.components.StringWidget;
26
import net.minecraft.client.gui.components.events.GuiEventListener;
27
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
28
import net.minecraft.client.gui.layouts.LinearLayout;
29
import net.minecraft.client.gui.narration.NarratableEntry;
30
import net.minecraft.client.gui.screens.Screen;
31
import net.minecraft.client.gui.screens.worldselection.AbstractGameRulesScreen.RuleList.1;
32
import net.minecraft.network.chat.CommonComponents;
33
import net.minecraft.network.chat.Component;
34
import net.minecraft.network.chat.MutableComponent;
35
import net.minecraft.util.FormattedCharSequence;
36
import net.minecraft.world.level.gamerules.GameRule;
37
import net.minecraft.world.level.gamerules.GameRuleCategory;
38
import net.minecraft.world.level.gamerules.GameRules;
39
import org.jspecify.annotations.Nullable;
40
41
@Environment(EnvType.CLIENT)
42
public abstract class AbstractGameRulesScreen extends Screen {
43
	protected static final Component TITLE = Component.translatable("editGamerule.title");
44
	private static final Component SEARCH_HINT = Component.translatable("gui.game_rule.search").withStyle(EditBox.SEARCH_HINT_STYLE);
45
	private static final int SEARCH_BOX_HEIGHT = 15;
46
	private final Set<AbstractGameRulesScreen.RuleEntry> invalidEntries = Sets.newHashSet();
47
	private final Consumer<Optional<GameRules>> exitCallback;
48
	protected final HeaderAndFooterLayout layout;
49
	protected final GameRules gameRules;
50
	protected @Nullable EditBox searchBox;
51
	protected AbstractGameRulesScreen.@Nullable RuleList ruleList;
52
	protected @Nullable Button doneButton;
53
54
	public AbstractGameRulesScreen(final GameRules gameRules, final Consumer<Optional<GameRules>> exitCallback) {
55
		super(TITLE);
56
		this.gameRules = gameRules;
57
		this.exitCallback = exitCallback;
58
		this.layout = new HeaderAndFooterLayout(this, (int)(12.0 + 9.0 + 15.0), 33);
59
	}
60
61
	protected void createAndConfigureSearchBox(final LinearLayout headerLayout) {
62
		this.searchBox = headerLayout.addChild(new EditBox(this.font, 200, 15, Component.empty()));
63
		this.searchBox.setHint(SEARCH_HINT);
64
		this.searchBox.setResponder(this::filterGameRules);
65
	}
66
67
	@Override
68
	protected void init() {
69
		LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(4));
70
		header.defaultCellSetting().alignHorizontallyCenter();
71
		header.addChild(new StringWidget(TITLE, this.font));
72
		this.createAndConfigureSearchBox(header);
73
		this.initContent();
74
		LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
75
		this.doneButton = footer.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).build());
76
		footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).build());
77
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
78
		this.repositionElements();
79
	}
80
81
	protected abstract void initContent();
82
83
	protected abstract void onDone();
84
85
	@Override
86
	protected void repositionElements() {
87
		this.layout.arrangeElements();
88
		if (this.ruleList != null) {
89
			this.ruleList.updateSize(this.width, this.layout);
90
		}
91
	}
92
93
	@Override
94
	protected void setInitialFocus() {
95
		if (this.searchBox != null) {
96
			this.setInitialFocus(this.searchBox);
97
		}
98
	}
99
100
	private void markInvalid(final AbstractGameRulesScreen.RuleEntry invalidEntry) {
101
		this.invalidEntries.add(invalidEntry);
102
		this.updateDoneButton();
103
	}
104
105
	private void clearInvalid(final AbstractGameRulesScreen.RuleEntry invalidEntry) {
106
		this.invalidEntries.remove(invalidEntry);
107
		this.updateDoneButton();
108
	}
109
110
	private void updateDoneButton() {
111
		if (this.doneButton != null) {
112
			this.doneButton.active = this.invalidEntries.isEmpty();
113
		}
114
	}
115
116
	protected void closeAndDiscardChanges() {
117
		this.exitCallback.accept(Optional.empty());
118
	}
119
120
	protected void closeAndApplyChanges() {
121
		this.exitCallback.accept(Optional.of(this.gameRules));
122
	}
123
124
	protected void filterGameRules(final String filter) {
125
		if (this.ruleList != null) {
126
			this.ruleList.populateChildren(filter);
127
			this.ruleList.setScrollAmount(0.0);
128
			this.repositionElements();
129
		}
130
	}
131
132
	@Environment(EnvType.CLIENT)
133
	public class BooleanRuleEntry extends AbstractGameRulesScreen.GameRuleEntry {
134
		private final CycleButton<Boolean> checkbox;
135
136
		public BooleanRuleEntry(final Component name, final List<FormattedCharSequence> tooltip, final String narration, final GameRule<Boolean> gameRule) {
137
			super(tooltip, name);
138
			this.checkbox = CycleButton.onOffBuilder(AbstractGameRulesScreen.this.gameRules.get(gameRule))
139
				.displayOnlyValue()
140
				.withCustomNarration(button -> button.createDefaultNarrationMessage().append("\n").append(narration))
141
				.create(10, 5, 44, 20, name, (button, newValue) -> AbstractGameRulesScreen.this.gameRules.set(gameRule, newValue, null));
142
			this.children.add(this.checkbox);
143
		}
144
145
		@Override
146
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
147
			this.extractLabel(graphics, this.getContentY(), this.getContentX());
148
			this.checkbox.setX(this.getContentRight() - 45);
149
			this.checkbox.setY(this.getContentY());
150
			this.checkbox.extractRenderState(graphics, mouseX, mouseY, a);
151
		}
152
	}
153
154
	@Environment(EnvType.CLIENT)
155
	public class CategoryRuleEntry extends AbstractGameRulesScreen.RuleEntry {
156
		private final FocusableTextWidget widget;
157
158
		public CategoryRuleEntry(final Component label) {
159
			super(null);
160
			this.widget = FocusableTextWidget.builder(label, AbstractGameRulesScreen.this.minecraft.font)
161
				.alwaysShowBorder(false)
162
				.backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
163
				.build();
164
		}
165
166
		@Override
167
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
168
			this.widget.setPosition(this.getContentXMiddle() - this.widget.getWidth() / 2, this.getContentYMiddle() - this.widget.getHeight() / 2);
169
			this.widget.extractRenderState(graphics, mouseX, mouseY, a);
170
		}
171
172
		@Override
173
		public List<? extends GuiEventListener> children() {
174
			return List.of(this.widget);
175
		}
176
177
		@Override
178
		public List<? extends NarratableEntry> narratables() {
179
			return List.of(this.widget);
180
		}
181
	}
182
183
	@FunctionalInterface
184
	@Environment(EnvType.CLIENT)
185
	private interface EntryFactory<T> {
186
		AbstractGameRulesScreen.RuleEntry create(Component name, List<FormattedCharSequence> tooltip, String narration, GameRule<T> gameRule);
187
	}
188
189
	@Environment(EnvType.CLIENT)
190
	public abstract class GameRuleEntry extends AbstractGameRulesScreen.RuleEntry {
191
		private final List<FormattedCharSequence> label;
192
		protected final List<AbstractWidget> children = Lists.newArrayList();
193
194
		public GameRuleEntry(final @Nullable List<FormattedCharSequence> tooltip, final Component label) {
195
			super(tooltip);
196
			this.label = AbstractGameRulesScreen.this.minecraft.font.split(label, 170);
197
		}
198
199
		@Override
200
		public List<? extends GuiEventListener> children() {
201
			return this.children;
202
		}
203
204
		@Override
205
		public List<? extends NarratableEntry> narratables() {
206
			return this.children;
207
		}
208
209
		protected void extractLabel(final GuiGraphicsExtractor graphics, final int rowTop, final int rowLeft) {
210
			if (this.label.size() == 1) {
211
				graphics.text(AbstractGameRulesScreen.this.minecraft.font, this.label.get(0), rowLeft, rowTop + 5, -1);
212
			} else if (this.label.size() >= 2) {
213
				graphics.text(AbstractGameRulesScreen.this.minecraft.font, this.label.get(0), rowLeft, rowTop, -1);
214
				graphics.text(AbstractGameRulesScreen.this.minecraft.font, this.label.get(1), rowLeft, rowTop + 10, -1);
215
			}
216
		}
217
	}
218
219
	@Environment(EnvType.CLIENT)
220
	public class IntegerRuleEntry extends AbstractGameRulesScreen.GameRuleEntry {
221
		private final EditBox input;
222
223
		public IntegerRuleEntry(final Component label, final List<FormattedCharSequence> tooltip, final String narration, final GameRule<Integer> gameRule) {
224
			super(tooltip, label);
225
			this.input = new EditBox(AbstractGameRulesScreen.this.minecraft.font, 10, 5, 44, 20, label.copy().append("\n").append(narration).append("\n"));
226
			this.input.setValue(AbstractGameRulesScreen.this.gameRules.getAsString(gameRule));
227
			this.input.setResponder(v -> {
228
				DataResult<Integer> value = gameRule.deserialize(v);
229
				if (value.isSuccess()) {
230
					this.input.setTextColor(-2039584);
231
					AbstractGameRulesScreen.this.clearInvalid(this);
232
					AbstractGameRulesScreen.this.gameRules.set(gameRule, value.getOrThrow(), null);
233
				} else {
234
					this.input.setTextColor(-65536);
235
					AbstractGameRulesScreen.this.markInvalid(this);
236
				}
237
			});
238
			this.children.add(this.input);
239
		}
240
241
		@Override
242
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
243
			this.extractLabel(graphics, this.getContentY(), this.getContentX());
244
			this.input.setX(this.getContentRight() - 45);
245
			this.input.setY(this.getContentY());
246
			this.input.extractRenderState(graphics, mouseX, mouseY, a);
247
		}
248
	}
249
250
	@Environment(EnvType.CLIENT)
251
	public abstract static class RuleEntry extends ContainerObjectSelectionList.Entry<AbstractGameRulesScreen.RuleEntry> {
252
		private final @Nullable List<FormattedCharSequence> tooltip;
253
254
		public RuleEntry(final @Nullable List<FormattedCharSequence> tooltip) {
255
			this.tooltip = tooltip;
256
		}
257
	}
258
259
	@Environment(EnvType.CLIENT)
260
	public class RuleList extends ContainerObjectSelectionList<AbstractGameRulesScreen.RuleEntry> {
261
		private static final int ITEM_HEIGHT = 24;
262
		private final GameRules gameRules;
263
264
		public RuleList(final GameRules gameRules) {
265
			super(
266
				Minecraft.getInstance(),
267
				AbstractGameRulesScreen.this.width,
268
				AbstractGameRulesScreen.this.layout.getContentHeight(),
269
				AbstractGameRulesScreen.this.layout.getHeaderHeight(),
270
				24
271
			);
272
			this.gameRules = gameRules;
273
			this.populateChildren("");
274
		}
275
276
		private void populateChildren(final String filter) {
277
			this.clearEntries();
278
			Map<GameRuleCategory, Map<GameRule<?>, AbstractGameRulesScreen.RuleEntry>> entries = Maps.newHashMap();
279
			String lowerCaseFilter = filter.toLowerCase(Locale.ROOT);
280
			this.gameRules.visitGameRuleTypes(new 1(this, lowerCaseFilter, entries));
281
			entries.entrySet()
282
				.stream()
283
				.sorted(Map.Entry.comparingByKey(Comparator.comparing(GameRuleCategory::getDescriptionId)))
284
				.forEach(
285
					e -> {
286
						this.addEntry(
287
							AbstractGameRulesScreen.this.new CategoryRuleEntry(e.getKey().label().withStyle(ChatFormatting.BOLD, ChatFormatting.YELLOW, ChatFormatting.UNDERLINE))
288
						);
289
						e.getValue()
290
							.entrySet()
291
							.stream()
292
							.sorted(Map.Entry.comparingByKey(Comparator.comparing(GameRule::getDescriptionId)))
293
							.forEach(v -> this.addEntry(v.getValue()));
294
					}
295
				);
296
		}
297
298
		private static boolean matchesFilter(
299
			final String gameRuleId,
300
			final String readableName,
301
			final String categoryName,
302
			final Optional<MutableComponent> optionalDescription,
303
			final String lowerCaseFilter
304
		) {
305
			return toLowerCaseMatchesFilter(gameRuleId, lowerCaseFilter)
306
				|| toLowerCaseMatchesFilter(readableName, lowerCaseFilter)
307
				|| toLowerCaseMatchesFilter(categoryName, lowerCaseFilter)
308
				|| optionalDescription.<Boolean>map(description -> toLowerCaseMatchesFilter(description.getString(), lowerCaseFilter)).orElse(false);
309
		}
310
311
		private static boolean toLowerCaseMatchesFilter(final String gameRuleId, final String lowerCaseFilter) {
312
			return gameRuleId.toLowerCase(Locale.ROOT).contains(lowerCaseFilter);
313
		}
314
315
		@Override
316
		public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
317
			super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
318
			AbstractGameRulesScreen.RuleEntry hovered = this.getHovered();
319
			if (hovered != null && hovered.tooltip != null) {
320
				graphics.setTooltipForNextFrame(hovered.tooltip, mouseX, mouseY);
321
			}
322
		}
323
	}
324
}
325

Decompiled Minecraft 26.3 source of AbstractGameRulesScreen (net/minecraft/client/gui/screens/worldselection/AbstractGameRulesScreen.java) with Mojang official mappings, stored verbatim on the site.