Core: Make /map use new LevelOptions code. (Still todo, make better help)

This commit is contained in:
UnknownShadow200 2016-10-05 08:08:25 +11:00
parent 55d1093a21
commit 525400617d
2 changed files with 37 additions and 116 deletions

View File

@ -62,86 +62,34 @@ namespace MCGalaxy.Commands.World {
} }
static bool IsMapOption(string[] args) { static bool IsMapOption(string[] args) {
string opt = LevelOptions.Map(args[0].ToLower()); string opt = LevelOptions.Map(args[0].ToLower());
if (!LevelOptions.Options.ContainsKey(opt)) return false; if (!HasOption(opt)) return false;
// In rare case someone uses /map motd motd My MOTD // In rare case someone uses /map motd motd My MOTD
if (opt == "motd" && (args.Length == 1 || !args[1].CaselessStarts("motd "))) return true; if (opt == "motd" && (args.Length == 1 || !args[1].CaselessStarts("motd "))) return true;
bool optHasArg = opt == "ps" || opt == "physicspeed" || opt == "overload" bool optHasArg = opt == "physicspeed" || opt == "overload"
|| opt == "fall" || opt == "drown" || opt == "realmowner"; || opt == "fall" || opt == "drown" || opt == "realmowner";
return args.Length == (optHasArg ? 2 : 1); return args.Length == (optHasArg ? 2 : 1);
} }
internal static void SetMapOption(Player p, Level lvl, string opt, string value) { internal static void SetMapOption(Player p, Level lvl, string opt, string value) {
switch (opt.ToLower()) { opt = LevelOptions.Map(opt.ToLower());
case "theme": foreach (var option in LevelOptions.Options) {
lvl.theme = value; if (!option.Key.CaselessEq(opt)) continue;
lvl.ChatLevel("Map theme: &b" + lvl.theme); break;
case "finite": option.Value(p, lvl, value);
SetBool(p, lvl, ref lvl.finite, "Finite mode: "); break; Level.SaveSettings(lvl);
case "ai": return;
SetBool(p, lvl, ref lvl.ai, "Animal AI: "); break;
case "edge":
SetBool(p, lvl, ref lvl.edgeWater, "Edge water: "); break;
case "grass":
SetBool(p, lvl, ref lvl.GrassGrow, "Growing grass: "); break;
case "ps":
case "physicspeed":
SetInt(p, lvl, ref lvl.speedPhysics, value, "Physics speed", PhysicsSpeedValidator); break;
case "overload":
SetInt(p, lvl, ref lvl.overload, value, "Physics overload", PhysicsOverloadValidator); break;
case "motd":
lvl.motd = value == "" ? "ignore" : value;
lvl.ChatLevel("Map's MOTD was changed to: &b" + lvl.motd);
UpdateMotd(lvl);
break;
case "death":
SetBool(p, lvl, ref lvl.Death, "Survival death: "); break;
case "killer":
SetBool(p, lvl, ref lvl.Killer, "Killer blocks: "); break;
case "fall":
SetInt(p, lvl, ref lvl.fall, value, "Fall distance", null); break;
case "drown":
SetInt(p, lvl, ref lvl.drown, value, "Drown time (in tenths of a second)", null); break;
case "unload":
SetBool(p, lvl, ref lvl.unload, "Auto unload: "); break;
case "chat":
SetBool(p, lvl, ref lvl.worldChat, "Roleplay (level only) chat: ", true); break;
case "load":
case "loadongoto":
SetBool(p, lvl, ref lvl.loadOnGoto, "Load on goto: "); break;
case "leaf":
case "leafdecay":
SetBool(p, lvl, ref lvl.leafDecay, "Leaf deacy: "); break;
case "flow":
case "randomflow":
SetBool(p, lvl, ref lvl.randomFlow, "Random flow: "); break;
case "tree":
case "growtrees":
SetBool(p, lvl, ref lvl.growTrees, "Tree growing: "); break;
case "buildable":
SetBool(p, lvl, ref lvl.Buildable, "Buildable: ");
lvl.UpdateBlockPermissions(); break;
case "deletable":
SetBool(p, lvl, ref lvl.Deletable, "Deletable: ");
lvl.UpdateBlockPermissions(); break;
case "realmowner":
lvl.RealmOwner = value;
if (value == "") Player.Message(p, "Removed realm owner for this level.");
else Player.Message(p, "Set realm owner/owners of this level to {0}.", value);
break;
default:
Player.Message(p, "Could not find option entered."); return;
} }
Level.SaveSettings(lvl); Player.Message(p, "Could not find option entered.");
} }
static void UpdateMotd(Level lvl) {
Player[] players = PlayerInfo.Online.Items; static bool HasOption(string opt) {
foreach (Player p in players) { foreach (var option in LevelOptions.Options) {
if (p.level != lvl || !p.HasCpeExt(CpeExt.HackControl)) continue; if (option.Key.CaselessEq(opt)) return true;
p.Send(Hacks.MakeHackControl(p));
} }
return false;
} }
static void PrintMapInfo(Player p, Level lvl) { static void PrintMapInfo(Player p, Level lvl) {
@ -172,22 +120,6 @@ namespace MCGalaxy.Commands.World {
GetBool(lvl.Buildable), GetBool(lvl.Deletable)); GetBool(lvl.Buildable), GetBool(lvl.Deletable));
} }
static bool PhysicsSpeedValidator(Player p, int raw) {
if (raw < 10) { Player.Message(p, "Physics speed cannot be below 10 milliseconds."); return false; }
return true;
}
static bool PhysicsOverloadValidator(Player p, int raw) {
if (raw < 500) {
Player.Message(p, "Physics overload cannot go below 500 (default is 1500)"); return false;
}
if (p != null && p.Rank < LevelPermission.Admin && raw > 2500) {
Player.Message(p, "Only SuperOPs may set physics overload higher than 2500"); return false;
}
return true;
}
internal static void SetBool(Player p, Level lvl, ref bool target, string name, bool negate = false) { internal static void SetBool(Player p, Level lvl, ref bool target, string name, bool negate = false) {
target = !target; target = !target;
bool display = negate ? !target : target; bool display = negate ? !target : target;
@ -197,17 +129,6 @@ namespace MCGalaxy.Commands.World {
Player.Message(p, name + GetBool(display, p == null)); Player.Message(p, name + GetBool(display, p == null));
} }
static void SetInt(Player p, Level lvl, ref int target, string value, string name,
Func<Player, int, bool> validator) {
if (value == "") { Player.Message(p, "You must provide an integer."); return; }
int raw;
if (!int.TryParse(value, out raw)) { Player.Message(p, "\"{0}\" is not a valid integer.", value); return; }
if (validator != null && !validator(p, raw)) return;
target = raw;
lvl.ChatLevel(name + ": &b" + target);
}
static string GetBool(bool value, bool console = false) { static string GetBool(bool value, bool console = false) {
return console ? (value ? "ON" : "OFF") : (value ? "&aON" : "&cOFF"); return console ? (value ? "ON" : "OFF") : (value ? "&aON" : "&cOFF");
} }

View File

@ -26,32 +26,32 @@ namespace MCGalaxy {
public static Dictionary<string, OptionSetter> Options = public static Dictionary<string, OptionSetter> Options =
new Dictionary<string, OptionSetter>() { new Dictionary<string, OptionSetter>() {
{ "theme", SetTheme }, { "Theme", SetTheme },
{ "motd", SetMotd }, { "motd", SetMotd },
{ "realmowner", SetRealmOwner }, { "RealmOwner", SetRealmOwner },
{ "physicsspeed", (p, l, value) => SetPhysicsSpeed(p, l, value, "Physics speed") }, { "PhysicSpeed", (p, l, value) => SetPhysicsSpeed(p, l, value, "Physics speed") },
{ "overload", (p, l, value) => SetPhysicsOverload(p, l, value, "Physics overload") }, { "Overload", (p, l, value) => SetPhysicsOverload(p, l, value, "Physics overload") },
{ "fall", (p, l, value) => SetInt(p, l, ref l.fall, value, "Fall distance") }, { "Fall", (p, l, value) => SetInt(p, l, ref l.fall, value, "Fall distance") },
{ "drown", (p, l, value) => SetInt(p, l, ref l.drown, value, "Drown time (in tenths of a second)") }, { "Drown", (p, l, value) => SetInt(p, l, ref l.drown, value, "Drown time (in tenths of a second)") },
{ "finite", (p, l, value) => Set(p, l, ref l.finite, "Finite mode") }, { "Finite", (p, l, value) => Set(p, l, ref l.finite, "Finite mode") },
{ "ai", (p, l, value) => Set(p, l, ref l.ai, "Animal AI") }, { "AI", (p, l, value) => Set(p, l, ref l.ai, "Animal AI") },
{ "edge", (p, l, value) => Set(p, l, ref l.edgeWater, "Edge water") }, { "Edge", (p, l, value) => Set(p, l, ref l.edgeWater, "Edge water") },
{ "grass", (p, l, value) => Set(p, l, ref l.GrassGrow, "Growing grass") }, { "Grass", (p, l, value) => Set(p, l, ref l.GrassGrow, "Growing grass") },
{ "death", (p, l, value) => Set(p, l, ref l.Death, "Survival death") }, { "Death", (p, l, value) => Set(p, l, ref l.Death, "Survival death") },
{ "killer", (p, l, value) => Set(p, l, ref l.Killer, "Killer blocks") }, { "Killer", (p, l, value) => Set(p, l, ref l.Killer, "Killer blocks") },
{ "unload", (p, l, value) => Set(p, l, ref l.unload, "Auto unload") }, { "Unload", (p, l, value) => Set(p, l, ref l.unload, "Auto unload") },
{ "loadongoto", (p, l, value) => Set(p, l, ref l.loadOnGoto, "Load on goto") }, { "LoadOnGoto", (p, l, value) => Set(p, l, ref l.loadOnGoto, "Load on goto") },
{ "leafdecay", (p, l, value) => Set(p, l, ref l.leafDecay, "Leaf decay") }, { "LeafDecay", (p, l, value) => Set(p, l, ref l.leafDecay, "Leaf decay") },
{ "randomflow", (p, l, value) => Set(p, l, ref l.randomFlow, "Random flow") }, { "RandomFlow", (p, l, value) => Set(p, l, ref l.randomFlow, "Random flow") },
{ "growtrees", (p, l, value) => Set(p, l, ref l.growTrees, "Tree growing") }, { "GrowTrees", (p, l, value) => Set(p, l, ref l.growTrees, "Tree growing") },
{ "chat", (p, l, value) => Set(p, l, ref l.worldChat, "Roleplay (level only) chat: ", true) }, { "Chat", (p, l, value) => Set(p, l, ref l.worldChat, "Roleplay (level only) chat: ", true) },
{ "buildable", (p, l, value) => SetPerms(p, l, ref l.Buildable, "Buildable") }, { "Buildable", (p, l, value) => SetPerms(p, l, ref l.Buildable, "Buildable") },
{ "deletable", (p, l, value) => SetPerms(p, l, ref l.Deletable, "Deletable") }, { "Deletable", (p, l, value) => SetPerms(p, l, ref l.Deletable, "Deletable") },
}; };
public static string Map(string opt) { public static string Map(string opt) {
if (opt == "ps") return "physicsspeed"; if (opt == "ps") return "physicspeed";
if (opt == "load") return "loadongoto"; if (opt == "load") return "loadongoto";
if (opt == "leaf") return "leafdecay"; if (opt == "leaf") return "leafdecay";
if (opt == "flow") return "randomflow"; if (opt == "flow") return "randomflow";