diff --git a/MCGalaxy/Commands/Fun/CmdCountdown.cs b/MCGalaxy/Commands/Fun/CmdCountdown.cs index dbf3a34b6..a672456f9 100644 --- a/MCGalaxy/Commands/Fun/CmdCountdown.cs +++ b/MCGalaxy/Commands/Fun/CmdCountdown.cs @@ -20,6 +20,7 @@ using System; using MCGalaxy.Commands.World; using MCGalaxy.Games; +using MCGalaxy.Generator; namespace MCGalaxy.Commands.Fun { @@ -61,7 +62,7 @@ namespace MCGalaxy.Commands.Fun { } ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.GetDimensions(p, args, 1, ref x, ref y, ref z)) return; + if (!MapGen.GetDimensions(p, args, 1, ref x, ref y, ref z)) return; CountdownGame game = (CountdownGame)game_; game.GenerateMap(p, x, y, z); diff --git a/MCGalaxy/Commands/World/CmdNewLvl.cs b/MCGalaxy/Commands/World/CmdNewLvl.cs index 819329f6e..c5684d536 100644 --- a/MCGalaxy/Commands/World/CmdNewLvl.cs +++ b/MCGalaxy/Commands/World/CmdNewLvl.cs @@ -51,61 +51,11 @@ namespace MCGalaxy.Commands.World { if (gen == null) { MapGen.PrintThemes(p); return null; } ushort x = 0, y = 0, z = 0; - string name = args[0].ToLower(); - if (!GetDimensions(p, args, 1, ref x, ref y, ref z)) return null; + if (!MapGen.GetDimensions(p, args, 1, ref x, ref y, ref z)) return null; string seed = args.Length == 6 ? args[5] : ""; - if (!Formatter.ValidMapName(p, name)) return null; - if (LevelInfo.MapExists(name)) { - p.Message("%WLevel \"{0}\" already exists", name); return null; - } if (gen.Type == GenType.Advanced && !CheckExtraPerm(p, data, 1)) return null; - - if (Interlocked.CompareExchange(ref p.GeneratingMap, 1, 0) == 1) { - p.Message("You are already generating a map, please wait until that map has finished generating first."); - return null; - } - - Level lvl; - try { - p.Message("Generating map \"{0}\"..", name); - lvl = new Level(name, x, y, z); - if (!gen.Generate(p, lvl, seed)) { lvl.Dispose(); return null; } - - string format = seed.Length > 0 ? "{0}%S created level {1}%S with seed \"{2}\"" : "{0}%S created level {1}"; - string msg = string.Format(format, p.ColoredName, lvl.ColoredName, seed); - Chat.MessageGlobal(msg); - } finally { - Interlocked.Exchange(ref p.GeneratingMap, 0); - Server.DoGC(); - } - return lvl; - } - - - internal static bool GetDimensions(Player p, string[] args, int i, ref ushort x, ref ushort y, ref ushort z) { - return - CheckMapAxis(p, args[i ], "Width", ref x) && - CheckMapAxis(p, args[i + 1], "Height", ref y) && - CheckMapAxis(p, args[i + 2], "Length", ref z) && - CheckMapVolume(p, x, y, z); - } - - static bool CheckMapAxis(Player p, string input, string type, ref ushort len) { - return CommandParser.GetUShort(p, input, type, ref len, 1, 16384); - } - - static bool CheckMapVolume(Player p, int x, int y, int z) { - if (p.IsConsole) return true; - int limit = p.group.GenVolume; - if ((long)x * y * z <= limit) return true; - - string text = "%WYou cannot create a map with over "; - if (limit > 1000 * 1000) text += (limit / (1000 * 1000)) + " million blocks"; - else if (limit > 1000) text += (limit / 1000) + " thousand blocks"; - else text += limit + " blocks"; - p.Message(text); - return false; + return MapGen.Generate(p, gen, args[0], x, y, z, seed); } public override void Help(Player p) { diff --git a/MCGalaxy/Commands/World/CmdResizeLvl.cs b/MCGalaxy/Commands/World/CmdResizeLvl.cs index 8191fdb7d..b7062e48c 100644 --- a/MCGalaxy/Commands/World/CmdResizeLvl.cs +++ b/MCGalaxy/Commands/World/CmdResizeLvl.cs @@ -50,7 +50,7 @@ namespace MCGalaxy.Commands.World { if (!LevelInfo.Check(p, data.Rank, lvl, "resize this level")) return false; ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.GetDimensions(p, args, 1, ref x, ref y, ref z)) return false; + if (!MapGen.GetDimensions(p, args, 1, ref x, ref y, ref z)) return false; bool confirmed = args.Length > 4 && args[4].CaselessEq("confirm"); if (!confirmed && (x < lvl.Width || y < lvl.Height || z < lvl.Length)) { diff --git a/MCGalaxy/Economy/Item.cs b/MCGalaxy/Economy/Item.cs index 3849a6e79..c07412f1c 100644 --- a/MCGalaxy/Economy/Item.cs +++ b/MCGalaxy/Economy/Item.cs @@ -70,7 +70,7 @@ namespace MCGalaxy.Eco { protected void UseCommand(Player p, string cmd, string args) { CommandData data = default(CommandData); - data.Rank = LevelPermission.Nobody; + data.Rank = LevelPermission.Nobody; data.Context = CommandContext.Purchase; Command.Find(cmd).Use(p, args, data); } diff --git a/MCGalaxy/Economy/LevelItem.cs b/MCGalaxy/Economy/LevelItem.cs index 4c4c80fcc..891b915ba 100644 --- a/MCGalaxy/Economy/LevelItem.cs +++ b/MCGalaxy/Economy/LevelItem.cs @@ -130,7 +130,7 @@ namespace MCGalaxy.Eco { preset.name = args[2]; ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.GetDimensions(p, args, 3, ref x, ref y, ref z)) return; + if (!MapGen.GetDimensions(p, args, 3, ref x, ref y, ref z)) return; preset.x = args[3]; preset.y = args[4]; preset.z = args[5]; if (MapGen.Find(args[6]) == null) { @@ -166,7 +166,7 @@ namespace MCGalaxy.Eco { if (args[3] == "z") dims[2] = args[4]; ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.GetDimensions(p, dims, 0, ref x, ref y, ref z)) return; + if (!MapGen.GetDimensions(p, dims, 0, ref x, ref y, ref z)) return; preset.x = dims[0]; preset.y = dims[1]; preset.z = dims[2]; p.Message("&aSuccessfully changed preset {0} size to &f{1}", args[3], args[4]); diff --git a/MCGalaxy/Generator/MapGen.cs b/MCGalaxy/Generator/MapGen.cs index 272f24431..42f53905a 100644 --- a/MCGalaxy/Generator/MapGen.cs +++ b/MCGalaxy/Generator/MapGen.cs @@ -17,6 +17,8 @@ */ using System; using System.Collections.Generic; +using System.Threading; +using MCGalaxy.Commands; namespace MCGalaxy.Generator { public delegate bool MapGenFunc(Player p, Level lvl, string seed); @@ -83,6 +85,62 @@ namespace MCGalaxy.Generator { AdvNoiseGen.RegisterGenerators(); Register("Heightmap", GenType.Advanced, HeightmapGen.Generate, "%HSeed specifies the URL of the heightmap image"); + } + + + public static Level Generate(Player p, MapGen gen, string name, + ushort x, ushort y, ushort z, string seed) { + name = name.ToLower(); + if (!Formatter.ValidMapName(p, name)) return null; + if (LevelInfo.MapExists(name)) { + p.Message("%WLevel \"{0}\" already exists", name); return null; + } + + if (Interlocked.CompareExchange(ref p.GeneratingMap, 1, 0) == 1) { + p.Message("You are already generating a map, please wait until that map has finished generating first."); + return null; + } + + Level lvl; + try { + p.Message("Generating map \"{0}\"..", name); + lvl = new Level(name, x, y, z); + if (!gen.Generate(p, lvl, seed)) { lvl.Dispose(); return null; } + + string format = seed.Length > 0 ? "{0}%S created level {1}%S with seed \"{2}\"" : "{0}%S created level {1}"; + string msg = string.Format(format, p.ColoredName, lvl.ColoredName, seed); + Chat.MessageGlobal(msg); + } finally { + Interlocked.Exchange(ref p.GeneratingMap, 0); + Server.DoGC(); + } + return lvl; + } + + public static bool GetDimensions(Player p, string[] args, int i, + ref ushort x, ref ushort y, ref ushort z) { + return + CheckMapAxis(p, args[i ], "Width", ref x) && + CheckMapAxis(p, args[i + 1], "Height", ref y) && + CheckMapAxis(p, args[i + 2], "Length", ref z) && + CheckMapVolume(p, x, y, z); + } + + static bool CheckMapAxis(Player p, string input, string type, ref ushort len) { + return CommandParser.GetUShort(p, input, type, ref len, 1, 16384); + } + + static bool CheckMapVolume(Player p, int x, int y, int z) { + if (p.IsConsole) return true; + int limit = p.group.GenVolume; + if ((long)x * y * z <= limit) return true; + + string text = "%WYou cannot create a map with over "; + if (limit > 1000 * 1000) text += (limit / (1000 * 1000)) + " million blocks"; + else if (limit > 1000) text += (limit / 1000) + " thousand blocks"; + else text += limit + " blocks"; + p.Message(text); + return false; } } }