示例#1
0
        public void OnSpawn(PlayerSpawnEvent ev)
        {
            // Only runs if not already running
            if (!plugin.spawning.Contains(ev.Player.SteamId))
            {
                plugin.spawning.Add(ev.Player.SteamId);
                new Task(async() =>
                {
                    // Delays execution until smod has created the object
                    await Task.Delay(500);
                    await Task.Delay(plugin.delay);

                    Player player = plugin.Server.GetPlayers(ev.Player.SteamId)[0];
                    if (player == null)
                    {
                        plugin.Warn("Could not find spawning player '" + ev.Player.Name + "', did they disconnect?");
                        plugin.spawning.Remove(ev.Player.SteamId);
                        return;
                    }
                    try
                    {
                        // Check each registered permission node
                        JProperty[] permissionNodes = plugin.loadouts.Properties().ToArray();
                        foreach (JProperty permissionNode in permissionNodes)
                        {
                            if (player.HasPermission("customloadouts." + permissionNode.Name))
                            {
                                try
                                {
                                    // Check if their role has a custom loadout registered
                                    JProperty[] roles = permissionNode.Value.Value <JObject>().Properties().ToArray();
                                    foreach (JProperty role in roles)
                                    {
                                        if (player.TeamRole.Role.ToString() == role.Name || role.Name == "all")
                                        {
                                            try
                                            {
                                                plugin.GiveItems(role.Value, ev.Player);
                                            }
                                            catch (Exception e)
                                            {
                                                plugin.Error("Error giving items: " + e.ToString());
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    plugin.Error("Error checking role: " + e.ToString());
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        plugin.Error("Error checking permission: " + e.ToString());
                    }
                    plugin.spawning.Remove(ev.Player.SteamId);
                }).Start();
            }
        }