Fix items added by plugins to the economy not being loaded after restart, document stuff in Item

This commit is contained in:
UnknownShadow200 2016-09-11 23:34:12 +10:00
parent d6ff32a2d9
commit 9ecf404f69
10 changed files with 63 additions and 60 deletions

View File

@ -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) {

View File

@ -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;
}

View File

@ -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();

View File

@ -44,11 +44,9 @@ namespace MCGalaxy.Eco {
/// <summary> Writes the properties of this item to the economy.properties file. </summary>
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);
/// <summary> Called when the player does /buy [item name] &lt;value&gt; </summary>
protected internal abstract void OnBuyCommand(Player p, string message, string[] args);
/// <summary> Called when the player does /eco [item name] [option] &lt;value&gt; </summary>
protected internal abstract void OnSetupCommand(Player p, string[] args);
/// <summary> Called when the player does /eco help [item name] </summary>
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.");
}
/// <summary> Called when the player does /store </summary>
protected internal abstract void OnStoreOverview(Player p);
/// <summary> Called when the player does /store [item name] </summary>
protected internal abstract void OnStoreCommand(Player p);
}
@ -89,6 +94,8 @@ namespace MCGalaxy.Eco {
/// <summary> How much this item costs to purchase. </summary>
public int Price = 100;
/// <summary> Whether providing no arguments should be treated as
/// resetting the item for the player to its default value. </summary>
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);
}
}
}

View File

@ -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":

View File

@ -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.");

View File

@ -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; }

View File

@ -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) {

View File

@ -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.");

View File

@ -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);