| 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 |
|