From 9ecf404f690f7c98c5b67e16ab8550d282dd799b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 11 Sep 2016 23:34:12 +1000 Subject: [PATCH] Fix items added by plugins to the economy not being loaded after restart, document stuff in Item --- Commands/Economy/CmdBuy.cs | 2 +- Commands/Economy/CmdEconomy.cs | 2 +- Economy/Economy.cs | 12 ++++--- Economy/Item.cs | 59 ++++++++++++++++++---------------- Economy/LevelItem.cs | 7 ++-- Economy/MessageItems.cs | 4 +-- Economy/NameItems.cs | 8 ++--- Economy/RankItem.cs | 11 +++---- Economy/ReviveItem.cs | 7 ++-- Economy/ZombieItems.cs | 11 +++---- 10 files changed, 63 insertions(+), 60 deletions(-) diff --git a/Commands/Economy/CmdBuy.cs b/Commands/Economy/CmdBuy.cs index 514983a05..41361e609 100644 --- a/Commands/Economy/CmdBuy.cs +++ b/Commands/Economy/CmdBuy.cs @@ -40,7 +40,7 @@ namespace MCGalaxy.Commands { if (p.Rank < item.PurchaseRank) { Formatter.MessageNeedMinPerm(p, "purchase a " + item.Name, item.PurchaseRank); return; } - item.OnBuyCommand(this, p, message, parts); + item.OnBuyCommand(p, message, parts); } public override void Help(Player p) { diff --git a/Commands/Economy/CmdEconomy.cs b/Commands/Economy/CmdEconomy.cs index fa28b8faf..0435fc3ed 100644 --- a/Commands/Economy/CmdEconomy.cs +++ b/Commands/Economy/CmdEconomy.cs @@ -56,7 +56,7 @@ namespace MCGalaxy.Commands { } else { Item item = Economy.GetItem(args[0]); if (item != null) { - item.OnSetupCommand(p, args); + item.Setup(p, args); Economy.Save(); return; } diff --git a/Economy/Economy.cs b/Economy/Economy.cs index 3d1f1822e..d9b94a9e0 100644 --- a/Economy/Economy.cs +++ b/Economy/Economy.cs @@ -29,6 +29,7 @@ namespace MCGalaxy { public static bool Enabled; + const string propertiesFile = "properties/economy.properties"; const string createTable = @"CREATE TABLE if not exists Economy ( player VARCHAR(20), @@ -78,21 +79,22 @@ PRIMARY KEY(player) } public static void Load() { - if (!File.Exists("properties/economy.properties")) { + if (!File.Exists(propertiesFile)) { Server.s.Log("Economy properties don't exist, creating"); Save(); } - using (StreamReader r = new StreamReader("properties/economy.properties")) { + using (StreamReader r = new StreamReader(propertiesFile)) { string line; while ((line = r.ReadLine()) != null) { line = line.ToLower().Trim(); try { ParseLine(line); - } catch { } + } catch (Exception ex) { + Server.ErrorLog(ex); + } } } - Save(); } static void ParseLine(string line) { @@ -113,7 +115,7 @@ PRIMARY KEY(player) } public static void Save() { - using (StreamWriter w = new StreamWriter("properties/economy.properties", false)) { + using (StreamWriter w = new StreamWriter(propertiesFile, false)) { w.WriteLine("enabled:" + Enabled); foreach (Item item in Items) { w.WriteLine(); diff --git a/Economy/Item.cs b/Economy/Item.cs index 9415dc77d..6b6a6fab5 100644 --- a/Economy/Item.cs +++ b/Economy/Item.cs @@ -44,11 +44,9 @@ namespace MCGalaxy.Eco { /// Writes the properties of this item to the economy.properties file. public abstract void Serialise(StreamWriter writer); + - protected internal abstract void OnBuyCommand(Command cmd, Player p, - string message, string[] args); - - internal void OnSetupCommand(Player p, string[] args) { + internal void Setup(Player p, string[] args) { switch (args[1].ToLower()) { case "enable": Player.Message(p, "%aThe {0} item is now enabled.", Name); @@ -62,15 +60,20 @@ namespace MCGalaxy.Eco { if (grp == null) return; PurchaseRank = grp.Permission; - Player.Message(p, "Purchase rank for {0} item set to {1}%S.", Name, grp.ColoredName); + Player.Message(p, "Min purchase rank for {0} item set to {1}%S.", Name, grp.ColoredName); break; default: - OnSetupCommandOther(p, args); break; + OnSetupCommand(p, args); break; } } - protected internal abstract void OnSetupCommandOther(Player p, string[] args); + /// Called when the player does /buy [item name] <value> + protected internal abstract void OnBuyCommand(Player p, string message, string[] args); + /// Called when the player does /eco [item name] [option] <value> + protected internal abstract void OnSetupCommand(Player p, string[] args); + + /// Called when the player does /eco help [item name] protected internal virtual void OnSetupCommandHelp(Player p) { Player.Message(p, "%T/eco {0} enable/disable", Name.ToLower()); Player.Message(p, "%HEnables/disables purchasing this item."); @@ -78,8 +81,10 @@ namespace MCGalaxy.Eco { Player.Message(p, "%HSets the lowest rank which can purchase this item."); } + /// Called when the player does /store protected internal abstract void OnStoreOverview(Player p); + /// Called when the player does /store [item name] protected internal abstract void OnStoreCommand(Player p); } @@ -89,6 +94,8 @@ namespace MCGalaxy.Eco { /// How much this item costs to purchase. public int Price = 100; + /// Whether providing no arguments should be treated as + /// resetting the item for the player to its default value. protected bool NoArgsResetsItem; public override void Parse(string line, string[] args) { @@ -102,33 +109,31 @@ namespace MCGalaxy.Eco { writer.WriteLine(Name + ":purchaserank:" + (int)PurchaseRank); } - protected internal override void OnBuyCommand(Command cmd, Player p, - string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (NoArgsResetsItem && args.Length == 1) { - OnBuyCommand(p, message, args); return; + DoPurchase(p, message, args); return; } // Must always provide an argument. - if (args.Length < 2) { cmd.Help(p); return; } + if (args.Length < 2) { OnStoreCommand(p); return; } if (p.money < Price) { Player.Message(p, "%cYou don't have enough &3{1}&c to buy a {0}.", Name, Server.moneys); return; } - OnBuyCommand(p, message, args); + DoPurchase(p, message, args); } - protected abstract void OnBuyCommand(Player p, string message, string[] args); + protected abstract void DoPurchase(Player p, string message, string[] args); - protected internal override void OnSetupCommandOther(Player p, string[] args) { - switch (args[1].ToLower()) { - case "price": - int cost; - if (!int.TryParse(args[2], out cost)) { - Player.Message(p, "\"{0}\" is not a valid integer.", args[2]); return; - } - - Player.Message(p, "%aSuccessfully changed the {0} price to &f{1} &3{2}", Name, cost, Server.moneys); - Price = cost; break; - default: - Player.Message(p, "Supported actions: enable, disable, price [cost]"); break; + protected internal override void OnSetupCommand(Player p, string[] args) { + if (args[1].CaselessEq("price")) { + int cost; + if (!int.TryParse(args[2], out cost)) { + Player.Message(p, "\"{0}\" is not a valid integer.", args[2]); return; + } + + Player.Message(p, "Changed price of {0} item to &f{1} &3{2}", Name, cost, Server.moneys); + Price = cost; + } else { + Player.Message(p, "Supported actions: enable, disable, price [cost]"); } } @@ -149,8 +154,8 @@ namespace MCGalaxy.Eco { } protected internal override void OnStoreCommand(Player p) { - Player.Message(p, "Syntax: %T/buy {0} [value]", Name); - Player.Message(p, "Costs &f{0} &3{1} %Seach time the item is bought.", Price, Server.moneys); + Player.Message(p, "%T/buy {0} [value]", Name); + Player.Message(p, "%hCosts &f{0} &3{1} %Seach time the item is bought.", Price, Server.moneys); } } } diff --git a/Economy/LevelItem.cs b/Economy/LevelItem.cs index 710a689d4..b8bffbad2 100644 --- a/Economy/LevelItem.cs +++ b/Economy/LevelItem.cs @@ -74,9 +74,8 @@ namespace MCGalaxy.Eco { } } - protected internal override void OnBuyCommand(Command cmd, Player p, - string message, string[] args) { - if (args.Length < 3) { cmd.Help(p); return; } + protected internal override void OnBuyCommand(Player p, string message, string[] args) { + if (args.Length < 3) { OnStoreCommand(p); return; } LevelPreset preset = FindPreset(args[1]); if (preset == null) { Player.Message(p, "%cThat isn't a level preset"); return; } @@ -112,7 +111,7 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, preset.price, "%3Map: %f" + preset.name); } - protected internal override void OnSetupCommandOther(Player p, string[] args) { + protected internal override void OnSetupCommand(Player p, string[] args) { LevelPreset preset = FindPreset(args[2]); switch (args[1].ToLower()) { case "new": diff --git a/Economy/MessageItems.cs b/Economy/MessageItems.cs index 3dcfe8e48..77512f8f7 100644 --- a/Economy/MessageItems.cs +++ b/Economy/MessageItems.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "LoginMessage"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (args.Length == 1) { Command.all.Find("loginmessage").Use(null, p.name + " joined the game."); Player.Message(p, "%aYour login message was removed for free."); @@ -57,7 +57,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "LogoutMessage"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (args.Length == 1) { Command.all.Find("logoutmessage").Use(null, p.name + " disconnected"); Player.Message(p, "%aYour logout message was removed for free."); diff --git a/Economy/NameItems.cs b/Economy/NameItems.cs index 8dd003c54..96240cd72 100644 --- a/Economy/NameItems.cs +++ b/Economy/NameItems.cs @@ -28,7 +28,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Title"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (args.Length == 1) { Command.all.Find("title").Use(null, p.name); Player.Message(p, "%aYour title was removed for free."); return; @@ -57,7 +57,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Nickname"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (args.Length == 1) { Command.all.Find("nick").Use(null, p.name); Player.Message(p, "%aYour nickname was removed for free."); return; @@ -85,7 +85,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "TitleColor"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (!args[1].StartsWith("&") || !args[1].StartsWith("%")) { args[1] = Colors.Parse(args[1]); if (args[1] == "") { Player.Message(p, "%cThat wasn't a color"); return; } @@ -108,7 +108,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Color"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (!args[1].StartsWith("&") || !args[1].StartsWith("%")) { args[1] = Colors.Parse(args[1]); if (args[1] == "") { Player.Message(p, "%cThat wasn't a color"); return; } diff --git a/Economy/RankItem.cs b/Economy/RankItem.cs index eb8d62f8b..d88e86de1 100644 --- a/Economy/RankItem.cs +++ b/Economy/RankItem.cs @@ -64,8 +64,7 @@ namespace MCGalaxy.Eco { } } - protected internal override void OnBuyCommand(Command cmd, Player p, - string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length >= 2) { Player.Message(p, "%cYou cannot provide a rank name, use %a/buy rank %cto buy the NEXT rank."); return; } @@ -79,11 +78,11 @@ namespace MCGalaxy.Eco { } Command.all.Find("setrank").Use(null, "+up " + p.name); - Player.Message(p, "%aYou've successfully bought the rank " + p.group.ColoredName); + Player.Message(p, "You bought the rank " + p.group.ColoredName); Economy.MakePurchase(p, FindRank(p.group.name).price, "%3Rank: " + p.group.ColoredName); } - protected internal override void OnSetupCommandOther(Player p, string[] args) { + protected internal override void OnSetupCommand(Player p, string[] args) { switch (args[1].ToLower()) { case "price": Rank rnk = FindRank(args[2]); @@ -133,8 +132,8 @@ namespace MCGalaxy.Eco { protected internal override void OnStoreCommand(Player p) { Group maxrank = Group.Find(MaxRank); - Player.Message(p, "Syntax: %T/buy rankup"); - Player.Message(p, "%fThe max buyable rank is: " + maxrank.ColoredName); + Player.Message(p, "%T/buy rankup"); + Player.Message(p, "%fThe highest buyable rank is: " + maxrank.ColoredName); Player.Message(p, "%cYou can only buy ranks one at a time, in sequential order."); foreach (Rank rnk in RanksList) { diff --git a/Economy/ReviveItem.cs b/Economy/ReviveItem.cs index a59bdc6d0..d4af7a75a 100644 --- a/Economy/ReviveItem.cs +++ b/Economy/ReviveItem.cs @@ -30,8 +30,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Revive"; } } - protected internal override void OnBuyCommand(Command cmd, Player p, - string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (p.money < Price) { Player.Message(p, "&cYou don't have enough &3" + Server.moneys + "&c to buy a " + Name + "."); return; } @@ -69,13 +68,13 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, Price, "%3Revive:"); } - protected override void OnBuyCommand(Player p, string message, string[] args) { } + protected override void DoPurchase(Player p, string message, string[] args) { } protected internal override void OnStoreCommand(Player p) { base.OnStoreCommand(p); int time = ZombieGame.ReviveNoTime, expiry = ZombieGame.ReviveTooSlow; int potions = ZombieGame.ReviveTimes; - Player.Message(p, "Syntax: %T/buy " + Name); + Player.Message(p, "%T/buy " + Name); Player.Message(p, "%HCannot be used in the last &a" + time + " %Hseconds of a round."); Player.Message(p, "%HCan only be used within &a" + expiry + " %Hseconds after being infected."); Player.Message(p, "%HYou can only buy &a" + potions + " %Hrevive potions per round."); diff --git a/Economy/ZombieItems.cs b/Economy/ZombieItems.cs index c51aade6e..d8584b94d 100644 --- a/Economy/ZombieItems.cs +++ b/Economy/ZombieItems.cs @@ -30,7 +30,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "10Blocks"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { byte count = 1; if (args.Length >= 2 && !byte.TryParse(args[1], out count) || count == 0 || count > 10) { Player.Message(p, "Number of groups of 10 blocks to buy must be an integer between 1 and 10."); return; @@ -60,7 +60,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "QueueLevel"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { if (Server.zombie.QueuedLevel != null) { Player.Message(p, "Someone else has already queued a level."); return; } @@ -81,7 +81,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "InfectMessage"; } } - protected override void OnBuyCommand(Player p, string message, string[] args) { + protected override void DoPurchase(Player p, string message, string[] args) { string text = message.SplitSpaces(2)[1]; // keep spaces this way bool hasAToken = false; for (int i = 0; i < text.Length; i++) { @@ -116,8 +116,7 @@ namespace MCGalaxy.Eco { protected abstract int Duration { get; } protected abstract bool ForHumans { get; } - protected internal override void OnBuyCommand(Command cmd, Player p, - string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (p.money < Price) { Player.Message(p, "%cYou don't have enough &3{1} &c to buy a {0}.", Name, Server.moneys); return; } @@ -153,7 +152,7 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, Price, "%3Invisibility: " + Duration); } - protected override void OnBuyCommand(Player p, string message, string[] args) { } + protected override void DoPurchase(Player p, string message, string[] args) { } protected internal override void OnStoreCommand(Player p) { base.OnStoreCommand(p);