| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import io.netty.buffer.ByteBuf;
|
| 7 | import java.nio.file.Path;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.function.Consumer;
|
| 12 | import java.util.function.IntFunction;
|
| 13 | import net.minecraft.ChatFormatting;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.core.Holder;
|
| 16 | import net.minecraft.core.HolderLookup;
|
| 17 | import net.minecraft.core.Vec3i;
|
| 18 | import net.minecraft.core.registries.Registries;
|
| 19 | import net.minecraft.gametest.framework.FailedTestTracker;
|
| 20 | import net.minecraft.gametest.framework.GameTestInfo;
|
| 21 | import net.minecraft.gametest.framework.GameTestInstance;
|
| 22 | import net.minecraft.gametest.framework.GameTestRunner;
|
| 23 | import net.minecraft.gametest.framework.GameTestTicker;
|
| 24 | import net.minecraft.gametest.framework.RetryOptions;
|
| 25 | import net.minecraft.gametest.framework.StructureUtils;
|
| 26 | import net.minecraft.gametest.framework.TestCommand;
|
| 27 | import net.minecraft.nbt.CompoundTag;
|
| 28 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 29 | import net.minecraft.network.chat.Component;
|
| 30 | import net.minecraft.network.chat.ComponentSerialization;
|
| 31 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 32 | import net.minecraft.network.codec.StreamCodec;
|
| 33 | import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
| 34 | import net.minecraft.resources.Identifier;
|
| 35 | import net.minecraft.resources.ResourceKey;
|
| 36 | import net.minecraft.server.level.ServerLevel;
|
| 37 | import net.minecraft.util.ARGB;
|
| 38 | import net.minecraft.util.ByIdMap;
|
| 39 | import net.minecraft.util.StringRepresentable;
|
| 40 | import net.minecraft.world.entity.Entity;
|
| 41 | import net.minecraft.world.entity.player.Player;
|
| 42 | import net.minecraft.world.level.block.Blocks;
|
| 43 | import net.minecraft.world.level.block.Rotation;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 46 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 47 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 48 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 49 | import net.minecraft.world.level.levelgen.structure.templatesystem.loader.TemplatePathFactory;
|
| 50 | import net.minecraft.world.level.storage.ValueInput;
|
| 51 | import net.minecraft.world.level.storage.ValueOutput;
|
| 52 | import net.minecraft.world.phys.AABB;
|
| 53 | import org.slf4j.Logger;
|
| 54 |
|
| 55 | public class TestInstanceBlockEntity extends BlockEntity implements BoundingBoxRenderable, BeaconBeamOwner {
|
| 56 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 57 | private static final Component INVALID_TEST_NAME = Component.translatable("test_instance_block.invalid_test");
|
| 58 | private static final List<BeaconBeamOwner.Section> BEAM_CLEARED = List.of();
|
| 59 | private static final List<BeaconBeamOwner.Section> BEAM_RUNNING = List.of(new BeaconBeamOwner.Section(ARGB.color(128, 128, 128)));
|
| 60 | private static final List<BeaconBeamOwner.Section> BEAM_SUCCESS = List.of(new BeaconBeamOwner.Section(ARGB.color(0, 255, 0)));
|
| 61 | private static final List<BeaconBeamOwner.Section> BEAM_REQUIRED_FAILED = List.of(new BeaconBeamOwner.Section(ARGB.color(255, 0, 0)));
|
| 62 | private static final List<BeaconBeamOwner.Section> BEAM_OPTIONAL_FAILED = List.of(new BeaconBeamOwner.Section(ARGB.color(255, 128, 0)));
|
| 63 | private static final Vec3i STRUCTURE_OFFSET = new Vec3i(0, 1, 1);
|
| 64 | private TestInstanceBlockEntity.Data data;
|
| 65 | private final List<TestInstanceBlockEntity.ErrorMarker> errorMarkers = new ArrayList<>();
|
| 66 |
|
| 67 | public TestInstanceBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 68 | super(BlockEntityTypes.TEST_INSTANCE_BLOCK, worldPosition, blockState);
|
| 69 | this.data = new TestInstanceBlockEntity.Data(Optional.empty(), Vec3i.ZERO, Rotation.NONE, false, TestInstanceBlockEntity.Status.CLEARED, Optional.empty());
|
| 70 | }
|
| 71 |
|
| 72 | public void set(final TestInstanceBlockEntity.Data data) {
|
| 73 | this.data = data;
|
| 74 | this.setChanged();
|
| 75 | }
|
| 76 |
|
| 77 | public static Optional<Vec3i> getStructureSize(final ServerLevel level, final ResourceKey<GameTestInstance> testKey) {
|
| 78 | return getStructureTemplate(level, testKey).map(StructureTemplate::getSize);
|
| 79 | }
|
| 80 |
|
| 81 | public BoundingBox getStructureBoundingBox() {
|
| 82 | BlockPos corner1 = this.getStructurePos();
|
| 83 | BlockPos corner2 = corner1.offset(this.getTransformedSize()).offset(-1, -1, -1);
|
| 84 | return BoundingBox.fromCorners(corner1, corner2);
|
| 85 | }
|
| 86 |
|
| 87 | public BoundingBox getTestBoundingBox() {
|
| 88 | return this.getStructureBoundingBox().inflatedBy(this.getPadding());
|
| 89 | }
|
| 90 |
|
| 91 | public AABB getStructureBounds() {
|
| 92 | return AABB.of(this.getStructureBoundingBox());
|
| 93 | }
|
| 94 |
|
| 95 | public AABB getTestBounds() {
|
| 96 | return this.getStructureBounds().inflate(this.getPadding());
|
| 97 | }
|
| 98 |
|
| 99 | private static Optional<StructureTemplate> getStructureTemplate(final ServerLevel level, final ResourceKey<GameTestInstance> testKey) {
|
| 100 | return level.registryAccess().get(testKey).map(test -> test.value().structure()).flatMap(template -> level.getStructureTemplateManager().get(template));
|
| 101 | }
|
| 102 |
|
| 103 | public Optional<ResourceKey<GameTestInstance>> test() {
|
| 104 | return this.data.test();
|
| 105 | }
|
| 106 |
|
| 107 | public Component getTestName() {
|
| 108 | return this.test().map(key -> Component.literal(key.identifier().toString())).orElse(INVALID_TEST_NAME);
|
| 109 | }
|
| 110 |
|
| 111 | private Optional<Holder.Reference<GameTestInstance>> getTestHolder() {
|
| 112 | return this.test().flatMap(this.level.registryAccess()::get);
|
| 113 | }
|
| 114 |
|
| 115 | public boolean ignoreEntities() {
|
| 116 | return this.data.ignoreEntities();
|
| 117 | }
|
| 118 |
|
| 119 | public Vec3i getSize() {
|
| 120 | return this.data.size();
|
| 121 | }
|
| 122 |
|
| 123 | public Rotation getRotation() {
|
| 124 | return this.getTestHolder().map(Holder::value).map(GameTestInstance::rotation).orElse(Rotation.NONE).getRotated(this.data.rotation());
|
| 125 | }
|
| 126 |
|
| 127 | public Optional<Component> errorMessage() {
|
| 128 | return this.data.errorMessage();
|
| 129 | }
|
| 130 |
|
| 131 | public void setErrorMessage(final Component errorMessage) {
|
| 132 | this.set(this.data.withError(errorMessage));
|
| 133 | }
|
| 134 |
|
| 135 | public void setSuccess() {
|
| 136 | this.set(this.data.withStatus(TestInstanceBlockEntity.Status.FINISHED));
|
| 137 | }
|
| 138 |
|
| 139 | public void setRunning() {
|
| 140 | this.set(this.data.withStatus(TestInstanceBlockEntity.Status.RUNNING));
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public void setChanged() {
|
| 145 | super.setChanged();
|
| 146 | if (this.level instanceof ServerLevel) {
|
| 147 | this.level.sendBlockUpdated(this.getBlockPos(), Blocks.AIR.defaultBlockState(), this.getBlockState(), 3);
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | public ClientboundBlockEntityDataPacket getUpdatePacket() {
|
| 152 | return ClientboundBlockEntityDataPacket.create(this);
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
|
| 157 | return this.saveCustomOnly(registries);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected void loadAdditional(final ValueInput input) {
|
| 162 | input.read("data", TestInstanceBlockEntity.Data.CODEC).ifPresent(this::set);
|
| 163 | this.errorMarkers.clear();
|
| 164 | this.errorMarkers.addAll(input.read("errors", TestInstanceBlockEntity.ErrorMarker.LIST_CODEC).orElse(List.of()));
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected void saveAdditional(final ValueOutput output) {
|
| 169 | output.store("data", TestInstanceBlockEntity.Data.CODEC, this.data);
|
| 170 | if (!this.errorMarkers.isEmpty()) {
|
| 171 | output.store("errors", TestInstanceBlockEntity.ErrorMarker.LIST_CODEC, this.errorMarkers);
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public BoundingBoxRenderable.Mode renderMode() {
|
| 177 | return BoundingBoxRenderable.Mode.BOX;
|
| 178 | }
|
| 179 |
|
| 180 | public BlockPos getStructurePos() {
|
| 181 | int padding = this.getPadding();
|
| 182 | return getStructurePos(this.getBlockPos().offset(padding, padding, padding));
|
| 183 | }
|
| 184 |
|
| 185 | public static BlockPos getStructurePos(final BlockPos blockPos) {
|
| 186 | return blockPos.offset(STRUCTURE_OFFSET);
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public BoundingBoxRenderable.RenderableBox getRenderableBox() {
|
| 191 | int padding = this.getPadding();
|
| 192 | BlockPos offsetPos = new BlockPos(STRUCTURE_OFFSET.getX() + padding, STRUCTURE_OFFSET.getY() + padding, STRUCTURE_OFFSET.getZ() + padding);
|
| 193 | return new BoundingBoxRenderable.RenderableBox(offsetPos, this.getTransformedSize());
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public List<BeaconBeamOwner.Section> getBeamSections() {
|
| 198 | return switch (this.data.status()) {
|
| 199 | case CLEARED -> BEAM_CLEARED;
|
| 200 | case RUNNING -> BEAM_RUNNING;
|
| 201 | case FINISHED -> this.errorMessage().isEmpty()
|
| 202 | ? BEAM_SUCCESS
|
| 203 | : (this.getTestHolder().map(Holder::value).map(GameTestInstance::required).orElse(true) ? BEAM_REQUIRED_FAILED : BEAM_OPTIONAL_FAILED);
|
| 204 | };
|
| 205 | }
|
| 206 |
|
| 207 | private Vec3i getTransformedSize() {
|
| 208 | Vec3i size = this.getSize();
|
| 209 | Rotation rotation = this.getRotation();
|
| 210 | boolean axesSwitched = rotation == Rotation.CLOCKWISE_90 || rotation == Rotation.COUNTERCLOCKWISE_90;
|
| 211 | int xSize = axesSwitched ? size.getZ() : size.getX();
|
| 212 | int zSize = axesSwitched ? size.getX() : size.getZ();
|
| 213 | return new Vec3i(xSize, size.getY(), zSize);
|
| 214 | }
|
| 215 |
|
| 216 | public void resetTest(final Consumer<Component> feedbackOutput) {
|
| 217 | this.removeBarriers();
|
| 218 | this.clearErrorMarkers();
|
| 219 | boolean placed = this.placeStructure();
|
| 220 | if (placed) {
|
| 221 | feedbackOutput.accept(Component.translatable("test_instance_block.reset_success", this.getTestName()).withStyle(ChatFormatting.GREEN));
|
| 222 | }
|
| 223 |
|
| 224 | this.set(this.data.withStatus(TestInstanceBlockEntity.Status.CLEARED));
|
| 225 | }
|
| 226 |
|
| 227 | public Optional<Identifier> saveTest(final Consumer<Component> feedbackOutput) {
|
| 228 | Optional<Holder.Reference<GameTestInstance>> test = this.getTestHolder();
|
| 229 | Optional<Identifier> identifier;
|
| 230 | if (test.isPresent()) {
|
| 231 | identifier = Optional.of(test.get().value().structure());
|
| 232 | } else {
|
| 233 | identifier = this.test().map(ResourceKey::identifier);
|
| 234 | }
|
| 235 |
|
| 236 | if (identifier.isEmpty()) {
|
| 237 | BlockPos pos = this.getBlockPos();
|
| 238 | feedbackOutput.accept(Component.translatable("test_instance_block.error.unable_to_save", pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.RED));
|
| 239 | return identifier;
|
| 240 | }
|
| 241 |
|
| 242 | if (this.level instanceof ServerLevel serverLevel) {
|
| 243 | StructureBlockEntity.saveStructure(
|
| 244 | serverLevel, identifier.get(), this.getStructurePos(), this.getSize(), this.ignoreEntities(), "", true, List.of(Blocks.AIR)
|
| 245 | );
|
| 246 | }
|
| 247 |
|
| 248 | return identifier;
|
| 249 | }
|
| 250 |
|
| 251 | public boolean exportTest(final Consumer<Component> feedbackOutput) {
|
| 252 | Optional<Identifier> saved = this.saveTest(feedbackOutput);
|
| 253 | return !saved.isEmpty() && this.level instanceof ServerLevel serverLevel ? export(serverLevel, saved.get(), feedbackOutput) : false;
|
| 254 | }
|
| 255 |
|
| 256 | public static boolean export(final ServerLevel level, final Identifier structureId, final Consumer<Component> feedbackOutput) {
|
| 257 | StructureTemplateManager structureManager = level.getStructureTemplateManager();
|
| 258 | TemplatePathFactory testTemplatePathFactory = structureManager.testTemplates();
|
| 259 | if (testTemplatePathFactory == null) {
|
| 260 | feedbackOutput.accept(Component.literal("Test structure exporting is disabled").withStyle(ChatFormatting.RED));
|
| 261 | return true;
|
| 262 | }
|
| 263 |
|
| 264 | Optional<StructureTemplate> structureTemplate = structureManager.get(structureId);
|
| 265 | if (structureTemplate.isEmpty()) {
|
| 266 | feedbackOutput.accept(Component.literal("Could not find structure " + structureId).withStyle(ChatFormatting.RED));
|
| 267 | return true;
|
| 268 | }
|
| 269 |
|
| 270 | Path outputFile = testTemplatePathFactory.createAndValidatePathToStructure(structureId, StructureTemplateManager.RESOURCE_TEXT_STRUCTURE_LISTER);
|
| 271 |
|
| 272 | try {
|
| 273 | StructureTemplateManager.save(outputFile, structureTemplate.get(), true);
|
| 274 | } catch (Exception e) {
|
| 275 | LOGGER.error("Failed to save structure file {} to {}", structureId, outputFile, e);
|
| 276 | feedbackOutput.accept(Component.literal("Failed to save structure file " + structureId + " to " + outputFile).withStyle(ChatFormatting.RED));
|
| 277 | return true;
|
| 278 | }
|
| 279 |
|
| 280 | feedbackOutput.accept(Component.literal("Exported " + structureId + " to " + outputFile.toAbsolutePath()));
|
| 281 | return false;
|
| 282 | }
|
| 283 |
|
| 284 | public void runTest(final Consumer<Component> feedbackOutput) {
|
| 285 | if (this.level instanceof ServerLevel serverLevel) {
|
| 286 | Optional var7 = this.getTestHolder();
|
| 287 | BlockPos pos = this.getBlockPos();
|
| 288 | if (var7.isEmpty()) {
|
| 289 | feedbackOutput.accept(Component.translatable("test_instance_block.error.no_test", pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.RED));
|
| 290 | } else if (!this.placeStructure()) {
|
| 291 | feedbackOutput.accept(
|
| 292 | Component.translatable("test_instance_block.error.no_test_structure", pos.getX(), pos.getY(), pos.getZ()).withStyle(ChatFormatting.RED)
|
| 293 | );
|
| 294 | } else {
|
| 295 | this.clearErrorMarkers();
|
| 296 | GameTestTicker.SINGLETON.clear();
|
| 297 | FailedTestTracker.forgetFailedTests();
|
| 298 | feedbackOutput.accept(Component.translatable("test_instance_block.starting", ((Holder.Reference)var7.get()).getRegisteredName()));
|
| 299 | GameTestInfo gameTestInfo = new GameTestInfo((Holder.Reference<GameTestInstance>)var7.get(), this.data.rotation(), serverLevel, RetryOptions.noRetries());
|
| 300 | gameTestInfo.setTestBlockPos(pos);
|
| 301 | GameTestRunner runner = GameTestRunner.Builder.fromInfo(List.of(gameTestInfo), serverLevel.getServer()).build();
|
| 302 | TestCommand.trackAndStartRunner(serverLevel.getServer().createCommandSourceStack(), runner);
|
| 303 | }
|
| 304 | }
|
| 305 | }
|
| 306 |
|
| 307 | public boolean placeStructure() {
|
| 308 | if (this.level instanceof ServerLevel serverLevel) {
|
| 309 | Optional<StructureTemplate> template = this.data.test().flatMap(test -> getStructureTemplate(serverLevel, (ResourceKey<GameTestInstance>)test));
|
| 310 | if (template.isPresent()) {
|
| 311 | this.placeStructure(serverLevel, template.get());
|
| 312 | return true;
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | return false;
|
| 317 | }
|
| 318 |
|
| 319 | private void placeStructure(final ServerLevel level, final StructureTemplate template) {
|
| 320 | StructurePlaceSettings placeSettings = new StructurePlaceSettings()
|
| 321 | .setRotation(this.getRotation())
|
| 322 | .setIgnoreEntities(this.data.ignoreEntities())
|
| 323 | .setKnownShape(true);
|
| 324 | BlockPos pos = this.getStartCorner();
|
| 325 | this.forceLoadChunks();
|
| 326 | int padding = this.getPadding();
|
| 327 | StructureUtils.clearSpaceForStructure(this.getTestBoundingBox(), level);
|
| 328 | this.removeEntities();
|
| 329 | template.placeInWorld(level, pos, pos, placeSettings, level.getRandom(), 818);
|
| 330 | }
|
| 331 |
|
| 332 | private int getPadding() {
|
| 333 | return this.getTestHolder().map(r -> r.value().padding()).orElse(0);
|
| 334 | }
|
| 335 |
|
| 336 | private void removeEntities() {
|
| 337 | this.level.getEntities(null, this.getTestBounds()).stream().filter(entity -> !(entity instanceof Player)).forEach(Entity::discard);
|
| 338 | }
|
| 339 |
|
| 340 | private void forceLoadChunks() {
|
| 341 | if (this.level instanceof ServerLevel serverLevel) {
|
| 342 | this.getStructureBoundingBox().intersectingChunks().forEach(pos -> serverLevel.setChunkForced(pos.x(), pos.z(), true));
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | public BlockPos getStartCorner() {
|
| 347 | Vec3i structureSize = this.getSize();
|
| 348 | Rotation rotation = this.getRotation();
|
| 349 | BlockPos northWestCorner = this.getStructurePos();
|
| 350 |
|
| 351 | return switch (rotation) {
|
| 352 | case NONE -> northWestCorner;
|
| 353 | case CLOCKWISE_90 -> northWestCorner.offset(structureSize.getZ() - 1, 0, 0);
|
| 354 | case CLOCKWISE_180 -> northWestCorner.offset(structureSize.getX() - 1, 0, structureSize.getZ() - 1);
|
| 355 | case COUNTERCLOCKWISE_90 -> northWestCorner.offset(0, 0, structureSize.getX() - 1);
|
| 356 | };
|
| 357 | }
|
| 358 |
|
| 359 | public void encaseStructure() {
|
| 360 | this.processStructureBoundary(blockPos -> {
|
| 361 | if (!this.level.getBlockState(blockPos).is(Blocks.TEST_INSTANCE_BLOCK)) {
|
| 362 | this.level.setBlockAndUpdate(blockPos, Blocks.BARRIER.defaultBlockState());
|
| 363 | }
|
| 364 | });
|
| 365 | }
|
| 366 |
|
| 367 | public void removeBarriers() {
|
| 368 | this.processStructureBoundary(blockPos -> {
|
| 369 | if (this.level.getBlockState(blockPos).is(Blocks.BARRIER)) {
|
| 370 | this.level.setBlockAndUpdate(blockPos, Blocks.AIR.defaultBlockState());
|
| 371 | }
|
| 372 | });
|
| 373 | }
|
| 374 |
|
| 375 | public void processStructureBoundary(final Consumer<BlockPos> action) {
|
| 376 | AABB bounds = this.getStructureBounds();
|
| 377 | boolean hasCeiling = !this.getTestHolder().map(h -> h.value().skyAccess()).orElse(false);
|
| 378 | BlockPos low = BlockPos.containing(bounds.minX, bounds.minY, bounds.minZ).offset(-1, -1, -1);
|
| 379 | BlockPos high = BlockPos.containing(bounds.maxX, bounds.maxY, bounds.maxZ);
|
| 380 | BlockPos.betweenClosedStream(low, high)
|
| 381 | .forEach(
|
| 382 | blockPos -> {
|
| 383 | boolean isNonCeilingEdge = blockPos.getX() == low.getX()
|
| 384 | || blockPos.getX() == high.getX()
|
| 385 | || blockPos.getZ() == low.getZ()
|
| 386 | || blockPos.getZ() == high.getZ()
|
| 387 | || blockPos.getY() == low.getY();
|
| 388 | boolean isCeiling = blockPos.getY() == high.getY();
|
| 389 | if (isNonCeilingEdge || isCeiling && hasCeiling) {
|
| 390 | action.accept(blockPos);
|
| 391 | }
|
| 392 | }
|
| 393 | );
|
| 394 | }
|
| 395 |
|
| 396 | public void markError(final BlockPos pos, final Component text) {
|
| 397 | this.errorMarkers.add(new TestInstanceBlockEntity.ErrorMarker(pos, text));
|
| 398 | this.setChanged();
|
| 399 | }
|
| 400 |
|
| 401 | public void clearErrorMarkers() {
|
| 402 | if (!this.errorMarkers.isEmpty()) {
|
| 403 | this.errorMarkers.clear();
|
| 404 | this.setChanged();
|
| 405 | }
|
| 406 | }
|
| 407 |
|
| 408 | public List<TestInstanceBlockEntity.ErrorMarker> getErrorMarkers() {
|
| 409 | return this.errorMarkers;
|
| 410 | }
|
| 411 |
|
| 412 | public record Data(
|
| 413 | Optional<ResourceKey<GameTestInstance>> test,
|
| 414 | Vec3i size,
|
| 415 | Rotation rotation,
|
| 416 | boolean ignoreEntities,
|
| 417 | TestInstanceBlockEntity.Status status,
|
| 418 | Optional<Component> errorMessage
|
| 419 | ) {
|
| 420 | public static final Codec<TestInstanceBlockEntity.Data> CODEC = RecordCodecBuilder.create(
|
| 421 | i -> i.group(
|
| 422 | ResourceKey.codec(Registries.TEST_INSTANCE).optionalFieldOf("test").forGetter(TestInstanceBlockEntity.Data::test),
|
| 423 | Vec3i.CODEC.fieldOf("size").forGetter(TestInstanceBlockEntity.Data::size),
|
| 424 | Rotation.CODEC.fieldOf("rotation").forGetter(TestInstanceBlockEntity.Data::rotation),
|
| 425 | Codec.BOOL.fieldOf("ignore_entities").forGetter(TestInstanceBlockEntity.Data::ignoreEntities),
|
| 426 | TestInstanceBlockEntity.Status.CODEC.fieldOf("status").forGetter(TestInstanceBlockEntity.Data::status),
|
| 427 | ComponentSerialization.CODEC.optionalFieldOf("error_message").forGetter(TestInstanceBlockEntity.Data::errorMessage)
|
| 428 | )
|
| 429 | .apply(i, TestInstanceBlockEntity.Data::new)
|
| 430 | );
|
| 431 | public static final StreamCodec<RegistryFriendlyByteBuf, TestInstanceBlockEntity.Data> STREAM_CODEC = StreamCodec.composite(
|
| 432 | ByteBufCodecs.optional(ResourceKey.streamCodec(Registries.TEST_INSTANCE)),
|
| 433 | TestInstanceBlockEntity.Data::test,
|
| 434 | Vec3i.STREAM_CODEC,
|
| 435 | TestInstanceBlockEntity.Data::size,
|
| 436 | Rotation.STREAM_CODEC,
|
| 437 | TestInstanceBlockEntity.Data::rotation,
|
| 438 | ByteBufCodecs.BOOL,
|
| 439 | TestInstanceBlockEntity.Data::ignoreEntities,
|
| 440 | TestInstanceBlockEntity.Status.STREAM_CODEC,
|
| 441 | TestInstanceBlockEntity.Data::status,
|
| 442 | ByteBufCodecs.optional(ComponentSerialization.STREAM_CODEC),
|
| 443 | TestInstanceBlockEntity.Data::errorMessage,
|
| 444 | TestInstanceBlockEntity.Data::new
|
| 445 | );
|
| 446 |
|
| 447 | public TestInstanceBlockEntity.Data withSize(final Vec3i size) {
|
| 448 | return new TestInstanceBlockEntity.Data(this.test, size, this.rotation, this.ignoreEntities, this.status, this.errorMessage);
|
| 449 | }
|
| 450 |
|
| 451 | public TestInstanceBlockEntity.Data withStatus(final TestInstanceBlockEntity.Status status) {
|
| 452 | return new TestInstanceBlockEntity.Data(this.test, this.size, this.rotation, this.ignoreEntities, status, Optional.empty());
|
| 453 | }
|
| 454 |
|
| 455 | public TestInstanceBlockEntity.Data withError(final Component error) {
|
| 456 | return new TestInstanceBlockEntity.Data(
|
| 457 | this.test, this.size, this.rotation, this.ignoreEntities, TestInstanceBlockEntity.Status.FINISHED, Optional.of(error)
|
| 458 | );
|
| 459 | }
|
| 460 | }
|
| 461 |
|
| 462 | public record ErrorMarker(BlockPos pos, Component text) {
|
| 463 | public static final Codec<TestInstanceBlockEntity.ErrorMarker> CODEC = RecordCodecBuilder.create(
|
| 464 | i -> i.group(
|
| 465 | BlockPos.CODEC.fieldOf("pos").forGetter(TestInstanceBlockEntity.ErrorMarker::pos),
|
| 466 | ComponentSerialization.CODEC.fieldOf("text").forGetter(TestInstanceBlockEntity.ErrorMarker::text)
|
| 467 | )
|
| 468 | .apply(i, TestInstanceBlockEntity.ErrorMarker::new)
|
| 469 | );
|
| 470 | public static final Codec<List<TestInstanceBlockEntity.ErrorMarker>> LIST_CODEC = CODEC.listOf();
|
| 471 | }
|
| 472 |
|
| 473 | public enum Status implements StringRepresentable {
|
| 474 | CLEARED("cleared", 0),
|
| 475 | RUNNING("running", 1),
|
| 476 | FINISHED("finished", 2);
|
| 477 |
|
| 478 | private static final IntFunction<TestInstanceBlockEntity.Status> ID_MAP = ByIdMap.continuous(s -> s.index, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 479 | public static final Codec<TestInstanceBlockEntity.Status> CODEC = StringRepresentable.fromEnum(TestInstanceBlockEntity.Status::values);
|
| 480 | public static final StreamCodec<ByteBuf, TestInstanceBlockEntity.Status> STREAM_CODEC = ByteBufCodecs.idMapper(
|
| 481 | TestInstanceBlockEntity.Status::byIndex, s -> s.index
|
| 482 | );
|
| 483 | private final String id;
|
| 484 | private final int index;
|
| 485 |
|
| 486 | Status(final String id, final int index) {
|
| 487 | this.id = id;
|
| 488 | this.index = index;
|
| 489 | }
|
| 490 |
|
| 491 | @Override
|
| 492 | public String getSerializedName() {
|
| 493 | return this.id;
|
| 494 | }
|
| 495 |
|
| 496 | public static TestInstanceBlockEntity.Status byIndex(final int index) {
|
| 497 | return ID_MAP.apply(index);
|
| 498 | }
|
| 499 | }
|
| 500 | }
|
| 501 |
|