Skip to content

Commit

Permalink
Updated eventsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Zauberer committed Feb 5, 2022
1 parent 4a22353 commit 5640e53
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 99 deletions.
13 changes: 13 additions & 0 deletions src/eu/derzauberer/javautils/annotations/EventListener.java
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;
}
1 change: 0 additions & 1 deletion src/eu/derzauberer/javautils/annotations/JsonElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface JsonElement {

public String key() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 2 additions & 8 deletions src/eu/derzauberer/javautils/events/ClientMessageSendEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,22 @@

public class CommandPreProcessingEvent extends Event {

private boolean cancled;
private Console console;
private Command command;
private String string;
private String label;
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;
this.label = label;
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() {
Expand Down
10 changes: 2 additions & 8 deletions src/eu/derzauberer/javautils/events/ConsoleInputEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 2 additions & 8 deletions src/eu/derzauberer/javautils/events/ConsoleOutputEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions src/eu/derzauberer/javautils/handler/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -124,4 +124,4 @@ private static String[] getSplitedCommand(String string) {
return command;
}

}
}
56 changes: 45 additions & 11 deletions src/eu/derzauberer/javautils/handler/EventHandler.java
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();
}
}

}
14 changes: 7 additions & 7 deletions src/eu/derzauberer/javautils/util/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -131,4 +131,4 @@ public Server getServer() {
return server;
}

}
}
8 changes: 4 additions & 4 deletions src/eu/derzauberer/javautils/util/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ 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());
}

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 {
Expand Down
16 changes: 14 additions & 2 deletions src/eu/derzauberer/javautils/util/Event.java
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;
}

}
7 changes: 0 additions & 7 deletions src/eu/derzauberer/javautils/util/EventExecuter.java

This file was deleted.

21 changes: 0 additions & 21 deletions src/eu/derzauberer/javautils/util/EventProfile.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/eu/derzauberer/javautils/util/Listener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package eu.derzauberer.javautils.util;

public interface Listener {}

0 comments on commit 5640e53

Please sign in to comment.