Better error handling when loading offline player data

This commit is contained in:
Marc Baloup 2024-03-05 16:29:11 +01:00
parent e02ccc2b60
commit 33e4e053cf
1 changed files with 11 additions and 7 deletions

View File

@ -155,10 +155,11 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
* Gets the NBT data from the player-data file.
* It will not work if the player is online, because the data on the file are not synchronized with real-time values.
* @param convertTag true to convert the data to the current MC version, false to keep the saved version
* @return the NBT data from the player-data file.
* @return the NBT data from the player-data file, or null if the file does not exists.
* @throws IllegalStateException if the player is online.
* @throws IOException if an error occurs reading the data.
*/
default CompoundTag getPlayerData(boolean convertTag) {
default CompoundTag getPlayerData(boolean convertTag) throws IOException {
if (isOnline())
throw new IllegalStateException("Cannot access data file of " + getName() + " because theyre online.");
CompoundTag data = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class)
@ -166,13 +167,13 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
.getPlayerList()
.playerIo()
.getPlayerData(getUniqueId().toString());
if (convertTag) {
if (data != null && convertTag) {
int srcVersion = data.contains("DataVersion", Tag.TAG_ANY_NUMERIC()) ? data.getInt("DataVersion") : -1;
int destVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion();
try {
data = MCDataConverter.convertTag(MCTypeRegistry.PLAYER(), data, srcVersion, destVersion);
} catch (Exception e) {
throw new RuntimeException("Unable to upgrade data format of player " + getName() + " (" + getUniqueId() + ") from version " + destVersion + " to " + destVersion);
throw new IOException("Unable to upgrade data format of player " + getName() + " (" + getUniqueId() + ") from version " + destVersion + " to " + destVersion);
}
}
return data;
@ -183,8 +184,9 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
* It will not work if the player is online, because the data on the file are not synchronized with real-time values.
* @return the NBT data from the player-data file.
* @throws IllegalStateException if the player is online.
* @throws IOException if an error occurs reading the data.
*/
default PlayerDataWrapper getPlayerDataWrapper() {
default PlayerDataWrapper getPlayerDataWrapper() throws IOException {
return new PlayerDataWrapper(getPlayerData(true));
}
@ -218,16 +220,18 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
/**
* Gets the players inventory.
* @return the players inventory.
* @throws IOException if an error occurs reading the data.
*/
default PlayerInventory getInventory() {
default PlayerInventory getInventory() throws IOException {
return getPlayerDataWrapper().getInventory();
}
/**
* Gets the players enderchest.
* @return the players enderchest.
* @throws IOException if an error occurs reading the data.
*/
default Inventory getEnderChest() {
default Inventory getEnderChest() throws IOException {
return getPlayerDataWrapper().getEnderChest();
}