Fix concurrency crashes for player list

Bug report by doranchak (forum post no 1136)
This commit is contained in:
ORelio 2015-03-19 22:08:26 +01:00
parent e3c38ed6ac
commit 858ad12783

View file

@ -20,7 +20,7 @@ namespace MinecraftClient
private static List<string> cmd_names = new List<string>(); private static List<string> cmd_names = new List<string>();
private static Dictionary<string, Command> cmds = new Dictionary<string, Command>(); private static Dictionary<string, Command> cmds = new Dictionary<string, Command>();
private List<ChatBot> bots = new List<ChatBot>(); private List<ChatBot> bots = new List<ChatBot>();
private Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid,string>(); private readonly Dictionary<Guid, string> onlinePlayers = new Dictionary<Guid,string>();
private static List<ChatBots.Script> scripts_on_hold = new List<ChatBots.Script>(); private static List<ChatBots.Script> scripts_on_hold = new List<ChatBots.Script>();
public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; } public void BotLoad(ChatBot b) { b.SetHandler(this); bots.Add(b); b.Initialize(); Settings.SingleCommand = ""; }
public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); } public void BotUnLoad(ChatBot b) { bots.RemoveAll(item => object.ReferenceEquals(item, b)); }
@ -411,9 +411,12 @@ namespace MinecraftClient
/// <param name="name">Name of the player</param> /// <param name="name">Name of the player</param>
public void OnPlayerJoin(Guid uuid, string name) public void OnPlayerJoin(Guid uuid, string name)
{
lock (onlinePlayers)
{ {
onlinePlayers[uuid] = name; onlinePlayers[uuid] = name;
} }
}
/// <summary> /// <summary>
/// Triggered when a player has left the game /// Triggered when a player has left the game
@ -421,9 +424,12 @@ namespace MinecraftClient
/// <param name="uuid">UUID of the player</param> /// <param name="uuid">UUID of the player</param>
public void OnPlayerLeave(Guid uuid) public void OnPlayerLeave(Guid uuid)
{
lock (onlinePlayers)
{ {
onlinePlayers.Remove(uuid); onlinePlayers.Remove(uuid);
} }
}
/// <summary> /// <summary>
/// Get a set of online player names /// Get a set of online player names
@ -431,8 +437,11 @@ namespace MinecraftClient
/// <returns>Online player names</returns> /// <returns>Online player names</returns>
public string[] getOnlinePlayers() public string[] getOnlinePlayers()
{
lock (onlinePlayers)
{ {
return onlinePlayers.Values.Distinct().ToArray(); return onlinePlayers.Values.Distinct().ToArray();
} }
} }
} }
}