Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. package me.tojatta.api.event;
  2.  
  3. import me.tojatta.api.event.types.Event;
  4. import me.tojatta.api.management.MapManager;
  5.  
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8. import java.util.*;
  9.  
  10. /**
  11. * Created by Tojatta on 6/28/2016.
  12. */
  13. public final class EventSystem extends MapManager<Class<? extends Event>, EnumMap<Priority, HashSet<MethodData>>> {
  14.  
  15. private static EventSystem instance;
  16.  
  17. public EventSystem() {
  18. setup();
  19. }
  20.  
  21. @Override
  22. public void setup() {
  23. if (map == null) {
  24. map = new HashMap<>();
  25. }
  26. map.clear();
  27. }
  28.  
  29. public static EventSystem getInstance() {
  30. if (instance == null) {
  31. instance = new EventSystem();
  32. }
  33. return instance;
  34. }
  35.  
  36. public void callEvent(Event event) {
  37. EnumMap<Priority, HashSet<MethodData>> prioritizedMethodMap = map.get(event.getClass());
  38.  
  39. if (prioritizedMethodMap == null) {
  40. return;
  41. }
  42.  
  43. Arrays.stream(Priority.values())
  44. .filter(priority -> prioritizedMethodMap.get(priority) != null)
  45. .forEach(priority -> prioritizedMethodMap.get(priority).stream().forEach(methodData -> {
  46.  
  47. boolean accessible = methodData.getMethod().isAccessible();
  48.  
  49. methodData.getMethod().setAccessible(true);
  50.  
  51. try {
  52.  
  53. methodData.getMethod().invoke(methodData.getObject(), event);
  54.  
  55. } catch (IllegalAccessException | InvocationTargetException e) {
  56. e.printStackTrace();
  57. }
  58.  
  59. methodData.getMethod().setAccessible(accessible);
  60.  
  61. }));
  62. }
  63.  
  64. public void registerListener(Object object) {
  65.  
  66. Arrays.stream(object.getClass().getDeclaredMethods())
  67. .filter(method -> method.getAnnotation(EventListener.class) != null)
  68. .filter(method -> method.getParameterTypes().length != 0)
  69. .forEach(method -> handleRegister(method, object));
  70.  
  71. }
  72.  
  73. public void unregisterListener(Object object) {
  74. HashSet<MethodData> remove = new HashSet<>();
  75.  
  76. Arrays.stream(object.getClass().getDeclaredMethods())
  77. .forEach(method -> map.values().stream()
  78. .forEach(priorityHashSetEnumMap -> Arrays.stream(Priority.values())
  79. .filter(priority -> priorityHashSetEnumMap.get(priority) != null)
  80. .forEach(priority -> {
  81.  
  82. priorityHashSetEnumMap.get(priority).stream()
  83. .filter(methodData -> methodData.getMethod().toString().equals(method.toString()))
  84. .forEach(methodData -> remove.add(methodData));
  85.  
  86. remove.stream().forEach(methodData -> priorityHashSetEnumMap.get(priority).remove(methodData));
  87.  
  88. })));
  89.  
  90. }
  91.  
  92. private void handleRegister(Method method, Object object) {
  93. EventListener listener = method.getAnnotation(EventListener.class);
  94. if (!map.containsKey(listener.event())) {
  95. HashSet<MethodData> hashSet = new HashSet<>();
  96. EnumMap<Priority, HashSet<MethodData>> enumMap = new EnumMap<>(Priority.class);
  97. hashSet.add(new MethodData(method, object));
  98. enumMap.put(listener.priority(), hashSet);
  99. map.put(listener.event(), enumMap);
  100. } else {
  101. Map<Priority, HashSet<MethodData>> eventTypeMap = map.get(listener.event());
  102. if (!eventTypeMap.containsKey(listener.priority())) {
  103. HashSet<MethodData> hashSet = new HashSet<>();
  104. hashSet.add(new MethodData(method, object));
  105. eventTypeMap.put(listener.priority(), hashSet);
  106. } else {
  107. eventTypeMap.get(listener.priority()).add(new MethodData(method, object));
  108. }
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement