This commit is contained in:
UnknownShadow200 2017-04-28 08:41:53 +10:00
parent d2f5f8fcbb
commit f578c6bc07
4 changed files with 19 additions and 16 deletions

View File

@ -38,21 +38,21 @@ namespace MCGalaxy {
}
/// <summary> Attempts to parse the given argument as an enumeration member. </summary>
public static bool GetEnum<TEnum>(Player p, string input, string type,
public static bool GetEnum<TEnum>(Player p, string input, string argName,
ref TEnum result) where TEnum : struct {
try {
result = (TEnum)Enum.Parse(typeof(TEnum), input, true);
return true;
} catch (Exception) {
string[] names = Enum.GetNames(typeof(TEnum));
Player.Message(p, type + " must be one of the following: " + names.Join());
Player.Message(p, argName + " must be one of the following: " + names.Join());
return false;
}
}
/// <summary> Attempts to parse the given argument as an integer. </summary>
public static bool GetInt(Player p, string input, string type, ref int result,
public static bool GetInt(Player p, string input, string argName, ref int result,
int min = int.MinValue, int max = int.MaxValue) {
int value;
if (!int.TryParse(input, out value)) {
@ -62,11 +62,11 @@ namespace MCGalaxy {
if (value < min || value > max) {
// Try to provide more helpful range messages
if (max == int.MaxValue) {
Player.Message(p, "{0} must be {1} or greater", type, min);
Player.Message(p, "{0} must be {1} or greater", argName, min);
} else if (min == int.MinValue) {
Player.Message(p, "{0} must be {1} or less", type, max);
Player.Message(p, "{0} must be {1} or less", argName, max);
} else {
Player.Message(p, "{0} must be between {1} and {2}", type, min, max);
Player.Message(p, "{0} must be between {1} and {2}", argName, min, max);
}
return false;
}
@ -76,19 +76,19 @@ namespace MCGalaxy {
/// <summary> Attempts to parse the given argument as an byte. </summary>
public static bool GetByte(Player p, string input, string type, ref byte result,
public static bool GetByte(Player p, string input, string argName, ref byte result,
byte min = byte.MinValue, byte max = byte.MaxValue) {
int temp = 0;
if (!GetInt(p, input, type, ref temp, min, max)) return false;
if (!GetInt(p, input, argName, ref temp, min, max)) return false;
result = (byte)temp; return true;
}
/// <summary> Attempts to parse the given argument as an byte. </summary>
public static bool GetUShort(Player p, string input, string type, ref ushort result,
public static bool GetUShort(Player p, string input, string argName, ref ushort result,
ushort min = ushort.MinValue, ushort max = ushort.MaxValue) {
int temp = 0;
if (!GetInt(p, input, type, ref temp, min, max)) return false;
if (!GetInt(p, input, argName, ref temp, min, max)) return false;
result = (ushort)temp; return true;
}

View File

@ -22,8 +22,6 @@ using MCGalaxy.Network;
namespace MCGalaxy {
public sealed partial class Player : IDisposable {
public NetworkStream Stream;
static void Receive(IAsyncResult result) {
//Server.s.Log(result.AsyncState.ToString());
Player p = (Player)result.AsyncState;

View File

@ -18,7 +18,7 @@
using System;
namespace MCGalaxy.Network {
/// <summary> Combines block changes and sends them as either a CPE BulkBlockUpdate packet,
/// <summary> Combines block changes and sends them as either a single CPE BulkBlockUpdate packet,
/// or 256 SetBlock packets combined as a single byte array to reduce overhead. </summary>
public sealed class BufferedBlockSender {
@ -42,6 +42,9 @@ namespace MCGalaxy.Network {
this.level = player.level;
}
/// <summary> Adds a block change, and potentially sends block change packets if
/// number of buffered block changes has reached the limit. </summary>
/// <returns> Whether block change packets were actually sent. </returns>
public bool Add(int index, byte block, byte extBlock) {
indices[count] = index;
if (block == Block.custom_block) types[count] = extBlock;
@ -51,16 +54,16 @@ namespace MCGalaxy.Network {
}
/// <summary> Sends the block change packets if either 'force' is true,
/// or the number of blocks in the buffer has reached the limit. </summary>
/// or the number of buffered block changes has reached the limit. </summary>
/// <returns> Whether block change packets were actually sent. </returns>
public bool Send(bool force) {
if (count > 0 && (force || count == 256)) {
if (player != null) SendPlayer();
else SendLevel();
count = 0;
return false;
return true;
}
return true;
return false;
}
void SendLevel() {

View File

@ -20,6 +20,8 @@ using System.IO;
using System.IO.Compression;
namespace MCGalaxy.Network {
/// <summary> Streams the compressed form of a map directly to a player's network socket. </summary>
public sealed class LevelChunkStream : Stream {
public override bool CanRead { get { return false; } }
public override bool CanSeek { get { return false; } }