Changed the GetData<T> and SetData<T> components of TSPlayer, and added RemoveData and ContainsData

This commit is contained in:
White 2016-05-16 16:45:00 +09:30
parent 275ca6f9d1
commit 457b3d40b3

View file

@ -542,34 +542,59 @@ namespace TShockAPI
} }
/// <summary> /// <summary>
/// Stores an object on this player, accessible with the given key /// Determines whether the player's storage contains the given key.
/// </summary> /// </summary>
/// <typeparam name="T">Type of the object being stored</typeparam> /// <param name="key">Key to test.</param>
/// <param name="key">Key with which to access the object</param> /// <returns></returns>
/// <param name="value">Object to store</param> public bool ContainsData(string key)
public void SetData<T>(string key, T value)
{ {
data.TryAdd(key, value); return data.ContainsKey(key);
} }
/// <summary> /// <summary>
/// Returns the stored object associated with the given key /// Returns the stored object associated with the given key.
/// </summary> /// </summary>
/// <typeparam name="T">Type of the object being retrieved</typeparam> /// <typeparam name="T">Type of the object being retrieved.</typeparam>
/// <param name="key">Key with which to access the object</param> /// <param name="key">Key with which to access the object.</param>
/// <param name="value">Stored object, or default(T) if not found</param> /// <returns>The stored object, or default(T) if not found.</returns>
/// <returns>True if the value was retrieved, else False</returns> public T GetData<T>(string key)
public bool GetData<T>(string key, out T value)
{ {
object obj; object obj;
if (!data.TryGetValue(key, out obj)) if (!data.TryGetValue(key, out obj))
{ {
value = default(T); return default(T);
return false;
} }
value = (T)obj; return (T)obj;
return true; }
/// <summary>
/// Stores an object on this player, accessible with the given key.
/// </summary>
/// <typeparam name="T">Type of the object being stored.</typeparam>
/// <param name="key">Key with which to access the object.</param>
/// <param name="value">Object to store.</param>
public void SetData<T>(string key, T value)
{
if (!data.TryAdd(key, value))
{
data.TryUpdate(key, value, data[key]);
}
}
/// <summary>
/// Removes the stored object associated with the given key.
/// </summary>
/// <param name="key">Key with which to access the object.</param>
/// <returns>The removed object. </returns>
public object RemoveData(string key)
{
object rem;
if (data.TryRemove(key, out rem))
{
return rem;
}
return null;
} }
public TSPlayer(int index) public TSPlayer(int index)