Administrator's Toolkit VS plugin
修订版 | a4b36e597c3df04e67017e08d0056bb239cb35ec (tree) |
---|---|
时间 | 2020-11-08 08:40:18 |
作者 | ![]() |
Commiter | melchior |
Pre Beta 0.3.5 changes & fixes
@@ -0,0 +1,121 @@ | ||
1 | +using System; | |
2 | +using System.Text; | |
3 | +using System.Linq; | |
4 | +using System.Collections.Generic; | |
5 | +using System.IO; | |
6 | + | |
7 | +using Vintagestory.API.Common; | |
8 | +using Vintagestory.API.Server; | |
9 | +using Vintagestory.API.Config; | |
10 | +using Vintagestory.API.Datastructures; | |
11 | + | |
12 | +namespace AdminToolkit | |
13 | +{ | |
14 | + public partial class AdminToolkit : ModSystem | |
15 | + { | |
16 | + private void PopulateAdminRoleTable( ) | |
17 | + { | |
18 | + AdminRoles = new List<string>( ); | |
19 | + foreach (var role in ServerAPI.Server.Config.Roles) { | |
20 | + | |
21 | + if (role.PrivilegeLevel >= 200 && role.Privileges.Any(adminPriviledges.Contains)) { | |
22 | + | |
23 | + AdminRoles.Add(role.Code); | |
24 | + | |
25 | + #if DEBUG | |
26 | + Mod.Logger.VerboseDebug("Role {0} =[{1}] considerd Admin-like", role.Name, role.Code); | |
27 | + #endif | |
28 | + } | |
29 | + } | |
30 | + } | |
31 | + | |
32 | + private void PrepareServersideConfig( ) | |
33 | + { | |
34 | + AdminModConfig config = ServerAPI.LoadModConfig<AdminModConfig>(_configFilename); | |
35 | + | |
36 | + if (config == null) { | |
37 | + //Regen default | |
38 | + Mod.Logger.Warning("Regenerating default config as it was missing / unparsable..."); | |
39 | + ServerAPI.StoreModConfig<AdminModConfig>(new AdminModConfig( ), _configFilename); | |
40 | + config = ServerAPI.LoadModConfig<AdminModConfig>(_configFilename); | |
41 | + } | |
42 | + | |
43 | + this.CachedConfiguration = config; | |
44 | + | |
45 | + if (this.CachedConfiguration.RuleRoleChangerEnabled) { Mod.Logger.Notification("Admin toolkit; Role change on Rule accept: * ENABLED *"); } | |
46 | + } | |
47 | + | |
48 | + /// <summary> | |
49 | + /// Makes Administrator text messages appear 'different' | |
50 | + /// </summary> | |
51 | + /// <returns>The voice of authority.</returns> | |
52 | + /// <param name="byPlayer">By player.</param> | |
53 | + /// <param name="channelId">Channel identifier.</param> | |
54 | + /// <param name="message">Message.</param> | |
55 | + /// <param name="consumed">Consumed.</param> | |
56 | + private void BoomingVoiceOfAuthority(IServerPlayer byPlayer, int channelId, ref string message, ref string data, BoolRef consumed) | |
57 | + { | |
58 | + if (AdminRoles.Contains(byPlayer.Role.Code)) { | |
59 | + //Make text lined | |
60 | + consumed.value = false; | |
61 | + StringBuilder adminMessage = new StringBuilder( ); | |
62 | + foreach (char letter in message) { | |
63 | + | |
64 | + adminMessage.Append(letter); | |
65 | + if (!Char.IsWhiteSpace(letter)) { | |
66 | + //adminMessage.Append(@"̅̅̅"); | |
67 | + adminMessage.Append(@"̲"); | |
68 | + } | |
69 | + } | |
70 | + | |
71 | + message = adminMessage.ToString( ); | |
72 | + } | |
73 | + } | |
74 | + | |
75 | + private void SaveConfigSettings( ) | |
76 | + { | |
77 | + if (this.CachedConfiguration != null) { | |
78 | + Mod.Logger.Notification("Persisting configuration."); | |
79 | + ServerAPI.StoreModConfig<AdminModConfig>(this.CachedConfiguration, _configFilename); | |
80 | + } | |
81 | + } | |
82 | + | |
83 | + private void WelcomeMessage(IServerPlayer byPlayer) | |
84 | + { | |
85 | + if (AdminToolkit.AdminRoles.Contains(byPlayer.Role.Code)) { | |
86 | + //Annonce to all players that Admin\Moderator has arrived | |
87 | + | |
88 | + StringBuilder adminMessage = new StringBuilder( ); | |
89 | + | |
90 | + adminMessage.AppendFormat("<font color='{1}' weight='bold'>«{0}» {2}</font> Present.", byPlayer.Role.Name, byPlayer.Role.Color.Name, byPlayer.PlayerName); | |
91 | + | |
92 | + ServerAPI.SendMessageToGroup(GlobalConstants.AllChatGroups, adminMessage.ToString( ), EnumChatType.AllGroups); | |
93 | + | |
94 | + byPlayer.ServerData.CustomPlayerData[AdminToolkit._lastLoginKey] = DateTimeOffset.UtcNow.ToString("u"); | |
95 | + } | |
96 | + | |
97 | + double elapsedDays = 0D; | |
98 | + string elapsedDayStr; | |
99 | + | |
100 | + if (byPlayer.ServerData.CustomPlayerData.TryGetValue(AdminToolkit._lastGameDayCountKey, out elapsedDayStr) | |
101 | + && | |
102 | + double.TryParse(elapsedDayStr, out elapsedDays)) { | |
103 | + elapsedDays = ServerAPI.World.Calendar.TotalDays - elapsedDays; | |
104 | + var playerPos = byPlayer.Entity.Pos.AsBlockPos.Copy(); | |
105 | + //No Localized Season names? | |
106 | + var welcomeMsg = $"Meantime... {elapsedDays:F1} days have passed. {ServerAPI.World.AllOnlinePlayers.Length} Players online. It is {ServerAPI.World.Calendar.GetSeason(playerPos)} in the {ServerAPI.World.Calendar.GetHemisphere(playerPos)}."; | |
107 | + | |
108 | + byPlayer.SendMessage(GlobalConstants.CurrentChatGroup, welcomeMsg, EnumChatType.OthersMessage); | |
109 | + } | |
110 | + else { | |
111 | + byPlayer.SendMessage(GlobalConstants.CurrentChatGroup, $"In the year; {ServerAPI.World.Calendar.PrettyDate( )}", EnumChatType.OthersMessage); | |
112 | + } | |
113 | + | |
114 | + byPlayer.ServerData.CustomPlayerData[AdminToolkit._lastGameDayCountKey] = ServerAPI.World.Calendar.TotalDays.ToString("R"); | |
115 | + | |
116 | + | |
117 | + } | |
118 | + | |
119 | + } | |
120 | +} | |
121 | + |
@@ -77,6 +77,7 @@ | ||
77 | 77 | <Compile Include="Commands\BannerControl.cs" /> |
78 | 78 | <Compile Include="Commands\PingerCommand.cs" /> |
79 | 79 | <Compile Include="Commands\VariableSpawnpoints.cs" /> |
80 | + <Compile Include="ATK_BasicFeatures.cs" /> | |
80 | 81 | </ItemGroup> |
81 | 82 | <ItemGroup> |
82 | 83 | <Folder Include="VS_libs\" /> |
@@ -15,7 +15,10 @@ namespace AdminToolkit | ||
15 | 15 | /// <summary> |
16 | 16 | /// Admininstrators toolkit Mod system |
17 | 17 | /// </summary> |
18 | - public class AdminToolkit : ModSystem | |
18 | + /// <remarks> | |
19 | + /// Boilerplate; all Functions in 'ATK_BasicFeatures.cs' | |
20 | + /// </remarks> | |
21 | + public partial class AdminToolkit : ModSystem | |
19 | 22 | { |
20 | 23 | /* Things it should do: |
21 | 24 | * ============== |
@@ -27,7 +30,7 @@ namespace AdminToolkit | ||
27 | 30 | * [DONE] Cyclic automatic Backups |
28 | 31 | * [WIP} Broadcast messages, on a schedule ?????? |
29 | 32 | * [???] Custom Server name : custom formats to indicate server state/time/things |
30 | - * [IDEA]: Player inventory Insurance; recover lost items for a (two) time fee... one policy per player. | |
33 | + * [IDEA]: Player VIRTUAL (temp) Inventory; Move Player inventory to alternate [world] D.B. as Inventory backup | |
31 | 34 | * [WIP]: Variable Player (re)Spawn points from a list ~ at random |
32 | 35 | */ |
33 | 36 |
@@ -96,87 +99,24 @@ namespace AdminToolkit | ||
96 | 99 | PopulateAdminRoleTable( ); |
97 | 100 | PrepareServersideConfig( ); |
98 | 101 | |
99 | - this.ServerAPI.RegisterCommand(new RulesCommand(this.ServerAPI) ); | |
102 | + if (this.CachedConfiguration.RuleRoleChangerEnabled) { this.ServerAPI.RegisterCommand(new RulesCommand(this.ServerAPI)); } | |
100 | 103 | this.ServerAPI.RegisterCommand(new AdminListingCommand(this.ServerAPI) ); |
101 | 104 | this.ServerAPI.RegisterCommand(new BackupCycleCommand(this.ServerAPI) ); |
102 | 105 | //this.ServerAPI.RegisterCommand(new BannerControl(this.ServerAPI)); |
103 | 106 | this.ServerAPI.RegisterCommand(new PingerCommand(this.ServerAPI)); |
104 | - this.ServerAPI.RegisterCommand(new VariableSpawnpoints(this.ServerAPI)); | |
107 | + if (this.CachedConfiguration.VariableSpawnpoints) { this.ServerAPI.RegisterCommand(new VariableSpawnpoints(this.ServerAPI)); } | |
105 | 108 | |
106 | 109 | if (CachedConfiguration.BoomingVoice) { |
107 | 110 | this.ServerAPI.Event.PlayerChat += BoomingVoiceOfAuthority; |
108 | 111 | } |
109 | - //TODO: Server MOTD / Name, DYNAMIC status info [season, moon-phase, events... ect] | |
110 | 112 | |
111 | - this.ServerAPI.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, SaveConfigSettings); | |
112 | - } | |
113 | - | |
114 | - private void PopulateAdminRoleTable( ) | |
115 | - { | |
116 | - AdminRoles = new List<string>( ); | |
117 | - foreach (var role in ServerAPI.Server.Config.Roles) { | |
118 | - | |
119 | - if (role.PrivilegeLevel >= 200 && role.Privileges.Any(adminPriviledges.Contains )) { | |
120 | - | |
121 | - AdminRoles.Add(role.Code); | |
122 | - | |
123 | - #if DEBUG | |
124 | - Mod.Logger.VerboseDebug("Role {0} =[{1}] considerd Admin-like", role.Name, role.Code); | |
125 | - #endif | |
126 | - } | |
127 | - } | |
128 | - } | |
129 | - | |
130 | - private void PrepareServersideConfig( ) | |
131 | - { | |
132 | - AdminModConfig config = ServerAPI.LoadModConfig<AdminModConfig>(_configFilename); | |
133 | - | |
134 | - if (config == null) { | |
135 | - //Regen default | |
136 | - Mod.Logger.Warning("Regenerating default config as it was missing / unparsable..."); | |
137 | - ServerAPI.StoreModConfig<AdminModConfig>(new AdminModConfig( ), _configFilename); | |
138 | - config = ServerAPI.LoadModConfig<AdminModConfig>(_configFilename); | |
139 | - } | |
113 | + this.ServerAPI.Event.PlayerNowPlaying += WelcomeMessage; | |
140 | 114 | |
141 | - this.CachedConfiguration = config; | |
142 | 115 | |
143 | - if (this.CachedConfiguration.RuleRoleChangerEnabled) { Mod.Logger.Notification("Admin toolkit; Role change on Rule accept: * ENABLED *"); } | |
116 | + this.ServerAPI.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, SaveConfigSettings); | |
144 | 117 | } |
145 | 118 | |
146 | - /// <summary> | |
147 | - /// Makes Administrator text messages appear 'different' | |
148 | - /// </summary> | |
149 | - /// <returns>The voice of authority.</returns> | |
150 | - /// <param name="byPlayer">By player.</param> | |
151 | - /// <param name="channelId">Channel identifier.</param> | |
152 | - /// <param name="message">Message.</param> | |
153 | - /// <param name="consumed">Consumed.</param> | |
154 | - private void BoomingVoiceOfAuthority(IServerPlayer byPlayer, int channelId, ref string message, ref string data, BoolRef consumed) | |
155 | - { | |
156 | - if (AdminRoles.Contains(byPlayer.Role.Code)) { | |
157 | - //Make text lined | |
158 | - consumed.value = false; | |
159 | - StringBuilder adminMessage = new StringBuilder( ); | |
160 | - foreach (char letter in message) { | |
161 | - | |
162 | - adminMessage.Append(letter); | |
163 | - if (!Char.IsWhiteSpace(letter)) { | |
164 | - //adminMessage.Append(@"̅̅̅"); | |
165 | - adminMessage.Append(@"̲"); | |
166 | - } | |
167 | - } | |
168 | - | |
169 | - message = adminMessage.ToString( ); | |
170 | - } | |
171 | - } | |
172 | 119 | |
173 | - private void SaveConfigSettings( ) | |
174 | - { | |
175 | - if (this.CachedConfiguration != null) { | |
176 | - Mod.Logger.Notification("Persisting configuration."); | |
177 | - ServerAPI.StoreModConfig<AdminModConfig>(this.CachedConfiguration, _configFilename); | |
178 | - } | |
179 | - } | |
180 | 120 | |
181 | 121 | } |
182 | 122 | } |
@@ -277,7 +277,7 @@ namespace AdminToolkit | ||
277 | 277 | { |
278 | 278 | //ServerAPI.WorldManager.CurrentWorldName |
279 | 279 | //ServerAPI.WorldManager.SaveGame.WorldName |
280 | - string worldNameTrimmed = Path.GetFileNameWithoutExtension(ServerAPI.WorldManager.CurrentWorldName); | |
280 | + string worldNameTrimmed = Path.GetFileNameWithoutExtension( String.IsNullOrEmpty(ServerAPI.WorldManager.CurrentWorldName) ? GamePaths.DefaultSaveFilenameWithoutExtension : ServerAPI.WorldManager.CurrentWorldName); | |
281 | 281 | |
282 | 282 | fileFilter = $"{worldNameTrimmed}-*{GlobalConstants.WorldSaveExtension}"; |
283 | 283 | Logger.VerboseDebug("File Filter: {0}", fileFilter); |
@@ -10,6 +10,8 @@ using Vintagestory.API.Server; | ||
10 | 10 | using Vintagestory.API.Common; |
11 | 11 | using Vintagestory.API.Config; |
12 | 12 | |
13 | +using Vintagestory.Client.NoObf;//Perhaps this 'LanguageConfig' moves to a more general place? ...API.Config... | |
14 | + | |
13 | 15 | namespace AdminToolkit |
14 | 16 | { |
15 | 17 | public class RulesCommand : AdminModCommand |
@@ -41,8 +43,8 @@ namespace AdminToolkit | ||
41 | 43 | { |
42 | 44 | Rules = new Dictionary<string, List<string>>( ); |
43 | 45 | RulesPath = ServerAPI.GetOrCreateDataPath(_rulesPathKey); |
44 | - | |
45 | - string[] lang_codes = { "en", "de", "it", "fr", "pt-br", "ru" };//FIXME: this should extracted from language codes | |
46 | + GuiCompositeSettings.LanguageConfig[] supportedLangs = ServerAPI.Assets.Get<GuiCompositeSettings.LanguageConfig[]>(new AssetLocation("lang/languages.json")); | |
47 | + string[ ] lang_codes = supportedLangs.Select(sl => sl.Code).ToArray( );//{ "en", "de", "it", "fr", "pt-br", "ru" }; | |
46 | 48 | |
47 | 49 | foreach (var languageCode in lang_codes) { |
48 | 50 | //For each locale that is configured... get rules_?.txt |
@@ -148,17 +150,7 @@ namespace AdminToolkit | ||
148 | 150 | /// <param name="byPlayer">By player.</param> |
149 | 151 | private void UponJoin(IServerPlayer byPlayer) |
150 | 152 | { |
151 | - if (AdminToolkit.AdminRoles.Contains(byPlayer.Role.Code)) { | |
152 | - //Annonce to all Admin\Moderator is here. | |
153 | - | |
154 | - StringBuilder adminMessage = new StringBuilder( ); | |
155 | - | |
156 | - adminMessage.AppendFormat("<font color='{1}' weight='bold'>«{0}» {2}</font> Present.", byPlayer.Role.Name, byPlayer.Role.Color.Name, byPlayer.PlayerName); | |
157 | - | |
158 | - ServerAPI.SendMessageToGroup(GlobalConstants.AllChatGroups, adminMessage.ToString( ), EnumChatType.AllGroups); | |
159 | - | |
160 | - byPlayer.ServerData.CustomPlayerData[AdminToolkit._lastLoginKey] = DateTimeOffset.UtcNow.ToString("u"); | |
161 | - } else { | |
153 | + if (!AdminToolkit.AdminRoles.Contains(byPlayer.Role.Code)) { | |
162 | 154 | //For regular players |
163 | 155 | if (RulesCommand.CheckRuleAccepted(byPlayer) == false) { |
164 | 156 | //TODO: localize |
@@ -169,22 +161,6 @@ namespace AdminToolkit | ||
169 | 161 | } |
170 | 162 | } |
171 | 163 | } |
172 | - | |
173 | - double elapsedDays = 0D; | |
174 | - string elapsedDayStr; | |
175 | - | |
176 | - if (byPlayer.ServerData.CustomPlayerData.TryGetValue(AdminToolkit._lastGameDayCountKey, out elapsedDayStr) | |
177 | - && | |
178 | - double.TryParse(elapsedDayStr, out elapsedDays)) | |
179 | - { | |
180 | - elapsedDays = ServerAPI.World.Calendar.TotalDays - elapsedDays; | |
181 | - byPlayer.SendMessage(GlobalConstants.CurrentChatGroup, $"Meantime... {elapsedDays:F1} days have passed. {ServerAPI.World.AllOnlinePlayers.Length} Players online.", EnumChatType.OthersMessage); | |
182 | - } else | |
183 | - { | |
184 | - byPlayer.SendMessage(GlobalConstants.CurrentChatGroup, $"In the year; {ServerAPI.World.Calendar.PrettyDate( )}", EnumChatType.OthersMessage); | |
185 | - } | |
186 | - | |
187 | - byPlayer.ServerData.CustomPlayerData[AdminToolkit._lastGameDayCountKey] = ServerAPI.World.Calendar.TotalDays.ToString("R"); | |
188 | 164 | } |
189 | 165 | |
190 | 166 | private void PlayerAcceptsRulesAction(IServerPlayer byPlayer ) |