-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
261 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/loohp/limbo/events/RegisteredCachedListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.loohp.limbo.events; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import com.loohp.limbo.plugins.LimboPlugin; | ||
|
||
public class RegisteredCachedListener { | ||
|
||
private LimboPlugin plugin; | ||
private Map<Class<? extends Event>, Map<EventPriority, List<Method>>> listeners; | ||
|
||
@SuppressWarnings("unchecked") | ||
public RegisteredCachedListener(LimboPlugin plugin, Listener listener) { | ||
this.plugin = plugin; | ||
this.listeners = new ConcurrentHashMap<>(); | ||
for (Method method : listener.getClass().getMethods()) { | ||
if (method.isAnnotationPresent(EventHandler.class) && method.getParameterCount() == 1 && Event.class.isAssignableFrom(method.getParameterTypes()[0])) { | ||
Class<? extends Event> eventClass = (Class<? extends Event>) method.getParameterTypes()[0]; | ||
listeners.putIfAbsent(eventClass, new ConcurrentHashMap<>()); | ||
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass); | ||
EventPriority priority = method.getAnnotation(EventHandler.class).priority(); | ||
mapping.putIfAbsent(priority, new ArrayList<>()); | ||
List<Method> list = mapping.get(priority); | ||
list.add(method); | ||
} | ||
} | ||
} | ||
|
||
public LimboPlugin getPlugin() { | ||
return plugin; | ||
} | ||
|
||
public List<Method> getListeners(Class<? extends Event> eventClass, EventPriority priority) { | ||
Map<EventPriority, List<Method>> mapping = listeners.get(eventClass); | ||
if (mapping == null) { | ||
return Collections.emptyList(); | ||
} | ||
List<Method> list = mapping.get(priority); | ||
return list == null ? Collections.emptyList() : Collections.unmodifiableList(list); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.