Skip to content

Commit

Permalink
Remove cmd.Enabled property and do the 'only allow use if Economy is …
Browse files Browse the repository at this point in the history
…currently enabled' in the actual commands instead. Fixes #762
  • Loading branch information
UnknownShadow200 committed Jun 3, 2023
1 parent 2c2c7d3 commit d16f74f
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 40 deletions.
16 changes: 4 additions & 12 deletions MCGalaxy/Commands/Command.Helpers.cs
Expand Up @@ -20,18 +20,10 @@
using MCGalaxy.Eco;
using MCGalaxy.Games;

namespace MCGalaxy {

public abstract partial class Command {

public static string GetDisabledReason(CommandEnable enable) {
if (enable == CommandEnable.Always) return null;
if (enable == CommandEnable.Economy && !Economy.Enabled)
return "economy is disabled.";

return null;
}

namespace MCGalaxy
{
public abstract partial class Command
{
protected bool CheckSuper(Player p, string message, string type) {
if (message.Length > 0 || !p.IsSuper) return false;
SuperRequiresArgs(p, type);
Expand Down
9 changes: 1 addition & 8 deletions MCGalaxy/Commands/Command.cs
Expand Up @@ -44,7 +44,6 @@ public abstract partial class Command
public virtual void Help(Player p, string message) { Help(p); Formatter.PrintCommandInfo(p, this); }

public virtual CommandPerm[] ExtraPerms { get { return null; } }
public virtual CommandEnable Enabled { get { return CommandEnable.Always; } }
public virtual CommandAlias[] Aliases { get { return null; } }

/// <summary> Whether this command is usable by 'super' players (Console, IRC, etc) </summary>
Expand Down Expand Up @@ -191,13 +190,7 @@ public abstract class Command2 : Command
Use(p, message, p.DefaultCmdData);
}
}

[Flags]
public enum CommandEnable
{
Always = 0, Economy = 1
}


public enum CommandParallelism
{
NoAndSilent, NoAndWarn, Yes
Expand Down
9 changes: 6 additions & 3 deletions MCGalaxy/Commands/Economy/CmdBalance.cs
Expand Up @@ -18,18 +18,21 @@
using System;
using MCGalaxy.Eco;

namespace MCGalaxy.Commands.Eco {
public sealed class CmdBalance : Command2 {
namespace MCGalaxy.Commands.Eco
{
public sealed class CmdBalance : Command2
{
public override string name { get { return "Balance"; } }
public override string shortcut { get { return "Money"; } }
public override string type { get { return CommandTypes.Economy; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }

public override void Use(Player p, string message, CommandData data) {
if (CheckSuper(p, message, "player name")) return;
if (message.Length == 0) message = p.name;
if (!Formatter.ValidPlayerName(p, message)) return;

if (!Economy.CheckIsEnabled(p, this)) return;

int matches = 1;
Player who = PlayerInfo.FindMatches(p, message, out matches);
if (matches > 1) return;
Expand Down
9 changes: 6 additions & 3 deletions MCGalaxy/Commands/Economy/CmdBuy.cs
Expand Up @@ -18,15 +18,18 @@
using System;
using MCGalaxy.Eco;

namespace MCGalaxy.Commands.Eco {
public sealed class CmdBuy : Command2 {
namespace MCGalaxy.Commands.Eco
{
public sealed class CmdBuy : Command2
{
public override string name { get { return "Buy"; } }
public override string shortcut { get { return "Purchase"; } }
public override string type { get { return CommandTypes.Economy; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public override bool SuperUseable { get { return false; } }

public override void Use(Player p, string message, CommandData data) {
if (!Economy.CheckIsEnabled(p, this)) return;

string[] parts = message.SplitSpaces(2);
Item item = Economy.GetItem(parts[0]);
if (item == null) { Help(p); return; }
Expand Down
5 changes: 3 additions & 2 deletions MCGalaxy/Commands/Economy/CmdStore.cs
Expand Up @@ -26,12 +26,13 @@ public sealed class CmdStore : Command2
public override string name { get { return "Store"; } }
public override string shortcut { get { return "Shop"; } }
public override string type { get { return CommandTypes.Economy; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }
public override CommandAlias[] Aliases {
get { return new[] { new CommandAlias("Item") }; }
}

public override void Use(Player p, string message, CommandData data) {
public override void Use(Player p, string message, CommandData data) {
if (!Economy.CheckIsEnabled(p, this)) return;

if (message.Length == 0 || IsListModifier(message)) {
Paginator.Output(p, Economy.GetEnabledItems(),
PrintItemOverview, "Store", "enabled Items", message);
Expand Down
9 changes: 6 additions & 3 deletions MCGalaxy/Commands/Economy/MoneyCmd.cs
Expand Up @@ -19,17 +19,20 @@
using MCGalaxy.Eco;
using MCGalaxy.Events.EconomyEvents;

namespace MCGalaxy.Commands.Eco {
public abstract class MoneyCmd : Command2 {
namespace MCGalaxy.Commands.Eco
{
public abstract class MoneyCmd : Command2
{
public override string type { get { return CommandTypes.Economy; } }
public override CommandEnable Enabled { get { return CommandEnable.Economy; } }

protected bool ParseArgs(Player p, string message, ref bool all,
string action, out EcoTransaction data) {
data = new EcoTransaction();
string[] args = message.SplitSpaces(3);
if (args.Length < 2) { Help(p); return false; }

if (!Economy.CheckIsEnabled(p, this)) return false;

data.TargetName = args[0];
data.Reason = args.Length > 2 ? args[2] : null;
data.Source = p;
Expand Down
6 changes: 2 additions & 4 deletions MCGalaxy/Commands/Information/CmdCommands.cs
Expand Up @@ -81,8 +81,7 @@ public sealed class CmdCommands : Command2
List<Command> cmds = new List<Command>();
foreach (Command c in Command.allCmds)
{
string disabled = Command.GetDisabledReason(c.Enabled);
if (disabled == null && c.Permissions.UsableBy(group.Permission)) cmds.Add(c);
if (c.Permissions.UsableBy(group.Permission)) cmds.Add(c);
}

if (cmds.Count == 0) {
Expand Down Expand Up @@ -123,11 +122,10 @@ public sealed class CmdCommands : Command2

foreach (Command c in Command.allCmds)
{
string disabled = Command.GetDisabledReason(c.Enabled);
string category = MapCategory(c.type);
if (!type.CaselessEq(category)) continue;

if (disabled == null && p.CanUse(c)) cmds.Add(c);
if (p.CanUse(c)) cmds.Add(c);
foundAny = true;
}
if (!foundAny) return false;
Expand Down
7 changes: 7 additions & 0 deletions MCGalaxy/Economy/Economy.cs
Expand Up @@ -27,6 +27,13 @@ public static partial class Economy
public static bool Enabled;
static Dictionary<string, List<string>> itemCfg = new Dictionary<string, List<string>>();

public static bool CheckIsEnabled(Player p, Command cmd) {
if (Economy.Enabled) return true;

p.Message("Cannot use &T/{0} &Scurrently as economy is disabled", cmd.name);
return false;
}

static List<string> GetConfig(string item) {
List<string> cfg;
if (itemCfg.TryGetValue(item, out cfg)) return cfg;
Expand Down
3 changes: 2 additions & 1 deletion MCGalaxy/Levels/Level.cs
Expand Up @@ -387,7 +387,8 @@ public struct UndoPos {
}

void LoadDefaultProps() {
for (int b = 0; b < Props.Length; b++) {
for (int b = 0; b < Props.Length; b++)
{
Props[b] = BlockProps.MakeDefault(Props, this, (BlockID)b);
}
}
Expand Down
4 changes: 0 additions & 4 deletions MCGalaxy/Player/Player.Handlers.cs
Expand Up @@ -631,10 +631,6 @@ public partial class Player : IDisposable
return null;
}

string reason = Command.GetDisabledReason(command.Enabled);
if (reason != null) {
Message("Command is disabled as " + reason); return null;
}
if (level != null && level.IsMuseum && !command.museumUsable) {
Message("Cannot use &T/{0} &Swhile in a museum.", command.name); return null;
}
Expand Down

0 comments on commit d16f74f

Please sign in to comment.