Fix being unable to do /transform scale 63/64

This commit is contained in:
UnknownShadow200 2017-08-27 17:53:53 +10:00
parent b2ce0de82e
commit 9000302f43

View File

@ -16,6 +16,7 @@
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using MCGalaxy.Commands;
using MCGalaxy.Commands.Building; using MCGalaxy.Commands.Building;
namespace MCGalaxy.Drawing.Transforms { namespace MCGalaxy.Drawing.Transforms {
@ -51,16 +52,17 @@ namespace MCGalaxy.Drawing.Transforms {
int mul = 0, div = 0; int mul = 0, div = 0;
ScaleTransform scaler = new ScaleTransform(); ScaleTransform scaler = new ScaleTransform();
if (!ParseFraction(p, args[0], out mul, out div)) return null;
if (args.Length <= 2) { if (args.Length <= 2) {
if (!ParseFraction(p, args[0], "Scale", out mul, out div)) return null;
scaler.XMul = mul; scaler.XDiv = div; scaler.XMul = mul; scaler.XDiv = div;
scaler.YMul = mul; scaler.YDiv = div; scaler.YMul = mul; scaler.YDiv = div;
scaler.ZMul = mul; scaler.ZDiv = div; scaler.ZMul = mul; scaler.ZDiv = div;
} else { } else {
if (!ParseFraction(p, args[0], "X scale", out mul, out div)) return null;
scaler.XMul = mul; scaler.XDiv = div; scaler.XMul = mul; scaler.XDiv = div;
if (!ParseFraction(p, args[1], out mul, out div)) return null; if (!ParseFraction(p, args[1], "Y scale", out mul, out div)) return null;
scaler.YMul = mul; scaler.YDiv = div; scaler.YMul = mul; scaler.YDiv = div;
if (!ParseFraction(p, args[2], out mul, out div)) return null; if (!ParseFraction(p, args[2], "Z scale", out mul, out div)) return null;
scaler.ZMul = mul; scaler.ZDiv = div; scaler.ZMul = mul; scaler.ZDiv = div;
} }
@ -72,21 +74,22 @@ namespace MCGalaxy.Drawing.Transforms {
return scaler; return scaler;
} }
static bool ParseFraction(Player p, string input, out int mul, out int div) { static bool ParseFraction(Player p, string input, string argName, out int mul, out int div) {
int sep = input.IndexOf('/'); int sep = input.IndexOf('/');
bool success = false; div = 1; mul = 1;
div = 1;
if (sep == -1) { // single whole number if (sep == -1) { // single whole number
success = int.TryParse(input, out mul); return CommandParser.GetInt(p, input, argName, ref mul, -32, 32);
} else {
string top = input.Substring(0, sep), bottom = input.Substring(sep + 1);
success = int.TryParse(top, out mul) && int.TryParse(bottom, out div);
} }
if (mul > 32 || mul < -32) { Player.Message(p, "Scale must be between -32 and 32."); return false; } string top = input.Substring(0, sep), bottom = input.Substring(sep + 1);
if (!success) { Player.MessageLines(p, HelpString); } if (!CommandParser.GetInt(p, top, argName + " (numerator)", ref mul, -32768, 32768)) return false;
return success; if (!CommandParser.GetInt(p, bottom, argName + " (denominator)", ref div, -32768, 32768)) return false;
if (div == 0) { Player.Message(p, "Cannot divide by 0."); return false; }
float fract = mul / (float)div;
if (Math.Abs(fract) > 32) { Player.Message(p, argName + " must be between -32 and 32."); return false; }
return true;
} }
} }
} }