GameModeSwitcherScreen .java

net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen (class): members: GAMEMODE_SWITCHER_LOCATION, SPRITE_SHEET_HEIGHT, HELP_TIPS_OFFSET_Y, slots.

net.minecraft.client.gui.screens.debugclass243 linesclientextends Screen9.0 KB

API Surface (11)

void init()
void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
boolean keyPressed(final KeyEvent event)
boolean keyReleased(final KeyEvent event)
boolean mouseReleased(final MouseButtonEvent event)
boolean isPauseScreen()
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void updateWidgetNarration(final NarrationElementOutput output)
boolean isHoveredOrFocused()
void setSelected(final boolean isSelected)

Fields & Constants (16)

SLOT_SPRITE
SELECTION_SPRITE
GAMEMODE_SWITCHER_LOCATION
SPRITE_SHEET_WIDTH
SPRITE_SHEET_HEIGHT
SLOT_AREA
SLOT_PADDING
SLOT_AREA_PADDED
HELP_TIPS_OFFSET_Y
ALL_SLOTS_WIDTH
previousHovered
currentlyHovered
firstMouseX
firstMouseY
setFirstMousePos
slots

String literals (8)

"gamemode_switcher/slot"
"gamemode_switcher/selection"
"textures/gui/container/gamemode_switcher.png"
"debug.gamemodes.select_next"
"gameMode.creative"
"gameMode.survival"
"gameMode.adventure"
"gameMode.spectator"

Full Source

GameModeSwitcherScreen.java243 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.debug;
2
3
import com.google.common.collect.Lists;
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.GameNarrator;
9
import net.minecraft.client.Minecraft;
10
import net.minecraft.client.gui.GuiGraphicsExtractor;
11
import net.minecraft.client.gui.components.AbstractWidget;
12
import net.minecraft.client.gui.narration.NarrationElementOutput;
13
import net.minecraft.client.gui.screens.Screen;
14
import net.minecraft.client.input.KeyEvent;
15
import net.minecraft.client.input.MouseButtonEvent;
16
import net.minecraft.client.multiplayer.MultiPlayerGameMode;
17
import net.minecraft.client.renderer.RenderPipelines;
18
import net.minecraft.network.chat.Component;
19
import net.minecraft.network.chat.MutableComponent;
20
import net.minecraft.network.protocol.game.ServerboundChangeGameModePacket;
21
import net.minecraft.resources.Identifier;
22
import net.minecraft.server.commands.GameModeCommand;
23
import net.minecraft.world.item.ItemStack;
24
import net.minecraft.world.item.Items;
25
import net.minecraft.world.level.GameType;
26
import net.minecraft.world.level.block.Blocks;
27
28
@Environment(EnvType.CLIENT)
29
public class GameModeSwitcherScreen extends Screen {
30
	private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("gamemode_switcher/slot");
31
	private static final Identifier SELECTION_SPRITE = Identifier.withDefaultNamespace("gamemode_switcher/selection");
32
	private static final Identifier GAMEMODE_SWITCHER_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/gamemode_switcher.png");
33
	private static final int SPRITE_SHEET_WIDTH = 128;
34
	private static final int SPRITE_SHEET_HEIGHT = 128;
35
	private static final int SLOT_AREA = 26;
36
	private static final int SLOT_PADDING = 5;
37
	private static final int SLOT_AREA_PADDED = 31;
38
	private static final int HELP_TIPS_OFFSET_Y = 5;
39
	private static final int ALL_SLOTS_WIDTH = GameModeSwitcherScreen.GameModeIcon.values().length * 31 - 5;
40
	private final GameModeSwitcherScreen.GameModeIcon previousHovered;
41
	private GameModeSwitcherScreen.GameModeIcon currentlyHovered;
42
	private int firstMouseX;
43
	private int firstMouseY;
44
	private boolean setFirstMousePos;
45
	private final List<GameModeSwitcherScreen.GameModeSlot> slots = Lists.newArrayList();
46
47
	public GameModeSwitcherScreen() {
48
		super(GameNarrator.NO_TITLE);
49
		this.previousHovered = GameModeSwitcherScreen.GameModeIcon.getFromGameType(this.getDefaultSelected());
50
		this.currentlyHovered = this.previousHovered;
51
	}
52
53
	private GameType getDefaultSelected() {
54
		MultiPlayerGameMode gameMode = Minecraft.getInstance().gameMode;
55
		GameType previous = gameMode.getPreviousPlayerMode();
56
		if (previous != null) {
57
			return previous;
58
		} else {
59
			return gameMode.getPlayerMode() == GameType.CREATIVE ? GameType.SURVIVAL : GameType.CREATIVE;
60
		}
61
	}
62
63
	@Override
64
	protected void init() {
65
		super.init();
66
		this.slots.clear();
67
		this.currentlyHovered = this.previousHovered;
68
69
		for (int i = 0; i < GameModeSwitcherScreen.GameModeIcon.VALUES.length; i++) {
70
			GameModeSwitcherScreen.GameModeIcon icon = GameModeSwitcherScreen.GameModeIcon.VALUES[i];
71
			this.slots.add(new GameModeSwitcherScreen.GameModeSlot(icon, this.width / 2 - ALL_SLOTS_WIDTH / 2 + i * 31, this.height / 2 - 31));
72
		}
73
	}
74
75
	@Override
76
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
77
		graphics.centeredText(this.font, this.currentlyHovered.name, this.width / 2, this.height / 2 - 31 - 20, -1);
78
		MutableComponent selectKey = Component.translatable(
79
			"debug.gamemodes.select_next", this.minecraft.options.keyDebugSwitchGameMode.getTranslatedKeyMessage().copy().withStyle(ChatFormatting.AQUA)
80
		);
81
		graphics.centeredText(this.font, selectKey, this.width / 2, this.height / 2 + 5, -1);
82
		if (!this.setFirstMousePos) {
83
			this.firstMouseX = mouseX;
84
			this.firstMouseY = mouseY;
85
			this.setFirstMousePos = true;
86
		}
87
88
		boolean sameAsFirstMousePos = this.firstMouseX == mouseX && this.firstMouseY == mouseY;
89
90
		for (GameModeSwitcherScreen.GameModeSlot slot : this.slots) {
91
			slot.extractRenderState(graphics, mouseX, mouseY, a);
92
			slot.setSelected(this.currentlyHovered == slot.icon);
93
			if (!sameAsFirstMousePos && slot.isHoveredOrFocused()) {
94
				this.currentlyHovered = slot.icon;
95
			}
96
		}
97
	}
98
99
	@Override
100
	public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
101
		int xo = this.width / 2 - 62;
102
		int yo = this.height / 2 - 31 - 27;
103
		graphics.blit(RenderPipelines.GUI_TEXTURED, GAMEMODE_SWITCHER_LOCATION, xo, yo, 0.0F, 0.0F, 125, 75, 128, 128);
104
	}
105
106
	private void switchToHoveredGameMode() {
107
		switchToHoveredGameMode(this.minecraft, this.currentlyHovered);
108
	}
109
110
	private static void switchToHoveredGameMode(final Minecraft minecraft, final GameModeSwitcherScreen.GameModeIcon toGameMode) {
111
		if (minecraft.canSwitchGameMode()) {
112
			GameModeSwitcherScreen.GameModeIcon currentGameMode = GameModeSwitcherScreen.GameModeIcon.getFromGameType(minecraft.gameMode.getPlayerMode());
113
			if (toGameMode != currentGameMode && GameModeCommand.PERMISSION_CHECK.check(minecraft.player.permissions())) {
114
				minecraft.player.connection.send(new ServerboundChangeGameModePacket(toGameMode.mode));
115
			}
116
		}
117
	}
118
119
	@Override
120
	public boolean keyPressed(final KeyEvent event) {
121
		if (this.minecraft.options.keyDebugSwitchGameMode.matches(event)) {
122
			this.setFirstMousePos = false;
123
			this.currentlyHovered = this.currentlyHovered.getNext();
124
			return true;
125
		} else {
126
			return super.keyPressed(event);
127
		}
128
	}
129
130
	@Override
131
	public boolean keyReleased(final KeyEvent event) {
132
		if (this.minecraft.options.keyDebugModifier.matches(event)) {
133
			this.switchToHoveredGameMode();
134
			this.minecraft.gui.setScreen(null);
135
			return true;
136
		} else {
137
			return super.keyReleased(event);
138
		}
139
	}
140
141
	@Override
142
	public boolean mouseReleased(final MouseButtonEvent event) {
143
		if (this.minecraft.options.keyDebugModifier.matchesMouse(event)) {
144
			this.switchToHoveredGameMode();
145
			this.minecraft.gui.setScreen(null);
146
			return true;
147
		} else {
148
			return super.mouseReleased(event);
149
		}
150
	}
151
152
	@Override
153
	public boolean isPauseScreen() {
154
		return false;
155
	}
156
157
	@Environment(EnvType.CLIENT)
158
	private enum GameModeIcon {
159
		CREATIVE(Component.translatable("gameMode.creative"), GameType.CREATIVE, new ItemStack(Blocks.GRASS_BLOCK)),
160
		SURVIVAL(Component.translatable("gameMode.survival"), GameType.SURVIVAL, new ItemStack(Items.IRON_SWORD)),
161
		ADVENTURE(Component.translatable("gameMode.adventure"), GameType.ADVENTURE, new ItemStack(Items.BURIED_TREASURE_MAP)),
162
		SPECTATOR(Component.translatable("gameMode.spectator"), GameType.SPECTATOR, new ItemStack(Items.ENDER_EYE));
163
164
		private static final GameModeSwitcherScreen.GameModeIcon[] VALUES = values();
165
		private static final int ICON_AREA = 16;
166
		private static final int ICON_TOP_LEFT = 5;
167
		private final Component name;
168
		private final GameType mode;
169
		private final ItemStack renderStack;
170
171
		GameModeIcon(final Component name, final GameType mode, final ItemStack renderStack) {
172
			this.name = name;
173
			this.mode = mode;
174
			this.renderStack = renderStack;
175
		}
176
177
		private void extractIcon(final GuiGraphicsExtractor graphics, final int x, final int y) {
178
			graphics.item(this.renderStack, x, y);
179
		}
180
181
		private GameModeSwitcherScreen.GameModeIcon getNext() {
182
			return switch (this) {
183
				case CREATIVE -> SURVIVAL;
184
				case SURVIVAL -> ADVENTURE;
185
				case ADVENTURE -> SPECTATOR;
186
				case SPECTATOR -> CREATIVE;
187
			};
188
		}
189
190
		private static GameModeSwitcherScreen.GameModeIcon getFromGameType(final GameType gameType) {
191
			return switch (gameType) {
192
				case SPECTATOR -> SPECTATOR;
193
				case SURVIVAL -> SURVIVAL;
194
				case CREATIVE -> CREATIVE;
195
				case ADVENTURE -> ADVENTURE;
196
			};
197
		}
198
	}
199
200
	@Environment(EnvType.CLIENT)
201
	public static class GameModeSlot extends AbstractWidget {
202
		private final GameModeSwitcherScreen.GameModeIcon icon;
203
		private boolean isSelected;
204
205
		public GameModeSlot(final GameModeSwitcherScreen.GameModeIcon icon, final int x, final int y) {
206
			super(x, y, 26, 26, icon.name);
207
			this.icon = icon;
208
		}
209
210
		@Override
211
		public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
212
			this.extractSlot(graphics);
213
			if (this.isSelected) {
214
				this.extractSelection(graphics);
215
			}
216
217
			this.icon.extractIcon(graphics, this.getX() + 5, this.getY() + 5);
218
		}
219
220
		@Override
221
		public void updateWidgetNarration(final NarrationElementOutput output) {
222
			this.defaultButtonNarrationText(output);
223
		}
224
225
		@Override
226
		public boolean isHoveredOrFocused() {
227
			return super.isHoveredOrFocused() || this.isSelected;
228
		}
229
230
		public void setSelected(final boolean isSelected) {
231
			this.isSelected = isSelected;
232
		}
233
234
		private void extractSlot(final GuiGraphicsExtractor graphics) {
235
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, GameModeSwitcherScreen.SLOT_SPRITE, this.getX(), this.getY(), 26, 26);
236
		}
237
238
		private void extractSelection(final GuiGraphicsExtractor graphics) {
239
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, GameModeSwitcherScreen.SELECTION_SPRITE, this.getX(), this.getY(), 26, 26);
240
		}
241
	}
242
}
243

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