TestEnvironmentDefinition.java

net.minecraft.gametest.framework.TestEnvironmentDefinition

信息

  • 全限定名:net.minecraft.gametest.framework.TestEnvironmentDefinition
  • 类型:public interface
  • 包:net.minecraft.gametest.framework
  • 源码路径:src/main/java/net/minecraft/gametest/framework/TestEnvironmentDefinition.java
  • 起始行号:L33
  • 职责:

    TODO

字段/常量

  • DIRECT_CODEC

    • 类型: Codec<TestEnvironmentDefinition<?>>
    • 修饰符: package-private
    • 源码定位: L34
    • 说明:

      TODO

  • CODEC

    • 类型: Codec<Holder<TestEnvironmentDefinition<?>>>
    • 修饰符: package-private
    • 源码定位: L37
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.Activation

    • 类型: class
    • 修饰符: public static
    • 源码定位: L58
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.AllOf

    • 类型: record
    • 修饰符: public
    • 源码定位: L74
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.ClockTime

    • 类型: record
    • 修饰符: public
    • 源码定位: L103
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.Functions

    • 类型: record
    • 修饰符: public
    • 源码定位: L130
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.SetGameRules

    • 类型: record
    • 修饰符: public
    • 源码定位: L170
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.Timelines

    • 类型: record
    • 修饰符: public
    • 源码定位: L198
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.Weather

    • 类型: record
    • 修饰符: public
    • 源码定位: L224
    • 说明:

      TODO

  • net.minecraft.gametest.framework.TestEnvironmentDefinition.Weather.Type

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L254
    • 说明:

      TODO

构造器

方法

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

static MapCodec<?extends TestEnvironmentDefinition<?>> bootstrap(Registry<MapCodec<?extends TestEnvironmentDefinition<?>>> registry) @ L39

  • 方法名:bootstrap
  • 源码定位:L39
  • 返回类型:MapCodec>
  • 修饰符:static

参数:

  • registry: Registry<MapCodec>>

说明:

TODO

SavedDataType setup(ServerLevel level) @ L48

  • 方法名:setup
  • 源码定位:L48
  • 返回类型:SavedDataType
  • 修饰符:package-private

参数:

  • level: ServerLevel

说明:

TODO

void teardown(ServerLevel level, SavedDataType saveData) @ L50

  • 方法名:teardown
  • 源码定位:L50
  • 返回类型:void
  • 修饰符:package-private

参数:

  • level: ServerLevel
  • saveData: SavedDataType

说明:

TODO

MapCodec<?extends TestEnvironmentDefinition<SavedDataType>> codec() @ L52

  • 方法名:codec
  • 源码定位:L52
  • 返回类型:MapCodec<?extends TestEnvironmentDefinition>
  • 修饰符:package-private

参数:

说明:

TODO

static <T> TestEnvironmentDefinition.Activation<T> activate(TestEnvironmentDefinition<T> environment, ServerLevel level) @ L54

  • 方法名:activate
  • 源码定位:L54
  • 返回类型: TestEnvironmentDefinition.Activation
  • 修饰符:static

参数:

  • environment: TestEnvironmentDefinition
  • level: ServerLevel

说明:

TODO

代码

public interface TestEnvironmentDefinition<SavedDataType> {
    Codec<TestEnvironmentDefinition<?>> DIRECT_CODEC = BuiltInRegistries.TEST_ENVIRONMENT_DEFINITION_TYPE
        .byNameCodec()
        .dispatch(TestEnvironmentDefinition::codec, c -> c);
    Codec<Holder<TestEnvironmentDefinition<?>>> CODEC = RegistryFileCodec.create(Registries.TEST_ENVIRONMENT, DIRECT_CODEC);
 
    static MapCodec<? extends TestEnvironmentDefinition<?>> bootstrap(Registry<MapCodec<? extends TestEnvironmentDefinition<?>>> registry) {
        Registry.register(registry, "all_of", TestEnvironmentDefinition.AllOf.CODEC);
        Registry.register(registry, "game_rules", TestEnvironmentDefinition.SetGameRules.CODEC);
        Registry.register(registry, "clock_time", TestEnvironmentDefinition.ClockTime.CODEC);
        Registry.register(registry, "timeline_attributes", TestEnvironmentDefinition.Timelines.CODEC);
        Registry.register(registry, "weather", TestEnvironmentDefinition.Weather.CODEC);
        return Registry.register(registry, "function", TestEnvironmentDefinition.Functions.CODEC);
    }
 
    SavedDataType setup(ServerLevel level);
 
    void teardown(final ServerLevel level, final SavedDataType saveData);
 
    MapCodec<? extends TestEnvironmentDefinition<SavedDataType>> codec();
 
    static <T> TestEnvironmentDefinition.Activation<T> activate(TestEnvironmentDefinition<T> environment, ServerLevel level) {
        return new TestEnvironmentDefinition.Activation<>(environment.setup(level), environment, level);
    }
 
    public static class Activation<T> {
        private final T value;
        private final TestEnvironmentDefinition<T> definition;
        private final ServerLevel level;
 
        private Activation(T value, TestEnvironmentDefinition<T> definition, ServerLevel level) {
            this.value = value;
            this.definition = definition;
            this.level = level;
        }
 
        public void teardown() {
            this.definition.teardown(this.level, this.value);
        }
    }
 
    public record AllOf(List<Holder<TestEnvironmentDefinition<?>>> definitions)
        implements TestEnvironmentDefinition<List<? extends TestEnvironmentDefinition.Activation<?>>> {
        public static final MapCodec<TestEnvironmentDefinition.AllOf> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(TestEnvironmentDefinition.CODEC.listOf().fieldOf("definitions").forGetter(TestEnvironmentDefinition.AllOf::definitions))
                .apply(i, TestEnvironmentDefinition.AllOf::new)
        );
 
        public AllOf(TestEnvironmentDefinition<?>... defs) {
            this(Arrays.stream(defs).map(TestEnvironmentDefinition.AllOf::holder).toList());
        }
 
        private static Holder<TestEnvironmentDefinition<?>> holder(TestEnvironmentDefinition<?> holder) {
            return Holder.direct(holder);
        }
 
        public List<? extends TestEnvironmentDefinition.Activation<?>> setup(ServerLevel level) {
            return this.definitions.stream().map(b -> TestEnvironmentDefinition.activate(b.value(), level)).toList();
        }
 
        public void teardown(ServerLevel level, List<? extends TestEnvironmentDefinition.Activation<?>> activations) {
            activations.reversed().forEach(TestEnvironmentDefinition.Activation::teardown);
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.AllOf> codec() {
            return CODEC;
        }
    }
 
    public record ClockTime(Holder<WorldClock> clock, int time) implements TestEnvironmentDefinition<Long> {
        public static final MapCodec<TestEnvironmentDefinition.ClockTime> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(
                    WorldClock.CODEC.fieldOf("clock").forGetter(TestEnvironmentDefinition.ClockTime::clock),
                    ExtraCodecs.NON_NEGATIVE_INT.fieldOf("time").forGetter(TestEnvironmentDefinition.ClockTime::time)
                )
                .apply(i, TestEnvironmentDefinition.ClockTime::new)
        );
 
        public Long setup(ServerLevel level) {
            MinecraftServer server = level.getServer();
            long previous = server.clockManager().getTotalTicks(this.clock);
            server.clockManager().setTotalTicks(this.clock, this.time);
            return previous;
        }
 
        public void teardown(ServerLevel level, Long saveData) {
            MinecraftServer server = level.getServer();
            server.clockManager().setTotalTicks(this.clock, saveData);
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.ClockTime> codec() {
            return CODEC;
        }
    }
 
    public record Functions(Optional<Identifier> setupFunction, Optional<Identifier> teardownFunction) implements TestEnvironmentDefinition<Unit> {
        private static final Logger LOGGER = LogUtils.getLogger();
        public static final MapCodec<TestEnvironmentDefinition.Functions> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(
                    Identifier.CODEC.optionalFieldOf("setup").forGetter(TestEnvironmentDefinition.Functions::setupFunction),
                    Identifier.CODEC.optionalFieldOf("teardown").forGetter(TestEnvironmentDefinition.Functions::teardownFunction)
                )
                .apply(i, TestEnvironmentDefinition.Functions::new)
        );
 
        public Unit setup(ServerLevel level) {
            this.setupFunction.ifPresent(p -> run(level, p));
            return Unit.INSTANCE;
        }
 
        public void teardown(ServerLevel level, Unit saveData) {
            this.teardownFunction.ifPresent(p -> run(level, p));
        }
 
        private static void run(ServerLevel level, Identifier functionId) {
            MinecraftServer server = level.getServer();
            ServerFunctionManager functions = server.getFunctions();
            Optional<CommandFunction<CommandSourceStack>> function = functions.get(functionId);
            if (function.isPresent()) {
                CommandSourceStack source = server.createCommandSourceStack()
                    .withPermission(LevelBasedPermissionSet.GAMEMASTER)
                    .withSuppressedOutput()
                    .withLevel(level);
                functions.execute(function.get(), source);
            } else {
                LOGGER.error("Test Batch failed for non-existent function {}", functionId);
            }
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.Functions> codec() {
            return CODEC;
        }
    }
 
    public record SetGameRules(GameRuleMap gameRulesMap) implements TestEnvironmentDefinition<GameRuleMap> {
        public static final MapCodec<TestEnvironmentDefinition.SetGameRules> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(GameRuleMap.CODEC.fieldOf("rules").forGetter(TestEnvironmentDefinition.SetGameRules::gameRulesMap))
                .apply(i, TestEnvironmentDefinition.SetGameRules::new)
        );
 
        public GameRuleMap setup(ServerLevel level) {
            GameRuleMap originalState = GameRuleMap.of();
            GameRules gameRules = level.getGameRules();
            this.gameRulesMap.keySet().forEach(rule -> setFromActive(originalState, (GameRule<?>)rule, gameRules));
            gameRules.setAll(this.gameRulesMap, level.getServer());
            return originalState;
        }
 
        private static <T> void setFromActive(GameRuleMap map, GameRule<T> rule, GameRules rules) {
            map.set(rule, rules.get(rule));
        }
 
        public void teardown(ServerLevel level, GameRuleMap saveData) {
            level.getGameRules().setAll(saveData, level.getServer());
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.SetGameRules> codec() {
            return CODEC;
        }
    }
 
    public record Timelines(List<Holder<Timeline>> timelines) implements TestEnvironmentDefinition<EnvironmentAttributeSystem> {
        public static final MapCodec<TestEnvironmentDefinition.Timelines> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(Timeline.CODEC.listOf().fieldOf("timelines").forGetter(TestEnvironmentDefinition.Timelines::timelines))
                .apply(i, TestEnvironmentDefinition.Timelines::new)
        );
 
        public EnvironmentAttributeSystem setup(ServerLevel level) {
            EnvironmentAttributeSystem.Builder builder = EnvironmentAttributeSystem.builder().addDefaultLayers(level);
 
            for (Holder<Timeline> timeline : this.timelines) {
                builder.addTimelineLayer(timeline, level.clockManager());
            }
 
            return level.setEnvironmentAttributes(builder.build());
        }
 
        public void teardown(ServerLevel level, EnvironmentAttributeSystem saveData) {
            level.setEnvironmentAttributes(saveData);
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.Timelines> codec() {
            return CODEC;
        }
    }
 
    public record Weather(TestEnvironmentDefinition.Weather.Type weather) implements TestEnvironmentDefinition<TestEnvironmentDefinition.Weather.Type> {
        public static final MapCodec<TestEnvironmentDefinition.Weather> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(TestEnvironmentDefinition.Weather.Type.CODEC.fieldOf("weather").forGetter(TestEnvironmentDefinition.Weather::weather))
                .apply(i, TestEnvironmentDefinition.Weather::new)
        );
 
        public TestEnvironmentDefinition.Weather.Type setup(ServerLevel level) {
            TestEnvironmentDefinition.Weather.Type previous;
            if (level.isThundering()) {
                previous = TestEnvironmentDefinition.Weather.Type.THUNDER;
            } else if (level.isRaining()) {
                previous = TestEnvironmentDefinition.Weather.Type.RAIN;
            } else {
                previous = TestEnvironmentDefinition.Weather.Type.CLEAR;
            }
 
            this.weather.apply(level);
            return previous;
        }
 
        public void teardown(ServerLevel level, TestEnvironmentDefinition.Weather.Type saveData) {
            level.resetWeatherCycle();
            saveData.apply(level);
        }
 
        @Override
        public MapCodec<TestEnvironmentDefinition.Weather> codec() {
            return CODEC;
        }
 
        public static enum Type implements StringRepresentable {
            CLEAR("clear", 100000, 0, false, false),
            RAIN("rain", 0, 100000, true, false),
            THUNDER("thunder", 0, 100000, true, true);
 
            public static final Codec<TestEnvironmentDefinition.Weather.Type> CODEC = StringRepresentable.fromEnum(
                TestEnvironmentDefinition.Weather.Type::values
            );
            private final String id;
            private final int clearTime;
            private final int rainTime;
            private final boolean raining;
            private final boolean thundering;
 
            private Type(String id, int clearTime, int rainTime, boolean raining, boolean thundering) {
                this.id = id;
                this.clearTime = clearTime;
                this.rainTime = rainTime;
                this.raining = raining;
                this.thundering = thundering;
            }
 
            void apply(ServerLevel level) {
                level.getServer().setWeatherParameters(this.clearTime, this.rainTime, this.raining, this.thundering);
            }
 
            @Override
            public String getSerializedName() {
                return this.id;
            }
        }
    }
}

引用的其他类

  • Holder

    • 引用位置: 字段/方法调用
    • 关联成员: Holder.direct()
  • Registry

    • 引用位置: 参数/方法调用
    • 关联成员: Registry.register()
  • RegistryFileCodec

    • 引用位置: 方法调用
    • 关联成员: RegistryFileCodec.create()
  • ServerLevel

    • 引用位置: 参数
  • StringRepresentable

    • 引用位置: 方法调用
    • 关联成员: StringRepresentable.fromEnum()
  • EnvironmentAttributeSystem

    • 引用位置: 方法调用
    • 关联成员: EnvironmentAttributeSystem.builder()
  • GameRuleMap

    • 引用位置: 方法调用
    • 关联成员: GameRuleMap.of()
  • SavedDataType

    • 引用位置: 参数/返回值