From eb9c19ca932962e08bc21797e3261385d8d8233e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 9 Mar 2018 20:53:20 +1100 Subject: [PATCH] Fix manual place and map send for blocks > 255 --- MCGalaxy/Blocks/BlockDefinitions.cs | 6 +- MCGalaxy/Network/Player.Networking.cs | 23 ++++---- MCGalaxy/Network/Utils/LevelChunkStream.cs | 64 ++++++++++++++++++---- MCGalaxy/Player/Player.CPE.cs | 3 + MCGalaxy/Player/Player.Fields.cs | 1 + MCGalaxy/Player/Player.cs | 7 --- 6 files changed, 70 insertions(+), 34 deletions(-) diff --git a/MCGalaxy/Blocks/BlockDefinitions.cs b/MCGalaxy/Blocks/BlockDefinitions.cs index f8d83bef2..345fe4640 100644 --- a/MCGalaxy/Blocks/BlockDefinitions.cs +++ b/MCGalaxy/Blocks/BlockDefinitions.cs @@ -242,20 +242,18 @@ namespace MCGalaxy { internal static void SendLevelCustomBlocks(Player pl) { BlockDefinition[] defs = pl.level.CustomBlockDefs; - BlockID_ maxRaw = pl.MaxRawBlock; for (int i = 0; i < defs.Length; i++) { BlockDefinition def = defs[i]; - if (def == null || def.BlockID > maxRaw) continue; + if (def == null || def.BlockID > pl.MaxRawBlock) continue; pl.Send(def.MakeDefinePacket(pl)); } } internal static void SendLevelInventoryOrder(Player pl) { BlockDefinition[] defs = pl.level.CustomBlockDefs; - BlockID_ maxRaw = pl.MaxRawBlock; for (int b = 0; b < defs.Length; b++) { BlockDefinition def = defs[b]; - if (def == null || def.BlockID > maxRaw) continue; + if (def == null || def.BlockID > pl.MaxRawBlock) continue; if (def.InventoryOrder >= 0) { pl.Send(Packet.SetInventoryOrder(def, pl.hasExtBlocks)); } diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index 8070c3476..8e3232a59 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -264,22 +264,23 @@ namespace MCGalaxy { NetUtils.WriteU16(y, buffer, 3); NetUtils.WriteU16(z, buffer, 5); + BlockID raw; if (block >= Block.Extended) { - block = hasBlockDefs ? Block.ToRaw(block) : level.RawFallback(block); + raw = Block.ToRaw(block); } else { - block = Block.Convert(block); - // Invalid block physics won't have converted form - if (block >= Block.CpeCount) block = Block.Orange; - } + raw = Block.Convert(block); + if (raw >= Block.CpeCount) raw = Block.Orange; + } + if (raw > MaxRawBlock) raw = level.RawFallback(block); // Custom block replaced a core block - if (!hasBlockDefs && block < Block.CpeCount) { - BlockDefinition def = level.CustomBlockDefs[block]; - if (def != null) block = def.FallBack; - } + if (!hasBlockDefs && raw < Block.CpeCount) { + BlockDefinition def = level.CustomBlockDefs[raw]; + if (def != null) raw = def.FallBack; + } + if (!hasCustomBlocks) raw = Block.ConvertCPE((BlockRaw)raw); - if (!hasCustomBlocks) block = Block.ConvertCPE((BlockRaw)block); // doesn't support CPE blocks - NetUtils.WriteBlock(block, buffer, 7, hasExtBlocks); + NetUtils.WriteBlock(raw, buffer, 7, hasExtBlocks); Socket.SendLowPriority(buffer); } diff --git a/MCGalaxy/Network/Utils/LevelChunkStream.cs b/MCGalaxy/Network/Utils/LevelChunkStream.cs index 578336410..12c8cb4bd 100644 --- a/MCGalaxy/Network/Utils/LevelChunkStream.cs +++ b/MCGalaxy/Network/Utils/LevelChunkStream.cs @@ -38,8 +38,8 @@ namespace MCGalaxy.Network { internal int index, position, length; Player p; - byte[] data = new byte[chunkSize + 4]; - const int chunkSize = 1024; + byte[] data = new byte[chunkSize + 4]; + const int chunkSize = 1024; public LevelChunkStream(Player p) { this.p = p; } public override void Close() { @@ -96,14 +96,11 @@ namespace MCGalaxy.Network { conv[b] = (byte)Block.Convert((byte)b); if (conv[b] > Block.CpeCount) conv[b] = Block.Orange; } - + + // Convert custom blocks (that overwrote core blocks) to their fallbacks + #if !TEN_BIT_BLOCKS if (!p.hasBlockDefs) { - // Convert custom blocks (that overwrote core blocks) to their fallbacks - for (int b = 0; b < Block.CpeCount; b++) { - BlockDefinition def = p.level.CustomBlockDefs[b]; - if (def != null) conv[b] = def.FallBack; - } - + #endif for (int b = 0; b < Block.Count; b++) { BlockID block = Block.FromRaw((byte)b); BlockDefinition def = p.level.CustomBlockDefs[block]; @@ -114,6 +111,16 @@ namespace MCGalaxy.Network { convExt[b] = def.FallBack; } } + #if !TEN_BIT_BLOCKS + } + #endif + + // Convert custom blocks (that overwrote core blocks) to their fallbacks + if (!p.hasBlockDefs) { + for (int b = 0; b < Block.CpeCount; b++) { + BlockDefinition def = p.level.CustomBlockDefs[b]; + if (def != null) conv[b] = def.FallBack; + } } // Convert CPE blocks to their fallbacks @@ -157,9 +164,39 @@ namespace MCGalaxy.Network { gs.Write(buffer, 0, bufferSize); bIndex = 0; } } - } else - #endif - + } else if (p.hasBlockDefs) { + for (int i = 0; i < blocks.Length; ++i) { + byte block = blocks[i]; + if (block == Block.custom_block) { + buffer[bIndex] = lvl.GetExtTile(i); + } else if (block == Block.custom_block_2 || block == Block.custom_block_3) { + buffer[bIndex] = convExt[lvl.GetExtTile(i)]; + } else { + buffer[bIndex] = conv[block]; + } + + bIndex++; + if (bIndex == bufferSize) { + dst.position = i; + gs.Write(buffer, 0, bufferSize); bIndex = 0; + } + } + } else { + for (int i = 0; i < blocks.Length; ++i) { + byte block = blocks[i]; + if (block == Block.custom_block || block == Block.custom_block_2 || block == Block.custom_block_3) { + block = convExt[lvl.GetExtTile(i)]; + } + buffer[bIndex] = conv[block]; + + bIndex++; + if (bIndex == bufferSize) { + dst.position = i; + gs.Write(buffer, 0, bufferSize); bIndex = 0; + } + } + } + #else if (p.hasBlockDefs) { for (int i = 0; i < blocks.Length; ++i) { byte block = blocks[i]; @@ -190,6 +227,9 @@ namespace MCGalaxy.Network { } } } + #endif + + if (bIndex > 0) gs.Write(buffer, 0, bIndex); } } diff --git a/MCGalaxy/Player/Player.CPE.cs b/MCGalaxy/Player/Player.CPE.cs index 7f8c7441c..125e73ab6 100644 --- a/MCGalaxy/Player/Player.CPE.cs +++ b/MCGalaxy/Player/Player.CPE.cs @@ -73,6 +73,7 @@ namespace MCGalaxy { if (ext.ExtName == CpeExt.CustomBlocks) { if (version == 1) Send(Packet.CustomBlockSupportLevel(1)); hasCustomBlocks = true; + if (MaxRawBlock < Block.CpeMaxBlock) MaxRawBlock = Block.CpeMaxBlock; } else if (ext.ExtName == CpeExt.ChangeModel) { hasChangeModel = true; } else if (ext.ExtName == CpeExt.FullCP437) { @@ -81,6 +82,7 @@ namespace MCGalaxy { hasExtList = true; } else if (ext.ExtName == CpeExt.BlockDefinitions) { hasBlockDefs = true; + if (MaxRawBlock < 255) MaxRawBlock = 255; } else if (ext.ExtName == CpeExt.TextColors) { hasTextColors = true; for (int i = 0; i < Colors.List.Length; i++) { @@ -97,6 +99,7 @@ namespace MCGalaxy { #if TEN_BIT_BLOCKS else if (ext.ExtName == CpeExt.ExtBlocks) { hasExtBlocks = true; + if (MaxRawBlock < 767) MaxRawBlock = 767; } #endif } diff --git a/MCGalaxy/Player/Player.Fields.cs b/MCGalaxy/Player/Player.Fields.cs index bf61fe736..a03441e41 100644 --- a/MCGalaxy/Player/Player.Fields.cs +++ b/MCGalaxy/Player/Player.Fields.cs @@ -55,6 +55,7 @@ namespace MCGalaxy { internal bool nonPlayerClient = false; public INetworkSocket Socket; public PingList Ping = new PingList(); + public BlockID MaxRawBlock = Block.OriginalMaxBlock; public DateTime LastAction, AFKCooldown; public bool IsAfk, AutoAfk; diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index 9ca51ade9..f7988d4e2 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -58,13 +58,6 @@ namespace MCGalaxy { } } - public BlockID MaxRawBlock { get { - if (hasExtBlocks) return 767; - if (hasBlockDefs) return 255; - return hasCustomBlocks ? Block.CpeMaxBlock : Block.OriginalMaxBlock; - } - } - internal void Connect(Socket s) { try { Socket = new TcpSocket(this, s);