From 6e5c332272d053aaac96616b9d951ece34e1a02e Mon Sep 17 00:00:00 2001 From: Jikoo Date: Tue, 16 Mar 2021 23:42:30 -0400 Subject: [PATCH] Extract player implementation --- .../openinv/internal/v1_16_R3/OpenPlayer.java | 69 +++++++++++++++++++ .../internal/v1_16_R3/PlayerDataManager.java | 41 +---------- 2 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/OpenPlayer.java diff --git a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/OpenPlayer.java b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/OpenPlayer.java new file mode 100644 index 0000000..def3f96 --- /dev/null +++ b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/OpenPlayer.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2011-2021 lishid. All rights reserved. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.lishid.openinv.internal.v1_16_R3; + +import java.io.File; +import java.io.FileOutputStream; +import net.minecraft.server.v1_16_R3.EntityPlayer; +import net.minecraft.server.v1_16_R3.NBTCompressedStreamTools; +import net.minecraft.server.v1_16_R3.NBTTagCompound; +import net.minecraft.server.v1_16_R3.WorldNBTStorage; +import org.apache.logging.log4j.LogManager; +import org.bukkit.craftbukkit.v1_16_R3.CraftServer; +import org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer; + +public class OpenPlayer extends CraftPlayer { + + public OpenPlayer(CraftServer server, EntityPlayer entity) { + super(server, entity); + } + + @Override + public void saveData() { + super.saveData(); + EntityPlayer player = this.getHandle(); + // See net.minecraft.server.WorldNBTStorage#save(EntityPlayer) + try { + WorldNBTStorage worldNBTStorage = player.server.getPlayerList().playerFileData; + + NBTTagCompound playerData = player.save(new NBTTagCompound()); + + if (!isOnline()) { + // Special case: save old vehicle data + NBTTagCompound oldData = worldNBTStorage.load(player); + + if (oldData != null && oldData.hasKeyOfType("RootVehicle", 10)) { + // See net.minecraft.server.PlayerList#a(NetworkManager, EntityPlayer) and net.minecraft.server.EntityPlayer#b(NBTTagCompound) + playerData.set("RootVehicle", oldData.getCompound("RootVehicle")); + } + } + + File file = new File(worldNBTStorage.getPlayerDir(), player.getUniqueIDString() + ".dat.tmp"); + File file1 = new File(worldNBTStorage.getPlayerDir(), player.getUniqueIDString() + ".dat"); + + NBTCompressedStreamTools.a(playerData, new FileOutputStream(file)); + + if (file1.exists() && !file1.delete() || !file.renameTo(file1)) { + LogManager.getLogger().warn("Failed to save player data for {}", player.getDisplayName().getString()); + } + + } catch (Exception e) { + LogManager.getLogger().warn("Failed to save player data for {}", player.getDisplayName().getString()); + } + } + +} diff --git a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java index 4c029db..3871520 100644 --- a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java +++ b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java @@ -20,8 +20,6 @@ import com.lishid.openinv.internal.IPlayerDataManager; import com.lishid.openinv.internal.ISpecialInventory; import com.lishid.openinv.internal.OpenInventoryView; import com.mojang.authlib.GameProfile; -import java.io.File; -import java.io.FileOutputStream; import java.lang.reflect.Field; import net.minecraft.server.v1_16_R3.ChatComponentText; import net.minecraft.server.v1_16_R3.ChatMessageType; @@ -30,16 +28,12 @@ import net.minecraft.server.v1_16_R3.Containers; import net.minecraft.server.v1_16_R3.Entity; import net.minecraft.server.v1_16_R3.EntityPlayer; import net.minecraft.server.v1_16_R3.MinecraftServer; -import net.minecraft.server.v1_16_R3.NBTCompressedStreamTools; -import net.minecraft.server.v1_16_R3.NBTTagCompound; import net.minecraft.server.v1_16_R3.PacketPlayOutChat; import net.minecraft.server.v1_16_R3.PacketPlayOutOpenWindow; import net.minecraft.server.v1_16_R3.PlayerInteractManager; import net.minecraft.server.v1_16_R3.SystemUtils; import net.minecraft.server.v1_16_R3.World; -import net.minecraft.server.v1_16_R3.WorldNBTStorage; import net.minecraft.server.v1_16_R3.WorldServer; -import org.apache.logging.log4j.LogManager; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.Server; @@ -131,40 +125,7 @@ public class PlayerDataManager implements IPlayerDataManager { bukkitEntity.setAccessible(true); - bukkitEntity.set(player, new CraftPlayer(player.server.server, player) { - @Override - public void saveData() { - super.saveData(); - // See net.minecraft.server.WorldNBTStorage#save(EntityPlayer) - try { - WorldNBTStorage worldNBTStorage = player.server.getPlayerList().playerFileData; - - NBTTagCompound playerData = player.save(new NBTTagCompound()); - - if (!isOnline()) { - // Special case: save old vehicle data - NBTTagCompound oldData = worldNBTStorage.load(player); - - if (oldData != null && oldData.hasKeyOfType("RootVehicle", 10)) { - // See net.minecraft.server.PlayerList#a(NetworkManager, EntityPlayer) and net.minecraft.server.EntityPlayer#b(NBTTagCompound) - playerData.set("RootVehicle", oldData.getCompound("RootVehicle")); - } - } - - File file = new File(worldNBTStorage.getPlayerDir(), player.getUniqueIDString() + ".dat.tmp"); - File file1 = new File(worldNBTStorage.getPlayerDir(), player.getUniqueIDString() + ".dat"); - - NBTCompressedStreamTools.a(playerData, new FileOutputStream(file)); - - if (file1.exists() && !file1.delete() || !file.renameTo(file1)) { - LogManager.getLogger().warn("Failed to save player data for {}", player.getDisplayName().getString()); - } - - } catch (Exception e) { - LogManager.getLogger().warn("Failed to save player data for {}", player.getDisplayName().getString()); - } - } - }); + bukkitEntity.set(player, new OpenPlayer(player.server.server, player)); } @NotNull