mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 04:32:50 -04:00
CommandParser.GetEnum should only allow actual values of the enum, improve CTFConfig load/save, start modifying /ctf to let you set config values
This commit is contained in:
parent
e27c8ec482
commit
2f2ac17758
@ -43,13 +43,14 @@ namespace MCGalaxy.Commands {
|
|||||||
ref TEnum result) where TEnum : struct {
|
ref TEnum result) where TEnum : struct {
|
||||||
try {
|
try {
|
||||||
result = (TEnum)Enum.Parse(typeof(TEnum), input, true);
|
result = (TEnum)Enum.Parse(typeof(TEnum), input, true);
|
||||||
return true;
|
if (Enum.IsDefined(typeof(TEnum), result)) return true;
|
||||||
} catch (Exception) {
|
} catch {
|
||||||
|
}
|
||||||
|
|
||||||
string[] names = Enum.GetNames(typeof(TEnum));
|
string[] names = Enum.GetNames(typeof(TEnum));
|
||||||
Player.Message(p, argName + " must be one of the following: " + names.Join());
|
Player.Message(p, argName + " must be one of the following: " + names.Join());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary> Attempts to parse the given argument as an timespan in short form. </summary>
|
/// <summary> Attempts to parse the given argument as an timespan in short form. </summary>
|
||||||
public static bool GetTimespan(Player p, string input, ref TimeSpan span,
|
public static bool GetTimespan(Player p, string input, ref TimeSpan span,
|
||||||
|
@ -16,11 +16,13 @@
|
|||||||
permissions and limitations under the Licenses.
|
permissions and limitations under the Licenses.
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using MCGalaxy.Games;
|
using MCGalaxy.Games;
|
||||||
|
|
||||||
namespace MCGalaxy.Commands.Fun {
|
namespace MCGalaxy.Commands.Fun {
|
||||||
public sealed class CmdCTF : Command {
|
public sealed class CmdCTF : Command {
|
||||||
public override string name { get { return "ctf"; } }
|
public override string name { get { return "ctf"; } }
|
||||||
|
public override string shortcut { get { return "ctfsetup"; } }
|
||||||
public override string type { get { return CommandTypes.Games; } }
|
public override string type { get { return CommandTypes.Games; } }
|
||||||
public override bool museumUsable { get { return false; } }
|
public override bool museumUsable { get { return false; } }
|
||||||
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
|
public override LevelPermission defaultRank { get { return LevelPermission.Operator; } }
|
||||||
@ -35,17 +37,44 @@ namespace MCGalaxy.Commands.Fun {
|
|||||||
|
|
||||||
if (!Server.ctf.Start(p)) return;
|
if (!Server.ctf.Start(p)) return;
|
||||||
Chat.MessageGlobal("A CTF GAME IS STARTING AT CTF! TYPE /goto CTF to join!");
|
Chat.MessageGlobal("A CTF GAME IS STARTING AT CTF! TYPE /goto CTF to join!");
|
||||||
} else if (message == "stop") {
|
} else if (message.CaselessEq("stop")) {
|
||||||
if (Server.ctf == null || !Server.ctf.started) {
|
if (Server.ctf == null || !Server.ctf.started) {
|
||||||
Player.Message(p, "No CTF game is active."); return;
|
Player.Message(p, "No CTF game is active."); return;
|
||||||
}
|
}
|
||||||
Server.ctf.Stop();
|
Server.ctf.Stop();
|
||||||
|
} else if (message.CaselessEq("bluespawn")) {
|
||||||
|
CTFConfig cfg = Retrieve(p);
|
||||||
|
cfg.BlueSpawnX = p.Pos.X; cfg.BlueSpawnY = p.Pos.Y; cfg.BlueSpawnZ = p.Pos.Z;
|
||||||
|
|
||||||
|
Update(p, cfg);
|
||||||
|
Player.Message(p, "Set spawn of blue team to your position.");
|
||||||
|
} else if (message.CaselessEq("redspawn")) {
|
||||||
|
CTFConfig cfg = Retrieve(p);
|
||||||
|
cfg.RedSpawnX = p.Pos.X; cfg.RedSpawnY = p.Pos.Y; cfg.RedSpawnZ = p.Pos.Z;
|
||||||
|
|
||||||
|
Update(p, cfg);
|
||||||
|
Player.Message(p, "Set spawn of red team to your position.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CTFConfig Retrieve(Player p) {
|
||||||
|
CTFConfig cfg = new CTFConfig();
|
||||||
|
cfg.SetDefaults(p.level);
|
||||||
|
cfg.Retrieve(p.level.name);
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Update(Player p, CTFConfig cfg) {
|
||||||
|
if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF");
|
||||||
|
cfg.Save(p.level.name);
|
||||||
|
if (Server.ctf != null && p.level == Server.ctf.map) Server.ctf.UpdateConfig();
|
||||||
|
}
|
||||||
|
|
||||||
public override void Help(Player p) {
|
public override void Help(Player p) {
|
||||||
Player.Message(p, "%T/ctf start/stop");
|
Player.Message(p, "%T/ctf start/stop");
|
||||||
Player.Message(p, "%HStarts/Stops the CTF game.");
|
Player.Message(p, "%HStarts/stops the CTF game.");
|
||||||
|
Player.Message(p, "%T/ctf redspawn/bluespawn");
|
||||||
|
Player.Message(p, "%HSets spawn of red/blue team to your position.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ namespace MCGalaxy {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Writes all config elements to the given stream, grouped by named sections. </summary>
|
||||||
public static void Serialise(ConfigElement[] elements, string suffix,
|
public static void Serialise(ConfigElement[] elements, string suffix,
|
||||||
StreamWriter dst, object instance) {
|
StreamWriter dst, object instance) {
|
||||||
Dictionary<string, List<ConfigElement>> sections
|
Dictionary<string, List<ConfigElement>> sections
|
||||||
@ -81,5 +82,13 @@ namespace MCGalaxy {
|
|||||||
dst.WriteLine();
|
dst.WriteLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Writes all config elements to the given stream. </summary>
|
||||||
|
public static void SerialisePlain(ConfigElement[] elements, StreamWriter dst, object instance) {
|
||||||
|
foreach (ConfigElement elem in elements) {
|
||||||
|
object value = elem.Field.GetValue(instance);
|
||||||
|
dst.WriteLine(elem.Attrib.Name + " = " + elem.Attrib.Serialise(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
permissions and limitations under the Licenses.
|
permissions and limitations under the Licenses.
|
||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using MCGalaxy.Config;
|
using MCGalaxy.Config;
|
||||||
|
|
||||||
namespace MCGalaxy.Games {
|
namespace MCGalaxy.Games {
|
||||||
@ -69,6 +70,8 @@ namespace MCGalaxy.Games {
|
|||||||
[ConfigInt("game.capture.points-lose", null, 0)]
|
[ConfigInt("game.capture.points-lose", null, 0)]
|
||||||
public int Capture_PointsLost;
|
public int Capture_PointsLost;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary> Sets the default CTF config values for the given map. </summary>
|
||||||
public void SetDefaults(Level map) {
|
public void SetDefaults(Level map) {
|
||||||
ZDivider = map.Length / 2;
|
ZDivider = map.Length / 2;
|
||||||
RedFlagBlock = Block.red;
|
RedFlagBlock = Block.red;
|
||||||
@ -89,5 +92,24 @@ namespace MCGalaxy.Games {
|
|||||||
Capture_PointsGained = 10;
|
Capture_PointsGained = 10;
|
||||||
Capture_PointsLost = 10;
|
Capture_PointsLost = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static ConfigElement[] elems;
|
||||||
|
public void Retrieve(string mapName) {
|
||||||
|
if (elems == null) elems = ConfigElement.GetAll(typeof(CTFConfig));
|
||||||
|
PropertiesFile.Read("CTF/" + mapName + ".config", LineProcessor);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Save(string mapName) {
|
||||||
|
using (StreamWriter w = new StreamWriter("CTF/" + mapName + ".config")) {
|
||||||
|
ConfigElement.SerialisePlain(elems, w, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LineProcessor(string key, string value) {
|
||||||
|
if (!ConfigElement.Parse(elems, key, value, this)) {
|
||||||
|
Logger.Log(LogType.Warning, "\"{0}\" was not a recognised CTF config key.", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,12 +48,11 @@ namespace MCGalaxy.Games {
|
|||||||
|
|
||||||
CtfTeam2 red;
|
CtfTeam2 red;
|
||||||
CtfTeam2 blue;
|
CtfTeam2 blue;
|
||||||
Level map;
|
public Level map;
|
||||||
List<string> maps = new List<string>();
|
List<string> maps = new List<string>();
|
||||||
List<Data> cache = new List<Data>();
|
List<Data> cache = new List<Data>();
|
||||||
|
|
||||||
public CTFConfig Config = new CTFConfig();
|
public CTFConfig Config = new CTFConfig();
|
||||||
ConfigElement[] ctfConfigElems;
|
|
||||||
|
|
||||||
/// <summary> Create a new CTF object </summary>
|
/// <summary> Create a new CTF object </summary>
|
||||||
public CTFGame() {
|
public CTFGame() {
|
||||||
@ -95,15 +94,12 @@ namespace MCGalaxy.Games {
|
|||||||
File.Copy("CTF/maps/" + mapName + ".lvl", "levels/ctf.lvl");
|
File.Copy("CTF/maps/" + mapName + ".lvl", "levels/ctf.lvl");
|
||||||
CmdLoad.LoadLevel(null, "ctf");
|
CmdLoad.LoadLevel(null, "ctf");
|
||||||
map = LevelInfo.FindExact("ctf");
|
map = LevelInfo.FindExact("ctf");
|
||||||
LoadMapConfig();
|
UpdateConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LoadMapConfig() {
|
public void UpdateConfig() {
|
||||||
if (ctfConfigElems == null)
|
|
||||||
ctfConfigElems = ConfigElement.GetAll(typeof(CTFConfig));
|
|
||||||
|
|
||||||
Config.SetDefaults(map);
|
Config.SetDefaults(map);
|
||||||
PropertiesFile.Read("CTF/" + map.name + ".config", LineProcessor);
|
Config.Retrieve(map.name);
|
||||||
CTFConfig cfg = Config;
|
CTFConfig cfg = Config;
|
||||||
|
|
||||||
red.FlagBlock = ExtBlock.FromRaw(cfg.RedFlagBlock);
|
red.FlagBlock = ExtBlock.FromRaw(cfg.RedFlagBlock);
|
||||||
@ -115,12 +111,6 @@ namespace MCGalaxy.Games {
|
|||||||
blue.SpawnPos = new Position(cfg.BlueSpawnX, cfg.BlueSpawnY, cfg.BlueSpawnZ);
|
blue.SpawnPos = new Position(cfg.BlueSpawnX, cfg.BlueSpawnY, cfg.BlueSpawnZ);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineProcessor(string key, string value) {
|
|
||||||
if (!ConfigElement.Parse(ctfConfigElems, key, value, Config)) {
|
|
||||||
Logger.Log(LogType.Warning, "\"{0}\" was not a recognised CTF config key.", key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LoadConfig() {
|
bool LoadConfig() {
|
||||||
//Load some configs
|
//Load some configs
|
||||||
if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF");
|
if (!Directory.Exists("CTF")) Directory.CreateDirectory("CTF");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user