-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathEventFieldHandler.cs
More file actions
53 lines (45 loc) · 1.1 KB
/
Copy pathEventFieldHandler.cs
File metadata and controls
53 lines (45 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
namespace RPGCore.Events;
public class EventFieldHandler<TValue> : IEventFieldHandler, IHandlerUsedCallback
{
private IReadOnlyEventField<TValue>? eventField;
protected virtual void OnBeforeChanged(TValue oldValue)
{
}
protected virtual void OnAfterChanged(TValue newValue)
{
}
void IEventFieldHandler.OnBeforeChanged()
{
if (eventField != null)
{
OnBeforeChanged(eventField.Value);
}
}
void IEventFieldHandler.OnAfterChanged()
{
if (eventField != null)
{
OnAfterChanged(eventField.Value);
}
}
void IHandlerUsedCallback.OnUse(IReadOnlyEventField field)
{
if (field == null)
{
throw new ArgumentNullException(nameof(field), $"{nameof(field)} is null");
}
if (eventField != null)
{
throw new InvalidOperationException($"Event handler of type {GetType()} cannot be used on multiple EventFields.");
}
if (field is IReadOnlyEventField<TValue> castedField)
{
eventField = castedField;
}
else
{
throw new InvalidOperationException($"Event handler for type {typeof(TValue)} cannot be used on event field of type {field.GetType()}.");
}
}
}