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

@ -1,20 +1,20 @@
/* /*
Copyright 2011 MCForge Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing, Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS" software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses. permissions and limitations under the Licenses.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace MCGalaxy.Commands namespace MCGalaxy.Commands
@ -28,141 +28,105 @@ namespace MCGalaxy.Commands
public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
public CmdRestartPhysics() { } public CmdRestartPhysics() { }
public override void Use(Player p, string message) public override void Use(Player p, string message) {
{ CatchPos cpos = default(CatchPos);
CatchPos cpos;
cpos.x = 0; cpos.y = 0; cpos.z = 0;
message = message.ToLower(); message = message.ToLower();
cpos.extraInfo = ""; cpos.extraInfo = "";
if (message != "" && !ParseArgs(p, message, ref cpos)) return;
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; }
}
p.blockchangeObject = cpos; p.blockchangeObject = cpos;
Player.SendMessage(p, "Place two blocks to determine the edges."); Player.SendMessage(p, "Place two blocks to determine the edges.");
p.ClearBlockchange(); p.ClearBlockchange();
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
} }
public override void Help(Player p)
{ bool ParseArgs(Player p, string args, ref CatchPos cpos) {
Player.SendMessage(p, "/restartphysics ([type] [num]) ([type2] [num2]) (...) - Restarts every physics block in an area"); string[] parts = args.Split(' ');
Player.SendMessage(p, "[type] will set custom physics for selected blocks"); if (parts.Length % 2 == 1) {
Player.SendMessage(p, "Possible [types]: drop, explode, dissipate, wait, rainbow, revert"); Player.SendMessage(p, "Number of parameters must be even");
Player.SendMessage(p, "/rp revert takes block names"); Help(p); return false;
}
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;
} }
public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType)
{ void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) {
RevertAndClearState(p, x, y, z); RevertAndClearState(p, x, y, z);
CatchPos bp = (CatchPos)p.blockchangeObject; CatchPos bp = (CatchPos)p.blockchangeObject;
bp.x = x; bp.y = y; bp.z = z; p.blockchangeObject = bp; bp.x = x; bp.y = y; bp.z = z; p.blockchangeObject = bp;
p.Blockchange += new Player.BlockchangeEventHandler(Blockchange2); 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); RevertAndClearState(p, x, y, z);
CatchPos cpos = (CatchPos)p.blockchangeObject; CatchPos cpos = (CatchPos)p.blockchangeObject;
List<CatchPos> buffer = new List<CatchPos>(); List<int> buffer = new List<int>();
CatchPos pos = new CatchPos();
//int totalChecks = 0; 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 (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; } for (ushort xx = Math.Min(cpos.x, x); xx <= Math.Max(cpos.x, x); ++xx)
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) int index = p.level.PosToInt(xx, yy, zz);
{ if (index >= 0 && p.level.blocks[index] != Block.air)
for (ushort zz = Math.Min(cpos.z, z); zz <= Math.Max(cpos.z, z); ++zz) buffer.Add(index);
{
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);
}
}
}
} }
try if (cpos.extraInfo == "") {
{ if (buffer.Count > Server.rpNormLimit) {
if (cpos.extraInfo == "") Player.SendMessage(p, "Cannot restart more than " + Server.rpNormLimit + " blocks.");
{ Player.SendMessage(p, "Tried to restart " + buffer.Count + " blocks.");
if (buffer.Count > Server.rpNormLimit) return;
{
Player.SendMessage(p, "Cannot restart more than " + Server.rpNormLimit + " blocks.");
Player.SendMessage(p, "Tried to restart " + buffer.Count + " blocks.");
return;
}
} }
else } else if (buffer.Count > Server.rpLimit) {
{ Player.SendMessage(p, "Tried to add physics to " + buffer.Count + " blocks.");
if (buffer.Count > Server.rpLimit) Player.SendMessage(p, "Cannot add physics to more than " + Server.rpLimit + " blocks.");
{ return;
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."); 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; } 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");
}
} }
} }