| 1 | package net.minecraft.world.level.chunk.storage;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 5 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 6 | import java.io.IOException;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.concurrent.CompletableFuture;
|
| 11 | import java.util.concurrent.Executor;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.ListTag;
|
| 14 | import net.minecraft.nbt.NbtUtils;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.util.ProblemReporter;
|
| 17 | import net.minecraft.util.thread.ConsecutiveExecutor;
|
| 18 | import net.minecraft.world.entity.Entity;
|
| 19 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 20 | import net.minecraft.world.entity.EntityType;
|
| 21 | import net.minecraft.world.level.ChunkPos;
|
| 22 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 23 | import net.minecraft.world.level.entity.ChunkEntities;
|
| 24 | import net.minecraft.world.level.entity.EntityPersistentStorage;
|
| 25 | import net.minecraft.world.level.storage.TagValueInput;
|
| 26 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 27 | import net.minecraft.world.level.storage.ValueInput;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class EntityStorage implements EntityPersistentStorage<Entity> {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private static final String ENTITIES_TAG = "Entities";
|
| 33 | private static final String POSITION_TAG = "Position";
|
| 34 | private final ServerLevel level;
|
| 35 | private final SimpleRegionStorage simpleRegionStorage;
|
| 36 | private final LongSet emptyChunks = new LongOpenHashSet();
|
| 37 | private final ConsecutiveExecutor entityDeserializerQueue;
|
| 38 |
|
| 39 | public EntityStorage(final SimpleRegionStorage simpleRegionStorage, final ServerLevel level, final Executor mainThreadExecutor) {
|
| 40 | this.simpleRegionStorage = simpleRegionStorage;
|
| 41 | this.level = level;
|
| 42 | this.entityDeserializerQueue = new ConsecutiveExecutor(mainThreadExecutor, "entity-deserializer");
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public CompletableFuture<ChunkEntities<Entity>> loadEntities(final ChunkPos pos) {
|
| 47 | if (this.emptyChunks.contains(pos.pack())) {
|
| 48 | return CompletableFuture.completedFuture(emptyChunk(pos));
|
| 49 | }
|
| 50 |
|
| 51 | CompletableFuture<Optional<CompoundTag>> loadFuture = this.simpleRegionStorage.read(pos);
|
| 52 | this.reportLoadFailureIfPresent(loadFuture, pos);
|
| 53 | return loadFuture.thenApplyAsync(tag -> {
|
| 54 | if (tag.isEmpty()) {
|
| 55 | this.emptyChunks.add(pos.pack());
|
| 56 | return emptyChunk(pos);
|
| 57 | }
|
| 58 |
|
| 59 | try {
|
| 60 | ChunkPos storedPos = tag.get().read("Position", ChunkPos.CODEC).orElseThrow();
|
| 61 | if (!Objects.equals(pos, storedPos)) {
|
| 62 | LOGGER.error("Chunk file at {} is in the wrong location. (Expected {}, got {})", pos, pos, storedPos);
|
| 63 | this.level.getServer().reportMisplacedChunk(storedPos, pos, this.simpleRegionStorage.storageInfo());
|
| 64 | }
|
| 65 | } catch (Exception e) {
|
| 66 | LOGGER.warn("Failed to parse chunk {} position info", pos, e);
|
| 67 | this.level.getServer().reportChunkLoadFailure(e, this.simpleRegionStorage.storageInfo(), pos);
|
| 68 | }
|
| 69 |
|
| 70 | CompoundTag upgradedChunkTag = this.simpleRegionStorage.upgradeChunkTag(tag.get(), -1);
|
| 71 |
|
| 72 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(ChunkAccess.problemPath(pos), LOGGER)) {
|
| 73 | ValueInput chunkRoot = TagValueInput.create(reporter, this.level.registryAccess(), upgradedChunkTag);
|
| 74 | ValueInput.ValueInputList entities = chunkRoot.childrenListOrEmpty("Entities");
|
| 75 | List<Entity> chunkEntities = EntityType.loadEntitiesRecursive(entities, this.level, EntitySpawnReason.LOAD).toList();
|
| 76 | return new ChunkEntities<>(pos, chunkEntities);
|
| 77 | }
|
| 78 | }, this.entityDeserializerQueue::schedule);
|
| 79 | }
|
| 80 |
|
| 81 | private static ChunkEntities<Entity> emptyChunk(final ChunkPos pos) {
|
| 82 | return new ChunkEntities<>(pos, List.of());
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void storeEntities(final ChunkEntities<Entity> chunk) {
|
| 87 | ChunkPos pos = chunk.getPos();
|
| 88 | if (chunk.isEmpty()) {
|
| 89 | if (this.emptyChunks.add(pos.pack())) {
|
| 90 | this.reportSaveFailureIfPresent(this.simpleRegionStorage.write(pos, IOWorker.STORE_EMPTY), pos);
|
| 91 | }
|
| 92 | } else {
|
| 93 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(ChunkAccess.problemPath(pos), LOGGER)) {
|
| 94 | ListTag entities = new ListTag();
|
| 95 | chunk.getEntities().forEach(e -> {
|
| 96 | TagValueOutput output = TagValueOutput.createWithContext(reporter.forChild(e.problemPath()), e.registryAccess());
|
| 97 | if (e.save(output)) {
|
| 98 | CompoundTag result = output.buildResult();
|
| 99 | entities.add(result);
|
| 100 | }
|
| 101 | });
|
| 102 | CompoundTag chunkTag = NbtUtils.addCurrentDataVersion(new CompoundTag());
|
| 103 | chunkTag.put("Entities", entities);
|
| 104 | chunkTag.store("Position", ChunkPos.CODEC, pos);
|
| 105 | this.reportSaveFailureIfPresent(this.simpleRegionStorage.write(pos, chunkTag), pos);
|
| 106 | this.emptyChunks.remove(pos.pack());
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private void reportSaveFailureIfPresent(final CompletableFuture<?> operation, final ChunkPos pos) {
|
| 112 | operation.exceptionally(t -> {
|
| 113 | LOGGER.error("Failed to store entity chunk {}", pos, t);
|
| 114 | this.level.getServer().reportChunkSaveFailure(t, this.simpleRegionStorage.storageInfo(), pos);
|
| 115 | return null;
|
| 116 | });
|
| 117 | }
|
| 118 |
|
| 119 | private void reportLoadFailureIfPresent(final CompletableFuture<?> operation, final ChunkPos pos) {
|
| 120 | operation.exceptionally(t -> {
|
| 121 | LOGGER.error("Failed to load entity chunk {}", pos, t);
|
| 122 | this.level.getServer().reportChunkLoadFailure(t, this.simpleRegionStorage.storageInfo(), pos);
|
| 123 | return null;
|
| 124 | });
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public void flush(final boolean flushStorage) {
|
| 129 | this.simpleRegionStorage.synchronize(flushStorage).join();
|
| 130 | this.entityDeserializerQueue.runAll();
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void close() throws IOException {
|
| 135 | this.simpleRegionStorage.close();
|
| 136 | }
|
| 137 | }
|
| 138 |
|