using System; using System.Collections.Generic; using System.Text; using System.IO; using Substrate.Core; using Substrate.Nbt; namespace Substrate { /// /// Functions to manage multiple entities and files in multiplayer settings. /// /// This manager is intended for player files stored in standard compressed NBT format. public class PlayerManager : IPlayerManager { private string _playerPath; /// /// Create a new for a given file path. /// /// Path to a directory containing player data files. public PlayerManager (string playerDir) { _playerPath = playerDir; } /// /// Gets a representing the backing NBT data stream. /// /// The name of the player to fetch. /// A for the given player. protected PlayerFile GetPlayerFile (string name) { return new PlayerFile(_playerPath, name); } /// /// Gets a raw of data for the given player. /// /// The name of the player to fetch. /// An containing the given player's raw data. /// Thrown when the manager cannot read in an NBT data stream. public NbtTree GetPlayerTree (string name) { PlayerFile pf = GetPlayerFile(name); Stream nbtstr = pf.GetDataInputStream(); if (nbtstr == null) { throw new NbtIOException("Failed to initialize NBT data stream for input."); } return new NbtTree(nbtstr); } /// /// Saves a raw representing a player to the given player's file. /// /// The name of the player to write data to. /// The player's data as an . /// Thrown when the manager cannot initialize an NBT data stream for output. public void SetPlayerTree (string name, NbtTree tree) { PlayerFile pf = GetPlayerFile(name); Stream zipstr = pf.GetDataOutputStream(); if (zipstr == null) { throw new NbtIOException("Failed to initialize NBT data stream for output."); } tree.WriteTo(zipstr); zipstr.Close(); } /// /// Gets a object for the given player. /// /// The name of the player to fetch. /// A object for the given player, or null if the player could not be found. /// Thrown when the manager cannot read in a player that should exist. public Player GetPlayer (string name) { if (!PlayerExists(name)) { return null; } try { return new Player().LoadTreeSafe(GetPlayerTree(name).Root); } catch (Exception ex) { PlayerIOException pex = new PlayerIOException("Could not load player", ex); pex.Data["PlayerName"] = name; throw pex; } } /// /// Saves a object's data back to the given player's file. /// /// The name of the player to write back to. /// The object containing data to write back. /// Thrown when the manager cannot write out the player. public void SetPlayer (string name, Player player) { try { SetPlayerTree(name, new NbtTree(player.BuildTree() as TagNodeCompound)); } catch (Exception ex) { PlayerIOException pex = new PlayerIOException("Could not save player", ex); pex.Data["PlayerName"] = name; throw pex; } } /// /// Checks if data for a player with the given name exists. /// /// The name of the player to look up. /// True if player data was found; false otherwise. public bool PlayerExists (string name) { return new PlayerFile(_playerPath, name).Exists(); } /// /// Deletes a player with the given name from the underlying data store. /// /// The name of the player to delete. /// Thrown when the manager cannot delete the player. public void DeletePlayer (string name) { try { new PlayerFile(_playerPath, name).Delete(); } catch (Exception ex) { PlayerIOException pex = new PlayerIOException("Could not save player", ex); pex.Data["PlayerName"] = name; throw pex; } } } }