From d1a04a7a66a8b36ffd032fce3db109c26c634cc2 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 19 Jul 2024 00:23:12 +0200 Subject: [PATCH] Proper exception handling when not able to load player data file --- .../lib/paper/players/PaperOffPlayer.java | 17 +++++++++++------ .../lib/paper/util/PlayerDataWrapper.java | 11 +++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java index 0b67b9c..7aaf900 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/players/PaperOffPlayer.java @@ -5,6 +5,7 @@ import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.CompoundTag; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.NbtIo; import fr.pandacube.lib.paper.util.PlayerDataWrapper; +import fr.pandacube.lib.paper.util.PlayerDataWrapper.PlayerDataLoadException; import fr.pandacube.lib.paper.world.WorldUtil; import fr.pandacube.lib.players.standalone.AbstractOffPlayer; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; @@ -155,12 +156,16 @@ public interface PaperOffPlayer extends AbstractOffPlayer { */ default CompoundTag getPlayerData() { if (isOnline()) - throw new IllegalStateException("Cannot access data file of " + getName() + " because they’re online."); - return ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) - .getServer() - .getPlayerList() - .playerIo() - .load(getName(), getUniqueId().toString()).orElse(null); + throw new IllegalStateException("Cannot access data file of " + getName() + " because they're online."); + try { + return ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) + .getServer() + .getPlayerList() + .playerIo() + .load(getName(), getUniqueId().toString()).orElse(null); + } catch (Exception|LinkageError e) { + throw new PlayerDataLoadException(getName(), getUniqueId(), e); + } } /** diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java index fd7c06c..10a6910 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/PlayerDataWrapper.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.TreeMap; +import java.util.UUID; import java.util.function.IntUnaryOperator; /** @@ -362,4 +363,14 @@ public record PlayerDataWrapper(CompoundTag data) { return null; } } + + + + + + public static class PlayerDataLoadException extends RuntimeException { + public PlayerDataLoadException(String playerName, UUID playerId, Throwable cause) { + super("Unable to load data of player " + playerName + " (" + playerId + ")", cause); + } + } }