Remove more pointless Send funcs

This commit is contained in:
UnknownShadow200 2017-05-08 22:06:55 +10:00
parent ea09af941f
commit 8b69431c5a
8 changed files with 46 additions and 47 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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);

View File

@ -22,8 +22,8 @@ namespace MCGalaxy.Network {
public static partial class Packet {
/// <summary> Constructs a handshake/motd packet. The text is shown to clients during map loading. </summary>
/// <remarks> Some clients recognise special modifiers such as -hax +fly in this packet. </remarks>
/// <summary> Constructs a handshake/motd packet. The text is shown to clients during map loading. </summary>
/// <remarks> Some clients recognise special modifiers such as -hax +fly in this packet. </remarks>
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;
}
/// <summary> Constructs a packet describing the dimensions of a level. </summary>
/// <summary> Constructs a ping packet. </remarks>
public static byte[] Ping() {
return new byte[] { Opcode.Ping };
}
/// <summary> Constructs a packet that specified map data is about to be sent. </remarks>
public static byte[] LevelInitalise() {
return new byte[] { Opcode.LevelInitialise };
}
/// <summary> Constructs a packet describing the dimensions of a level. </summary>
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;
}
/// <summary> Constructs a packet that spawns / adds an entity. </summary>
/// <summary> Constructs a packet that adds/spawns an entity. </summary>
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;
}
/// <summary> Constructs an absolute position / teleport and rotation movement packet. </summary>
/// <summary> Constructs an absolute position/teleport and rotation movement packet. </summary>
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;
}
/// <summary> Constructs a packet that removes/despawns an entity. </summary>
public static byte[] RemoveEntity(byte id) {
return new byte[] { Opcode.RemoveEntity, id };
}
/// <summary> Constructs a chat message packet with an empty message. </summary>
/// <summary> Constructs a chat message packet with an empty message. </summary>
public static byte[] BlankMessage() { return Message("", 0, false); }
/// <summary> Constructs a chat message packet. </summary>
@ -89,6 +104,14 @@ namespace MCGalaxy.Network {
return buffer;
}
/// <summary> Constructs a kick / disconnect packet with the given reason / message. </summary>
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;
}
/// <summary> Constructs a set user type/permission packet. </summary>
/// <remarks> For certain clients, sets whether they are allowed to place bedrock, use ophax, place liquids. </remarks>
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;
}
/// <summary> Constructs a kick / disconnect packet with the given reason / message. </summary>
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;
}
}
}

View File

@ -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));
}
}

View File

@ -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;

View File

@ -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;