From c9a2ae44e2c5dd6947467023fca87581561a4f16 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Sat, 11 Nov 2023 01:06:40 -0500 Subject: [PATCH] Fix loading world on Paper --- .../internal/v1_20_R2/PlayerDataManager.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/internal/v1_20_R2/src/main/java/com/lishid/openinv/internal/v1_20_R2/PlayerDataManager.java b/internal/v1_20_R2/src/main/java/com/lishid/openinv/internal/v1_20_R2/PlayerDataManager.java index 0f3d5ce4..5b85e265 100644 --- a/internal/v1_20_R2/src/main/java/com/lishid/openinv/internal/v1_20_R2/PlayerDataManager.java +++ b/internal/v1_20_R2/src/main/java/com/lishid/openinv/internal/v1_20_R2/PlayerDataManager.java @@ -21,7 +21,9 @@ import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.OpenInventoryView; import com.mojang.authlib.GameProfile; +import com.mojang.serialization.Dynamic; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.NbtOps; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; import net.minecraft.server.MinecraftServer; @@ -33,6 +35,7 @@ import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.level.Level; +import net.minecraft.world.level.dimension.DimensionType; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Server; @@ -42,6 +45,7 @@ import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftContainer; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryView; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,7 +60,7 @@ public PlayerDataManager() { try { bukkitEntity = Entity.class.getDeclaredField("bukkitEntity"); } catch (NoSuchFieldException e) { - Logger logger = OpenInv.getPlugin(OpenInv.class).getLogger(); + Logger logger = JavaPlugin.getPlugin(OpenInv.class).getLogger(); logger.warning("Unable to obtain field to inject custom save process - players' mounts may be deleted when loaded."); logger.log(java.util.logging.Level.WARNING, e.getMessage(), e); bukkitEntity = null; @@ -121,7 +125,10 @@ public PlayerDataManager() { try { injectPlayer(entity); } catch (IllegalAccessException e) { - e.printStackTrace(); + JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + java.util.logging.Level.WARNING, + e, + () -> "Unable to inject ServerPlayer, players' mounts may be lost!"); } // Load data. This also reads basic data into the player. @@ -137,8 +144,14 @@ public PlayerDataManager() { entity.readAdditionalSaveData(loadedData); if (entity.level() == null) { - // Paper: Move player to spawn - entity.spawnIn(null); + // Paper: Also read world. + // See PlayerList#placeNewPlayer + // Using legacy parse supports ancient vanilla player data. + DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, loadedData.get("Dimension"))) + .resultOrPartial(JavaPlugin.getPlugin(OpenInv.class).getLogger()::warning) + .map(server::getLevel) + // If ServerLevel exists, set, otherwise move to spawn. + .ifPresentOrElse(entity::setServerLevel, () -> entity.spawnIn(null)); } // Return the Bukkit entity. @@ -166,7 +179,10 @@ public Player inject(@NotNull Player player) { injectPlayer(nmsPlayer); return nmsPlayer.getBukkitEntity(); } catch (IllegalAccessException e) { - e.printStackTrace(); + JavaPlugin.getPlugin(OpenInv.class).getLogger().log( + java.util.logging.Level.WARNING, + e, + () -> "Unable to inject ServerPlayer, players' mounts may be lost!"); return player; } }