-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
4a22353
commit 5640e53
Showing
15 changed files
with
102 additions
and
99 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/eu/derzauberer/javautils/annotations/EventListener.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,13 @@ | ||
package eu.derzauberer.javautils.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import eu.derzauberer.javautils.util.Event.EventPriority; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface EventListener { | ||
public EventPriority priority() default EventPriority.NORMAL; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,59 @@ | ||
package eu.derzauberer.javautils.handler; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
|
||
import java.util.HashMap; | ||
import eu.derzauberer.javautils.annotations.EventListener; | ||
import eu.derzauberer.javautils.util.Event; | ||
import eu.derzauberer.javautils.util.EventExecuter; | ||
import eu.derzauberer.javautils.util.EventProfile; | ||
import eu.derzauberer.javautils.util.Listener; | ||
|
||
public class EventHandler { | ||
|
||
private static ArrayList<EventProfile> events = new ArrayList<>(); | ||
|
||
public static void registerEvent(Class<? extends Event> type, EventExecuter executer) { | ||
events.add(new EventProfile(type, executer)); | ||
private static HashMap<Method, Listener> methods = new HashMap<>(); | ||
|
||
public static void registerEvents(Listener listener) { | ||
for (Method method : listener.getClass().getDeclaredMethods()) { | ||
if (method.getAnnotation(EventListener.class) != null) { | ||
methods.put(method, listener); | ||
} | ||
} | ||
} | ||
|
||
public static void executeEvent(Class<? extends Event> type, Event event) { | ||
for (EventProfile profile : events) { | ||
if (profile.getType() == type) { | ||
profile.getExecuter().executeEvent(event); | ||
public static void executeEvent(Event event) { | ||
ArrayList<Method> highestPriority = new ArrayList<>(); | ||
ArrayList<Method> hightPriority = new ArrayList<>(); | ||
ArrayList<Method> normalPriority = new ArrayList<>(); | ||
ArrayList<Method> lowPriority = new ArrayList<>(); | ||
ArrayList<Method> lowestPriority = new ArrayList<>(); | ||
for (Method method : methods.keySet()) { | ||
for (Class<?> type : method.getParameterTypes()) { | ||
if (type == event.getClass()) { | ||
switch (method.getAnnotation(EventListener.class).priority()) { | ||
case HIGHEST: highestPriority.add(method); break; | ||
case HIGHT: hightPriority.add(method); break; | ||
case NORMAL: normalPriority.add(method); break; | ||
case LOW: lowPriority.add(method); break; | ||
case LOWEST: lowestPriority.add(method); break; | ||
default: break; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
highestPriority.forEach(method -> executeMethod(method, event)); | ||
hightPriority.forEach(method -> executeMethod(method, event)); | ||
normalPriority.forEach(method -> executeMethod(method, event)); | ||
lowPriority.forEach(method -> executeMethod(method, event)); | ||
lowestPriority.forEach(method -> executeMethod(method, event)); | ||
} | ||
|
||
private static void executeMethod(Method method, Event event) { | ||
try { | ||
method.invoke(methods.get(method), event); | ||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) { | ||
exception.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
package eu.derzauberer.javautils.util; | ||
|
||
public abstract class Event { | ||
|
||
} | ||
|
||
public enum EventPriority{HIGHEST, HIGHT, NORMAL, LOW, LOWEST}; | ||
|
||
private boolean cancelled; | ||
|
||
protected void setCancelled(boolean cancelled) { | ||
this.cancelled = cancelled; | ||
} | ||
|
||
public boolean isCancelled() { | ||
return cancelled; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,3 @@ | ||
package eu.derzauberer.javautils.util; | ||
|
||
public interface Listener {} |