-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathButtonHandler.java
More file actions
47 lines (41 loc) · 1.65 KB
/
ButtonHandler.java
File metadata and controls
47 lines (41 loc) · 1.65 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
package uk.co.nyvil.bot.buttons.manager;
import lombok.Getter;
import uk.co.nyvil.bot.buttons.status.ButtonClickInfo;
import uk.co.nyvil.bot.commands.commands.PingCommand;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class ButtonHandler {
@Getter
public final Map<String, ButtonMethods> buttonMap = new HashMap<>();
/**
* Declare all classes that contain buttons in this class
*/
public ButtonHandler(){
registerButtons(PingCommand.class);
}
private void registerButtons(Object... objs) {
for (Object obj : objs) {
final Class<?> cls = obj.getClass();
Arrays.stream(cls.getDeclaredMethods())
.filter(method ->
(method.getModifiers() & (Modifier.PROTECTED | Modifier.PRIVATE)) == 0
&& method.isAnnotationPresent(ButtonAnnotation.class)
&& method.getParameterCount() == 1
&& method.getParameterTypes()[0] == ButtonClickInfo.class
)
.sorted(Comparator.comparing(Method::getName))
.forEach(method -> {
final String id = method.getAnnotation(ButtonAnnotation.class).id();
if (!id.isEmpty()) {
buttonMap.put(id, new ButtonMethods(cls, method));
} else {
System.err.println("No declared buttons found in " + cls.getName() + ".");
}
});
}
}
}