From 62acc1f3c31a8dd089677314d598a9738220b16e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 21 Sep 2017 15:42:10 +1000 Subject: [PATCH] Fix two bots being able to send up with same entity ID --- MCGalaxy/Bots/PlayerBot.cs | 47 +++++++++++++-------------------- MCGalaxy/Player/Player.Login.cs | 18 +++++++++++++ MCGalaxy/Player/Player.cs | 20 -------------- 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/MCGalaxy/Bots/PlayerBot.cs b/MCGalaxy/Bots/PlayerBot.cs index d423c565f..2671404d1 100644 --- a/MCGalaxy/Bots/PlayerBot.cs +++ b/MCGalaxy/Bots/PlayerBot.cs @@ -46,21 +46,11 @@ namespace MCGalaxy { public int movementSpeed = 3; internal sbyte curJump = 0; - public PlayerBot(string n, Level lvl) { Init(n, lvl); } - - public PlayerBot(string n, Level lvl, ushort x, ushort y, ushort z, byte rotx, byte roty) { - Pos = new Position(x, y, z); - SetYawPitch(rotx, roty); - Init(n, lvl); - } - - void Init(string n, Level lvl) { + public PlayerBot(string n, Level lvl) { name = n; DisplayName = n; SkinName = n; color = "&1"; ModelBB = AABB.ModelAABB(Model, lvl); - level = lvl; - id = NextFreeId(this); hasExtPositions = true; BotsScheduler.Activate(); } @@ -70,7 +60,11 @@ namespace MCGalaxy { public override Level Level { get { return level; } } public static void Add(PlayerBot bot, bool save = true) { - bot.level.Bots.Add(bot); + // Lock to ensure that no two bots can end up with the same playerid + lock (bot.level.Bots.locker) { + bot.id = NextFreeId(bot); + bot.level.Bots.Add(bot); + } bot.GlobalSpawn(); if (save) BotsFile.Save(bot.level); } @@ -109,13 +103,10 @@ namespace MCGalaxy { for (int i = 0; i < 256; i++) used[i] = 0; - // Lock to ensure that no two bots can end up with the same playerid - lock (bot.level.Bots.locker) { - PlayerBot[] bots = bot.level.Bots.Items; - for (int i = 0; i < bots.Length; i++) { - byte id = bots[i].id; - used[id] = 1; - } + PlayerBot[] bots = bot.level.Bots.Items; + for (int i = 0; i < bots.Length; i++) { + byte id = bots[i].id; + used[id] = 1; } for (byte i = 127; i >= 64; i-- ) { @@ -163,18 +154,18 @@ namespace MCGalaxy { PlayerBot[] bots = lvl.Bots.Items; for (int i = 0; i < bots.Length; i++) { PlayerBot bot = bots[i]; - if (bot.movement) bot.PerformMovement(); + if (bot.movement) bot.PerformMovement(); Position pos = bot.Pos; Orientation rot = bot.Rot; Entities.GetPositionPacket(ref ptrSrc, bot.id, true, false, pos, bot.lastPos, rot, bot.lastRot); - Entities.GetPositionPacket(ref ptrExt, bot.id, true, true, - pos, bot.lastPos, rot, bot.lastRot); + Entities.GetPositionPacket(ref ptrExt, bot.id, true, true, + pos, bot.lastPos, rot, bot.lastRot); bot.lastPos = pos; bot.lastRot = rot; } - int countSrc = (int)(ptrSrc - src); - if (countSrc == 0) return; + int countSrc = (int)(ptrSrc - src); + if (countSrc == 0) return; int countExt = (int)(ptrExt - ext); byte[] srcPacket = new byte[countSrc]; @@ -184,7 +175,7 @@ namespace MCGalaxy { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - if (p.level != lvl) continue; + if (p.level != lvl) continue; byte[] packet = p.hasExtPositions ? extPacket : srcPacket; p.Send(packet); } @@ -240,9 +231,9 @@ namespace MCGalaxy { // Does the bot fall down a block if (hitY < 0) { - pos.X += dx; pos.Y += hitY; pos.Z += dz; Pos = pos; + pos.X += dx; pos.Y += hitY; pos.Z += dz; Pos = pos; RecalcDownExtent(ref bb, steps, dx, dz); - RecalcUpExtent(ref bb, steps, dx, dz); + RecalcUpExtent(ref bb, steps, dx, dz); return; } @@ -256,7 +247,7 @@ namespace MCGalaxy { } if (!intersectsAny) { - pos.X += dx; pos.Y += dy; pos.Z += dz; Pos = pos; + pos.X += dx; pos.Y += dy; pos.Z += dz; Pos = pos; if (dy != 0) { RecalcDownExtent(ref bb, steps, dx, dz); diff --git a/MCGalaxy/Player/Player.Login.cs b/MCGalaxy/Player/Player.Login.cs index 259ddd382..81a881a39 100644 --- a/MCGalaxy/Player/Player.Login.cs +++ b/MCGalaxy/Player/Player.Login.cs @@ -61,6 +61,7 @@ namespace MCGalaxy { } void CompleteLoginProcess() { + // Lock to ensure that no two players can end up with the same playerid lock (PlayerInfo.Online.locker) { id = NextFreeId(); PlayerInfo.Online.Add(this); @@ -158,6 +159,23 @@ namespace MCGalaxy { Loading = false; } + unsafe static byte NextFreeId() { + byte* used = stackalloc byte[256]; + for (int i = 0; i < 256; i++) + used[i] = 0; + + Player[] players = PlayerInfo.Online.Items; + for (int i = 0; i < players.Length; i++) { + byte id = players[i].id; + used[id] = 1; + } + + for (byte i = 0; i < 255; i++ ) { + if (used[i] == 0) return i; + } + return 1; + } + void LoadCpeData() { string skin = Server.skins.FindData(name); if (skin != null) SkinName = skin; diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 5fed3ab40..9800a3e09 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -366,26 +366,6 @@ namespace MCGalaxy { [Obsolete("Use PlayerInfo.FindExact(name)")] public static Player FindExact(string name) { return PlayerInfo.FindExact(name); } - - unsafe static byte NextFreeId() { - byte* used = stackalloc byte[256]; - for (int i = 0; i < 256; i++) - used[i] = 0; - - // Lock to ensure that no two players can end up with the same playerid - lock (PlayerInfo.Online.locker) { - Player[] players = PlayerInfo.Online.Items; - for (int i = 0; i < players.Length; i++) { - byte id = players[i].id; - used[id] = 1; - } - } - - for (byte i = 0; i < 255; i++ ) { - if (used[i] == 0) return i; - } - return 1; - } public static bool ValidName(string name) { const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+";