-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerBadgeMain.cs
More file actions
94 lines (81 loc) · 2.75 KB
/
PlayerBadgeMain.cs
File metadata and controls
94 lines (81 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using LabApi.Events.CustomHandlers;
using LabApi.Features;
using LabApi.Loader;
using LabApi.Loader.Features.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using LabApi.Features.Console;
namespace LabAPI_PlayerBadge;
public class PlayerBadgeMain : Plugin {
public static PlayerBadgeMain Singleton { get; private set; }
public override string Name => "Player Badge Plugin";
public override string Description => "Award a badge when a player joins.";
public override string Author => "TASA-Ed Studio";
public override Version Version => new(1, 2, 0, 0);
public override Version RequiredApiVersion => new(LabApiProperties.CompiledVersion);
public PlayerBadgeEvent Events { get; private set; }
public Dictionary<string, BadgeListType> BadgeCache { get; private set; }
public float RainbowInterval { get; private set; }
public override void LoadConfigs()
{
base.LoadConfigs();
var config = this.LoadConfig<PlayerBadgeConfig>("configs.yml") ?? new PlayerBadgeConfig();
var list = config.PlayerBadgeList ?? [];
var grouped = list.GroupBy(x => x.PlayerId).ToArray();
// 检查 PlayerId 是否有重复
if (grouped.Any(g => g.Count() > 1))
{
Logger.Warn("Duplicate PlayerId found in configuration. Duplicates will be ignored, keeping the first entry.");
}
// 构建徽章缓存
BadgeCache = grouped
.ToDictionary(g => g.Key, g => g.First());
RainbowInterval = config.RainbowInterval;
}
public override void Enable()
{
Singleton = this;
Events = new PlayerBadgeEvent();
CustomHandlersManager.RegisterEventsHandler(Events);
}
public override void Disable()
{
CustomHandlersManager.UnregisterEventsHandler(Events);
Events?.CleanupAllRainbowEffects();
Events = null;
BadgeCache = null;
Singleton = null;
}
}
public class PlayerBadgeConfig
{
/// <summary>
/// 玩家徽章列表。如 [new BadgeListType { PlayerId, BadgeName, BadgeColor }]。
/// </summary>
public BadgeListType[] PlayerBadgeList { get; set; } = [new()
{
PlayerId = "PlayerId@steam",
BadgeName = "徽章名称",
BadgeColor = "rainbow"
}];
/// <summary>
/// 彩虹颜色切换间隔,单位为秒。
/// </summary>
public float RainbowInterval { get; set; } = 0.6f;
}
public class BadgeListType
{
/// <summary>
/// 玩家 ID。
/// </summary>
public string PlayerId { get; set; }
/// <summary>
/// 徽章名称。
/// </summary>
public string BadgeName { get; set; }
/// <summary>
/// 徽章颜色。
/// </summary>
public string BadgeColor { get; set; }
}