| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Arrays;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.functions.CommandFunction;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.Registry;
|
| 14 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 15 | import net.minecraft.core.registries.Registries;
|
| 16 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.server.MinecraftServer;
|
| 19 | import net.minecraft.server.ServerFunctionManager;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.server.permissions.LevelBasedPermissionSet;
|
| 22 | import net.minecraft.util.ExtraCodecs;
|
| 23 | import net.minecraft.util.StringRepresentable;
|
| 24 | import net.minecraft.util.Unit;
|
| 25 | import net.minecraft.world.Difficulty;
|
| 26 | import net.minecraft.world.attribute.EnvironmentAttributeSystem;
|
| 27 | import net.minecraft.world.clock.WorldClock;
|
| 28 | import net.minecraft.world.level.gamerules.GameRule;
|
| 29 | import net.minecraft.world.level.gamerules.GameRuleMap;
|
| 30 | import net.minecraft.world.level.gamerules.GameRules;
|
| 31 | import net.minecraft.world.timeline.Timeline;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | public interface TestEnvironmentDefinition<SavedDataType> {
|
| 35 | Codec<TestEnvironmentDefinition<?>> DIRECT_CODEC = BuiltInRegistries.TEST_ENVIRONMENT_DEFINITION_TYPE
|
| 36 | .byNameCodec()
|
| 37 | .dispatch(TestEnvironmentDefinition::codec, c -> c);
|
| 38 | Codec<Holder<TestEnvironmentDefinition<?>>> CODEC = RegistryCodecs.holder(Registries.TEST_ENVIRONMENT, DIRECT_CODEC);
|
| 39 |
|
| 40 | static MapCodec<? extends TestEnvironmentDefinition<?>> bootstrap(final Registry<MapCodec<? extends TestEnvironmentDefinition<?>>> registry) {
|
| 41 | Registry.register(registry, "all_of", TestEnvironmentDefinition.AllOf.CODEC);
|
| 42 | Registry.register(registry, "clock_time", TestEnvironmentDefinition.ClockTime.CODEC);
|
| 43 | Registry.register(registry, "difficulty", TestEnvironmentDefinition.SetDifficulty.CODEC);
|
| 44 | Registry.register(registry, "function", TestEnvironmentDefinition.Functions.CODEC);
|
| 45 | Registry.register(registry, "game_rules", TestEnvironmentDefinition.SetGameRules.CODEC);
|
| 46 | Registry.register(registry, "timeline_attributes", TestEnvironmentDefinition.Timelines.CODEC);
|
| 47 | return Registry.register(registry, "weather", TestEnvironmentDefinition.Weather.CODEC);
|
| 48 | }
|
| 49 |
|
| 50 | SavedDataType setup(ServerLevel level);
|
| 51 |
|
| 52 | void teardown(final ServerLevel level, final SavedDataType saveData);
|
| 53 |
|
| 54 | MapCodec<? extends TestEnvironmentDefinition<SavedDataType>> codec();
|
| 55 |
|
| 56 | static <T> TestEnvironmentDefinition.Activation<T> activate(final TestEnvironmentDefinition<T> environment, final ServerLevel level) {
|
| 57 | return new TestEnvironmentDefinition.Activation<>(environment.setup(level), environment, level);
|
| 58 | }
|
| 59 |
|
| 60 | class Activation<T> {
|
| 61 | private final T value;
|
| 62 | private final TestEnvironmentDefinition<T> definition;
|
| 63 | private final ServerLevel level;
|
| 64 |
|
| 65 | private Activation(final T value, final TestEnvironmentDefinition<T> definition, final ServerLevel level) {
|
| 66 | this.value = value;
|
| 67 | this.definition = definition;
|
| 68 | this.level = level;
|
| 69 | }
|
| 70 |
|
| 71 | public void teardown() {
|
| 72 | this.definition.teardown(this.level, this.value);
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | record AllOf(List<Holder<TestEnvironmentDefinition<?>>> definitions)
|
| 77 | implements TestEnvironmentDefinition<List<? extends TestEnvironmentDefinition.Activation<?>>> {
|
| 78 | public static final MapCodec<TestEnvironmentDefinition.AllOf> CODEC = RecordCodecBuilder.mapCodec(
|
| 79 | i -> i.group(TestEnvironmentDefinition.CODEC.listOf().fieldOf("definitions").forGetter(TestEnvironmentDefinition.AllOf::definitions))
|
| 80 | .apply(i, TestEnvironmentDefinition.AllOf::new)
|
| 81 | );
|
| 82 |
|
| 83 | public AllOf(final TestEnvironmentDefinition<?>... defs) {
|
| 84 | this(Arrays.stream(defs).map(TestEnvironmentDefinition.AllOf::holder).toList());
|
| 85 | }
|
| 86 |
|
| 87 | private static Holder<TestEnvironmentDefinition<?>> holder(final TestEnvironmentDefinition<?> holder) {
|
| 88 | return Holder.direct(holder);
|
| 89 | }
|
| 90 |
|
| 91 | public List<? extends TestEnvironmentDefinition.Activation<?>> setup(final ServerLevel level) {
|
| 92 | return this.definitions.stream().map(b -> TestEnvironmentDefinition.activate(b.value(), level)).toList();
|
| 93 | }
|
| 94 |
|
| 95 | public void teardown(final ServerLevel level, final List<? extends TestEnvironmentDefinition.Activation<?>> activations) {
|
| 96 | activations.reversed().forEach(TestEnvironmentDefinition.Activation::teardown);
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public MapCodec<TestEnvironmentDefinition.AllOf> codec() {
|
| 101 | return CODEC;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | record ClockTime(Holder<WorldClock> clock, int time) implements TestEnvironmentDefinition<Long> {
|
| 106 | public static final MapCodec<TestEnvironmentDefinition.ClockTime> CODEC = RecordCodecBuilder.mapCodec(
|
| 107 | i -> i.group(
|
| 108 | WorldClock.CODEC.fieldOf("clock").forGetter(TestEnvironmentDefinition.ClockTime::clock),
|
| 109 | ExtraCodecs.NON_NEGATIVE_INT.fieldOf("time").forGetter(TestEnvironmentDefinition.ClockTime::time)
|
| 110 | )
|
| 111 | .apply(i, TestEnvironmentDefinition.ClockTime::new)
|
| 112 | );
|
| 113 |
|
| 114 | public Long setup(final ServerLevel level) {
|
| 115 | MinecraftServer server = level.getServer();
|
| 116 | long previous = server.clockManager().getInstance(this.clock).totalTicks();
|
| 117 | server.clockManager().setTotalTicks(this.clock, this.time);
|
| 118 | return previous;
|
| 119 | }
|
| 120 |
|
| 121 | public void teardown(final ServerLevel level, final Long saveData) {
|
| 122 | MinecraftServer server = level.getServer();
|
| 123 | server.clockManager().setTotalTicks(this.clock, saveData);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public MapCodec<TestEnvironmentDefinition.ClockTime> codec() {
|
| 128 | return CODEC;
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | record Functions(Optional<Identifier> setupFunction, Optional<Identifier> teardownFunction) implements TestEnvironmentDefinition<Unit> {
|
| 133 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 134 | public static final MapCodec<TestEnvironmentDefinition.Functions> CODEC = RecordCodecBuilder.mapCodec(
|
| 135 | i -> i.group(
|
| 136 | Identifier.CODEC.optionalFieldOf("setup").forGetter(TestEnvironmentDefinition.Functions::setupFunction),
|
| 137 | Identifier.CODEC.optionalFieldOf("teardown").forGetter(TestEnvironmentDefinition.Functions::teardownFunction)
|
| 138 | )
|
| 139 | .apply(i, TestEnvironmentDefinition.Functions::new)
|
| 140 | );
|
| 141 |
|
| 142 | public Unit setup(final ServerLevel level) {
|
| 143 | this.setupFunction.ifPresent(p -> run(level, p));
|
| 144 | return Unit.INSTANCE;
|
| 145 | }
|
| 146 |
|
| 147 | public void teardown(final ServerLevel level, final Unit saveData) {
|
| 148 | this.teardownFunction.ifPresent(p -> run(level, p));
|
| 149 | }
|
| 150 |
|
| 151 | private static void run(final ServerLevel level, final Identifier functionId) {
|
| 152 | MinecraftServer server = level.getServer();
|
| 153 | ServerFunctionManager functions = server.getFunctions();
|
| 154 | Optional<CommandFunction<CommandSourceStack>> function = functions.get(functionId);
|
| 155 | if (function.isPresent()) {
|
| 156 | CommandSourceStack source = server.createCommandSourceStack().withPermission(LevelBasedPermissionSet.GAMEMASTER).withSuppressedOutput().withLevel(level);
|
| 157 | functions.execute(function.get(), source);
|
| 158 | } else {
|
| 159 | LOGGER.error("Test Batch failed for non-existent function {}", functionId);
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public MapCodec<TestEnvironmentDefinition.Functions> codec() {
|
| 165 | return CODEC;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | record SetDifficulty(Difficulty difficulty) implements TestEnvironmentDefinition<Difficulty> {
|
| 170 | public static final MapCodec<TestEnvironmentDefinition.SetDifficulty> CODEC = RecordCodecBuilder.mapCodec(
|
| 171 | i -> i.group(Difficulty.CODEC.fieldOf("difficulty").forGetter(TestEnvironmentDefinition.SetDifficulty::difficulty))
|
| 172 | .apply(i, TestEnvironmentDefinition.SetDifficulty::new)
|
| 173 | );
|
| 174 |
|
| 175 | public Difficulty setup(final ServerLevel level) {
|
| 176 | Difficulty oldDifficulty = level.getDifficulty();
|
| 177 | level.getServer().setDifficulty(this.difficulty, true);
|
| 178 | return oldDifficulty;
|
| 179 | }
|
| 180 |
|
| 181 | public void teardown(final ServerLevel level, final Difficulty saveData) {
|
| 182 | level.getServer().setDifficulty(saveData, true);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public MapCodec<TestEnvironmentDefinition.SetDifficulty> codec() {
|
| 187 | return CODEC;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | record SetGameRules(GameRuleMap gameRulesMap) implements TestEnvironmentDefinition<GameRuleMap> {
|
| 192 | public static final MapCodec<TestEnvironmentDefinition.SetGameRules> CODEC = RecordCodecBuilder.mapCodec(
|
| 193 | i -> i.group(GameRuleMap.CODEC.fieldOf("rules").forGetter(TestEnvironmentDefinition.SetGameRules::gameRulesMap))
|
| 194 | .apply(i, TestEnvironmentDefinition.SetGameRules::new)
|
| 195 | );
|
| 196 |
|
| 197 | public GameRuleMap setup(final ServerLevel level) {
|
| 198 | GameRuleMap originalState = GameRuleMap.of();
|
| 199 | GameRules gameRules = level.getGameRules();
|
| 200 | this.gameRulesMap.keySet().forEach(rule -> setFromActive(originalState, (GameRule<?>)rule, gameRules));
|
| 201 | gameRules.setAll(this.gameRulesMap, level.getServer());
|
| 202 | return originalState;
|
| 203 | }
|
| 204 |
|
| 205 | private static <T> void setFromActive(final GameRuleMap map, final GameRule<T> rule, final GameRules rules) {
|
| 206 | map.set(rule, rules.get(rule));
|
| 207 | }
|
| 208 |
|
| 209 | public void teardown(final ServerLevel level, final GameRuleMap saveData) {
|
| 210 | level.getGameRules().setAll(saveData, level.getServer());
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public MapCodec<TestEnvironmentDefinition.SetGameRules> codec() {
|
| 215 | return CODEC;
|
| 216 | }
|
| 217 | }
|
| 218 |
|
| 219 | record Timelines(List<Holder<Timeline>> timelines) implements TestEnvironmentDefinition<EnvironmentAttributeSystem> {
|
| 220 | public static final MapCodec<TestEnvironmentDefinition.Timelines> CODEC = RecordCodecBuilder.mapCodec(
|
| 221 | i -> i.group(Timeline.CODEC.listOf().fieldOf("timelines").forGetter(TestEnvironmentDefinition.Timelines::timelines))
|
| 222 | .apply(i, TestEnvironmentDefinition.Timelines::new)
|
| 223 | );
|
| 224 |
|
| 225 | public EnvironmentAttributeSystem setup(final ServerLevel level) {
|
| 226 | EnvironmentAttributeSystem.Builder builder = EnvironmentAttributeSystem.builder().addDefaultLayers(level);
|
| 227 |
|
| 228 | for (Holder<Timeline> timeline : this.timelines) {
|
| 229 | builder.addTimelineLayer(timeline, level.clockManager());
|
| 230 | }
|
| 231 |
|
| 232 | return level.setEnvironmentAttributes(builder.build());
|
| 233 | }
|
| 234 |
|
| 235 | public void teardown(final ServerLevel level, final EnvironmentAttributeSystem saveData) {
|
| 236 | level.setEnvironmentAttributes(saveData);
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public MapCodec<TestEnvironmentDefinition.Timelines> codec() {
|
| 241 | return CODEC;
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | record Weather(TestEnvironmentDefinition.Weather.Type weather) implements TestEnvironmentDefinition<TestEnvironmentDefinition.Weather.Type> {
|
| 246 | public static final MapCodec<TestEnvironmentDefinition.Weather> CODEC = RecordCodecBuilder.mapCodec(
|
| 247 | i -> i.group(TestEnvironmentDefinition.Weather.Type.CODEC.fieldOf("weather").forGetter(TestEnvironmentDefinition.Weather::weather))
|
| 248 | .apply(i, TestEnvironmentDefinition.Weather::new)
|
| 249 | );
|
| 250 |
|
| 251 | public TestEnvironmentDefinition.Weather.Type setup(final ServerLevel level) {
|
| 252 | TestEnvironmentDefinition.Weather.Type previous;
|
| 253 | if (level.isThundering()) {
|
| 254 | previous = TestEnvironmentDefinition.Weather.Type.THUNDER;
|
| 255 | } else if (level.isRaining()) {
|
| 256 | previous = TestEnvironmentDefinition.Weather.Type.RAIN;
|
| 257 | } else {
|
| 258 | previous = TestEnvironmentDefinition.Weather.Type.CLEAR;
|
| 259 | }
|
| 260 |
|
| 261 | this.weather.apply(level);
|
| 262 | return previous;
|
| 263 | }
|
| 264 |
|
| 265 | public void teardown(final ServerLevel level, final TestEnvironmentDefinition.Weather.Type saveData) {
|
| 266 | level.resetWeatherCycle();
|
| 267 | saveData.apply(level);
|
| 268 | }
|
| 269 |
|
| 270 | @Override
|
| 271 | public MapCodec<TestEnvironmentDefinition.Weather> codec() {
|
| 272 | return CODEC;
|
| 273 | }
|
| 274 |
|
| 275 | public enum Type implements StringRepresentable {
|
| 276 | CLEAR("clear", 100000, 0, false, false),
|
| 277 | RAIN("rain", 0, 100000, true, false),
|
| 278 | THUNDER("thunder", 0, 100000, true, true);
|
| 279 |
|
| 280 | public static final Codec<TestEnvironmentDefinition.Weather.Type> CODEC = StringRepresentable.fromEnum(TestEnvironmentDefinition.Weather.Type::values);
|
| 281 | private final String id;
|
| 282 | private final int clearTime;
|
| 283 | private final int rainTime;
|
| 284 | private final boolean raining;
|
| 285 | private final boolean thundering;
|
| 286 |
|
| 287 | Type(final String id, final int clearTime, final int rainTime, final boolean raining, final boolean thundering) {
|
| 288 | this.id = id;
|
| 289 | this.clearTime = clearTime;
|
| 290 | this.rainTime = rainTime;
|
| 291 | this.raining = raining;
|
| 292 | this.thundering = thundering;
|
| 293 | }
|
| 294 |
|
| 295 | public void apply(final ServerLevel level) {
|
| 296 | level.getServer().setWeatherParameters(this.clearTime, this.rainTime, this.raining, this.thundering);
|
| 297 | }
|
| 298 |
|
| 299 | @Override
|
| 300 | public String getSerializedName() {
|
| 301 | return this.id;
|
| 302 | }
|
| 303 | }
|
| 304 | }
|
| 305 | }
|
| 306 |
|