Administrator's Toolkit VS plugin
修订版 | c98f626ee6c0ff75fa6262c2e4f9de5ac299e5a1 (tree) |
---|---|
时间 | 2020-11-05 07:23:22 |
作者 | melchior <melchior@user...> |
Commiter | melchior |
Attempt to change spawn points for First time joins
@@ -47,19 +47,20 @@ namespace AdminToolkit | ||
47 | 47 | public VariableSpawnpoints(ICoreServerAPI _serverAPI) : base(_serverAPI) |
48 | 48 | { |
49 | 49 | this.Command = "spawnpoints"; |
50 | - this.Description = "Control add/remove/adjust List of approved player spawn points"; | |
50 | + this.Description = "Control add / remove / adjust List of approved player spawn points"; | |
51 | 51 | this.RequiredPrivilege = Privilege.setspawn; |
52 | 52 | this.Syntax = @"Add {name} {123 456 -678}|here / Remove {name} / List / Enable / Disable"; |
53 | 53 | this.handler += HandleCommand; |
54 | 54 | |
55 | 55 | ServerAPI.Event.ServerRunPhase(EnumServerRunPhase.RunGame, ReloadSpawnpoints); |
56 | - ServerAPI.Event.PlayerJoin += PlayerJoins; | |
56 | + | |
57 | 57 | ServerAPI.Event.PlayerDeath += PlayerDied; |
58 | + ServerAPI.Event.PlayerCreate += (byPlayer) => ChangeDefaultRoleSpawnPoint(); | |
59 | + | |
60 | + | |
58 | 61 | ServerAPI.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, PersistSpawnpoints); |
59 | 62 | } |
60 | 63 | |
61 | - | |
62 | - | |
63 | 64 | private void HandleCommand(IServerPlayer player, int groupId, CmdArgs args) |
64 | 65 | { |
65 | 66 | string name; |
@@ -114,9 +115,12 @@ namespace AdminToolkit | ||
114 | 115 | name = args.PopWord("?"); |
115 | 116 | #if DEBUG |
116 | 117 | ResetPlayer(name); |
117 | - player.SendMessage(groupId, $"Reset Playerdata for '{name}'", EnumChatType.CommandSuccess); | |
118 | + player.SendMessage(groupId, $"Reset State / Playerdata for '{name}'", EnumChatType.CommandSuccess); | |
118 | 119 | #endif |
119 | 120 | } |
121 | + else { | |
122 | + player.SendMessage(groupId, "Only CONSOLE may use command!", EnumChatType.CommandError); | |
123 | + } | |
120 | 124 | break; |
121 | 125 | |
122 | 126 | default: |
@@ -186,11 +190,16 @@ namespace AdminToolkit | ||
186 | 190 | if (byPlayer != null && this.Spawnpoints != null && this.Spawnpoints.Count > 0) |
187 | 191 | { |
188 | 192 | byPlayer.ClearSpawnPosition( ); |
189 | - //var datas = ServerAPI.PlayerData.GetPlayerDataByUid(byPlayer.PlayerUID); | |
190 | - //datas. | |
191 | - //ServerAPI.Server.Players.Remove(byPlayer); //List is a array copy | |
193 | + | |
194 | + var serverMain = ServerAPI.World as ServerMain; | |
192 | 195 | var servPlayer = byPlayer as ServerPlayer; |
196 | + var pdm = ServerAPI.PlayerData as PlayerDataManager; | |
197 | + var swpd = servPlayer.WorldData as ServerWorldPlayerData; | |
198 | + //swpd.Init(serverMain); | |
199 | + pdm.PlayerDataByUid.Remove(byPlayer.PlayerUID); | |
200 | + pdm.WorldDataByUID.Remove(byPlayer.PlayerUID); | |
193 | 201 | |
202 | + //pdm.GetOrCreateServerPlayerData(byPlayer.PlayerUID); | |
194 | 203 | } |
195 | 204 | } |
196 | 205 |
@@ -214,7 +223,7 @@ namespace AdminToolkit | ||
214 | 223 | Logger.Notification("Created (empty) Spawnpoints Data"); |
215 | 224 | } |
216 | 225 | |
217 | - | |
226 | + ChangeDefaultRoleSpawnPoint( ); | |
218 | 227 | } |
219 | 228 | |
220 | 229 | private void PersistSpawnpoints( ) |
@@ -227,12 +236,12 @@ namespace AdminToolkit | ||
227 | 236 | |
228 | 237 | } |
229 | 238 | |
230 | - private void PlayerJoins( IServerPlayer byPlayer) | |
239 | + private void RotateSpawnPoint( IServerPlayer byPlayer) | |
231 | 240 | { |
232 | 241 | if (this.CachedConfiguration.VariableSpawnpoints && this.Spawnpoints.Count > 0) { |
233 | 242 | |
234 | - var swpd = byPlayer.WorldData as ServerWorldPlayerData; | |
235 | - if (swpd.SpawnPosition == null || KnownSpawn(swpd.SpawnPosition)) | |
243 | + var swpd = byPlayer.WorldData as ServerWorldPlayerData; | |
244 | + if (swpd != null && (swpd.SpawnPosition == null || KnownSpawn(swpd.SpawnPosition))) | |
236 | 245 | { |
237 | 246 | RandomizeSpawnpoint(byPlayer); |
238 | 247 | } |
@@ -243,27 +252,43 @@ namespace AdminToolkit | ||
243 | 252 | { |
244 | 253 | if (this.CachedConfiguration.VariableSpawnpoints && this.Spawnpoints.Count > 0) { |
245 | 254 | |
246 | - var swpd = byPlayer.WorldData as ServerWorldPlayerData; | |
247 | - if (swpd.SpawnPosition == null || KnownSpawn(swpd.SpawnPosition)) | |
248 | - { | |
255 | + var swpd = byPlayer.WorldData as ServerWorldPlayerData; | |
256 | + if (swpd != null && (swpd.SpawnPosition == null || KnownSpawn(swpd.SpawnPosition))) { | |
249 | 257 | RandomizeSpawnpoint(byPlayer); |
250 | 258 | } |
251 | 259 | } |
252 | 260 | } |
253 | 261 | |
262 | + private void ChangeDefaultRoleSpawnPoint( ) | |
263 | + { | |
264 | + if (this.CachedConfiguration.VariableSpawnpoints && this.Spawnpoints.Count > 0) { | |
265 | + | |
266 | + var randSp = this.Spawnpoints[random.Next(0, Spawnpoints.Count)]; | |
267 | + | |
268 | + string roleCode = ServerAPI.Server.Config.DefaultRoleCode; | |
269 | + var serverPlayerRole = ServerAPI.Server.Config.Roles.Single(pr => pr.Code == roleCode) as PlayerRole; | |
270 | + | |
271 | + #if DEBUG | |
272 | + Logger.VerboseDebug($"Changing ({serverPlayerRole.Name}) to spawn '{randSp.Name}' @ {randSp.Location}"); | |
273 | + #endif | |
274 | + serverPlayerRole.DefaultSpawn = randSp.Location; | |
275 | + } | |
276 | + | |
277 | + } | |
278 | + | |
254 | 279 | #endregion |
255 | 280 | |
256 | 281 | |
257 | - private void RandomizeSpawnpoint(IServerPlayer byPlayer) | |
282 | + internal void RandomizeSpawnpoint(IServerPlayer byPlayer) | |
258 | 283 | { |
259 | - var randSp = this.Spawnpoints[random.Next(0, Spawnpoints.Count - 1)]; | |
284 | + var randSp = this.Spawnpoints[random.Next(0, Spawnpoints.Count)]; | |
260 | 285 | byPlayer.SetSpawnPosition(randSp.Location); |
261 | 286 | #if DEBUG |
262 | 287 | Logger.VerboseDebug($"Changed spawn Pos: '{randSp.Name}' @ {randSp.Location} for {byPlayer.PlayerName}"); |
263 | 288 | #endif |
264 | 289 | } |
265 | 290 | |
266 | - private bool KnownSpawn(PlayerSpawnPos aSpawn) | |
291 | + internal bool KnownSpawn(PlayerSpawnPos aSpawn) | |
267 | 292 | { |
268 | 293 | if (this.Spawnpoints.Count > 0) { |
269 | 294 | return this.Spawnpoints.Any(vsps => vsps.Location.x == aSpawn.x |
@@ -274,7 +299,7 @@ namespace AdminToolkit | ||
274 | 299 | return false; |
275 | 300 | } |
276 | 301 | |
277 | - private Vec3i RelativeToAbsolute(Vec3i location) | |
302 | + internal Vec3i RelativeToAbsolute(Vec3i location) | |
278 | 303 | { |
279 | 304 | var start = ServerAPI.World.DefaultSpawnPosition.AsBlockPos; |
280 | 305 | return new Vec3i( location.X + start.X, location.Y, location.Z + start.Z); |
@@ -286,7 +311,7 @@ namespace AdminToolkit | ||
286 | 311 | return new Vec3i(start.X - location.X, location.Y, start.Z - location.Z); |
287 | 312 | } |
288 | 313 | |
289 | - private string PrettyFormat(PlayerSpawnPos spawnPos) | |
314 | + internal string PrettyFormat(PlayerSpawnPos spawnPos) | |
290 | 315 | { |
291 | 316 | var start = ServerAPI.World.DefaultSpawnPosition.AsBlockPos; |
292 | 317 |
@@ -106,7 +106,7 @@ edit file ('/ModConfig/admintoolkit.json' ) : | ||
106 | 106 | |
107 | 107 | |
108 | 108 | |
109 | -VAIRABLE SPAWNS | |
109 | +VARIABLE SPAWNS | |
110 | 110 | --------------- |
111 | 111 | |
112 | 112 | Lets admins define and have players spawn from a list of pre-defined coordinates. |