Skip to content

Commit 1bce747

Browse files
add checking for run context with flags
1 parent ed064af commit 1bce747

6 files changed

Lines changed: 30 additions & 7 deletions

File tree

Code/FlagSystem/Flags/CustomCommandFlag.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,17 @@ public override void Unbind()
155155
}
156156
}
157157
}
158-
158+
159+
public override Result OnScriptRunning(Script scr)
160+
{
161+
if (scr.Context == RunContext.CustomCommand)
162+
{
163+
return true;
164+
}
165+
166+
return $"A script using '{Name}' flag cannot be ran by any other mean than a custom command.";
167+
}
168+
159169
public class CustomCommand : ICommand, IUsageProvider, IHelpProvider
160170
{
161171
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
@@ -246,7 +256,7 @@ public static Result RunAttachedScript(CustomCommand cmd, ScriptExecutor sender,
246256
script.AddLocalVariable(new LiteralVariable<TextValue>(name, new StaticTextValue(slice.Value)));
247257
}
248258

249-
script.Run(RunContext.Command);
259+
script.Run(RunContext.CustomCommand);
250260
return true;
251261
}
252262

Code/FlagSystem/Flags/OnEventFlag.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using JetBrains.Annotations;
2+
using SER.Code.Helpers.ResultSystem;
3+
using SER.Code.ScriptSystem;
24
using EventHandler = SER.Code.EventSystem.EventHandler;
35

46
namespace SER.Code.FlagSystem.Flags;
@@ -32,7 +34,17 @@ public class OnEventFlag : Flag
3234
},
3335
true
3436
);
35-
37+
38+
public override Result OnScriptRunning(Script scr)
39+
{
40+
if (scr.Context == RunContext.Event)
41+
{
42+
return true;
43+
}
44+
45+
return $"A script using '{Name}' flag cannot be ran by any other mean than an event.";
46+
}
47+
3648
public override Argument[] Arguments => [];
3749

3850
public override void Unbind()

Code/Plugin/CommandEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ public static void CaptureCommand(CommandExecutingEventArgs ev)
7474

7575
ev.Sender.Respond($"Running method '{methodName}'!");
7676
ev.IsAllowed = false;
77-
script.Run(RunContext.Command);
77+
script.Run(RunContext.BaseCommand);
7878
}
7979
}

Code/Plugin/Commands/MethodCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
2727
Executor = ScriptExecutor.Get(sender)
2828
};
2929

30-
script.Run(RunContext.Command);
30+
script.Run(RunContext.BaseCommand);
3131
response = "Method executed.";
3232
return true;
3333
}

Code/Plugin/Commands/RunCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
3232
return false;
3333
}
3434

35-
script.Run(RunContext.Command);
35+
script.Run(RunContext.BaseCommand);
3636
response = $"Script '{script.Name}' was requested to run";
3737
return true;
3838
}

Code/ScriptSystem/Script.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public enum RunContext
2727
Unknown,
2828
Script,
2929
Event,
30-
Command
30+
BaseCommand,
31+
CustomCommand
3132
}
3233

3334
public class Script

0 commit comments

Comments
 (0)