JsonEvent.java
3.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.mumfrey.liteloader.transformers.event.json;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import com.mumfrey.liteloader.transformers.event.Event;
import com.mumfrey.liteloader.transformers.event.InjectionPoint;
import com.mumfrey.liteloader.transformers.event.MethodInfo;
/**
* An event definition in JSON, serialisable class read by Gson
*
* @author Adam Mummery-Smith
*/
public class JsonEvent implements Serializable
{
private static final long serialVersionUID = 1L;
private static int nextEventID = 0;
/**
* Event name
*/
@SerializedName("name")
private String name;
/**
* Whether the event is cancellable
*/
@SerializedName("cancellable")
private boolean cancellable;
/**
* Event priority (relative to other events at the same injection point)
*/
@SerializedName("priority")
private int priority = 1000;
/**
* Injection points specified in the JSON file
*/
@SerializedName("injections")
private List<JsonInjection> jsonInjections;
/**
* Listeners defined in the JSON file
*/
@SerializedName("listeners")
private List<String> jsonListeners;
/**
* Listener methods parsed from the JSON
*/
private transient List<MethodInfo> listeners = new ArrayList<MethodInfo>();
/**
* Get the name of this event
*/
public String getName()
{
if (this.name == null)
{
this.name = "onUserEvent" + (JsonEvent.nextEventID++);
}
return this.name;
}
/**
* Get whether this event is cancellable or not
*/
public boolean isCancellable()
{
return this.cancellable;
}
/**
* Get the event priority
*/
public int getPriority()
{
return this.priority;
}
/**
* Get the list of listeners parsed from the JSON
*/
public List<MethodInfo> getListeners()
{
return this.listeners;
}
/**
* Parse the JSON to initialise this object
*/
public void parse(JsonMethods methods)
{
this.parseInjectionPoints(methods);
this.parseListeners(methods);
}
/**
* @param methods
*/
private void parseInjectionPoints(JsonMethods methods)
{
if (this.jsonInjections == null || this.jsonInjections.isEmpty())
{
throw new InvalidEventJsonException("Event " + this.getName() + " does not have any defined injections");
}
for (JsonInjection injection : this.jsonInjections)
{
injection.parse(methods);
}
}
/**
* @param methods
*/
private void parseListeners(JsonMethods methods)
{
if (this.jsonListeners == null || this.jsonListeners.isEmpty())
{
throw new InvalidEventJsonException("Event " + this.getName() + " does not have any defined listeners");
}
for (String listener : this.jsonListeners)
{
this.listeners.add(methods.get(listener));
}
}
/**
* @param transformer Transformer to register events with
* @return Event which was registered
*/
public Event register(ModEventInjectionTransformer transformer)
{
Event event = Event.getOrCreate(this.getName(), this.isCancellable(), this.getPriority());
for (JsonInjection injection : this.jsonInjections)
{
MethodInfo targetMethod = injection.getMethod();
InjectionPoint injectionPoint = injection.getInjectionPoint();
transformer.registerEvent(event, targetMethod, injectionPoint);
}
for (MethodInfo listener : this.listeners)
{
event.addListener(listener);
}
return event;
}
}