Skip to content

Commit

Permalink
Fix loading world on Paper
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo committed Nov 11, 2023
1 parent faf1d38 commit c9a2ae4
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit c9a2ae4

Please sign in to comment.