From 13d7436189bd50c5e3695538be451c7196bf7483 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 25 Jul 2018 19:55:12 +1000 Subject: [PATCH] Allow level presets to be any size --- MCGalaxy/Commands/CommandParser.cs | 18 +++---- MCGalaxy/Commands/Fun/CmdCountdown.cs | 5 +- MCGalaxy/Commands/Information/CmdMapInfo.cs | 4 +- MCGalaxy/Commands/World/CmdNewLvl.cs | 21 +++++---- .../Commands/World/CmdOverseer.SubCommands.cs | 7 ++- MCGalaxy/Commands/World/CmdResizeLvl.cs | 17 ++++--- MCGalaxy/Economy/LevelItem.cs | 47 +++++++++---------- MCGalaxy/Generator/MapGen.cs | 8 +--- 8 files changed, 62 insertions(+), 65 deletions(-) diff --git a/MCGalaxy/Commands/CommandParser.cs b/MCGalaxy/Commands/CommandParser.cs index 900f34749..0e2d4a02f 100644 --- a/MCGalaxy/Commands/CommandParser.cs +++ b/MCGalaxy/Commands/CommandParser.cs @@ -37,8 +37,8 @@ namespace MCGalaxy.Commands { result = false; return true; } - p.Message("\"{0}\" is not a valid boolean.", input); - p.Message("Value must be either 1/yes/on or 0/no/off"); + p.Message("%W\"{0}\" is not a valid boolean.", input); + p.Message("%WValue must be either 1/yes/on or 0/no/off"); return false; } @@ -82,17 +82,17 @@ namespace MCGalaxy.Commands { int min = int.MinValue, int max = int.MaxValue) { int value; if (!int.TryParse(input, out value)) { - p.Message("\"{0}\" is not a valid integer.", input); return false; + p.Message("%W\"{0}\" is not a valid integer.", input); return false; } if (value < min || value > max) { // Try to provide more helpful range messages if (max == int.MaxValue) { - p.Message("{0} must be {1} or greater", argName, min); + p.Message("%W{0} must be {1} or greater", argName, min); } else if (min == int.MinValue) { - p.Message("{0} must be {1} or less", argName, max); + p.Message("%W{0} must be {1} or less", argName, max); } else { - p.Message("{0} must be between {1} and {2}", argName, min, max); + p.Message("%W{0} must be between {1} and {2}", argName, min, max); } return false; } @@ -105,11 +105,11 @@ namespace MCGalaxy.Commands { float min, float max) { float value; if (!Utils.TryParseDecimal(input, out value)) { - p.Message("\"{0}\" is not a valid number.", input); return false; + p.Message("%W\"{0}\" is not a valid number.", input); return false; } if (value < min || value > max) { - p.Message("{0} must be between {1} and {2}", argName, + p.Message("%W{0} must be between {1} and {2}", argName, min.ToString("F4"), max.ToString("F4")); return false; } @@ -140,7 +140,7 @@ namespace MCGalaxy.Commands { public static bool GetHex(Player p, string input, ref ColorDesc col) { ColorDesc tmp; if (!Colors.TryParseHex(input, out tmp)) { - p.Message("\"#{0}\" is not a valid HEX color.", input); return false; + p.Message("%W\"#{0}\" is not a valid HEX color.", input); return false; } col = tmp; return true; } diff --git a/MCGalaxy/Commands/Fun/CmdCountdown.cs b/MCGalaxy/Commands/Fun/CmdCountdown.cs index 86b6e9d44..39338eefc 100644 --- a/MCGalaxy/Commands/Fun/CmdCountdown.cs +++ b/MCGalaxy/Commands/Fun/CmdCountdown.cs @@ -61,10 +61,7 @@ namespace MCGalaxy.Commands.Fun { } ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.CheckMapAxis(p, args[1], "Width", ref x)) return; - if (!CmdNewLvl.CheckMapAxis(p, args[2], "Height", ref y)) return; - if (!CmdNewLvl.CheckMapAxis(p, args[3], "Length", ref z)) return; - if (!CmdNewLvl.CheckMapVolume(p, x, y, z)) return; + if (!CmdNewLvl.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/Information/CmdMapInfo.cs b/MCGalaxy/Commands/Information/CmdMapInfo.cs index 5cb82d923..f18ecc28b 100644 --- a/MCGalaxy/Commands/Information/CmdMapInfo.cs +++ b/MCGalaxy/Commands/Information/CmdMapInfo.cs @@ -169,7 +169,7 @@ namespace MCGalaxy.Commands.Info { p.Message("No custom texture pack set for this map."); } - const string format = "Colors: Fog {0}, Sky {1}, Clouds {2}, Sunlight {3}, Shadowlight {4}"; + const string format = "Colors: &eFog {0}, &eSky {1}, &eClouds {2}, &eSunlight {3}, &eShadowlight {4}"; p.Message(format, Color(cfg.FogColor), Color(cfg.SkyColor), Color(cfg.CloudColor), Color(cfg.LightColor), Color(cfg.ShadowColor)); @@ -227,7 +227,7 @@ namespace MCGalaxy.Commands.Info { } static string Color(string src) { - return (src == null || src.Length == 0 || src == "-1") ? "&bnone&e" : "&b" + src + "&e"; + return (src == null || src.Length == 0 || src == "-1") ? "&bnone" : "&b" + src; } public override void Help(Player p) { diff --git a/MCGalaxy/Commands/World/CmdNewLvl.cs b/MCGalaxy/Commands/World/CmdNewLvl.cs index 72957cb8d..a1c19b5d0 100644 --- a/MCGalaxy/Commands/World/CmdNewLvl.cs +++ b/MCGalaxy/Commands/World/CmdNewLvl.cs @@ -51,10 +51,7 @@ namespace MCGalaxy.Commands.World { ushort x = 0, y = 0, z = 0; string name = args[0].ToLower(); - if (!CheckMapAxis(p, args[1], "Width", ref x)) return null; - if (!CheckMapAxis(p, args[2], "Height", ref y)) return null; - if (!CheckMapAxis(p, args[3], "Length", ref z)) return null; - if (!CheckMapVolume(p, x, y, z)) return null; + if (!GetDimensions(p, args, 1, ref x, ref y, ref z)) return null; string seed = args.Length == 6 ? args[5] : ""; if (!Formatter.ValidName(p, name, "level")) return null; @@ -85,7 +82,15 @@ namespace MCGalaxy.Commands.World { } - internal static bool CheckMapAxis(Player p, string input, string type, ref ushort len) { + 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) { if (!CommandParser.GetUShort(p, input, type, ref len)) return false; if (len == 0) { p.Message("%W{0} cannot be 0.", type); return false; } if (len > 16384) { p.Message("%W{0} must be 16384 or less.", type); return false; } @@ -97,12 +102,12 @@ namespace MCGalaxy.Commands.World { return true; } - internal static bool CheckMapVolume(Player p, int x, int y, int z) { + 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 = "You cannot create a map with over "; + 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"; @@ -110,8 +115,6 @@ namespace MCGalaxy.Commands.World { return false; } - - public override void Help(Player p) { p.Message("%T/NewLvl [name] [width] [height] [length] [theme] "); p.Message("%HCreates/generates a new level."); diff --git a/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs b/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs index b497ac71d..1fe813e90 100644 --- a/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs +++ b/MCGalaxy/Commands/World/CmdOverseer.SubCommands.cs @@ -113,9 +113,12 @@ namespace MCGalaxy.Commands.World { string[] args = value.SplitSpaces(); if (args.Length < 4) { Command.Find("ResizeLvl").Help(p); return; } - if (CmdResizeLvl.DoResize(p, args, p.DefaultCmdData)) return; + bool needConfirm; + if (CmdResizeLvl.DoResize(p, args, p.DefaultCmdData, out needConfirm)) return; + + if (!needConfirm) return; p.Message("Type %T/os map resize {0} {1} {2} confirm %Sif you're sure.", - args[1], args[2], args[3]); + args[1], args[2], args[3]); } else if (cmd == "PERVISIT") { // Older realm maps didn't put you on visit whitelist, so make sure we put the owner here AccessController access = p.level.VisitAccess; diff --git a/MCGalaxy/Commands/World/CmdResizeLvl.cs b/MCGalaxy/Commands/World/CmdResizeLvl.cs index bbb67de46..a6d4b0f87 100644 --- a/MCGalaxy/Commands/World/CmdResizeLvl.cs +++ b/MCGalaxy/Commands/World/CmdResizeLvl.cs @@ -33,25 +33,28 @@ namespace MCGalaxy.Commands.World { string[] args = message.SplitSpaces(); if (args.Length < 4) { Help(p); return; } - if (DoResize(p, args, data)) return; + bool needConfirm; + if (DoResize(p, args, data, out needConfirm)) return; + + if (!needConfirm) return; p.Message("Type %T/ResizeLvl {0} {1} {2} {3} confirm %Sif you're sure.", - args[0], args[1], args[2], args[3]); + args[0], args[1], args[2], args[3]); } - public static bool DoResize(Player p, string[] args, CommandData data) { + public static bool DoResize(Player p, string[] args, CommandData data, out bool needConfirm) { + needConfirm = false; Level lvl = Matcher.FindLevels(p, args[0]); + if (lvl == null) return true; if (!LevelInfo.Check(p, data.Rank, lvl, "resize this level")) return false; ushort x = 0, y = 0, z = 0; - if (!CmdNewLvl.CheckMapAxis(p, args[1], "Width", ref x)) return false; - if (!CmdNewLvl.CheckMapAxis(p, args[2], "Height", ref y)) return false; - if (!CmdNewLvl.CheckMapAxis(p, args[3], "Length", ref z)) return false; - if (!CmdNewLvl.CheckMapVolume(p, x, y, z)) return true; + if (!CmdNewLvl.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)) { p.Message("New level dimensions are smaller than the current dimensions, %Wyou will lose blocks%S."); + needConfirm = true; return false; } diff --git a/MCGalaxy/Economy/LevelItem.cs b/MCGalaxy/Economy/LevelItem.cs index 03161dff6..f9f34fc6f 100644 --- a/MCGalaxy/Economy/LevelItem.cs +++ b/MCGalaxy/Economy/LevelItem.cs @@ -51,11 +51,11 @@ namespace MCGalaxy.Eco { } switch (args[3]) { - case "price": preset.price = int.Parse(args[4]); break; - case "x": preset.x = args[4]; break; - case "y": preset.y = args[4]; break; - case "z": preset.z = args[4]; break; - case "type": preset.type = args[4]; break; + case "price": preset.price = int.Parse(args[4]); break; + case "x": preset.x = args[4]; break; + case "y": preset.y = args[4]; break; + case "z": preset.z = args[4]; break; + case "type": preset.type = args[4]; break; } } @@ -84,7 +84,7 @@ namespace MCGalaxy.Eco { p.Message("&aCreating level: '&f" + name + "&a' . . ."); UseCommand(p, "NewLvl", name + " " + preset.x + " " + preset.y + " " + preset.z + " " + preset.type); - + Level level = LevelActions.Load(Player.Console, name, true); CmdOverseer.SetPerms(p, level); Level.SaveSettings(level); @@ -114,11 +114,10 @@ namespace MCGalaxy.Eco { preset = new LevelPreset(); preset.name = args[2]; - if (OkayAxis(args[3]) && OkayAxis(args[4]) && OkayAxis(args[5])) { - preset.x = args[3]; preset.y = args[4]; preset.z = args[5]; - } else { - p.Message("%WDimension must be a power of 2"); return; - } + + ushort x = 0, y = 0, z = 0; + if (!CmdNewLvl.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.IsRecognisedTheme(args[6])) { MapGen.PrintThemes(p); return; @@ -139,7 +138,7 @@ namespace MCGalaxy.Eco { Presets.Remove(preset); p.Message("&aSuccessfully removed preset: &f" + preset.name); } - + void EditPreset(Player p, string[] args, LevelPreset preset) { if (preset == null) { p.Message("%WThat preset level doesn't exist"); return; } @@ -147,11 +146,15 @@ namespace MCGalaxy.Eco { preset.name = args[4]; p.Message("&aSuccessfully changed preset name to &f" + preset.name); } else if (args[3] == "x" || args[3] == "y" || args[3] == "z") { - if (!OkayAxis(args[4])) { p.Message("%WDimension was wrong, it must be a power of 2"); return; } - - if (args[3] == "x") preset.x = args[4]; - if (args[3] == "y") preset.y = args[4]; - if (args[3] == "z") preset.z = args[4]; + string[] dims = new string[] { preset.x, preset.y, preset.z }; + if (args[3] == "x") dims[0] = args[4]; + if (args[3] == "y") dims[1] = args[4]; + 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; + 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]); } else if (args[3] == "type" || args[3] == "theme") { if (!MapGen.IsRecognisedTheme(args[4])) { MapGen.PrintThemes(p); return; } @@ -189,8 +192,8 @@ namespace MCGalaxy.Eco { foreach (LevelPreset preset in Presets) { p.Message("&6{0} %S({1}, {2}, {3}) {4}: &a{5} %S{6}", - preset.name, preset.x, preset.y, preset.z, - preset.type, preset.price, ServerConfig.Currency); + preset.name, preset.x, preset.y, preset.z, + preset.type, preset.price, ServerConfig.Currency); } } @@ -200,11 +203,5 @@ namespace MCGalaxy.Eco { } return null; } - - static bool OkayAxis(string value) { - ushort length; - if (!ushort.TryParse(value, out length)) return false; - return MapGen.OkayAxis(length); - } } } diff --git a/MCGalaxy/Generator/MapGen.cs b/MCGalaxy/Generator/MapGen.cs index d3fb712e3..761322c71 100644 --- a/MCGalaxy/Generator/MapGen.cs +++ b/MCGalaxy/Generator/MapGen.cs @@ -50,14 +50,8 @@ namespace MCGalaxy.Generator { p.Message("Advanced themes: " + advGens.Keys.Join(", ")); } - public static IEnumerable SimpleThemeNames { get { return simpleGens.Keys; } } - + public static IEnumerable SimpleThemeNames { get { return simpleGens.Keys; } } public static IEnumerable AdvancedThemeNames { get { return advGens.Keys; } } - - - public static bool OkayAxis(int len) { - return len >= 16 && len <= 8192 && (len % 16) == 0; - } public static bool Generate(Level lvl, string theme, string seed, Player p) { MapGenArgs genArgs = new MapGenArgs();