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

View File

@ -155,10 +155,11 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
* Gets the NBT data from the player-data file. * 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. * 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 * @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 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()) if (isOnline())
throw new IllegalStateException("Cannot access data file of " + getName() + " because theyre online."); throw new IllegalStateException("Cannot access data file of " + getName() + " because theyre online.");
CompoundTag data = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) CompoundTag data = ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class)
@ -166,13 +167,13 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
.getPlayerList() .getPlayerList()
.playerIo() .playerIo()
.getPlayerData(getUniqueId().toString()); .getPlayerData(getUniqueId().toString());
if (convertTag) { if (data != null && convertTag) {
int srcVersion = data.contains("DataVersion", Tag.TAG_ANY_NUMERIC()) ? data.getInt("DataVersion") : -1; int srcVersion = data.contains("DataVersion", Tag.TAG_ANY_NUMERIC()) ? data.getInt("DataVersion") : -1;
int destVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion(); int destVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion();
try { try {
data = MCDataConverter.convertTag(MCTypeRegistry.PLAYER(), data, srcVersion, destVersion); data = MCDataConverter.convertTag(MCTypeRegistry.PLAYER(), data, srcVersion, destVersion);
} catch (Exception e) { } 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; 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. * 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. * @return the NBT data from the player-data file.
* @throws IllegalStateException if the player is online. * @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)); return new PlayerDataWrapper(getPlayerData(true));
} }
@ -218,16 +220,18 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
/** /**
* Gets the players inventory. * Gets the players inventory.
* @return 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(); return getPlayerDataWrapper().getInventory();
} }
/** /**
* Gets the players enderchest. * Gets the players enderchest.
* @return 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(); return getPlayerDataWrapper().getEnderChest();
} }