GameRule .java

net.minecraft.world.level.gamerules.GameRule (class): members: getIdentifierWithFallback, commandResultFunction, getCommandResult, gameRuleType, argument.

net.minecraft.world.level.gamerulesclass118 linescommon3.3 KB

Implements

API Surface (16)

String toString()
String id()
Identifier getIdentifier()
Identifier getIdentifierWithFallback()
String getDescriptionId()
String serialize(final T value)
DataResult<T> deserialize(final String value)
Class<T> valueClass()
void callVisitor(final GameRuleTypeVisitor visitor)
int getCommandResult(final T value)
GameRuleCategory category()
GameRuleType gameRuleType()
ArgumentType<T> argument()
Codec<T> valueCodec()
T defaultValue()
FeatureFlagSet requiredFeatures()

Fields & Constants (8)

category
gameRuleType
argument
visitorCaller
valueCodec
commandResultFunction
defaultValue
requiredFeatures

String literals (3)

"unregistered_sadface"
"Failed to deserialize; trailing characters"
"Failed to deserialize"

Full Source

GameRule.java118 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.gamerules;
2
3
import com.mojang.brigadier.StringReader;
4
import com.mojang.brigadier.arguments.ArgumentType;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.serialization.Codec;
7
import com.mojang.serialization.DataResult;
8
import java.util.Objects;
9
import java.util.function.ToIntFunction;
10
import net.minecraft.core.registries.BuiltInRegistries;
11
import net.minecraft.resources.Identifier;
12
import net.minecraft.util.Util;
13
import net.minecraft.world.flag.FeatureElement;
14
import net.minecraft.world.flag.FeatureFlagSet;
15
16
public final class GameRule<T> implements FeatureElement {
17
	private final GameRuleCategory category;
18
	private final GameRuleType gameRuleType;
19
	private final ArgumentType<T> argument;
20
	private final GameRules.VisitorCaller<T> visitorCaller;
21
	private final Codec<T> valueCodec;
22
	private final ToIntFunction<T> commandResultFunction;
23
	private final T defaultValue;
24
	private final FeatureFlagSet requiredFeatures;
25
26
	public GameRule(
27
		final GameRuleCategory category,
28
		final GameRuleType gameRuleType,
29
		final ArgumentType<T> argument,
30
		final GameRules.VisitorCaller<T> visitorCaller,
31
		final Codec<T> valueCodec,
32
		final ToIntFunction<T> commandResultFunction,
33
		final T defaultValue,
34
		final FeatureFlagSet requiredFeatures
35
	) {
36
		this.category = category;
37
		this.gameRuleType = gameRuleType;
38
		this.argument = argument;
39
		this.visitorCaller = visitorCaller;
40
		this.valueCodec = valueCodec;
41
		this.commandResultFunction = commandResultFunction;
42
		this.defaultValue = defaultValue;
43
		this.requiredFeatures = requiredFeatures;
44
	}
45
46
	@Override
47
	public String toString() {
48
		return this.id();
49
	}
50
51
	public String id() {
52
		return this.getIdentifier().toShortString();
53
	}
54
55
	public Identifier getIdentifier() {
56
		return Objects.requireNonNull(BuiltInRegistries.GAME_RULE.getKey(this));
57
	}
58
59
	public Identifier getIdentifierWithFallback() {
60
		return Objects.requireNonNullElse(BuiltInRegistries.GAME_RULE.getKey(this), Identifier.withDefaultNamespace("unregistered_sadface"));
61
	}
62
63
	public String getDescriptionId() {
64
		return Util.makeDescriptionId("gamerule", this.getIdentifier());
65
	}
66
67
	public String serialize(final T value) {
68
		return value.toString();
69
	}
70
71
	public DataResult<T> deserialize(final String value) {
72
		try {
73
			StringReader reader = new StringReader(value);
74
			T result = this.argument.parse(reader);
75
			return reader.canRead() ? DataResult.error(() -> "Failed to deserialize; trailing characters", result) : DataResult.success(result);
76
		} catch (CommandSyntaxException ignored) {
77
			return DataResult.error(() -> "Failed to deserialize");
78
		}
79
	}
80
81
	public Class<T> valueClass() {
82
		return (Class<T>)this.defaultValue.getClass();
83
	}
84
85
	public void callVisitor(final GameRuleTypeVisitor visitor) {
86
		this.visitorCaller.call(visitor, this);
87
	}
88
89
	public int getCommandResult(final T value) {
90
		return this.commandResultFunction.applyAsInt(value);
91
	}
92
93
	public GameRuleCategory category() {
94
		return this.category;
95
	}
96
97
	public GameRuleType gameRuleType() {
98
		return this.gameRuleType;
99
	}
100
101
	public ArgumentType<T> argument() {
102
		return this.argument;
103
	}
104
105
	public Codec<T> valueCodec() {
106
		return this.valueCodec;
107
	}
108
109
	public T defaultValue() {
110
		return this.defaultValue;
111
	}
112
113
	@Override
114
	public FeatureFlagSet requiredFeatures() {
115
		return this.requiredFeatures;
116
	}
117
}
118

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