Ability to save offline player data

This commit is contained in:
Marc Baloup 2023-02-16 12:17:59 +01:00
parent dd2b4467ed
commit fb4c62a0bc

View File

@ -1,9 +1,13 @@
package fr.pandacube.lib.paper.players; package fr.pandacube.lib.paper.players;
import com.google.common.io.Files;
import fr.pandacube.lib.paper.reflect.util.PrimaryWorlds;
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack; import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftItemStack;
import fr.pandacube.lib.paper.reflect.wrapper.craftbukkit.CraftServer; 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.CompoundTag;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.ListTag; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.ListTag;
import fr.pandacube.lib.paper.reflect.wrapper.minecraft.nbt.NbtIo;
import fr.pandacube.lib.paper.util.WorldUtil;
import fr.pandacube.lib.players.standalone.AbstractOffPlayer; import fr.pandacube.lib.players.standalone.AbstractOffPlayer;
import fr.pandacube.lib.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.reflect.wrapper.ReflectWrapper;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -13,6 +17,8 @@ import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import java.io.File;
import java.io.IOException;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
/** /**
@ -142,7 +148,15 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
* Player data * Player data
*/ */
/**
* Gets the NBT data from the playerdata file.
* 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 playerdata file.
* @throws IllegalStateException if the player is online.
*/
default CompoundTag getPlayerData() { default CompoundTag getPlayerData() {
if (isOnline())
throw new IllegalStateException("Cannot access data file of " + getName() + " because theyre online.");
return ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class) return ReflectWrapper.wrapTyped(Bukkit.getServer(), CraftServer.class)
.getServer() .getServer()
.getPlayerList() .getPlayerList()
@ -150,6 +164,37 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
.getPlayerData(getUniqueId().toString()); .getPlayerData(getUniqueId().toString());
} }
/**
* Saves the provided NBT data to the playerdata file.
* It will not work if the player is online, because the provided data will be lost when the player disconnects.
* @param data the data to save.
* @throws IllegalStateException if the player is online.
* @throws IOException if an IO error occurs.
*/
default void savePlayerData(CompoundTag data) throws IOException {
if (isOnline())
throw new IllegalStateException("Cannot write data file of " + getName() + " because theyre online.");
File file = getPlayerDataFile(false);
File old = getPlayerDataFile(true);
old.delete();
Files.move(file, old);
NbtIo.writeCompressed(data, file);
}
/**
* Gets the file where the playerdata is stored.
* @param old true to return the path of old data, false to return the actual file.
* @return the file where the playerdata is stored.
*/
default File getPlayerDataFile(boolean old) {
File playerDataDir = new File(WorldUtil.worldDir(PrimaryWorlds.PRIMARY_WORLDS.get(0)), "playerdata");
return new File(playerDataDir, getUniqueId() + (old ? ".dat_old" : ".dat"));
}
/**
* Gets the content of the players inventory.
* @return the content of the players inventory.
*/
default ItemStack[] getInventoryContent() { default ItemStack[] getInventoryContent() {
ItemStack[] content = new ItemStack[InventoryType.PLAYER.getDefaultSize()]; ItemStack[] content = new ItemStack[InventoryType.PLAYER.getDefaultSize()];
CompoundTag playerData = getPlayerData(); CompoundTag playerData = getPlayerData();
@ -179,6 +224,10 @@ public interface PaperOffPlayer extends AbstractOffPlayer {
return content; return content;
} }
/**
* Gets the content of the players enderchest.
* @return the content of the players enderchest.
*/
default ItemStack[] getEnderchestContent() { default ItemStack[] getEnderchestContent() {
ItemStack[] content = new ItemStack[InventoryType.ENDER_CHEST.getDefaultSize()]; ItemStack[] content = new ItemStack[InventoryType.ENDER_CHEST.getDefaultSize()];
CompoundTag playerData = getPlayerData(); CompoundTag playerData = getPlayerData();