Cleanup /restore.

This commit is contained in:
UnknownShadow200 2016-05-15 14:59:31 +10:00
parent c692c89c8a
commit 280640f928

View File

@ -1,136 +1,115 @@
/* /*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy) Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
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.IO; using System.IO;
namespace MCGalaxy.Commands using System.Text;
{
public sealed class CmdRestore : Command namespace MCGalaxy.Commands {
{ public sealed class CmdRestore : Command {
public override string name { get { return "restore"; } } public override string name { get { return "restore"; } }
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.World; } } public override string type { get { return CommandTypes.World; } }
public override bool museumUsable { get { return false; } } public override bool museumUsable { get { return false; } }
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
public CmdRestore() { } public CmdRestore() { }
public override void Use(Player p, string message) public override void Use(Player p, string message) {
{ if (message == "") { OutputBackups(p); return; }
if (message != "")
{
Level lvl;
string[] text = new string[2];
text[0] = "";
text[1] = "";
try
{
text[0] = message.Split(' ')[0].ToLower();
text[1] = message.Split(' ')[1].ToLower();
}
catch
{
text[1] = p.level.name;
}
if (message.Split(' ').Length >= 2)
{
lvl = LevelInfo.FindOrShowMatches(p, text[1]);
if (lvl == null) return;
}
else
{
if (p != null && p.level != null) lvl = p.level;
else
{
Server.s.Log("u dun derped, specify the level, silly head");
return;
}
}
if (File.Exists(LevelInfo.BackupPath(lvl.name, text[0]))) Level lvl;
{ string[] args = message.Split(' ');
try if (args.Length >= 2) {
{ lvl = LevelInfo.FindOrShowMatches(p, args[1]);
File.Copy(LevelInfo.BackupPath(lvl.name, text[0]), LevelInfo.LevelPath(lvl.name), true); if (lvl == null) return;
Level temp = Level.Load(lvl.name); } else {
temp.StartPhysics(); if (p != null && p.level != null) {
if (temp != null) { lvl = p.level;
lvl.spawnx = temp.spawnx; } else {
lvl.spawny = temp.spawny; Player.Message(p, "You must provide a level name when using /restore from console.");
lvl.spawnz = temp.spawnz; return;
lvl.Width = temp.Width; lvl.width = temp.Width;
lvl.Height = temp.Height; lvl.depth = temp.Height;
lvl.Length = temp.Length; lvl.length = temp.Length; lvl.height = temp.Length;
lvl.blocks = temp.blocks;
lvl.CustomBlocks = temp.CustomBlocks;
lvl.setPhysics(0);
lvl.ClearPhysics();
Command.all.Find("reveal").Use(null, "all " + text[1]);
} else {
Server.s.Log("Restore nulled");
File.Copy(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(lvl.name), true);
}
}
catch { Server.s.Log("Restore fail"); }
} }
else { Player.Message(p, "Backup " + text[0] + " does not exist."); }
} }
else
{
if (Directory.Exists(Server.backupLocation + "/" + p.level.name))
{
string[] directories = Directory.GetDirectories(Server.backupLocation + "/" + p.level.name);
int backupNumber = directories.Length;
Player.Message(p, p.level.name + " has " + backupNumber + " backups .");
bool foundOne = false; string foundRestores = ""; if (File.Exists(LevelInfo.BackupPath(lvl.name, args[0]))) {
foreach (string s in directories) try {
{ DoRestore(lvl, args[0]);
string directoryName = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1); } catch {
try Server.s.Log("Restore fail");
{
int.Parse(directoryName);
}
catch
{
foundOne = true;
foundRestores += ", " + directoryName;
}
}
if (foundOne)
{
Player.Message(p, "Custom-named restores:");
Player.Message(p, "> " + foundRestores.Remove(0, 2));
}
}
else
{
Player.Message(p, p.level.name + " has no backups yet.");
} }
} else {
Player.Message(p, "Backup " + args[0] + " does not exist.");
} }
} }
public override void Help(Player p) static void DoRestore(Level lvl, string backup) {
{ File.Copy(LevelInfo.BackupPath(lvl.name, backup), LevelInfo.LevelPath(lvl.name), true);
Player.Message(p, "/restore <number> - restores a previous backup of the current map"); Level temp = Level.Load(lvl.name);
Player.Message(p, "/restore <number> <name> - restores a previous backup of the selected map"); temp.StartPhysics();
if (temp != null) {
lvl.spawnx = temp.spawnx;
lvl.spawny = temp.spawny;
lvl.spawnz = temp.spawnz;
lvl.Width = temp.Width; lvl.width = temp.Width;
lvl.Height = temp.Height; lvl.depth = temp.Height;
lvl.Length = temp.Length; lvl.length = temp.Length; lvl.height = temp.Length;
lvl.blocks = temp.blocks;
lvl.CustomBlocks = temp.CustomBlocks;
lvl.setPhysics(0);
lvl.ClearPhysics();
Command.all.Find("reveal").Use(null, "all " + lvl.name);
} else {
Server.s.Log("Restore nulled");
File.Copy(LevelInfo.LevelPath(lvl.name) + ".backup", LevelInfo.LevelPath(lvl.name), true);
}
}
static void OutputBackups(Player p) {
if (!Directory.Exists(Server.backupLocation + "/" + p.level.name)) {
Player.Message(p, p.level.name + " has no backups yet."); return;
}
string[] dirs = Directory.GetDirectories(Server.backupLocation + "/" + p.level.name);
Player.Message(p, p.level.name + " has &b" + dirs.Length + " %Sbackups.");
int count = 0;
StringBuilder custom = new StringBuilder();
foreach (string s in dirs) {
string name = s.Substring(s.LastIndexOf(Path.DirectorySeparatorChar) + 1);
int num;
if (int.TryParse(name, out num)) continue;
count++;
custom.Append(", " + name);
}
if (count == 0) return;
Player.Message(p, "&b" + count + " %Sof these are custom-named restores:");
Player.Message(p, custom.ToString(2, custom.Length - 2));
}
public override void Help(Player p) {
Player.Message(p, "%T/restore %H- lists all backups for the current map");
Player.Message(p, "%T/restore [number] [name]");
Player.Message(p, "%HRestores a previous backup for the given map.");
Player.Message(p, "%H If [name] is not given, your current map is used.");
} }
} }
} }