Skip to content

Commit

Permalink
Make /eat an economy item, so price can be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Jun 23, 2020
1 parent c7b557a commit 52253a7
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 30 deletions.
1 change: 1 addition & 0 deletions GUI/Settings/LevelProperties.cs
Expand Up @@ -134,6 +134,7 @@ public sealed class LevelProperties {

void SetAutoload(bool value) {
if (value) {
// Use AddOrReplace for backwards compatibility
Server.AutoloadMaps.AddOrReplace(lvl.name, lvl.physics.ToString());
} else {
Server.AutoloadMaps.Remove(lvl.name);
Expand Down
30 changes: 7 additions & 23 deletions MCGalaxy/Commands/Chat/CmdEat.cs
Expand Up @@ -17,39 +17,23 @@
*/
using System;
using MCGalaxy.Eco;
using MCGalaxy.Util;

namespace MCGalaxy.Commands.Chatting {
public sealed class CmdEat : MessageCmd {
public override string name { get { return "Eat"; } }

// Custom command, so can still be used even when economy is disabled
public override void Use(Player p, string message, CommandData data) {
if (DateTime.UtcNow < p.NextEat) {
p.Message("You're still full - you need to wait at least " +
"10 seconds between snacks."); return;
}
if (Economy.Enabled && p.money < 1) {
p.Message("You need to have at least 1 &3" + Server.Config.Currency +
" %Sto purchase a snack."); return;
}

TextFile eatFile = TextFile.Files["Eat"];
eatFile.EnsureExists();

string[] actions = eatFile.GetText();
string action = "ate some food";
if (actions.Length > 0)
action = actions[new Random().Next(actions.Length)];

if (!TryMessage(p, "λNICK %S" + action)) return;
p.NextEat = DateTime.UtcNow.AddSeconds(10);
if (Economy.Enabled)
p.SetMoney(p.money - 1);
Item item = Economy.GetItem("Snack");
item.OnBuyCommand(p, message, message.SplitSpaces());
}

public override void Help(Player p) {
SimpleItem item = (SimpleItem)Economy.GetItem("Snack");
p.Message("%T/Eat %H- Eats a random snack.");
p.Message("%HIf economy is enabled, costs 1 &3" + Server.Config.Currency);

if (item.Price == 0) return;
p.Message("%HCosts {0} &3{1} %Heach time", item.Price, Server.Config.Currency);
}
}
}
5 changes: 1 addition & 4 deletions MCGalaxy/Economy/Economy.cs
Expand Up @@ -89,7 +89,7 @@ public static partial class Economy {
new TitleItem(), new RankItem(), new LevelItem(), new LoginMessageItem(),
new LogoutMessageItem(), new BlocksItem(), new QueueLevelItem(),
new InfectMessageItem(), new NickItem(), new ReviveItem(),
new InvisibilityItem() };
new InvisibilityItem(), new SnackItem() };

/// <summary> Finds the item whose name or one of its aliases caselessly matches the input. </summary>
public static Item GetItem(string name) {
Expand All @@ -109,9 +109,6 @@ public static partial class Economy {
return items.Length == 0 ? "(no enabled items)" : items;
}

public static SimpleItem Color { get { return (SimpleItem)Items[0]; } }
public static SimpleItem TitleColor { get { return (SimpleItem)Items[1]; } }
public static SimpleItem Title { get { return (SimpleItem)Items[2]; } }
public static RankItem Ranks { get { return (RankItem)Items[3]; } }
public static LevelItem Levels { get { return (LevelItem)Items[4]; } }

Expand Down
13 changes: 10 additions & 3 deletions MCGalaxy/Economy/Item.cs
Expand Up @@ -114,15 +114,22 @@ public abstract class SimpleItem : Item {
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; }
if (p.money < Price) {
p.Message("%WYou don't have enough &3{1} %Wto buy a {0}.", Name, Server.Config.Currency); return;
}
// TODO: Move this into item stuff
if (!CheckPrice(p)) return;
DoPurchase(p, message, args);
}

Expand Down
58 changes: 58 additions & 0 deletions MCGalaxy/Economy/OtherItems.cs
@@ -0,0 +1,58 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using MCGalaxy.Eco;
using MCGalaxy.Util;

namespace MCGalaxy.Eco {

public sealed class SnackItem : SimpleItem {

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) {
if (DateTime.UtcNow < p.NextEat) {
p.Message("You're still full - you need to wait at least " +
"10 seconds between snacks."); return;
}

if (!CheckPrice(p)) return;
TextFile eatFile = TextFile.Files["Eat"];
eatFile.EnsureExists();

string[] actions = eatFile.GetText();
string action = "ate some food";
if (actions.Length > 0)
action = actions[new Random().Next(actions.Length)];

if (!p.CheckCanSpeak("eat a snack")) return;
Chat.MessageFrom(p, "λNICK %S" + action, null);
p.CheckForMessageSpam();

p.NextEat = DateTime.UtcNow.AddSeconds(10);
// intentionally not using Economy.MakePurchase here
p.SetMoney(p.money - Price);
}
}
}
1 change: 1 addition & 0 deletions MCGalaxy/MCGalaxy_.csproj
Expand Up @@ -462,6 +462,7 @@
<Compile Include="Drawing\Transform\Transform.cs" />
<Compile Include="Economy\Awards.cs" />
<Compile Include="Economy\Economy.DB.cs" />
<Compile Include="Economy\OtherItems.cs" />
<Compile Include="Economy\ZombieItems.cs" />
<Compile Include="Economy\Economy.cs" />
<Compile Include="Economy\Item.cs" />
Expand Down

0 comments on commit 52253a7

Please sign in to comment.