GameType .java

net.minecraft.world.level.GameType (enum): members: SURVIVAL, CREATIVE, ADVENTURE, SPECTATOR, isBlockPlacingRestricted, updatePlayerAbilities, isValidId.

net.minecraft.world.levelenum118 linescommon3.5 KB

Implements

API Surface (12)

int getId()
String getName()
String getSerializedName()
Component getLongDisplayName()
Component getShortDisplayName()
void updatePlayerAbilities(final Abilities abilities)
boolean isBlockPlacingRestricted()
boolean isCreative()
boolean isSurvival()
GameType byId(final int id)
GameType byName(final String name)
boolean isValidId(final int id)

Fields & Constants (10)

DEFAULT_MODE
CODEC
BY_ID
STREAM_CODEC
OPTIONAL_STREAM_CODEC
LEGACY_ID_CODEC
id
name
shortName
longName

String literals (7)

"survival"
"creative"
"adventure"
"spectator"
"selectWorld.gameMode."
"gameMode."
"_,!null->!null;_,null->_"

Code Graph: Dependencies (6)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

GameType.java118 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.Arrays;
6
import java.util.Optional;
7
import java.util.OptionalInt;
8
import java.util.function.IntFunction;
9
import net.minecraft.network.chat.Component;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.util.ByIdMap;
13
import net.minecraft.util.StringRepresentable;
14
import net.minecraft.world.entity.player.Abilities;
15
import org.jetbrains.annotations.Contract;
16
import org.jspecify.annotations.Nullable;
17
18
public enum GameType implements StringRepresentable {
19
	SURVIVAL(0, "survival"),
20
	CREATIVE(1, "creative"),
21
	ADVENTURE(2, "adventure"),
22
	SPECTATOR(3, "spectator");
23
24
	public static final GameType DEFAULT_MODE = SURVIVAL;
25
	public static final StringRepresentable.EnumCodec<GameType> CODEC = StringRepresentable.fromEnum(GameType::values);
26
	private static final IntFunction<GameType> BY_ID = ByIdMap.continuous(GameType::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
27
	public static final StreamCodec<ByteBuf, GameType> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, GameType::getId);
28
	public static final StreamCodec<ByteBuf, Optional<GameType>> OPTIONAL_STREAM_CODEC = ByteBufCodecs.OPTIONAL_VAR_INT
29
		.map(
30
			id -> id.isPresent() ? Optional.of(byId(id.getAsInt())) : Optional.empty(),
31
			gameType -> gameType.isPresent() ? OptionalInt.of(gameType.get().getId()) : OptionalInt.empty()
32
		);
33
	@Deprecated
34
	public static final Codec<GameType> LEGACY_ID_CODEC = Codec.INT.xmap(GameType::byId, GameType::getId);
35
	private final int id;
36
	private final String name;
37
	private final Component shortName;
38
	private final Component longName;
39
40
	GameType(final int id, final String name) {
41
		this.id = id;
42
		this.name = name;
43
		this.shortName = Component.translatable("selectWorld.gameMode." + name);
44
		this.longName = Component.translatable("gameMode." + name);
45
	}
46
47
	public int getId() {
48
		return this.id;
49
	}
50
51
	public String getName() {
52
		return this.name;
53
	}
54
55
	@Override
56
	public String getSerializedName() {
57
		return this.name;
58
	}
59
60
	public Component getLongDisplayName() {
61
		return this.longName;
62
	}
63
64
	public Component getShortDisplayName() {
65
		return this.shortName;
66
	}
67
68
	public void updatePlayerAbilities(final Abilities abilities) {
69
		if (this == CREATIVE) {
70
			abilities.mayfly = true;
71
			abilities.instabuild = true;
72
			abilities.invulnerable = true;
73
		} else if (this == SPECTATOR) {
74
			abilities.mayfly = true;
75
			abilities.instabuild = false;
76
			abilities.invulnerable = true;
77
			abilities.flying = true;
78
		} else {
79
			abilities.mayfly = false;
80
			abilities.instabuild = false;
81
			abilities.invulnerable = false;
82
			abilities.flying = false;
83
		}
84
85
		abilities.mayBuild = !this.isBlockPlacingRestricted();
86
	}
87
88
	public boolean isBlockPlacingRestricted() {
89
		return this == ADVENTURE || this == SPECTATOR;
90
	}
91
92
	public boolean isCreative() {
93
		return this == CREATIVE;
94
	}
95
96
	public boolean isSurvival() {
97
		return this == SURVIVAL || this == ADVENTURE;
98
	}
99
100
	public static GameType byId(final int id) {
101
		return BY_ID.apply(id);
102
	}
103
104
	public static GameType byName(final String name) {
105
		return byName(name, SURVIVAL);
106
	}
107
108
	@Contract("_,!null->!null;_,null->_")
109
	public static @Nullable GameType byName(final String name, final @Nullable GameType defaultMode) {
110
		GameType result = CODEC.byName(name);
111
		return result != null ? result : defaultMode;
112
	}
113
114
	public static boolean isValidId(final int id) {
115
		return Arrays.stream(values()).anyMatch(gameType -> gameType.id == id);
116
	}
117
}
118

Decompiled Minecraft 26.3 source of GameType (net/minecraft/world/level/GameType.java) with Mojang official mappings, stored verbatim on the site.