Skip to content

Commit

Permalink
Improved events
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Zauberer committed Feb 5, 2022
1 parent 34efb75 commit 4d4638b
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ClientConnectEvent extends Event {

public ClientConnectEvent(Client client) {
this.client = client;
execute();
}

public Client getClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum DisconnectCause {
public ClientDisconnectEvent(Client client, DisconnectCause cause) {
this.client = client;
this.cause = cause;
execute();
}

public Client getClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ClientMessageRecieveEvent extends Event {
public ClientMessageRecieveEvent(Client client, String message) {
this.client = client;
this.message = message;
execute();
}

public void setCancled(boolean cancelled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ClientMessageSendEvent extends Event {
public ClientMessageSendEvent(Client client, String message) {
this.client = client;
this.message = message;
execute();
}

public void setCancelled(boolean cancelled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public CommandExecutionFailedEvent(Console console, Command command, String stri
this.string = string;
this.label = label;
this.args = args;
execute();
}

public Console getConsole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public CommandNotFoundEvent(Console console, String string, String label, String
this.string = string;
this.label = label;
this.args = args;
execute();
}

public Console getConsole() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public CommandPreProcessingEvent(Console console, Command command, String string
this.string = string;
this.label = label;
this.args = args;
execute();
}

public void setCancelled(boolean cancelled) {
Expand Down
1 change: 1 addition & 0 deletions src/eu/derzauberer/javautils/events/ConsoleInputEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ConsoleInputEvent extends Event {
public ConsoleInputEvent(Console console, String input) {
this.console = console;
this.input = input;
execute();
}

public void setCancelled(boolean cancelled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public ConsoleOutputEvent(Console console, String output, MessageType type) {
this.console = console;
this.output = output;
this.type = type;
execute();
}

public void setCancelled(boolean cancelled) {
Expand Down
11 changes: 3 additions & 8 deletions src/eu/derzauberer/javautils/handler/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,19 @@ 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(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);
new CommandExecutionFailedEvent(event.getConsole(), event.getCommand(), event.getString(), event.getLabel(), event.getArgs());
}
return success;
} else {
CommandNotFoundEvent commandNotFoundEvent = new CommandNotFoundEvent(console, command, label, args);
EventHandler.executeEvent(commandNotFoundEvent);
new CommandNotFoundEvent(console, command, label, args);
return false;
}

}
}
CommandNotFoundEvent commandNotFoundEvent = new CommandNotFoundEvent(console, command, label, args);
EventHandler.executeEvent(commandNotFoundEvent);
new CommandNotFoundEvent(console, command, label, args);
return false;
}

Expand Down
10 changes: 2 additions & 8 deletions src/eu/derzauberer/javautils/util/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import eu.derzauberer.javautils.action.ClientMessageReceiveAction;
import eu.derzauberer.javautils.events.ClientConnectEvent;
import eu.derzauberer.javautils.events.ClientDisconnectEvent;
import eu.derzauberer.javautils.events.ClientMessageRecieveEvent;
import eu.derzauberer.javautils.events.ClientMessageSendEvent;
import eu.derzauberer.javautils.events.ClientDisconnectEvent.DisconnectCause;
import eu.derzauberer.javautils.handler.EventHandler;

public class Client implements Runnable {

Expand All @@ -41,8 +39,7 @@ public Client(Socket socket, Server server) throws IOException {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
action = (client, message) -> {};
selfdisconnect = false;
ClientConnectEvent event = new ClientConnectEvent(this);
EventHandler.executeEvent(event);
new ClientConnectEvent(this);
if (!isPartOfServer()) {
thread = new Thread(this);
thread.start();
Expand All @@ -56,7 +53,6 @@ public void run() {
try {
message = input.readLine();
ClientMessageRecieveEvent event = new ClientMessageRecieveEvent(this, message);
EventHandler.executeEvent(event);
if (!event.isCancelled()) {
onMessageReceive(message);
}
Expand All @@ -76,13 +72,11 @@ public void run() {
if (selfdisconnect) {
cause = DisconnectCause.CONNECTTIONCLOSED;
}
ClientDisconnectEvent event = new ClientDisconnectEvent(this, cause);
EventHandler.executeEvent(event);
new ClientDisconnectEvent(this, cause);
}

public void sendMessage(String message) {
ClientMessageSendEvent event = new ClientMessageSendEvent(this, message);
EventHandler.executeEvent(event);
if (!event.isCancelled()) {
output.println(message);
}
Expand Down
3 changes: 0 additions & 3 deletions src/eu/derzauberer/javautils/util/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import eu.derzauberer.javautils.events.ConsoleInputEvent;
import eu.derzauberer.javautils.events.ConsoleOutputEvent;
import eu.derzauberer.javautils.handler.CommandHandler;
import eu.derzauberer.javautils.handler.EventHandler;
import eu.derzauberer.javautils.handler.FileHandler;

public class Console implements Runnable {
Expand Down Expand Up @@ -133,7 +132,6 @@ public Object getSender() {

public void sendInput(String string) {
ConsoleInputEvent event = new ConsoleInputEvent(this, string);
EventHandler.executeEvent(event);
if (!event.isCancelled()) {
CommandHandler.executeCommand(event.getConsole(), event.getInput());
}
Expand All @@ -142,7 +140,6 @@ public void sendInput(String string) {

private void sendOutput(String output, MessageType type) {
ConsoleOutputEvent event = new ConsoleOutputEvent(this, output, type);
EventHandler.executeEvent(event);
if (event.getMessageType() != MessageType.DEFAULT) {
event.setOutput("[" + type.toString() + "] " + event.getOutput());
}
Expand Down
6 changes: 6 additions & 0 deletions src/eu/derzauberer/javautils/util/Event.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package eu.derzauberer.javautils.util;

import eu.derzauberer.javautils.handler.EventHandler;

public abstract class Event {

public enum EventPriority{HIGHEST, HIGHT, NORMAL, LOW, LOWEST};

private boolean cancelled;

protected void execute() {
EventHandler.executeEvent(this);
}

protected void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
Expand Down

0 comments on commit 4d4638b

Please sign in to comment.