From c35dee868d69759665a95acee13e0592440a9670 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 30 Oct 2015 18:53:50 +1100 Subject: [PATCH] Cleanup CmdEnvironment, give CmdEnvironment some new-style help layout similar to fCraft. --- Commands/CmdEnvironment.cs | 1023 +++++++++++------------------------- 1 file changed, 310 insertions(+), 713 deletions(-) diff --git a/Commands/CmdEnvironment.cs b/Commands/CmdEnvironment.cs index 4b0c11b23..aa0cc57ce 100644 --- a/Commands/CmdEnvironment.cs +++ b/Commands/CmdEnvironment.cs @@ -1,27 +1,26 @@ /* - Copyright 2011 MCGalaxy - - Dual-licensed under the Educational Community License, Version 2.0 and - the GNU General Public License, Version 3 (the "Licenses"); you may - not use this file except in compliance with the Licenses. You may - obtain a copy of the Licenses at - - http://www.opensource.org/licenses/ecl2.php - http://www.gnu.org/licenses/gpl-3.0.html - - Unless required by applicable law or agreed to in writing, - software distributed under the Licenses are distributed on an "AS IS" - BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - or implied. See the Licenses for the specific language governing - permissions and limitations under the Licenses. -*/ -using System; -using System.Data; -using System.Text; -namespace MCGalaxy.Commands -{ - public sealed class CmdEnvironment : Command - { + Copyright 2011 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; + +namespace MCGalaxy.Commands { + + public sealed class CmdEnvironment : Command { + public override string name { get { return "environment"; } } public override string shortcut { get { return "env"; } } public override string type { get { return "other"; } } @@ -29,707 +28,305 @@ namespace MCGalaxy.Commands public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } public CmdEnvironment() { } - public override void Use(Player p, string message) - { - if (message == "") { Help(p); return; } - string[] pars = message.Split(' '); - string target = ""; - if(pars[0].ToLower() == "player" || pars[0].ToLower() == "p") - { - target = "p"; + public override void Use(Player p, string message) { + string[] args = null; + if (message == "" || (args = message.Split(' ')).Length < 3) { + Help(p); return; + } else { + string group = args[0].ToLower(); + string target = null; + + if (group == "player" || group == "p") { + target = "p"; + } else if (group == "level" || group == "l") { + target = "l"; + } else { + p.SendMessage(string.Format("Env: Unrecognised target \"{0}\".", group)); + return; + } + Handle(p, target, args[1].ToLower(), args[2].ToLower()); } - if(pars[0].ToLower() == "level" || pars[0].ToLower() == "l") - { - target = "l"; - } - if (target == "") - return; - Parse(p, target, pars); } - public override void Help(Player p) - { - p.SendMessage("/env [target] [variable] [value]"); - p.SendMessage("Valid targets: player, level [Abbreviated as p and l]"); - p.SendMessage("Valid variables: fog, cloud, sky, sun, shadow, level, horizon, border, weather, preset(l only)"); - p.SendMessage("Using 'normal' as a value will reset the variable"); - } - public void Parse(Player p, string target, string[] pars) - { - string valueText = ""; - try - { - valueText = pars[2]; - } - catch { } - bool isValid = true; - if (target == "p") - { - switch (pars[1].ToLower()) - { - case "fog": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset fog color for {0}&S to normal", "you")); - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.SendMessage(string.Format("Set fog color for {0}&S to #{1}", "you", valueText)); - } - } - if (p.HasExtension("EnvSetColor")) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + valueText.ToUpper()); - p.SendEnvSetColor(2, col.R, col.G, col.B); - } - catch - { - p.SendEnvSetColor(2, -1, -1, -1); - } - } - break; - case "cloud": - case "clouds": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset cloud color for {0}&S to normal", "you")); - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.SendMessage(string.Format("Set cloud color for {0}&S to #{1}", "you", valueText)); - - } - } - if (p.HasExtension("EnvSetColor")) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + valueText.ToUpper()); - p.SendEnvSetColor(1, col.R, col.G, col.B); - } - catch - { - p.SendEnvSetColor(1, -1, -1, -1); - } - } - break; - - case "sky": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset sky color for {0}&S to normal", "you")); - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.SendMessage(string.Format("Set sky color for {0}&S to #{1}", "you", valueText)); - } - } - - if (p.HasExtension("EnvSetColor")) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + valueText.ToUpper()); - p.SendEnvSetColor(0, col.R, col.G, col.B); - } - catch - { - p.SendEnvSetColor(0, -1, -1, -1); - } - } - break; - case "dark": - case "shadow": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset shadow color for {0}&S to normal", "you")); - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.SendMessage(string.Format("Set shadow color for {0}&S to #{1}", "you", valueText)); - } - } - if (p.HasExtension("EnvSetColor")) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + valueText.ToUpper()); - p.SendEnvSetColor(3, col.R, col.G, col.B); - } - catch - { - p.SendEnvSetColor(3, -1, -1, -1); - } - } - break; - - case "sun": - case "light": - case "sunlight": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset sunlight color for {0}&S to normal", "you")); - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.SendMessage(string.Format("Set sunlight color for {0}&S to #{1}", "you", valueText)); - } - } - if (p.HasExtension("EnvSetColor")) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + valueText); - p.SendEnvSetColor(4, col.R, col.G, col.B); - } - catch - { - p.SendEnvSetColor(4, -1, -1, -1); - } - } - break; - case "level": - case "horizon": - case "edge": - case "water": - case "side": - case "border": - case "bedrock": + + void Handle(Player p, string group, string variable, string value) { + bool level = group == "l"; + switch (variable) { + case "fog": + SetEnvColour(p, value, 2, "fog", ref p.level.FogColor, level); + break; + case "cloud": + case "clouds": + SetEnvColour(p, value, 1, "cloud", ref p.level.CloudColor, level); + break; + case "sky": + SetEnvColour(p, value, 0, "sky", ref p.level.SkyColor, level); + break; + case "dark": + case "shadow": + SetEnvColour(p, value, 3, "shadow", ref p.level.ShadowColor, level); + break; + case "sun": + case "light": + case "sunlight": + SetEnvColour(p, value, 4, "sunlight", ref p.level.LightColor, level); + break; + case "weather": + SetEnvWeather(p, value, ref p.level.weather, level); + break; + + case "level": + if (!level) { p.SendMessage("This feature is not available for 'player' target"); - break; - case "weather": - byte weather = 0; - if (valueText.Equals("normal", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset weather for {0}&S to normal(0) ", "you")); - } - else - { - if (!byte.TryParse(valueText, out weather)) - { - if (valueText.Equals("sun", StringComparison.OrdinalIgnoreCase)) - { - weather = 0; - } - else if (valueText.Equals("rain", StringComparison.OrdinalIgnoreCase)) - { - weather = 1; - } - else if (valueText.Equals("snow", StringComparison.OrdinalIgnoreCase)) - { - weather = 2; - } - } - if (weather < 0 || weather > 2) - { - p.SendMessage("Please use a valid integer(0,1,2) or string(sun,rain,snow)"); - return; - } - p.SendMessage(string.Format("&aSet weather for {0}&a to {1} ({2}&a)", "you", weather, weather == 0 ? "&sSun" : (weather == 1 ? "&1Rain" : "&fSnow"))); - p.SendSetMapWeather(byte.Parse(valueText)); - } - break; - default: - Help(p); - break; - } - } - if (target == "l") - { - switch (pars[1].ToLower()) - { - case "fog": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset fog color for {0}&S to normal", p.level.name)); - p.level.FogColor = null; - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.level.FogColor = valueText; - p.SendMessage(string.Format("Set fog color for {0}&S to #{1}", p.level.name, valueText)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvSetColor") && p.level == pl.level) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + p.level.FogColor.ToUpper()); - pl.SendEnvSetColor(2, col.R, col.G, col.B); - } - catch - { - pl.SendEnvSetColor(2, -1, -1, -1); - } - } - } - break; - case "cloud": - case "clouds": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset cloud color for {0}&S to normal", p.level.name)); - p.level.CloudColor = null; - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.level.CloudColor = valueText; - p.SendMessage(string.Format("Set cloud color for {0}&S to #{1}", p.level.name, valueText)); - - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvSetColor") && p.level == pl.level) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + p.level.CloudColor.ToUpper()); - pl.SendEnvSetColor(1, col.R, col.G, col.B); - } - catch - { - pl.SendEnvSetColor(1, -1, -1, -1); - } - } - } - break; - - case "sky": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset sky color for {0}&S to normal", p.level.name)); - p.level.SkyColor = null; - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.level.SkyColor = valueText; - p.SendMessage(string.Format("Set sky color for {0}&S to #{1}", p.level.name, valueText)); - } - } - - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvSetColor") && p.level == pl.level) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + p.level.SkyColor.ToUpper()); - pl.SendEnvSetColor(0, col.R, col.G, col.B); - } - catch - { - pl.SendEnvSetColor(0, -1, -1, -1); - } - } - } - break; - case "dark": - case "shadow": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset shadow color for {0}&S to normal", p.level.name)); - p.level.ShadowColor = null; - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.level.ShadowColor = valueText; - p.SendMessage(string.Format("Set shadow color for {0}&S to #{1}", p.level.name, valueText)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvSetColor") && p.level == pl.level) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + p.level.ShadowColor.ToUpper()); - pl.SendEnvSetColor(3, col.R, col.G, col.B); - } - catch - { - pl.SendEnvSetColor(3, -1, -1, -1); - } - } - } - break; - - case "sun": - case "light": - case "sunlight": - if (valueText.Equals("-1") || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset sunlight color for {0}&S to normal", p.level.name)); - p.level.LightColor = null; - } - else - { - isValid = IsValidHex(valueText); - if (!isValid) - { - p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", valueText)); - return; - } - else - { - p.level.LightColor = valueText; - p.SendMessage(string.Format("Set sunlight color for {0}&S to #{1}", p.level.name, valueText)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvSetColor") && p.level == pl.level) - { - try - { - System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + p.level.LightColor.ToUpper()); - pl.SendEnvSetColor(4, col.R, col.G, col.B); - } - catch - { - pl.SendEnvSetColor(4, -1, -1, -1); - } - } - } - break; - case "level": - short level; - if (valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("reset", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase) || valueText.Equals("middle", StringComparison.OrdinalIgnoreCase) || valueText.Equals("center", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset water level for {0}&S to normal", p.level.name)); - p.level.EdgeLevel = (short)(p.level.depth / 2); - } - else - { - if (!short.TryParse(valueText, out level)) - { - p.SendMessage(string.Format("Env: \"{0}\" is not a valid integer.", valueText)); - return; - } - else - { - p.level.EdgeLevel = level; - p.SendMessage(string.Format("Set water level for {0}&S to {1}", p.level.name, level)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvMapAppearance") && p.level == pl.level) - { - pl.SendSetMapAppearance(p.level.textureUrl, p.level.EdgeBlock, p.level.HorizonBlock, p.level.EdgeLevel); - } - } - break; - - case "horizon": - case "edge": - case "water": - byte block = Block.Byte(valueText); - if (block == Block.Zero && !(valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase))) - { - Help(p); - return; - } - if (block == Block.water || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset water block for {0}&S to normal (Water)", p.level.name)); - p.level.HorizonBlock = Block.water; - } - else - { - if (block == Block.air || block == Block.shrub || block == Block.glass || block == Block.yellowflower || block == Block.redflower || block == Block.mushroom || block == Block.redmushroom || block == Block.rope || block == Block.fire) - { - p.SendMessage(string.Format("Env: Cannot use {0} for water textures.", block)); - return; - } - else - { - p.level.HorizonBlock = block; - p.SendMessage(string.Format("Set water block for {0}&S to {1}", p.level.name, block)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvMapAppearance") && p.level == pl.level) - { - pl.SendSetMapAppearance(p.level.textureUrl, p.level.EdgeBlock, p.level.HorizonBlock, p.level.EdgeLevel); - } - } - break; - - case "side": - case "border": - case "bedrock": - byte blockhorizon = Block.Byte(valueText); - if (blockhorizon == Block.Zero && !(valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase))) - { - Help(p); - return; - } - if (blockhorizon == Block.blackrock || valueText.Equals("normal", StringComparison.OrdinalIgnoreCase) || valueText.Equals("default", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset bedrock block for {0}&S to normal (Bedrock)", p.level.name)); - p.level.EdgeBlock = Block.blackrock; - } - else - { - if (blockhorizon == Block.air || blockhorizon == Block.shrub || blockhorizon == Block.glass || blockhorizon == Block.yellowflower || blockhorizon == Block.redflower || blockhorizon == Block.mushroom || blockhorizon == Block.redmushroom || blockhorizon == Block.rope || blockhorizon == Block.fire) - { - p.SendMessage(string.Format("Env: Cannot use {0} for bedrock textures.", blockhorizon)); - return; - } - else - { - p.level.EdgeBlock = blockhorizon; - p.SendMessage(string.Format("Set bedrock block for {0}&S to {1}", p.level.name, blockhorizon)); - } - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvMapAppearance") && p.level == pl.level) - { - pl.SendSetMapAppearance(p.level.textureUrl, p.level.EdgeBlock, p.level.HorizonBlock, p.level.EdgeLevel); - } - } - break; - case "weather": - byte weather = 0; - if (valueText.Equals("normal", StringComparison.OrdinalIgnoreCase)) - { - p.SendMessage(string.Format("Reset weather for {0}&S to normal(0) ", p.level.name)); - p.level.weather = 0; - } - else - { - if (!byte.TryParse(valueText, out weather)) - { - if (valueText.Equals("sun", StringComparison.OrdinalIgnoreCase)) - { - weather = 0; - } - else if (valueText.Equals("rain", StringComparison.OrdinalIgnoreCase)) - { - weather = 1; - } - else if (valueText.Equals("snow", StringComparison.OrdinalIgnoreCase)) - { - weather = 2; - } - } - if (weather < 0 || weather > 2) - { - p.SendMessage("Please use a valid integer(0,1,2) or string(sun,rain,snow)"); - return; - } - p.level.weather = weather; - p.SendMessage(string.Format("&aSet weather for {0}&a to {1} ({2}&a)", p.level.name, weather, weather == 0 ? "&sSun" : (weather == 1 ? "&1Rain" : "&fSnow"))); - } - foreach (Player pl in Player.players) - { - if (pl.HasExtension("EnvWeatherType") && p.level == pl.level) - { - pl.SendSetMapWeather(p.level.weather); - } - } - break; - case "preset": - if (valueText.Equals("midnight", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog 8b8989"); - Command.all.Find("env").Use(p, "l shadow 918A3B"); - Command.all.Find("env").Use(p, "l clouds 000080"); - Command.all.Find("env").Use(p, "l sun 0000cd"); - Command.all.Find("env").Use(p, "l sky 191970"); - return; - } - if (valueText.Equals("cartoon", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog 00ffff"); - Command.all.Find("env").Use(p, "l shadow f4a460"); - Command.all.Find("env").Use(p, "l clouds 00bfff"); - Command.all.Find("env").Use(p, "l sun f5deb3"); - Command.all.Find("env").Use(p, "l sky 1e90ff"); - return; - } - if (valueText.Equals("noir", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog 000000"); - Command.all.Find("env").Use(p, "l shadow 1f1f1f"); - Command.all.Find("env").Use(p, "l clouds 000000"); - Command.all.Find("env").Use(p, "l sun 696969"); - Command.all.Find("env").Use(p, "l sky 1f1f1f"); - return; - } - if (valueText.Equals("trippy", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog 4B0082"); - Command.all.Find("env").Use(p, "l shadow B22222"); - Command.all.Find("env").Use(p, "l clouds 006400"); - Command.all.Find("env").Use(p, "l sun 7CFC00"); - Command.all.Find("env").Use(p, "l sky FFD700"); - return; - } - if (valueText.Equals("watery", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog 5f9ea0"); - Command.all.Find("env").Use(p, "l shadow 008B8B"); - Command.all.Find("env").Use(p, "l clouds 008B8B"); - Command.all.Find("env").Use(p, "l sun E0FFFF"); - Command.all.Find("env").Use(p, "l sky 008080"); - return; - } - if (valueText.Equals("normal", StringComparison.OrdinalIgnoreCase)) - { - Command.all.Find("env").Use(p, "l fog -1"); - Command.all.Find("env").Use(p, "l shadow -1"); - Command.all.Find("env").Use(p, "l clouds -1"); - Command.all.Find("env").Use(p, "l sun -1"); - Command.all.Find("env").Use(p, "l sky -1"); - Command.all.Find("env").Use(p, "l weather 0"); - Command.all.Find("env").Use(p, "l water normal"); - Command.all.Find("env").Use(p, "l bedrock normal"); - Command.all.Find("env").Use(p, "l level normal"); - return; - } - if (valueText.Equals("gloomy", StringComparison.OrdinalIgnoreCase))//if (args[0] == "gloomy") - { - Command.all.Find("env").Use(p, "l cloud 405875"); - Command.all.Find("env").Use(p, "l sky 405875"); - Command.all.Find("env").Use(p, "l sun 444466"); - Command.all.Find("env").Use(p, "l shadow 3B3B59"); - Command.all.Find("env").Use(p, "l fog 6A80A5"); - return; - } - if (valueText.Equals("cloudy", StringComparison.OrdinalIgnoreCase))//if (args[0] == "cloudy") - { - Command.all.Find("env").Use(p, "l cloud 8E8E8E"); - Command.all.Find("env").Use(p, "l sky 8E8E8E"); - Command.all.Find("env").Use(p, "l sun 9b9b9b"); - Command.all.Find("env").Use(p, "l shadow 8C8C8C"); - Command.all.Find("env").Use(p, "l fog AFAFAF"); - return; - } - if (valueText.Equals("sunset", StringComparison.OrdinalIgnoreCase))//if (args[0] == "sunset") - { - Command.all.Find("env").Use(p, "l cloud 9A6551"); - Command.all.Find("env").Use(p, "l sky 836668"); - Command.all.Find("env").Use(p, "l sun 7F6C60"); - Command.all.Find("env").Use(p, "l shadow 46444C"); - Command.all.Find("env").Use(p, "l fog FFA322"); - return; - } - if (valueText.Equals("midnight2", StringComparison.OrdinalIgnoreCase))//if (args[0] == "midnight") - { - Command.all.Find("env").Use(p, "l cloud 1E223A"); - Command.all.Find("env").Use(p, "l sky 070A23"); - Command.all.Find("env").Use(p, "l sun 181828"); - Command.all.Find("env").Use(p, "l shadow 0F0F19"); - Command.all.Find("env").Use(p, "l fog 131947"); - return; - } - p.SendMessage("/env l preset [type] -- Uses an env preset on your map"); - p.SendMessage("Valid types: Cartoon/Midnight/Midnight2/Noir/Normal/Trippy/Watery/Sunset/Gloomy/Cloudy"); return; - default: - Help(p); - break; + } + byte ignored = 0; + SetEnvMapAppearance(p, value, "water level", false, 0, ref ignored ); + break; + case "horizon": + case "edge": + case "water": + if (!level) { + p.SendMessage("This feature is not available for 'player' target"); + return; + } + SetEnvMapAppearance(p, value, "edge block", true, Block.blackrock, ref p.level.EdgeBlock); + break; + case "side": + case "border": + case "bedrock": + if (!level) { + p.SendMessage("This feature is not available for 'player' target"); + return; + } + SetEnvMapAppearance(p, value, "sides block", true, Block.blackrock, ref p.level.EdgeBlock); + break; + case "preset": + if (!SetPreset(p, value, level)) + return; + break; + default: + Help(p); + return; + } + + if (level ) + p.level.Save(true); + } + + void SetEnvColour(Player p, string value, byte envType, string envTypeName, + ref string modify, bool level) { + string target = level ? p.level.name : "you"; + if (IsResetString(value)) { + p.SendMessage(string.Format("Reset {0} color for {1}&S to normal", envTypeName, target)); + if( level ) + modify = null; + } else { + if (value.Length > 0 && value[0] == '#') + value = value.Substring(1); + if (value.Length != 6 || !IsValidHex(value)) { + p.SendMessage(string.Format("Env: \"#{0}\" is not a valid HEX color code.", value)); + return; + } + + p.SendMessage(string.Format("Set {0} color for {1}&S to #{2}", envTypeName, "you", value)); + if( level ) + modify = value; + } + SendEnvColorPacket(p, level, envType, value); + } + + void SendEnvColorPacket(Player p, bool level, byte envType, string value) { + if (level) { + foreach (Player pl in Player.players) { + if (pl.level == p.level) + SendEnvColorPacket(pl, envType, value); + } + } else { + SendEnvColorPacket(p, envType, value); + } + } + + void SendEnvColorPacket(Player p, byte envType, string value) { + if (p.HasExtension("EnvSetColor")) { + try { + System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#" + value.ToUpper()); + p.SendEnvSetColor(envType, col.R, col.G, col.B); + } catch { + p.SendEnvSetColor(envType, -1, -1, -1); } } - p.level.Save(true); } - /// Ensures that the hex color has the correct length (1-6 characters) - /// and character set (alphanumeric chars allowed). - public static bool IsValidHex(string hex) - { - if (hex == null) throw new ArgumentNullException("hex"); - if (hex.StartsWith("#")) hex = hex.Remove(0, 1); - if (hex.Length < 1 || hex.Length > 6) return false; - for (int i = 0; i < hex.Length; i++) - { + + void SetEnvWeather(Player p, string value, ref byte modify, bool level) { + byte weather = 255; + string target = level ? p.level.name : "you"; + if (IsResetString(value)) { + p.SendMessage(string.Format("Reset weather for {0}&S to 0 (sun)", target)); + if( level ) + p.level.weather = 0; + } else { + if (byte.TryParse(value, out weather)) { + } else if (CaselessEquals(value, "sun")) { + weather = 0; + } else if (CaselessEquals(value, "rain")) { + weather = 1; + } else if (CaselessEquals(value, "snow")) { + weather = 2; + } + + if (weather > 2) { + p.SendMessage("Please use a valid integer (0,1,2) or string (sun,rain,snow)"); + return; + } + } + + if( level ) + p.level.weather = weather; + string weatherType = weather == 0 ? "&sSun" : (weather == 1 ? "&1Rain" : "&fSnow"); + p.SendMessage(string.Format("&aSet weather for {0}&a to {1} ({2}&a)", target, weather, weatherType)); + + // Send the changed colour to all players affected by the command. + if (level) { + foreach (Player pl in Player.players) { + if (pl.level == p.level && pl.HasExtension("EnvWeatherType")) { + pl.SendSetMapWeather(weather); + } + } + } else if (p.HasExtension("EnvWeatherType")) { + p.SendSetMapWeather(weather); + } + } + + void SetEnvMapAppearance(Player p, string value, string variable, + bool block, byte defValue, ref byte modifyBlock) { + if (IsResetString(value)) { + p.SendMessage(string.Format("Reset {0} for {0}&S to normal", variable, p.level.name)); + if( block ) + modifyBlock = defValue; + else + p.level.EdgeLevel = (short)(p.level.depth / 2); + } else { + if (block && !CheckBlock(p, value, variable, ref modifyBlock)) + return; + if( !block && !CheckShort(p, value, variable, ref p.level.EdgeLevel)) + return; + } + + foreach (Player pl in Player.players) { + if (pl.HasExtension("EnvMapAppearance") && pl.level == p.level) { + pl.SendSetMapAppearance(p.level.textureUrl, p.level.EdgeBlock, p.level.HorizonBlock, p.level.EdgeLevel); + } + } + } + + bool CheckBlock(Player p, string value, string variable, ref byte modify) { + byte block = Block.Byte(value); + if (block == Block.Zero) { + Help(p); + } else if (block == Block.air || block == Block.shrub || block == Block.glass || + block == Block.yellowflower || block == Block.redflower || block == Block.mushroom || + block == Block.redmushroom || block == Block.rope || block == Block.fire) { + p.SendMessage(string.Format("Env: Cannot use {0} for {1}.", block, variable)); + } else { + modify = block; + p.SendMessage(string.Format("Set {0} for {1}&S to {2}", variable, p.level.name, block)); + return true; + } + return false; + } + + bool CheckShort(Player p, string value, string variable, ref short modify) { + short level; + if (!short.TryParse(value, out level)) { + p.SendMessage(string.Format("Env: \"{0}\" is not a valid integer.", value)); + return false; + } else { + modify = level; + p.SendMessage(string.Format("Set {0} for {1}&S to {2}", variable, p.level.name, level)); + return true; + } + } + + bool SetPreset(Player p, string value, bool level) { + EnvPreset preset = null; // fog, sky, clouds, sun, shadow + if (CaselessEquals(value, "midnight")) { + preset = new EnvPreset("8b8989", "191970", "000080", "0000cd", "918A3B"); + } else if (CaselessEquals(value, "cartoon")) { + preset = new EnvPreset("00ffff", "1e90ff", "00bfff", "f5deb3", "f4a460"); + } else if (CaselessEquals(value, "noir")) { + preset = new EnvPreset("000000", "1f1f1f", "000000", "696969", "1f1f1f"); + } else if (CaselessEquals(value, "trippy")) { + preset = new EnvPreset("4B0082", "FFD700", "006400", "7CFC00", "B22222"); + } else if (CaselessEquals(value, "watery")) { + preset = new EnvPreset("5f9ea0", "008080", "008B8B", "E0FFFF", "008B8B"); + } else if (CaselessEquals(value, "normal")) { + preset = new EnvPreset("-1", "-1", "-1", "-1", "-1"); + } else if (CaselessEquals(value, "gloomy")) { + preset = new EnvPreset("6A80A5", "405875", "405875", "444466", "3B3B59"); + } else if (CaselessEquals(value, "cloudy")) { + preset = new EnvPreset("AFAFAF", "8E8E8E", "8E8E8E", "9b9b9b", "8C8C8C"); + } else if (CaselessEquals(value, "sunset")) { + preset = new EnvPreset("FFA322", "836668", "9A6551", "7F6C60", "46444C"); + } else if (CaselessEquals(value, "midnight2")) { + preset = new EnvPreset("131947", "070A23", "1E223A", "181828", "0F0F19"); + } + + if( preset != null ) { + SendEnvColorPacket(p, level, 0, preset.Sky); + SendEnvColorPacket(p, level, 1, preset.Clouds); + SendEnvColorPacket(p, level, 2, preset.Fog); + SendEnvColorPacket(p, level, 3, preset.Shadow); + SendEnvColorPacket(p, level, 4, preset.Sun); + if (CaselessEquals( value, "normal") && level) { + Command.all.Find("env").Use(p, "l weather 0"); + Command.all.Find("env").Use(p, "l water normal"); + Command.all.Find("env").Use(p, "l bedrock normal"); + Command.all.Find("env").Use(p, "l level normal"); + } + return true; + } else { + p.SendMessage("/env l preset [type] -- Uses an env preset on your map"); + p.SendMessage("Valid types: Cartoon/Midnight/Midnight2/Noir/Normal/Trippy/Watery/Sunset/Gloomy/Cloudy"); + return false; + } + } + + class EnvPreset { + public string Fog, Sky, Clouds, Sun, Shadow; + + public EnvPreset( string fog, string sky, string clouds, + string sun, string shadow ) { + Fog = fog; Sky = sky; Clouds = clouds; + Sun = sun; Shadow = shadow; + } + } + + static bool IsValidHex(string hex) { + for (int i = 0; i < hex.Length; i++) { char ch = hex[i]; - if (ch < '0' || ch > '9' && - ch < 'A' || ch > 'Z' && - ch < 'a' || ch > 'z') - { + if (ch < '0' || ch > '9' && ch < 'A' || ch > 'F' && + ch < 'a' || ch > 'f') { return false; } } return true; } + + bool IsResetString(string value) { + return CaselessEquals(value, "-1") || CaselessEquals(value, "normal") || + CaselessEquals(value, "reset") || CaselessEquals(value, "default"); + } + + bool CaselessEquals(string a, string b) { + return a.Equals(b, StringComparison.OrdinalIgnoreCase); + } + + public override void Help(Player p) { + p.SendMessage("&a/env [target] [variable] [value]"); + p.SendMessage("&e Valid targets: player or p, level or l"); + p.SendMessage("&e Valid variables: fog, cloud, sky, sun, shadow, weather"); + p.SendMessage("&e level only variables: level, horizon, border, preset"); + p.SendMessage("&eUsing 'normal' as a value will reset the variable"); + } } -} +} \ No newline at end of file