Make /rp code actually sensible, also use packed integer indices for storing positions to update, instead of that pointless 12 byte struct.

This commit is contained in:
UnknownShadow200 2016-04-08 08:41:22 +10:00
parent a0cd1885f0
commit 54534354ba

View File

@ -28,141 +28,105 @@ namespace MCGalaxy.Commands
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public CmdRestartPhysics() { }
public override void Use(Player p, string message)
{
CatchPos cpos;
cpos.x = 0; cpos.y = 0; cpos.z = 0;
public override void Use(Player p, string message) {
CatchPos cpos = default(CatchPos);
message = message.ToLower();
cpos.extraInfo = "";
if (message != "")
{
int currentLoop = 0; string[] storedArray; bool skip = false;
retry: foreach (string s in message.Split(' '))
{
if (currentLoop % 2 == 0)
{
switch (s)
{
case "drop":
case "explode":
case "dissipate":
case "wait":
case "rainbow":
break;
case "revert":
if (skip) break;
storedArray = message.Split(' ');
try
{
storedArray[currentLoop + 1] = Block.Byte(message.Split(' ')[currentLoop + 1].ToString().ToLower()).ToString();
if (storedArray[currentLoop + 1].ToString() == "255") throw new OverflowException();
}
catch { Player.SendMessage(p, "Invalid block type."); return; }
message = string.Join(" ", storedArray);
skip = true; currentLoop = 0;
goto retry;
default:
Player.SendMessage(p, s + " is not supported."); return;
}
}
else
{
try
{
if (int.Parse(s) < 1) { Player.SendMessage(p, "Values must be above 0"); return; }
}
catch { Player.SendMessage(p, "/rp [text] [num] [text] [num]"); return; }
}
currentLoop++;
}
if (currentLoop % 2 != 1) cpos.extraInfo = message;
else { Player.SendMessage(p, "Number of parameters must be even"); Help(p); return; }
}
if (message != "" && !ParseArgs(p, message, ref cpos)) return;
p.blockchangeObject = cpos;
Player.SendMessage(p, "Place two blocks to determine the edges.");
p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
public override void Help(Player p)
{
Player.SendMessage(p, "/restartphysics ([type] [num]) ([type2] [num2]) (...) - Restarts every physics block in an area");
Player.SendMessage(p, "[type] will set custom physics for selected blocks");
Player.SendMessage(p, "Possible [types]: drop, explode, dissipate, wait, rainbow, revert");
Player.SendMessage(p, "/rp revert takes block names");
bool ParseArgs(Player p, string args, ref CatchPos cpos) {
string[] parts = args.Split(' ');
if (parts.Length % 2 == 1) {
Player.SendMessage(p, "Number of parameters must be even");
Help(p); return false;
}
public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType)
{
for (int i = 0; i < parts.Length; i++) {
string s = parts[i];
if (i % 2 != 0) {
int value;
if (!int.TryParse(s, out value)) {
Player.SendMessage(p, "/rp [type1] [num] [type2] [num]..."); return false;
}
if (value < 0) { Player.SendMessage(p, "Values must be above 0"); return false; }
continue;
}
switch (s) {
case "drop":
case "explode":
case "dissipate":
case "wait":
case "rainbow":
break;
case "revert":
byte block = Block.Byte(parts[i + 1]);
if (block == Block.Zero) { Player.SendMessage(p, "Invalid block type."); return false; }
parts[i + 1] = block.ToString();
break;
default:
Player.SendMessage(p, s + " type is not supported.");
return false;
}
}
cpos.extraInfo = string.Join(" ", parts);
return true;
}
void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
RevertAndClearState(p, x, y, z);
CatchPos bp = (CatchPos)p.blockchangeObject;
bp.x = x; bp.y = y; bp.z = z; p.blockchangeObject = bp;
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange2);
}
public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType)
{
void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
RevertAndClearState(p, x, y, z);
CatchPos cpos = (CatchPos)p.blockchangeObject;
List<CatchPos> buffer = new List<CatchPos>();
CatchPos pos = new CatchPos();
//int totalChecks = 0;
//if (Math.Abs(cpos.x - x) * Math.Abs(cpos.y - y) * Math.Abs(cpos.z - z) > 8000) { Player.SendMessage(p, "Tried to restart too many blocks. You may only restart 8000"); return; }
List<int> buffer = new List<int>();
for (ushort yy = Math.Min(cpos.y, y); yy <= Math.Max(cpos.y, y); ++yy)
for (ushort zz = Math.Min(cpos.z, z); zz <= Math.Max(cpos.z, z); ++zz)
for (ushort xx = Math.Min(cpos.x, x); xx <= Math.Max(cpos.x, x); ++xx)
{
for (ushort yy = Math.Min(cpos.y, y); yy <= Math.Max(cpos.y, y); ++yy)
{
for (ushort zz = Math.Min(cpos.z, z); zz <= Math.Max(cpos.z, z); ++zz)
{
if (p.level.GetTile(xx, yy, zz) != Block.air)
{
pos.x = xx; pos.y = yy; pos.z = zz;
pos.extraInfo = cpos.extraInfo;
buffer.Add(pos);
}
}
}
int index = p.level.PosToInt(xx, yy, zz);
if (index >= 0 && p.level.blocks[index] != Block.air)
buffer.Add(index);
}
try
{
if (cpos.extraInfo == "")
{
if (buffer.Count > Server.rpNormLimit)
{
if (cpos.extraInfo == "") {
if (buffer.Count > Server.rpNormLimit) {
Player.SendMessage(p, "Cannot restart more than " + Server.rpNormLimit + " blocks.");
Player.SendMessage(p, "Tried to restart " + buffer.Count + " blocks.");
return;
}
}
else
{
if (buffer.Count > Server.rpLimit)
{
} else if (buffer.Count > Server.rpLimit) {
Player.SendMessage(p, "Tried to add physics to " + buffer.Count + " blocks.");
Player.SendMessage(p, "Cannot add physics to more than " + Server.rpLimit + " blocks.");
return;
}
}
}
catch { return; }
foreach (CatchPos pos1 in buffer)
{
p.level.AddCheck(p.level.PosToInt(pos1.x, pos1.y, pos1.z), true, pos1.extraInfo);
}
foreach (int index in buffer)
p.level.AddCheck(index, true, cpos.extraInfo);
Player.SendMessage(p, "Activated " + buffer.Count + " blocks.");
if (p.staticCommands) p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
if (p.staticCommands)
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
}
struct CatchPos { public ushort x, y, z; public string extraInfo; }
public override void Help(Player p) {
Player.SendMessage(p, "/restartphysics ([type] [num]) ([type2] [num2]) (...) - Restarts every physics block in an area");
Player.SendMessage(p, "[type] will set custom physics for selected blocks");
Player.SendMessage(p, "Possible [types]: drop, explode, dissipate, wait, rainbow, revert");
Player.SendMessage(p, "/rp revert takes block names");
}
}
}