EventInfo.java
3.48 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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers.event;
import org.spongepowered.asm.lib.Type;
import com.mumfrey.liteloader.core.event.Cancellable;
import com.mumfrey.liteloader.core.event.EventCancellationException;
/**
* Contains information about an injected event, including the source object and
* whether the event is cancellable and/or cancelled.
*
* @author Adam Mummery-Smith
*
* @param <S> Source object type. For non-static methods this will be the
* containing object instance.
*/
public class EventInfo<S> implements Cancellable
{
protected static final String STRING = "Ljava/lang/String;";
protected static final String OBJECT = "Ljava/lang/Object;";
private final String name;
private final S source;
private final boolean cancellable;
private boolean cancelled;
public EventInfo(String name, S source, boolean cancellable)
{
this.name = name;
this.source = source;
this.cancellable = cancellable;
}
public S getSource()
{
return this.source;
}
public String getName()
{
return this.name;
}
protected String getSourceClass()
{
return this.source != null ? this.source.getClass().getSimpleName() : null;
}
@Override
public String toString()
{
return String.format("EventInfo(TYPE=%s,NAME=%s,SOURCE=%s,CANCELLABLE=%s)", this.getClass().getSimpleName(),
this.name, this.getSourceClass(), this.cancellable);
}
/* (non-Javadoc)
* @see com.mumfrey.liteloader.core.event.Cancellable#isCancellable()
*/
@Override
public final boolean isCancellable()
{
return this.cancellable;
}
/* (non-Javadoc)
* @see com.mumfrey.liteloader.transformers.event.Cancellable#isCancelled()
*/
@Override
public final boolean isCancelled()
{
return this.cancelled;
}
/* (non-Javadoc)
* @see com.mumfrey.liteloader.transformers.event.Cancellable#cancel()
*/
@Override
public void cancel() throws EventCancellationException
{
if (!this.cancellable)
{
throw new EventCancellationException(String.format("The event %s is not cancellable.", this.name));
}
this.cancelled = true;
}
protected static String getEventInfoClassName()
{
return EventInfo.class.getName();
}
/**
* @param returnType
*/
protected static String getEventInfoClassName(Type returnType)
{
return returnType.equals(Type.VOID_TYPE) ? EventInfo.class.getName() : ReturnEventInfo.class.getName();
}
public static String getConstructorDescriptor(Type returnType)
{
if (returnType.equals(Type.VOID_TYPE))
{
return EventInfo.getConstructorDescriptor();
}
if (returnType.getSort() == Type.OBJECT)
{
return String.format("(%s%sZ%s)V", EventInfo.STRING, EventInfo.OBJECT, EventInfo.OBJECT);
}
return String.format("(%s%sZ%s)V", EventInfo.STRING, EventInfo.OBJECT, returnType.getDescriptor());
}
public static String getConstructorDescriptor()
{
return String.format("(%s%sZ)V", EventInfo.STRING, EventInfo.OBJECT);
}
public static String getIsCancelledMethodName()
{
return "isCancelled";
}
public static String getIsCancelledMethodSig()
{
return "()Z";
}
}