GameRulesService .java

net.minecraft.server.jsonrpc.methods.GameRulesService (class, 75 lines): members: GameRuleUpdate, getTypedRule, update, get. strings: "\" of gamerule \"".

net.minecraft.server.jsonrpc.methodsclass75 linesserver3.6 KB

API Surface (3)

List<GameRulesService.GameRuleUpdate<?>> get(final MinecraftApi minecraftApi)
<T> GameRulesService.GameRuleUpdate<T> getTypedRule(final MinecraftApi minecraftApi, final GameRule<T> gameRule, final T value)
<T> GameRulesService.GameRuleUpdate<T> update(final MinecraftApi minecraftApi, final GameRulesService.GameRuleUpdate<T> update, final ClientInfo clientInfo)

Fields & Constants (1)

update

String literals (3)

"Stated type \""
"\" mismatches with actual type \""
"\" of gamerule \""

Code Graph: Dependencies (6)

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

Full Source

GameRulesService.java75 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.jsonrpc.methods;
2
3
import com.mojang.datafixers.kinds.App;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.MapCodec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
8
import java.util.ArrayList;
9
import java.util.List;
10
import net.minecraft.core.registries.BuiltInRegistries;
11
import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
12
import net.minecraft.util.StringRepresentable;
13
import net.minecraft.world.level.gamerules.GameRule;
14
import net.minecraft.world.level.gamerules.GameRuleType;
15
16
public class GameRulesService {
17
	public static List<GameRulesService.GameRuleUpdate<?>> get(final MinecraftApi minecraftApi) {
18
		List<GameRulesService.GameRuleUpdate<?>> rules = new ArrayList<>();
19
		minecraftApi.gameRuleService().getAvailableGameRules().forEach(gameRule -> addGameRule(minecraftApi, (GameRule<?>)gameRule, rules));
20
		return rules;
21
	}
22
23
	private static <T> void addGameRule(final MinecraftApi minecraftApi, final GameRule<T> gameRule, final List<GameRulesService.GameRuleUpdate<?>> rules) {
24
		T value = minecraftApi.gameRuleService().getRuleValue(gameRule);
25
		rules.add(getTypedRule(minecraftApi, gameRule, value));
26
	}
27
28
	public static <T> GameRulesService.GameRuleUpdate<T> getTypedRule(final MinecraftApi minecraftApi, final GameRule<T> gameRule, final T value) {
29
		return minecraftApi.gameRuleService().getTypedRule(gameRule, value);
30
	}
31
32
	public static <T> GameRulesService.GameRuleUpdate<T> update(
33
		final MinecraftApi minecraftApi, final GameRulesService.GameRuleUpdate<T> update, final ClientInfo clientInfo
34
	) {
35
		return minecraftApi.gameRuleService().updateGameRule(update, clientInfo);
36
	}
37
38
	public record GameRuleUpdate<T>(GameRule<T> gameRule, T value) {
39
		public static final Codec<GameRulesService.GameRuleUpdate<?>> TYPED_CODEC = BuiltInRegistries.GAME_RULE
40
			.byNameCodec()
41
			.dispatch("key", GameRulesService.GameRuleUpdate::gameRule, GameRulesService.GameRuleUpdate::getValueAndTypeCodec);
42
		public static final Codec<GameRulesService.GameRuleUpdate<?>> CODEC = BuiltInRegistries.GAME_RULE
43
			.byNameCodec()
44
			.dispatch("key", GameRulesService.GameRuleUpdate::gameRule, GameRulesService.GameRuleUpdate::getValueCodec);
45
46
		private static <T> MapCodec<? extends GameRulesService.GameRuleUpdate<T>> getValueCodec(final GameRule<T> gameRule) {
47
			return gameRule.valueCodec()
48
				.fieldOf("value")
49
				.xmap(value -> new GameRulesService.GameRuleUpdate<>(gameRule, (T)value), GameRulesService.GameRuleUpdate::value);
50
		}
51
52
		private static <T> MapCodec<? extends GameRulesService.GameRuleUpdate<T>> getValueAndTypeCodec(final GameRule<T> gameRule) {
53
			return RecordCodecBuilder.mapCodec(
54
				i -> i.group(
55
						(App<Mu<? extends GameRulesService.GameRuleUpdate<?>>, GameRuleType>)StringRepresentable.fromEnum(GameRuleType::values)
56
							.fieldOf("type")
57
							.forGetter(r -> r.gameRule.gameRuleType()),
58
						(App<Mu<? extends GameRulesService.GameRuleUpdate<?>>, T>)gameRule.valueCodec().fieldOf("value").forGetter(GameRulesService.GameRuleUpdate::value)
59
					)
60
					.apply(i, (type, value) -> getUntypedRule(gameRule, type, value))
61
			);
62
		}
63
64
		private static <T> GameRulesService.GameRuleUpdate<T> getUntypedRule(final GameRule<T> gameRule, final GameRuleType readType, final T value) {
65
			if (gameRule.gameRuleType() != readType) {
66
				throw new InvalidParameterJsonRpcException(
67
					"Stated type \"" + readType + "\" mismatches with actual type \"" + gameRule.gameRuleType() + "\" of gamerule \"" + gameRule.id() + "\""
68
				);
69
			} else {
70
				return new GameRulesService.GameRuleUpdate<>(gameRule, value);
71
			}
72
		}
73
	}
74
}
75

Decompiled Minecraft 26.3 source of GameRulesService (net/minecraft/server/jsonrpc/methods/GameRulesService.java) with Mojang official mappings, stored verbatim on the site.