Skip to content

Commit 66be5a5

Browse files
add base effect handling
1 parent 7fdfdd1 commit 66be5a5

8 files changed

Lines changed: 233 additions & 42 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using CustomPlayerEffects;
2+
using JetBrains.Annotations;
3+
using SER.Code.ArgumentSystem.BaseArguments;
4+
using SER.Code.Extensions;
5+
using SER.Code.Helpers.ResultSystem;
6+
using SER.Code.TokenSystem.Tokens;
7+
using SER.Code.TokenSystem.Tokens.Interfaces;
8+
using SER.Code.ValueSystem;
9+
10+
namespace SER.Code.ArgumentSystem.Arguments;
11+
12+
public class EffectTypeArgument(string name) : Argument(name)
13+
{
14+
public static readonly Type[] EffectTypes = typeof(StatusEffectBase).Assembly.GetTypes()
15+
.Where(t =>
16+
t.IsSubclassOf(typeof(StatusEffectBase)) &&
17+
!t.IsAbstract &&
18+
!typeof(IHolidayEffect).IsAssignableFrom(t)
19+
)
20+
.ToArray();
21+
22+
public static readonly Dictionary<string, Type> EffectNames = EffectTypes
23+
.ToDictionary(t => t.Name, t => t, StringComparer.OrdinalIgnoreCase);
24+
25+
public override string InputDescription =>
26+
"One of the following effects:\n"
27+
+ EffectNames.Keys.Select(n => $"> {n}").JoinStrings("\n");
28+
29+
[UsedImplicitly]
30+
public DynamicTryGet<Type> GetConvertSolution(BaseToken token)
31+
{
32+
if (!InternalConvert(token.GetBestTextRepresentation(Script)).HasErrored(out var error, out var type))
33+
{
34+
return type;
35+
}
36+
37+
if (token is not IValueToken valToken || valToken.CapableOf<LiteralValue>(out var get))
38+
{
39+
return error;
40+
}
41+
42+
return new(get().OnSuccess(lVal => InternalConvert(lVal.StringRep)));
43+
}
44+
45+
private static TryGet<Type> InternalConvert(string name)
46+
{
47+
if (EffectNames.TryGetValue(name, out var type))
48+
{
49+
return type;
50+
}
51+
52+
return "Value is not a valid effect name.";
53+
}
54+
}

Code/ArgumentSystem/ProvidedArguments.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ public string GetOption(string argName)
228228
return GetValue<string, OptionsArgument>(argName).ToLower();
229229
}
230230

231+
public Type GetEffectType(string argName)
232+
{
233+
return GetValue<Type, EffectTypeArgument>(argName);
234+
}
235+
231236
public CollectionVariable GetCollectionVariable(string argName)
232237
{
233238
return GetValue<CollectionVariable, CollectionVariableArgument>(argName);
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
1-
using Exiled.API.Enums;
2-
using Exiled.API.Features;
1+
using System.Reflection;
32
using JetBrains.Annotations;
3+
using LabApi.Features.Wrappers;
44
using SER.Code.ArgumentSystem.Arguments;
55
using SER.Code.ArgumentSystem.BaseArguments;
6-
using SER.Code.Helpers;
6+
using SER.Code.Extensions;
77
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8-
using SER.Code.MethodSystem.Structures;
98

109
namespace SER.Code.MethodSystem.Methods.EffectMethods;
1110

1211
[UsedImplicitly]
13-
public class ClearEffectMethod : SynchronousMethod, IDependOnFramework
12+
public class ClearEffectMethod : SynchronousMethod
1413
{
14+
private static readonly MethodInfo DisableEffectMethod =
15+
typeof(Player).GetMethod("DisableEffect", [])
16+
?? throw new Exception("Could not find EnableEffect method for Player");
17+
1518
public override string Description => "Removes the provided status effect from players.";
1619

1720
public override Argument[] ExpectedArguments =>
1821
[
1922
new PlayersArgument("players"),
20-
new EnumArgument<EffectType>("effect type")
21-
{ DefaultValue = new (null, "Removes all status effects") },
23+
new EffectTypeArgument("effect")
24+
{
25+
DefaultValue = new(null, "Removes all status effects")
26+
}
2227
];
23-
28+
2429
public override void Execute()
2530
{
2631
var players = Args.GetPlayers("players");
27-
var effectType = Args.GetNullableEnum<EffectType>("effect type");
32+
var effectType = Args.GetEffectType("effect").MaybeNull();
33+
34+
var disableEffect = DisableEffectMethod.MakeGenericMethod(effectType);
2835

29-
if (effectType.HasValue)
36+
if (effectType is null)
37+
{
3038
foreach (var plr in players)
31-
Player.Get(plr).DisableEffect(effectType.Value);
39+
{
40+
plr.DisableAllEffects();
41+
}
42+
}
3243
else
44+
{
3345
foreach (var plr in players)
34-
Player.Get(plr).DisableAllEffects();
46+
{
47+
disableEffect.Invoke(plr, null);
48+
}
49+
}
3550
}
36-
37-
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
3851
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Exiled.API.Enums;
2+
using Exiled.API.Features;
3+
using JetBrains.Annotations;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.Structures;
9+
10+
namespace SER.Code.MethodSystem.Methods.EffectMethods;
11+
12+
[UsedImplicitly]
13+
public class ClearExiledEffectMethod : SynchronousMethod, IDependOnFramework
14+
{
15+
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
16+
17+
public override string Description => "Removes the provided status effect from players.";
18+
19+
public override Argument[] ExpectedArguments =>
20+
[
21+
new PlayersArgument("players"),
22+
new EnumArgument<EffectType>("effect type")
23+
{
24+
DefaultValue = new(null, "Removes all status effects")
25+
}
26+
];
27+
28+
public override void Execute()
29+
{
30+
var players = Args.GetPlayers("players");
31+
var effectType = Args.GetNullableEnum<EffectType>("effect type");
32+
33+
if (effectType.HasValue)
34+
foreach (var plr in players)
35+
Player.Get(plr).DisableEffect(effectType.Value);
36+
else
37+
foreach (var plr in players)
38+
Player.Get(plr).DisableAllEffects();
39+
}
40+
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
using Exiled.API.Enums;
2-
using Exiled.API.Features;
1+
using System.Reflection;
32
using JetBrains.Annotations;
3+
using LabApi.Features.Wrappers;
44
using SER.Code.ArgumentSystem.Arguments;
55
using SER.Code.ArgumentSystem.BaseArguments;
6-
using SER.Code.Helpers;
76
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8-
using SER.Code.MethodSystem.Structures;
97

108
namespace SER.Code.MethodSystem.Methods.EffectMethods;
119

1210
[UsedImplicitly]
13-
public class GiveEffectMethod : SynchronousMethod, IDependOnFramework
11+
public class GiveEffectMethod : SynchronousMethod
1412
{
15-
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
13+
private static readonly MethodInfo EnableEffectMethod =
14+
typeof(Player).GetMethod("EnableEffect", [typeof(byte), typeof(float), typeof(bool)])
15+
?? throw new Exception("Could not find EnableEffect method for Player");
1616

17-
public override string Description => "Adds a provided effect to a player.";
17+
public override string Description => "Adds a provided effect for specified players.";
1818

19-
public override Argument[] ExpectedArguments =>
19+
public override Argument[] ExpectedArguments { get; } =
2020
[
2121
new PlayersArgument("players"),
22-
new EnumArgument<EffectType>("effect type"),
22+
new EffectTypeArgument("effect"),
2323
new DurationArgument("duration")
2424
{
2525
DefaultValue = new(TimeSpan.Zero, "infinite")
@@ -37,14 +37,16 @@ public class GiveEffectMethod : SynchronousMethod, IDependOnFramework
3737
public override void Execute()
3838
{
3939
var players = Args.GetPlayers("players");
40-
var effectType = Args.GetEnum<EffectType>("effect type");
40+
var effectType = Args.GetEffectType("effect");
4141
var duration = (float)Args.GetDuration("duration").TotalSeconds;
4242
var intensity = (byte)Args.GetInt("intensity");
4343
var addDurationIfActive = Args.GetBool("add duration if active");
44-
44+
45+
var method = EnableEffectMethod.MakeGenericMethod(effectType);
46+
4547
foreach (var plr in players)
4648
{
47-
Player.Get(plr).EnableEffect(effectType, intensity, duration, addDurationIfActive);
49+
method.Invoke(plr, [intensity, duration, addDurationIfActive]);
4850
}
4951
}
5052
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Exiled.API.Enums;
2+
using Exiled.API.Features;
3+
using JetBrains.Annotations;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.Structures;
9+
10+
namespace SER.Code.MethodSystem.Methods.EffectMethods;
11+
12+
[UsedImplicitly]
13+
public class GiveExiledEffectMethod : SynchronousMethod, IDependOnFramework
14+
{
15+
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
16+
17+
public override string Description => "Adds a provided effect to a player.";
18+
19+
public override Argument[] ExpectedArguments =>
20+
[
21+
new PlayersArgument("players"),
22+
new EnumArgument<EffectType>("effect type"),
23+
new DurationArgument("duration")
24+
{
25+
DefaultValue = new(TimeSpan.Zero, "infinite")
26+
},
27+
new IntArgument("intensity", 0, 255)
28+
{
29+
DefaultValue = new(1, null)
30+
},
31+
new BoolArgument("add duration if active")
32+
{
33+
DefaultValue = new(false, null)
34+
}
35+
];
36+
37+
public override void Execute()
38+
{
39+
var players = Args.GetPlayers("players");
40+
var effectType = Args.GetEnum<EffectType>("effect type");
41+
var duration = (float)Args.GetDuration("duration").TotalSeconds;
42+
var intensity = (byte)Args.GetInt("intensity");
43+
var addDurationIfActive = Args.GetBool("add duration if active");
44+
45+
foreach (var plr in players)
46+
{
47+
Player.Get(plr).EnableEffect(effectType, intensity, duration, addDurationIfActive);
48+
}
49+
}
50+
}
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
1-
using Exiled.API.Enums;
2-
using Exiled.API.Features;
31
using JetBrains.Annotations;
42
using SER.Code.ArgumentSystem.Arguments;
53
using SER.Code.ArgumentSystem.BaseArguments;
6-
using SER.Code.Helpers;
74
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8-
using SER.Code.MethodSystem.Structures;
95
using SER.Code.ValueSystem;
106

117
namespace SER.Code.MethodSystem.Methods.EffectMethods;
128

139
[UsedImplicitly]
14-
public class HasEffectMethod : LiteralValueReturningMethod, IDependOnFramework
10+
public class HasEffectMethod : ReturningMethod<BoolValue>
1511
{
16-
public override TypeOfValue LiteralReturnTypes => new SingleTypeOfValue(typeof(BoolValue));
17-
1812
public override string Description => "Returns true or false indicating if the player has the provided effect.";
1913

2014
public override Argument[] ExpectedArguments =>
2115
[
2216
new PlayerArgument("player"),
23-
new EnumArgument<EffectType>("effect type")
17+
new EffectTypeArgument("effect")
2418
];
2519

2620
public override void Execute()
2721
{
2822
var player = Args.GetPlayer("player");
29-
var effectType = Args.GetEnum<EffectType>("effect type");
30-
31-
if (!Player.Get(player).TryGetEffect(effectType, out var effect))
32-
ReturnValue = new BoolValue(false);
23+
var effectType = Args.GetEffectType("effect");
3324

34-
// this feels kinda stupid, but you never know...
35-
ReturnValue = new BoolValue(effect.IsEnabled);
25+
ReturnValue = player.ActiveEffects.Any(x => x.GetType() == effectType);
3626
}
37-
38-
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
3927
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Exiled.API.Enums;
2+
using Exiled.API.Features;
3+
using JetBrains.Annotations;
4+
using SER.Code.ArgumentSystem.Arguments;
5+
using SER.Code.ArgumentSystem.BaseArguments;
6+
using SER.Code.Helpers;
7+
using SER.Code.MethodSystem.BaseMethods.Synchronous;
8+
using SER.Code.MethodSystem.Structures;
9+
using SER.Code.ValueSystem;
10+
11+
namespace SER.Code.MethodSystem.Methods.EffectMethods;
12+
13+
[UsedImplicitly]
14+
public class HasExiledEffectMethod : LiteralValueReturningMethod, IDependOnFramework
15+
{
16+
public override TypeOfValue LiteralReturnTypes => new SingleTypeOfValue(typeof(BoolValue));
17+
18+
public override string Description => "Returns true or false indicating if the player has the provided effect.";
19+
20+
public override Argument[] ExpectedArguments =>
21+
[
22+
new PlayerArgument("player"),
23+
new EnumArgument<EffectType>("effect type")
24+
];
25+
26+
public override void Execute()
27+
{
28+
var player = Args.GetPlayer("player");
29+
var effectType = Args.GetEnum<EffectType>("effect type");
30+
31+
if (!Player.Get(player).TryGetEffect(effectType, out var effect))
32+
ReturnValue = new BoolValue(false);
33+
34+
// this feels kinda stupid, but you never know...
35+
ReturnValue = new BoolValue(effect.IsEnabled);
36+
}
37+
38+
public FrameworkBridge.Type DependsOn => FrameworkBridge.Type.Exiled;
39+
}

0 commit comments

Comments
 (0)