Skip to content

Commit

Permalink
Cleanup /deletelvl
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jan 20, 2020
1 parent 4dcca3f commit b9e06a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
11 changes: 3 additions & 8 deletions MCGalaxy/Commands/World/CmdDeleteLvl.cs
Expand Up @@ -31,16 +31,11 @@ public sealed class CmdDeleteLvl : Command2 {

public override void Use(Player p, string message, CommandData data) {
if (message.Length == 0 || message.SplitSpaces().Length > 1) { Help(p); return; }
if (!Formatter.ValidMapName(p, message)) return;
string map = Matcher.FindMaps(p, message);
string map = Matcher.FindMaps(p, message);
if (map == null) return;

if (map.CaselessEq(Server.Config.MainLevel)) { p.Message("Cannot delete the main level."); return; }
if (!LevelInfo.Check(p, data.Rank, map, "delete this map")) return;

p.Message("Created backup.");
if (LevelActions.Delete(map)) return;
p.Message(LevelActions.DeleteFailedMessage);
if (!LevelInfo.Check(p, data.Rank, map, "delete this map")) return;
LevelActions.Delete(p, map);
}

public override void Help(Player p) {
Expand Down
5 changes: 1 addition & 4 deletions MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs
Expand Up @@ -189,11 +189,8 @@ public sealed partial class CmdOverseer : Command2 {
}

string map = p.level.name;
p.Message("Created backup.");
if (LevelActions.Delete(map)) {
if (LevelActions.Delete(p, map)) {
p.Message("Map " + map + " was removed.");
} else {
p.Message(LevelActions.DeleteFailedMessage);
}
}

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Commands/World/CmdRenameLvl.cs
Expand Up @@ -44,7 +44,7 @@ public sealed class CmdRenameLvl : Command2 {
List<Player> players = lvl.getPlayers();
lvl.Unload();

LevelActions.Rename(lvl.name, newMap);
LevelActions.Rename(p, lvl.name, newMap);
LevelActions.Load(p, newMap, true);
Chat.MessageGlobal("Renamed {0} to {1}", lvl.name, newMap);
// Move all the old players to the renamed map
Expand Down
21 changes: 15 additions & 6 deletions MCGalaxy/Levels/LevelActions.cs
Expand Up @@ -47,8 +47,9 @@ public static class LevelActions {
return lvl && props && defs && blkOld && blkCur && bots;
}

/// <summary> Renames the .lvl (and related) files and database tables. Does not unload. </summary>
public static void Rename(string src, string dst) {
/// <summary> Renames the given map and associated metadata. Does not unload. </summary>
/// <remarks> Backups are NOT renamed. </remarks>
public static void Rename(Player p, string src, string dst) {
File.Move(LevelInfo.MapPath(src), LevelInfo.MapPath(dst));
DoAll(src, dst, action_move);

Expand Down Expand Up @@ -108,12 +109,20 @@ public static class LevelActions {
}*/


public const string DeleteFailedMessage = "Unable to delete the level, because it could not be unloaded. A game may currently be running on it.";
/// <summary> Deletes the .lvl (and related) files and database tables. Unloads level if it is loaded. </summary>
public static bool Delete(string map) {
/// <summary> Deletes a map and associated metadata. </summary>
public static bool Delete(Player p, string map) {
Level lvl = LevelInfo.FindExact(map);
if (lvl != null && !lvl.Unload()) return false;
if (lvl == Server.mainLevel) {
p.Message("Cannot delete the main level."); return false;
}

if (lvl != null && !lvl.Unload()) {
p.Message("Unable to delete the level, because it could not be unloaded. " +
"A game may currently be running on it.");
return false;
}

p.Message("Created backup.");
if (!Directory.Exists("levels/deleted"))
Directory.CreateDirectory("levels/deleted");

Expand Down

0 comments on commit b9e06a6

Please sign in to comment.