diff --git a/MCGalaxy/Economy/Item.cs b/MCGalaxy/Economy/Item.cs index 9df29de59..aef2ca374 100644 --- a/MCGalaxy/Economy/Item.cs +++ b/MCGalaxy/Economy/Item.cs @@ -47,6 +47,27 @@ namespace MCGalaxy.Eco { /// Writes the properties of this item to the economy.properties file. public abstract void Serialise(StreamWriter writer); + + /// 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) { + p.Message("%T/Eco {0} enable/disable", Name.ToLower()); + p.Message("%HEnables/disables purchasing this item."); + p.Message("%T/Eco {0} purchaserank [rank]", Name.ToLower()); + p.Message("%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); + internal void Setup(Player p, string[] args) { string cmd = args[1]; @@ -68,32 +89,20 @@ namespace MCGalaxy.Eco { } } - protected void UseCommand(Player p, string cmd, string args) { + protected static void UseCommand(Player p, string cmd, string args) { CommandData data = default(CommandData); data.Rank = LevelPermission.Nobody; data.Context = CommandContext.Purchase; Command.Find(cmd).Use(p, args, data); } - /// 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) { - p.Message("%T/Eco {0} enable/disable", Name.ToLower()); - p.Message("%HEnables/disables purchasing this item."); - p.Message("%T/Eco {0} purchaserank [rank]", Name.ToLower()); - p.Message("%HSets the lowest rank which can purchase this item."); + protected static bool CheckPrice(Player p, int price, string item) { + if (p.money < price) { + p.Message("%WYou don't have enough &3{1} %Wto buy {0}.", item, Server.Config.Currency); + return false; + } + return true; } - - /// 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); } /// Simple item, in that it only has one cost value. @@ -102,9 +111,6 @@ namespace MCGalaxy.Eco { /// How much this item costs to purchase. public int Price = 100; - /// Whether providing no arguments is allowed. - protected bool AllowsNoArgs; - public override void Parse(string line, string[] args) { if (args[1].CaselessEq("price")) Price = int.Parse(args[2]); @@ -114,26 +120,7 @@ namespace MCGalaxy.Eco { writer.WriteLine(Name + ":price:" + Price); } - protected bool CheckPrice(Player p) { - if (p.money < Price) { - p.Message("%WYou don't have enough &3{1} %Wto buy a {0}.", Name, Server.Config.Currency); - return false; - } - return true; - } - - protected internal override void OnBuyCommand(Player p, string message, string[] args) { - if (AllowsNoArgs && args.Length == 1) { - DoPurchase(p, message, args); return; - } - // Must always provide an argument. - if (args.Length < 2) { OnStoreCommand(p); return; } - // TODO: Move this into item stuff - if (!CheckPrice(p)) return; - DoPurchase(p, message, args); - } - - protected abstract void DoPurchase(Player p, string message, string[] args); + protected bool CheckPrice(Player p) { return CheckPrice(p, Price, "a " + Name); } protected internal override void OnSetupCommand(Player p, string[] args) { if (args[1].CaselessEq("price")) { diff --git a/MCGalaxy/Economy/LevelItem.cs b/MCGalaxy/Economy/LevelItem.cs index b5816196b..9aa613fb1 100644 --- a/MCGalaxy/Economy/LevelItem.cs +++ b/MCGalaxy/Economy/LevelItem.cs @@ -75,11 +75,9 @@ namespace MCGalaxy.Eco { protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length < 2) { OnStoreCommand(p); return; } LevelPreset preset = FindPreset(args[1]); - if (preset == null) { p.Message("%WThat isn't a level preset"); return; } - if (p.money < preset.price) { - p.Message("%WYou don't have enough &3" + Server.Config.Currency + "%W to buy that map"); return; - } + if (preset == null) { p.Message("%WThat isn't a level preset"); return; } + if (!CheckPrice(p, preset.price, "that map")) return; string name = null; if (args.Length >= 3) { diff --git a/MCGalaxy/Economy/MessageItems.cs b/MCGalaxy/Economy/MessageItems.cs index d788b13e0..96f3b23cc 100644 --- a/MCGalaxy/Economy/MessageItems.cs +++ b/MCGalaxy/Economy/MessageItems.cs @@ -24,18 +24,18 @@ namespace MCGalaxy.Eco { public LoginMessageItem() { Aliases = new string[] { "login", "loginmsg", "loginmessage" }; - AllowsNoArgs = true; } public override string Name { get { return "LoginMessage"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length == 1) { PlayerDB.SetLoginMessage(p.name, ""); p.Message("&aYour login message was removed for free."); return; } + if (!CheckPrice(p)) return; string msg = message.SplitSpaces(2)[1]; // keep spaces this way if (msg == PlayerDB.GetLoginMessage(p)) { p.Message("%WYou already have that login message."); return; @@ -53,18 +53,18 @@ namespace MCGalaxy.Eco { public LogoutMessageItem() { Aliases = new string[] { "logout", "logoutmsg", "logoutmessage" }; - AllowsNoArgs = true; } public override string Name { get { return "LogoutMessage"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length == 1) { PlayerDB.SetLogoutMessage(p.name, ""); p.Message("&aYour logout message was removed for free."); return; - } + } + if (!CheckPrice(p)) return; string msg = message.SplitSpaces(2)[1]; // keep spaces this way if (msg == PlayerDB.GetLogoutMessage(p)) { p.Message("%WYou already have that logout message."); return; diff --git a/MCGalaxy/Economy/NameItems.cs b/MCGalaxy/Economy/NameItems.cs index adc48ac60..55dce9bdc 100644 --- a/MCGalaxy/Economy/NameItems.cs +++ b/MCGalaxy/Economy/NameItems.cs @@ -23,17 +23,17 @@ namespace MCGalaxy.Eco { public TitleItem() { Aliases = new string[] { "titles", "title" }; - AllowsNoArgs = true; } public override string Name { get { return "Title"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length == 1) { UseCommand(p, "Title", "-own"); p.Message("&aYour title was removed for free."); return; } + if (!CheckPrice(p)) return; string title = message.SplitSpaces(2)[1]; // keep spaces this way if (title == p.title) { p.Message("%WYou already have that title."); return; @@ -51,17 +51,17 @@ namespace MCGalaxy.Eco { public NickItem() { Aliases = new string[] { "nickname", "nick", "name" }; - AllowsNoArgs = true; } public override string Name { get { return "Nickname"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (args.Length == 1) { UseCommand(p, "Nick", "-own"); p.Message("&aYour nickname was removed for free."); return; } + if (!CheckPrice(p)) return; string nick = message.SplitSpaces(2)[1]; // keep spaces this way if (nick == p.DisplayName) { p.Message("%WYou already have that nickname."); return; @@ -83,11 +83,13 @@ namespace MCGalaxy.Eco { public override string Name { get { return "TitleColor"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { + if (args.Length < 2) { OnStoreCommand(p); return; } string color = Matcher.FindColor(p, args[1]); if (color == null) return; string colName = Colors.Name(color); + if (!CheckPrice(p)) return; if (color == p.titlecolor) { p.Message("%WYour title color is already " + color + colName); return; } @@ -105,11 +107,13 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Color"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { + if (args.Length < 2) { OnStoreCommand(p); return; } string color = Matcher.FindColor(p, args[1]); if (color == null) return; string colName = Colors.Name(color); + if (!CheckPrice(p)) return; if (color == p.color) { p.Message("%WYour color is already " + color + colName); return; } diff --git a/MCGalaxy/Economy/OtherItems.cs b/MCGalaxy/Economy/OtherItems.cs index dbcdbf999..172c5df43 100644 --- a/MCGalaxy/Economy/OtherItems.cs +++ b/MCGalaxy/Economy/OtherItems.cs @@ -25,13 +25,12 @@ namespace MCGalaxy.Eco { public SnackItem() { Aliases = new string[] { "snack" }; - AllowsNoArgs = true; Price = 0; } public override string Name { get { return "Snack"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (DateTime.UtcNow < p.NextEat) { p.Message("You're still full - you need to wait at least " + "10 seconds between snacks."); return; diff --git a/MCGalaxy/Economy/RankItem.cs b/MCGalaxy/Economy/RankItem.cs index 160ee36d8..417ada0e5 100644 --- a/MCGalaxy/Economy/RankItem.cs +++ b/MCGalaxy/Economy/RankItem.cs @@ -87,10 +87,8 @@ namespace MCGalaxy.Eco { RankEntry nextRank = NextRank(p); if (nextRank == null) { p.Message("%WYou are already at or past the max buyable rank"); return; - } - if (p.money < nextRank.Price) { - p.Message("%WYou don't have enough &3" + Server.Config.Currency + " %Wto buy the next rank"); return; } + if (!CheckPrice(p, nextRank.Price, "the next rank")) return; Group rank = Group.Find(nextRank.Perm); // TODO: What if null reference happens here Command.Find("SetRank").Use(Player.Console, p.name + " " + rank.Name); diff --git a/MCGalaxy/Economy/ZombieItems.cs b/MCGalaxy/Economy/ZombieItems.cs index 21158ebf7..a2fc3a16e 100644 --- a/MCGalaxy/Economy/ZombieItems.cs +++ b/MCGalaxy/Economy/ZombieItems.cs @@ -28,20 +28,16 @@ namespace MCGalaxy.Eco { public BlocksItem() { Aliases = new string[] { "blocks", "bl", "b" }; Price = 1; - AllowsNoArgs = true; } public override string Name { get { return "10Blocks"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { int count = 1; const string group = "Number of groups of 10 blocks"; if (args.Length >= 2 && !CommandParser.GetInt(p, args[1], group, ref count, 0, 10)) return; - if (p.money < Price * count) { - p.Message("%WYou don't have enough &3{2} %Wto buy {1} {0}.", - Name, count * 10, Server.Config.Currency); return; - } + if (!CheckPrice(p, count * Price, (count * 10) + " blocks")) return; ZSData data = ZSGame.Get(p); data.BlocksLeft += 10 * count; @@ -64,13 +60,16 @@ namespace MCGalaxy.Eco { public override string Name { get { return "QueueLevel"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { if (ZSGame.Instance.Picker.QueuedMap != null) { p.Message("Someone else has already queued a level."); return; } + + if (args.Length < 2) { OnStoreCommand(p); return; } string map = Matcher.FindMaps(p, args[1]); if (map == null) return; + if (!CheckPrice(p)) return; UseCommand(p, "Queue", "level " + map); Economy.MakePurchase(p, Price, "%3QueueLevel: " + map); } @@ -92,7 +91,8 @@ namespace MCGalaxy.Eco { public override string Name { get { return "InfectMessage"; } } - protected override void DoPurchase(Player p, string message, string[] args) { + protected internal override void OnBuyCommand(Player p, string message, string[] args) { + if (args.Length < 2) { OnStoreCommand(p); return; } string text = message.SplitSpaces(2)[1]; // keep spaces this way bool hasAToken = false; for (int i = 0; i < text.Length; i++) { @@ -105,6 +105,7 @@ namespace MCGalaxy.Eco { "and/or a \"{1}\" (placeholder for human player) in the infect message."); return; } + if (!CheckPrice(p)) return; ZSData data = ZSGame.Get(p); if (data.InfectMessages == null) data.InfectMessages = new List(); data.InfectMessages.Add(text); @@ -134,9 +135,7 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Invisibility"; } } protected internal override void OnBuyCommand(Player p, string message, string[] args) { - if (p.money < Price) { - p.Message("%WYou don't have enough &3{1} %Wto buy an {0}.", Name, Server.Config.Currency); return; - } + if (!CheckPrice(p, Price, "an invisibility potion")) return; if (!ZSGame.Instance.Running || !ZSGame.Instance.RoundInProgress) { p.Message("You can only buy an invisiblity potion " + "when a round of zombie survival is in progress."); return; @@ -165,8 +164,6 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, Price, "%3Invisibility: " + duration); } - protected override void DoPurchase(Player p, string message, string[] args) { } - protected internal override void OnStoreCommand(Player p) { p.Message("%T/Buy " + Name); OutputItemInfo(p); @@ -188,11 +185,9 @@ namespace MCGalaxy.Eco { public override string Name { get { return "Revive"; } } protected internal override void OnBuyCommand(Player p, string message, string[] args) { - if (p.money < Price) { - p.Message("%WYou don't have enough &3" + Server.Config.Currency + "%W to buy a " + Name + "."); return; - } + if (!CheckPrice(p, Price, "a revive potion")) return; if (!ZSGame.Instance.Running || !ZSGame.Instance.RoundInProgress) { - p.Message("You can only buy an revive potion " + + p.Message("You can only buy a revive potion " + "when a round of zombie survival is in progress."); return; } @@ -222,8 +217,6 @@ namespace MCGalaxy.Eco { Economy.MakePurchase(p, Price, "%3Revive:"); } - protected override void DoPurchase(Player p, string message, string[] args) { } - protected internal override void OnStoreCommand(Player p) { int time = ZSGame.Config.ReviveNoTime, expiry = ZSGame.Config.ReviveTooSlow; int potions = ZSGame.Config.ReviveTimes;