diff --git a/src/eu/derzauberer/javautils/annotations/EventListener.java b/src/eu/derzauberer/javautils/annotations/EventListener.java new file mode 100644 index 0000000..5e66afa --- /dev/null +++ b/src/eu/derzauberer/javautils/annotations/EventListener.java @@ -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; +} diff --git a/src/eu/derzauberer/javautils/annotations/JsonElement.java b/src/eu/derzauberer/javautils/annotations/JsonElement.java index 8e9eca0..79f1469 100644 --- a/src/eu/derzauberer/javautils/annotations/JsonElement.java +++ b/src/eu/derzauberer/javautils/annotations/JsonElement.java @@ -8,6 +8,5 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface JsonElement { - public String key() default ""; } diff --git a/src/eu/derzauberer/javautils/events/ClientMessageRecieveEvent.java b/src/eu/derzauberer/javautils/events/ClientMessageRecieveEvent.java index d918e5a..5641b95 100644 --- a/src/eu/derzauberer/javautils/events/ClientMessageRecieveEvent.java +++ b/src/eu/derzauberer/javautils/events/ClientMessageRecieveEvent.java @@ -5,22 +5,16 @@ public class ClientMessageRecieveEvent extends Event { - private boolean cancled; private Client client; private String message; public ClientMessageRecieveEvent(Client client, String message) { - this.cancled = false; this.client = client; this.message = message; } - public void setCancled(boolean cancled) { - this.cancled = cancled; - } - - public boolean isCancled() { - return cancled; + public void setCancled(boolean cancelled) { + this.setCancelled(cancelled); } public Client getClient() { diff --git a/src/eu/derzauberer/javautils/events/ClientMessageSendEvent.java b/src/eu/derzauberer/javautils/events/ClientMessageSendEvent.java index a2decab..da24555 100644 --- a/src/eu/derzauberer/javautils/events/ClientMessageSendEvent.java +++ b/src/eu/derzauberer/javautils/events/ClientMessageSendEvent.java @@ -5,22 +5,16 @@ public class ClientMessageSendEvent extends Event { - private boolean cancled; private Client client; private String message; public ClientMessageSendEvent(Client client, String message) { - this.cancled = false; this.client = client; this.message = message; } - public void setCancled(boolean cancled) { - this.cancled = cancled; - } - - public boolean isCancled() { - return cancled; + public void setCancelled(boolean cancelled) { + this.setCancelled(cancelled); } public Client getClient() { diff --git a/src/eu/derzauberer/javautils/events/CommandPreProcessingEvent.java b/src/eu/derzauberer/javautils/events/CommandPreProcessingEvent.java index 91b10fc..38ba395 100644 --- a/src/eu/derzauberer/javautils/events/CommandPreProcessingEvent.java +++ b/src/eu/derzauberer/javautils/events/CommandPreProcessingEvent.java @@ -6,7 +6,6 @@ public class CommandPreProcessingEvent extends Event { - private boolean cancled; private Console console; private Command command; private String string; @@ -14,7 +13,6 @@ public class CommandPreProcessingEvent extends Event { private String args[]; public CommandPreProcessingEvent(Console console, Command command, String string, String label, String args[]) { - this.cancled = false; this.console = console; this.command = command; this.string = string; @@ -22,12 +20,8 @@ public CommandPreProcessingEvent(Console console, Command command, String string this.args = args; } - public void setCancled(boolean cancled) { - this.cancled = cancled; - } - - public boolean isCancled() { - return cancled; + public void setCancelled(boolean cancelled) { + this.setCancelled(cancelled); } public Console getConsole() { diff --git a/src/eu/derzauberer/javautils/events/ConsoleInputEvent.java b/src/eu/derzauberer/javautils/events/ConsoleInputEvent.java index 36f317d..822df45 100644 --- a/src/eu/derzauberer/javautils/events/ConsoleInputEvent.java +++ b/src/eu/derzauberer/javautils/events/ConsoleInputEvent.java @@ -5,22 +5,16 @@ public class ConsoleInputEvent extends Event { - private boolean cancled; private Console console; private String input; public ConsoleInputEvent(Console console, String input) { - this.cancled = false; this.console = console; this.input = input; } - public void setCancled(boolean cancled) { - this.cancled = cancled; - } - - public boolean isCancled() { - return cancled; + public void setCancelled(boolean cancelled) { + this.setCancelled(cancelled); } public Console getConsole() { diff --git a/src/eu/derzauberer/javautils/events/ConsoleOutputEvent.java b/src/eu/derzauberer/javautils/events/ConsoleOutputEvent.java index fe608f2..ac2328c 100644 --- a/src/eu/derzauberer/javautils/events/ConsoleOutputEvent.java +++ b/src/eu/derzauberer/javautils/events/ConsoleOutputEvent.java @@ -6,24 +6,18 @@ public class ConsoleOutputEvent extends Event { - private boolean cancled; private Console console; private String output; private MessageType type; public ConsoleOutputEvent(Console console, String output, MessageType type) { - this.cancled = false; this.console = console; this.output = output; this.type = type; } - public void setCancled(boolean cancled) { - this.cancled = cancled; - } - - public boolean isCancled() { - return cancled; + public void setCancelled(boolean cancelled) { + this.setCancelled(cancelled); } public Console getConsole() { diff --git a/src/eu/derzauberer/javautils/handler/CommandHandler.java b/src/eu/derzauberer/javautils/handler/CommandHandler.java index 7222aca..6c8aae1 100644 --- a/src/eu/derzauberer/javautils/handler/CommandHandler.java +++ b/src/eu/derzauberer/javautils/handler/CommandHandler.java @@ -35,24 +35,24 @@ private static boolean executeCommand(Console console, String command, String la return true; } CommandPreProcessingEvent event = new CommandPreProcessingEvent(console, commands.get(string), string, label, args); - EventHandler.executeEvent(CommandPreProcessingEvent.class, event); - if (!event.isCancled()) { + EventHandler.executeEvent(event); + if (!event.isCancelled()) { boolean success = commands.get(label).onCommand(event.getConsole(), event.getLabel(), event.getArgs()); if (!success) { CommandExecutionFailedEvent commandExecutionFailedEvent = new CommandExecutionFailedEvent(event.getConsole(), event.getCommand(), event.getString(), event.getLabel(), event.getArgs()); - EventHandler.executeEvent(CommandExecutionFailedEvent.class, commandExecutionFailedEvent); + EventHandler.executeEvent(commandExecutionFailedEvent); } return success; } else { CommandNotFoundEvent commandNotFoundEvent = new CommandNotFoundEvent(console, command, label, args); - EventHandler.executeEvent(CommandNotFoundEvent.class, commandNotFoundEvent); + EventHandler.executeEvent(commandNotFoundEvent); return false; } } } CommandNotFoundEvent commandNotFoundEvent = new CommandNotFoundEvent(console, command, label, args); - EventHandler.executeEvent(CommandNotFoundEvent.class, commandNotFoundEvent); + EventHandler.executeEvent(commandNotFoundEvent); return false; } @@ -124,4 +124,4 @@ private static String[] getSplitedCommand(String string) { return command; } -} +} \ No newline at end of file diff --git a/src/eu/derzauberer/javautils/handler/EventHandler.java b/src/eu/derzauberer/javautils/handler/EventHandler.java index 531978a..dda39cb 100644 --- a/src/eu/derzauberer/javautils/handler/EventHandler.java +++ b/src/eu/derzauberer/javautils/handler/EventHandler.java @@ -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 events = new ArrayList<>(); - - public static void registerEvent(Class type, EventExecuter executer) { - events.add(new EventProfile(type, executer)); + private static HashMap 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 type, Event event) { - for (EventProfile profile : events) { - if (profile.getType() == type) { - profile.getExecuter().executeEvent(event); + public static void executeEvent(Event event) { + ArrayList highestPriority = new ArrayList<>(); + ArrayList hightPriority = new ArrayList<>(); + ArrayList normalPriority = new ArrayList<>(); + ArrayList lowPriority = new ArrayList<>(); + ArrayList 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(); + } } } \ No newline at end of file diff --git a/src/eu/derzauberer/javautils/util/Client.java b/src/eu/derzauberer/javautils/util/Client.java index 0f5e4a6..f096183 100644 --- a/src/eu/derzauberer/javautils/util/Client.java +++ b/src/eu/derzauberer/javautils/util/Client.java @@ -42,7 +42,7 @@ public Client(Socket socket, Server server) throws IOException { action = (client, message) -> {}; selfdisconnect = false; ClientConnectEvent event = new ClientConnectEvent(this); - EventHandler.executeEvent(ClientConnectEvent.class, event); + EventHandler.executeEvent(event); if (!isPartOfServer()) { thread = new Thread(this); thread.start(); @@ -56,8 +56,8 @@ public void run() { try { message = input.readLine(); ClientMessageRecieveEvent event = new ClientMessageRecieveEvent(this, message); - EventHandler.executeEvent(ClientMessageRecieveEvent.class, event); - if (!event.isCancled()) { + EventHandler.executeEvent(event); + if (!event.isCancelled()) { onMessageReceive(message); } } catch (SocketException socketException) { @@ -77,13 +77,13 @@ public void run() { cause = DisconnectCause.CONNECTTIONCLOSED; } ClientDisconnectEvent event = new ClientDisconnectEvent(this, cause); - EventHandler.executeEvent(ClientDisconnectEvent.class, event); + EventHandler.executeEvent(event); } public void sendMessage(String message) { ClientMessageSendEvent event = new ClientMessageSendEvent(this, message); - EventHandler.executeEvent(ClientMessageSendEvent.class, event); - if (!event.isCancled()) { + EventHandler.executeEvent(event); + if (!event.isCancelled()) { output.println(message); } } @@ -131,4 +131,4 @@ public Server getServer() { return server; } -} +} \ No newline at end of file diff --git a/src/eu/derzauberer/javautils/util/Console.java b/src/eu/derzauberer/javautils/util/Console.java index 9eb643e..67fabf6 100644 --- a/src/eu/derzauberer/javautils/util/Console.java +++ b/src/eu/derzauberer/javautils/util/Console.java @@ -133,8 +133,8 @@ public Object getSender() { public void sendInput(String string) { ConsoleInputEvent event = new ConsoleInputEvent(this, string); - EventHandler.executeEvent(ConsoleInputEvent.class, event); - if (!event.isCancled()) { + EventHandler.executeEvent(event); + if (!event.isCancelled()) { CommandHandler.executeCommand(event.getConsole(), event.getInput()); } log(event.getInput()); @@ -142,14 +142,14 @@ public void sendInput(String string) { private void sendOutput(String output, MessageType type) { ConsoleOutputEvent event = new ConsoleOutputEvent(this, output, type); - EventHandler.executeEvent(ConsoleOutputEvent.class, event); + EventHandler.executeEvent(event); if (event.getMessageType() != MessageType.DEFAULT) { event.setOutput("[" + type.toString() + "] " + event.getOutput()); } if (!ansiEscapeCodesSupportetBySystem()) { event.setOutput(removeEscapeCodes(event.getOutput())); } - if (!event.isCancled()) { + if (!event.isCancelled()) { if (logTimestampEnabled) { outputAction.onAction(getTimeStamp() + event.getOutput()); } else { diff --git a/src/eu/derzauberer/javautils/util/Event.java b/src/eu/derzauberer/javautils/util/Event.java index 2e05924..1ad2c6e 100644 --- a/src/eu/derzauberer/javautils/util/Event.java +++ b/src/eu/derzauberer/javautils/util/Event.java @@ -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; + } + +} \ No newline at end of file diff --git a/src/eu/derzauberer/javautils/util/EventExecuter.java b/src/eu/derzauberer/javautils/util/EventExecuter.java deleted file mode 100644 index 71c95a3..0000000 --- a/src/eu/derzauberer/javautils/util/EventExecuter.java +++ /dev/null @@ -1,7 +0,0 @@ -package eu.derzauberer.javautils.util; - -public interface EventExecuter { - - public abstract void executeEvent(Event event); - -} diff --git a/src/eu/derzauberer/javautils/util/EventProfile.java b/src/eu/derzauberer/javautils/util/EventProfile.java deleted file mode 100644 index 1817642..0000000 --- a/src/eu/derzauberer/javautils/util/EventProfile.java +++ /dev/null @@ -1,21 +0,0 @@ -package eu.derzauberer.javautils.util; - -public class EventProfile { - - private Class type; - private EventExecuter executer; - - public EventProfile(Class type, EventExecuter executer) { - this.type = type; - this.executer = executer; - } - - public Class getType() { - return type; - } - - public EventExecuter getExecuter() { - return executer; - } - -} diff --git a/src/eu/derzauberer/javautils/util/Listener.java b/src/eu/derzauberer/javautils/util/Listener.java new file mode 100644 index 0000000..fbe97c7 --- /dev/null +++ b/src/eu/derzauberer/javautils/util/Listener.java @@ -0,0 +1,3 @@ +package eu.derzauberer.javautils.util; + +public interface Listener {} \ No newline at end of file