diff --git a/Economy/Economy.cs b/Economy/Economy.cs index 64122ab83..a335f8831 100644 --- a/Economy/Economy.cs +++ b/Economy/Economy.cs @@ -77,42 +77,48 @@ PRIMARY KEY(player) Server.s.Log("Economy properties don't exist, creating"); Save(); } + using (StreamReader r = new StreamReader("properties/economy.properties")) { string line; - while (!r.EndOfStream) { - line = r.ReadLine().ToLower().Trim(); - string[] linear = line.ToLower().Trim().Split(':'); + while ((line = r.ReadLine()) != null) { + line = line.ToLower().Trim(); try { - switch (linear[0]) { - case "enabled": - Enabled = linear[1].CaselessEq("true"); break; - default: - if (linear.Length < 3) break; - Item item = GetItem(linear[0]); - if (item != null) item.Parse(line, linear); - break; - } + ParseLine(line); } catch { } } - r.Close(); } Save(); } + + static void ParseLine(string line) { + string[] args = line.Split(':'); + if (args[0].CaselessEq("enabled")) { + Enabled = args[1].CaselessEq("true"); + } else if (args.Length >= 3) { + Item item = GetItem(args[0]); + if (item == null) return; + + if (args[1].CaselessEq("enabled")) + item.Enabled = args[2].CaselessEq("true"); + else if (args[1].CaselessEq("purchaserank")) + item.PurchaseRank = (LevelPermission)int.Parse(args[2]); + else + item.Parse(line, args); + } + } public static void Save() { using (StreamWriter w = new StreamWriter("properties/economy.properties", false)) { - //enabled w.WriteLine("enabled:" + Enabled); foreach (Item item in Items) { w.WriteLine(); item.Serialise(w); } - w.Close(); } } public static EcoStats RetrieveEcoStats(string playername) { - EcoStats es = default(EcoStats); + EcoStats es = default(EcoStats); es.playerName = playername; using (DataTable eco = Database.Fill("SELECT * FROM Economy WHERE player=@0", playername)) { if (eco.Rows.Count >= 1) { diff --git a/Economy/Item.cs b/Economy/Item.cs index 31e98d26f..6d1641174 100644 --- a/Economy/Item.cs +++ b/Economy/Item.cs @@ -39,8 +39,8 @@ namespace MCGalaxy.Eco { public bool Enabled; /// Reads the given property of this item from the economy.properties file. - /// split is line split by the : character. - public abstract void Parse(string line, string[] split); + /// args is line split by the : character. + public abstract void Parse(string line, string[] args); /// Writes the properties of this item to the economy.properties file. public abstract void Serialise(StreamWriter writer); @@ -48,7 +48,28 @@ namespace MCGalaxy.Eco { protected internal abstract void OnBuyCommand(Command cmd, Player p, string message, string[] args); - protected internal abstract void OnSetupCommand(Player p, string[] args); + internal void OnSetupCommand(Player p, string[] args) { + switch (args[1].ToLower()) { + case "enable": + Player.Message(p, "%aThe {0} item is now enabled.", Name); + Enabled = true; break; + case "disable": + Player.Message(p, "%aThe {0} item is now disabled.", Name); + Enabled = false; break; + case "purchaserank": + if (args.Length == 2) { Player.Message(p, "You need to provide a rank name."); return; } + Group grp = Group.FindMatches(p, args[2]); + if (grp == null) return; + + PurchaseRank = grp.Permission; + Player.Message(p, "Purchase rank for {0} item set to {1}%S.", Name, grp.ColoredName); + break; + default: + OnSetupCommandOther(p, args); break; + } + } + + protected internal abstract void OnSetupCommandOther(Player p, string[] args); protected internal abstract void OnStoreOverview(Player p); @@ -63,16 +84,9 @@ namespace MCGalaxy.Eco { protected bool NoArgsResetsItem; - public override void Parse(string line, string[] split) { - if (split.Length < 3) return; - - if (split[1].CaselessEq("enabled")) { - Enabled = split[2].CaselessEq("true"); - } else if (split[1].CaselessEq("purchaserank")) { - PurchaseRank = (LevelPermission)int.Parse(split[2]); - } else if (split[1].CaselessEq("price")) { - Price = int.Parse(split[2]); - } + public override void Parse(string line, string[] args) { + if (args[1].CaselessEq("price")) + Price = int.Parse(args[2]); } public override void Serialise(StreamWriter writer) { @@ -96,14 +110,8 @@ namespace MCGalaxy.Eco { protected abstract void OnBuyCommand(Player p, string message, string[] args); - protected internal override void OnSetupCommand(Player p, string[] args) { + protected internal override void OnSetupCommandOther(Player p, string[] args) { switch (args[1].ToLower()) { - case "enable": - Player.Message(p, "%aThe {0} item is now enabled.", Name); - Enabled = true; break; - case "disable": - Player.Message(p, "%aThe {0} item it now enabled.", Name); - Enabled = false; break; case "price": int cost; if (!int.TryParse(args[2], out cost)) { diff --git a/Economy/LevelItem.cs b/Economy/LevelItem.cs index 42294efe9..ec96e6dc4 100644 --- a/Economy/LevelItem.cs +++ b/Economy/LevelItem.cs @@ -39,26 +39,22 @@ namespace MCGalaxy.Eco { public string type; } - public override void Parse(string line, string[] split) { - if (split[1] == "enabled") { - Enabled = split[2].CaselessEq("true"); - } else if (split[1].CaselessEq("purchaserank")) { - PurchaseRank = (LevelPermission)int.Parse(split[2]); - } else if (split[1] == "levels") { - LevelPreset preset = FindPreset(split[2]); - if (preset == null) { - preset = new LevelPreset(); - Presets.Add(preset); - } - - switch (split[3]) { - case "name": preset.name = split[4]; break; - case "price": preset.price = int.Parse(split[4]); break; - case "x": preset.x = split[4]; break; - case "y": preset.y = split[4]; break; - case "z": preset.z = split[4]; break; - case "type": preset.type = split[4]; break; - } + public override void Parse(string line, string[] args) { + if (!args[1].CaselessEq("levels")) return; + + LevelPreset preset = FindPreset(args[2]); + if (preset == null) { + preset = new LevelPreset(); + Presets.Add(preset); + } + + switch (args[3]) { + case "name": preset.name = 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; } } @@ -117,7 +113,7 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, preset.price, "%3Map: %f" + preset.name); } - protected internal override void OnSetupCommand(Player p, string[] args) { + protected internal override void OnSetupCommandOther(Player p, string[] args) { LevelPreset preset = FindPreset(args[2]); switch (args[1].ToLower()) { case "new": @@ -213,14 +209,6 @@ namespace MCGalaxy.Eco { } break; - case "enable": - Player.Message(p, "%aMaps are now enabled for the economy system"); - Enabled = true; break; - - case "disable": - Player.Message(p, "%aMaps are now disabled for the economy system"); - Enabled = false; break; - default: Player.Message(p, "%cThat wasn't a valid command addition!"); break; diff --git a/Economy/RankItem.cs b/Economy/RankItem.cs index 6cb03855c..2f372cc84 100644 --- a/Economy/RankItem.cs +++ b/Economy/RankItem.cs @@ -39,21 +39,17 @@ namespace MCGalaxy.Eco { public int price = 1000; } - public override void Parse(string line, string[] split) { - if (split[1].CaselessEq("enabled")) { - Enabled = split[2].CaselessEq("true"); - } else if (split[1].CaselessEq("purchaserank")) { - PurchaseRank = (LevelPermission)int.Parse(split[2]); - } else if (split[1].CaselessEq("price")) { - Rank rnk = FindRank(split[2]); + public override void Parse(string line, string[] args) { + if (args[1].CaselessEq("price")) { + Rank rnk = FindRank(args[2]); if (rnk == null) { rnk = new Rank(); - rnk.group = Group.Find(split[2]); + rnk.group = Group.Find(args[2]); RanksList.Add(rnk); } - rnk.price = int.Parse(split[3]); - } else if (split[1] == "maxrank") { - if (Group.Exists(split[2])) MaxRank = split[2]; + rnk.price = int.Parse(args[3]); + } else if (args[1] == "maxrank") { + if (Group.Exists(args[2])) MaxRank = args[2]; } } @@ -87,14 +83,8 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, FindRank(p.group.name).price, "%3Rank: " + p.group.ColoredName); } - protected internal override void OnSetupCommand(Player p, string[] args) { + protected internal override void OnSetupCommandOther(Player p, string[] args) { switch (args[1].ToLower()) { - case "enable": - Player.Message(p, "%aThe {0} item is now enabled.", Name); - Enabled = true; break; - case "disable": - Player.Message(p, "%aThe {0} item is now disabled.", Name); - Enabled = false; break; case "price": Rank rnk = FindRank(args[2]); if (rnk == null) {