From 8b69431c5a161636a959baa878930be0c88b798d Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 8 May 2017 22:06:55 +1000 Subject: [PATCH] Remove more pointless Send funcs --- MCGalaxy/Blocks/BlockDefinitions.cs | 2 +- MCGalaxy/Entity/Entities.cs | 4 +- MCGalaxy/Network/Packets/Packet.BlockDefs.cs | 4 ++ MCGalaxy/Network/Packets/Packet.CPE.cs | 6 ++- MCGalaxy/Network/Packets/Packet.cs | 43 +++++++++++++------- MCGalaxy/Network/Player.Networking.cs | 29 ++----------- MCGalaxy/Player/Player.CPE.cs | 3 +- MCGalaxy/Server/Tasks/ServerTasks.cs | 2 +- 8 files changed, 46 insertions(+), 47 deletions(-) diff --git a/MCGalaxy/Blocks/BlockDefinitions.cs b/MCGalaxy/Blocks/BlockDefinitions.cs index 140f4df1f..8297d97cb 100644 --- a/MCGalaxy/Blocks/BlockDefinitions.cs +++ b/MCGalaxy/Blocks/BlockDefinitions.cs @@ -206,7 +206,7 @@ namespace MCGalaxy { if (global && pl.level.CustomBlockDefs[block] != null) continue; if (pl.hasBlockDefs) - pl.SendRaw(Opcode.CpeRemoveBlockDefinition, block); + pl.Send(Packet.UndefineBlock(block)); } Save(global, level); } diff --git a/MCGalaxy/Entity/Entities.cs b/MCGalaxy/Entity/Entities.cs index e30188d10..807e7274e 100644 --- a/MCGalaxy/Entity/Entities.cs +++ b/MCGalaxy/Entity/Entities.cs @@ -159,13 +159,13 @@ namespace MCGalaxy { } internal static void Despawn(Player dst, byte id) { - dst.SendRaw(Opcode.RemoveEntity, id); + dst.Send(Packet.RemoveEntity(id)); if (!Server.TablistGlobal) TabList.Remove(dst, id); } internal static void Despawn(Player dst, PlayerBot b) { - dst.SendRaw(Opcode.RemoveEntity, b.id); + dst.Send(Packet.RemoveEntity(b.id)); if (Server.TablistBots) TabList.Remove(dst, b.id); } diff --git a/MCGalaxy/Network/Packets/Packet.BlockDefs.cs b/MCGalaxy/Network/Packets/Packet.BlockDefs.cs index 03d3dc39e..18433ba7b 100644 --- a/MCGalaxy/Network/Packets/Packet.BlockDefs.cs +++ b/MCGalaxy/Network/Packets/Packet.BlockDefs.cs @@ -31,6 +31,10 @@ namespace MCGalaxy.Network { return buffer; } + public static byte[] UndefineBlock(byte block) { + return new byte[] { Opcode.CpeRemoveBlockDefinition, block }; + } + public static byte[] DefineBlockExt(BlockDefinition def, bool uniqueSideTexs, bool hasCP437) { byte[] buffer = new byte[uniqueSideTexs ? 88 : 85]; int i = 0; diff --git a/MCGalaxy/Network/Packets/Packet.CPE.cs b/MCGalaxy/Network/Packets/Packet.CPE.cs index aebad14fb..693e4eb90 100644 --- a/MCGalaxy/Network/Packets/Packet.CPE.cs +++ b/MCGalaxy/Network/Packets/Packet.CPE.cs @@ -44,6 +44,10 @@ namespace MCGalaxy.Network { return buffer; } + public static byte[] CustomBlockSupportLevel(byte level) { + return new byte[] { Opcode.CpeCustomBlockSupportLevel, level }; + } + public static byte[] HoldThis(byte block, bool locked) { byte[] buffer = new byte[3]; buffer[0] = Opcode.CpeHoldThis; @@ -198,7 +202,7 @@ namespace MCGalaxy.Network { public static byte[] ExtAddEntity2(byte id, string skinName, string displayName, Position pos, Orientation rot, bool hasCP437, bool extPos) { - byte[] buffer = new byte[138 + (extPos ? 6 : 0)]; + byte[] buffer = new byte[138 + (extPos ? 6 : 0)]; buffer[0] = Opcode.CpeExtAddEntity2; buffer[1] = id; NetUtils.Write(displayName.RemoveLastPlus(), buffer, 2, hasCP437); diff --git a/MCGalaxy/Network/Packets/Packet.cs b/MCGalaxy/Network/Packets/Packet.cs index f5cca2442..a36548bb9 100644 --- a/MCGalaxy/Network/Packets/Packet.cs +++ b/MCGalaxy/Network/Packets/Packet.cs @@ -22,8 +22,8 @@ namespace MCGalaxy.Network { public static partial class Packet { - /// Constructs a handshake/motd packet. The text is shown to clients during map loading. - /// Some clients recognise special modifiers such as -hax +fly in this packet. + /// Constructs a handshake/motd packet. The text is shown to clients during map loading. + /// Some clients recognise special modifiers such as -hax +fly in this packet. public static byte[] Motd(Player p, string motd) { byte[] buffer = new byte[131]; buffer[0] = Opcode.Handshake; @@ -41,7 +41,17 @@ namespace MCGalaxy.Network { return buffer; } - /// Constructs a packet describing the dimensions of a level. + /// Constructs a ping packet. + public static byte[] Ping() { + return new byte[] { Opcode.Ping }; + } + + /// Constructs a packet that specified map data is about to be sent. + public static byte[] LevelInitalise() { + return new byte[] { Opcode.LevelInitialise }; + } + + /// Constructs a packet describing the dimensions of a level. public static byte[] LevelFinalise(ushort width, ushort height, ushort length) { byte[] buffer = new byte[7]; buffer[0] = Opcode.LevelFinalise; @@ -51,7 +61,7 @@ namespace MCGalaxy.Network { return buffer; } - /// Constructs a packet that spawns / adds an entity. + /// Constructs a packet that adds/spawns an entity. public static byte[] AddEntity(byte id, string name, Position pos, Orientation rot, bool hasCP437, bool extPos) { byte[] buffer = new byte[74 + (extPos ? 6 : 0)]; @@ -65,7 +75,7 @@ namespace MCGalaxy.Network { return buffer; } - /// Constructs an absolute position / teleport and rotation movement packet. + /// Constructs an absolute position/teleport and rotation movement packet. public static byte[] Teleport(byte id, Position pos, Orientation rot, bool extPos) { byte[] buffer = new byte[10 + (extPos ? 6 : 0)]; buffer[0] = Opcode.EntityTeleport; @@ -76,8 +86,13 @@ namespace MCGalaxy.Network { buffer[3 + offset] = rot.HeadX; return buffer; } + + /// Constructs a packet that removes/despawns an entity. + public static byte[] RemoveEntity(byte id) { + return new byte[] { Opcode.RemoveEntity, id }; + } - /// Constructs a chat message packet with an empty message. + /// Constructs a chat message packet with an empty message. public static byte[] BlankMessage() { return Message("", 0, false); } /// Constructs a chat message packet. @@ -89,6 +104,14 @@ namespace MCGalaxy.Network { return buffer; } + /// Constructs a kick / disconnect packet with the given reason / message. + public static byte[] Kick(string message, bool cp437) { + byte[] buffer = new byte[65]; + buffer[0] = Opcode.Kick; + NetUtils.Write(message, buffer, 1, cp437); + return buffer; + } + /// Constructs a set user type/permission packet. /// For certain clients, sets whether they are allowed to place bedrock, use ophax, place liquids. public static byte[] UserType(Player p) { @@ -97,13 +120,5 @@ namespace MCGalaxy.Network { buffer[1] = BlockPerms.CanModify(p, Block.blackrock) ? (byte)100 : (byte)0; return buffer; } - - /// Constructs a kick / disconnect packet with the given reason / message. - public static byte[] Kick(string message, bool cp437) { - byte[] buffer = new byte[65]; - buffer[0] = Opcode.Kick; - NetUtils.Write(message, buffer, 1, cp437); - return buffer; - } } } diff --git a/MCGalaxy/Network/Player.Networking.cs b/MCGalaxy/Network/Player.Networking.cs index b4a87907f..c6081c66a 100644 --- a/MCGalaxy/Network/Player.Networking.cs +++ b/MCGalaxy/Network/Player.Networking.cs @@ -101,30 +101,6 @@ namespace MCGalaxy { finishedCpeLogin = true; } } - - - public void SendRaw(int id) { - byte[] buffer = new byte[] { (byte)id }; - Send(buffer); - } - - public void SendRaw(int id, byte data) { - byte[] buffer = new byte[] { (byte)id, data }; - Send(buffer); - } - - [Obsolete("Include the opcode in the array to avoid an extra temp allocation.")] - public void SendRaw(int id, byte[] send, bool sync = false) { - byte[] buffer = new byte[send.Length + 1]; - buffer[0] = (byte)id; - for ( int i = 0; i < send.Length; i++ ) - buffer[i + 1] = send[i]; - SendRaw(buffer, sync); - buffer = null; - } - - [Obsolete("Use Send() instead.")] - public void SendRaw(byte[] buffer, bool sync = false) { Send(buffer, sync); } public void Send(byte[] buffer, bool sync = false) { // Abort if socket has been closed @@ -269,7 +245,7 @@ namespace MCGalaxy { AllowBuild = access == AccessResult.Whitelisted || access == AccessResult.Allowed; try { - SendRaw(Opcode.LevelInitialise); + Send(Packet.LevelInitalise()); if (hasBlockDefs) { if (oldLevel != null && oldLevel != level) @@ -307,7 +283,8 @@ namespace MCGalaxy { for (int i = 1; i < 256; i++) { BlockDefinition def = defs[i]; if (def == null || def == BlockDefinition.GlobalDefs[i]) continue; - SendRaw(Opcode.CpeRemoveBlockDefinition, (byte)i); + + Send(Packet.UndefineBlock((byte)i)); } } diff --git a/MCGalaxy/Player/Player.CPE.cs b/MCGalaxy/Player/Player.CPE.cs index eafec10c3..9c63d8752 100644 --- a/MCGalaxy/Player/Player.CPE.cs +++ b/MCGalaxy/Player/Player.CPE.cs @@ -38,8 +38,7 @@ namespace MCGalaxy { ClickDistance = version; break; case CpeExt.CustomBlocks: CustomBlocks = version; - if (version == 1) - SendRaw(Opcode.CpeCustomBlockSupportLevel, 1); + if (version == 1) Send(Packet.CustomBlockSupportLevel(1)); hasCustomBlocks = true; break; case CpeExt.HeldBlock: HeldBlock = version; break; diff --git a/MCGalaxy/Server/Tasks/ServerTasks.cs b/MCGalaxy/Server/Tasks/ServerTasks.cs index 3472cf655..400cc2150 100644 --- a/MCGalaxy/Server/Tasks/ServerTasks.cs +++ b/MCGalaxy/Server/Tasks/ServerTasks.cs @@ -68,7 +68,7 @@ namespace MCGalaxy.Tasks { internal static void CheckState(SchedulerTask task) { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - p.SendRaw(Opcode.Ping); + p.Send(Packet.Ping()); if (Server.afkminutes <= 0) return; if (DateTime.UtcNow < p.AFKCooldown) return;