PlayerDataStorage .java

net.minecraft.world.level.storage.PlayerDataStorage (class, 88 lines): members: playerDir, fixerUpper, load, save, LOGGER. strings: ".dat_old", ".dat".

net.minecraft.world.level.storageclass88 linescommon3.4 KB

API Surface (2)

void save(final Player player)
Optional<CompoundTag> load(final NameAndId nameAndId)

Fields & Constants (3)

LOGGER
playerDir
fixerUpper

String literals (3)

".dat"
".dat_old"
"_corrupted_"

Full Source

PlayerDataStorage.java88 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage;
2
3
import com.mojang.datafixers.DataFixer;
4
import com.mojang.logging.LogUtils;
5
import java.io.File;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.StandardCopyOption;
9
import java.time.ZonedDateTime;
10
import java.util.Optional;
11
import net.minecraft.nbt.CompoundTag;
12
import net.minecraft.nbt.NbtAccounter;
13
import net.minecraft.nbt.NbtIo;
14
import net.minecraft.nbt.NbtUtils;
15
import net.minecraft.server.players.NameAndId;
16
import net.minecraft.util.ProblemReporter;
17
import net.minecraft.util.Util;
18
import net.minecraft.util.datafix.DataFixTypes;
19
import net.minecraft.world.entity.player.Player;
20
import org.slf4j.Logger;
21
22
public class PlayerDataStorage {
23
	private static final Logger LOGGER = LogUtils.getLogger();
24
	private final File playerDir;
25
	protected final DataFixer fixerUpper;
26
27
	public PlayerDataStorage(final LevelStorageSource.LevelStorageAccess levelAccess, final DataFixer fixerUpper) {
28
		this.fixerUpper = fixerUpper;
29
		this.playerDir = levelAccess.getLevelPath(LevelResource.PLAYER_DATA_DIR).toFile();
30
		this.playerDir.mkdirs();
31
	}
32
33
	public void save(final Player player) {
34
		try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(player.problemPath(), LOGGER)) {
35
			TagValueOutput output = TagValueOutput.createWithContext(reporter, player.registryAccess());
36
			player.saveWithoutId(output);
37
			Path playerDirPath = this.playerDir.toPath();
38
			Path tmpFile = Files.createTempFile(playerDirPath, player.getStringUUID() + "-", ".dat");
39
			CompoundTag dataToStore = output.buildResult();
40
			NbtIo.writeCompressed(dataToStore, tmpFile);
41
			Path realFile = playerDirPath.resolve(player.getStringUUID() + ".dat");
42
			Path oldFile = playerDirPath.resolve(player.getStringUUID() + ".dat_old");
43
			Util.safeReplaceFile(realFile, tmpFile, oldFile);
44
		} catch (Exception ignored) {
45
			LOGGER.warn("Failed to save player data for {}", player.getPlainTextName());
46
		}
47
	}
48
49
	private void backup(final NameAndId nameAndId, final String suffix) {
50
		Path playerDirPath = this.playerDir.toPath();
51
		String idString = nameAndId.id().toString();
52
		Path realPath = playerDirPath.resolve(idString + suffix);
53
		Path backupPath = playerDirPath.resolve(idString + "_corrupted_" + ZonedDateTime.now().format(FileNameDateFormatter.FORMATTER) + suffix);
54
		if (Files.isRegularFile(realPath)) {
55
			try {
56
				Files.copy(realPath, backupPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
57
			} catch (Exception e) {
58
				LOGGER.warn("Failed to copy the player.dat file for {}", nameAndId.name(), e);
59
			}
60
		}
61
	}
62
63
	private Optional<CompoundTag> load(final NameAndId nameAndId, final String suffix) {
64
		File realFile = new File(this.playerDir, nameAndId.id() + suffix);
65
		if (realFile.exists() && realFile.isFile()) {
66
			try {
67
				return Optional.of(NbtIo.readCompressed(realFile.toPath(), NbtAccounter.unlimitedHeap()));
68
			} catch (Exception ignored) {
69
				LOGGER.warn("Failed to load player data for {}", nameAndId.name());
70
			}
71
		}
72
73
		return Optional.empty();
74
	}
75
76
	public Optional<CompoundTag> load(final NameAndId nameAndId) {
77
		Optional<CompoundTag> optTag = this.load(nameAndId, ".dat");
78
		if (optTag.isEmpty()) {
79
			this.backup(nameAndId, ".dat");
80
		}
81
82
		return optTag.or(() -> this.load(nameAndId, ".dat_old")).map(tag -> {
83
			int version = NbtUtils.getDataVersion(tag);
84
			return DataFixTypes.PLAYER.updateToCurrentVersion(this.fixerUpper, tag, version);
85
		});
86
	}
87
}
88

Decompiled Minecraft 26.3 source of PlayerDataStorage (net/minecraft/world/level/storage/PlayerDataStorage.java) with Mojang official mappings, stored verbatim on the site.