GameRulesService.java

net.minecraft.server.jsonrpc.methods.GameRulesService

信息

  • 全限定名:net.minecraft.server.jsonrpc.methods.GameRulesService
  • 类型:public class
  • 包:net.minecraft.server.jsonrpc.methods
  • 源码路径:src/main/java/net/minecraft/server/jsonrpc/methods/GameRulesService.java
  • 起始行号:L16
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

  • net.minecraft.server.jsonrpc.methods.GameRulesService.GameRuleUpdate
    • 类型: record
    • 修饰符: public
    • 源码定位: L36
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public static List<GameRulesService.GameRuleUpdate<?>> get(MinecraftApi minecraftApi) @ L17

  • 方法名:get
  • 源码定位:L17
  • 返回类型:List<GameRulesService.GameRuleUpdate<?>>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi

说明:

TODO

private static <T> void addGameRule(MinecraftApi minecraftApi, GameRule<T> gameRule, List<GameRulesService.GameRuleUpdate<?>> rules) @ L23

  • 方法名:addGameRule
  • 源码定位:L23
  • 返回类型: void
  • 修饰符:private static

参数:

  • minecraftApi: MinecraftApi
  • gameRule: GameRule
  • rules: List<GameRulesService.GameRuleUpdate<?>>

说明:

TODO

public static <T> GameRulesService.GameRuleUpdate<T> getTypedRule(MinecraftApi minecraftApi, GameRule<T> gameRule, T value) @ L28

  • 方法名:getTypedRule
  • 源码定位:L28
  • 返回类型: GameRulesService.GameRuleUpdate
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • gameRule: GameRule
  • value: T

说明:

TODO

public static <T> GameRulesService.GameRuleUpdate<T> update(MinecraftApi minecraftApi, GameRulesService.GameRuleUpdate<T> update, ClientInfo clientInfo) @ L32

  • 方法名:update
  • 源码定位:L32
  • 返回类型: GameRulesService.GameRuleUpdate
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • update: GameRulesService.GameRuleUpdate
  • clientInfo: ClientInfo

说明:

TODO

代码

public class GameRulesService {
    public static List<GameRulesService.GameRuleUpdate<?>> get(MinecraftApi minecraftApi) {
        List<GameRulesService.GameRuleUpdate<?>> rules = new ArrayList<>();
        minecraftApi.gameRuleService().getAvailableGameRules().forEach(gameRule -> addGameRule(minecraftApi, (GameRule<?>)gameRule, rules));
        return rules;
    }
 
    private static <T> void addGameRule(MinecraftApi minecraftApi, GameRule<T> gameRule, List<GameRulesService.GameRuleUpdate<?>> rules) {
        T value = minecraftApi.gameRuleService().getRuleValue(gameRule);
        rules.add(getTypedRule(minecraftApi, gameRule, value));
    }
 
    public static <T> GameRulesService.GameRuleUpdate<T> getTypedRule(MinecraftApi minecraftApi, GameRule<T> gameRule, T value) {
        return minecraftApi.gameRuleService().getTypedRule(gameRule, value);
    }
 
    public static <T> GameRulesService.GameRuleUpdate<T> update(MinecraftApi minecraftApi, GameRulesService.GameRuleUpdate<T> update, ClientInfo clientInfo) {
        return minecraftApi.gameRuleService().updateGameRule(update, clientInfo);
    }
 
    public record GameRuleUpdate<T>(GameRule<T> gameRule, T value) {
        public static final Codec<GameRulesService.GameRuleUpdate<?>> TYPED_CODEC = BuiltInRegistries.GAME_RULE
            .byNameCodec()
            .dispatch("key", GameRulesService.GameRuleUpdate::gameRule, GameRulesService.GameRuleUpdate::getValueAndTypeCodec);
        public static final Codec<GameRulesService.GameRuleUpdate<?>> CODEC = BuiltInRegistries.GAME_RULE
            .byNameCodec()
            .dispatch("key", GameRulesService.GameRuleUpdate::gameRule, GameRulesService.GameRuleUpdate::getValueCodec);
 
        private static <T> MapCodec<? extends GameRulesService.GameRuleUpdate<T>> getValueCodec(GameRule<T> gameRule) {
            return gameRule.valueCodec()
                .fieldOf("value")
                .xmap(value -> new GameRulesService.GameRuleUpdate<>(gameRule, (T)value), GameRulesService.GameRuleUpdate::value);
        }
 
        private static <T> MapCodec<? extends GameRulesService.GameRuleUpdate<T>> getValueAndTypeCodec(GameRule<T> gameRule) {
            return RecordCodecBuilder.mapCodec(
                i -> i.group(
                        StringRepresentable.fromEnum(GameRuleType::values)
                            .fieldOf("type")
                            .forGetter(r -> r.gameRule.gameRuleType()),
                        gameRule.valueCodec()
                            .fieldOf("value")
                            .forGetter(GameRulesService.GameRuleUpdate::value)
                    )
                    .apply(i, (type, value) -> getUntypedRule(gameRule, type, value))
            );
        }
 
        private static <T> GameRulesService.GameRuleUpdate<T> getUntypedRule(GameRule<T> gameRule, GameRuleType readType, T value) {
            if (gameRule.gameRuleType() != readType) {
                throw new InvalidParameterJsonRpcException(
                    "Stated type \"" + readType + "\" mismatches with actual type \"" + gameRule.gameRuleType() + "\" of gamerule \"" + gameRule.id() + "\""
                );
            } else {
                return new GameRulesService.GameRuleUpdate<>(gameRule, value);
            }
        }
    }
}

引用的其他类