Commit 0475fcbd authored by Adam Gausmann's avatar Adam Gausmann

Add javadoc, refactor connect/disconnect events

parent 46cbf090
Pipeline #5064165 passed with stages
in 14 seconds
......@@ -6,25 +6,13 @@ import ninja.nonemu.samurai.event.Event;
* Dispatched by the connection manager when the bot has successfully connected to a server.
*/
public class BotConnectEvent extends Event {
private final String host;
private final int port;
private final String nickname;
private final Connection connection;
public BotConnectEvent(String host, int port, String nickname) {
this.host = host;
this.port = port;
this.nickname = nickname;
public BotConnectEvent(Connection connection) {
this.connection = connection;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getNickname() {
return nickname;
public Connection getConnection() {
return connection;
}
}
......@@ -6,24 +6,19 @@ import ninja.nonemu.samurai.event.Event;
* Dispatched by the connection manager when the bot disconnects from a server.
*/
public class BotDisconnectEvent extends Event {
private final String host;
private final int port;
private final Connection connection;
private final String comment;
public BotDisconnectEvent(String host, int port) {
this.host = host;
this.port = port;
public BotDisconnectEvent(Connection connection, String comment) {
this.connection = connection;
this.comment = comment;
}
public String getHost() {
return host;
public Connection getConnection() {
return connection;
}
public int getPort() {
return port;
}
@Override
public boolean isCancelled() {
return false;
public String getComment() {
return comment;
}
}
......@@ -11,17 +11,45 @@ import ninja.nonemu.irc.ConnectionBuilder;
*/
public interface ConnectionManager {
/**
* Gets a list of all the connections that are currently registered. They may or may not be connected.
*/
Connection[] getConnections();
/**
* Gets a connection given its name.
* @param name The name of the connection that was given at registration (case insensitive).
* @return The connection found, or null if there is no connection with that name.
*/
Connection getConnection(String name);
/**
* Builds and registers a connection to an IRC server. This does NOT automatically connect to the server.
* Since no name is provided, one is generated from the connection's hostname.
* @param builder A builder that will be responsible for building the connection.
* @return The created connection.
*/
Connection addConnection(ConnectionBuilder builder);
/**
* Builds and registers a connection to an IRC server. This does NOT automatically connect to the server.
* @param name The name of the new connection (case insensitive).
* @param builder A builder that will be responsible for building the connection.
* @return The created connection.
*/
Connection addConnection(String name, ConnectionBuilder builder);
/**
* Removes a connection if it has been registered.
* @param connection The connection to remove.
*/
default void removeConnection(Connection connection) {
removeConnection(connection.getName());
}
/**
* Removes a connection if it has been registered.
* @param name The name of the connection to remove (case insensitive).
*/
void removeConnection(String name);
}
......@@ -103,7 +103,7 @@ public class ConnectionImpl implements Connection {
}
}
else if (message.getCommand().equals("001")) { // RPL_WELCOME
bot.getEventSystem().dispatchEvent(new BotConnectEvent(getHost(), getPort(), getNickname()));
bot.getEventSystem().dispatchEvent(new BotConnectEvent(this));
if (channels != null && !channels.isEmpty()) {
connection.joinChannel(channels);
......@@ -164,11 +164,12 @@ public class ConnectionImpl implements Connection {
@Override
public void disconnect(String comment) throws IOException {
connection.disconnect(comment);
bot.getEventSystem().dispatchEvent(new BotDisconnectEvent(this, comment));
}
@Override
public void disconnect() throws IOException {
connection.disconnect();
disconnect("No comment");
}
@Override
......
......@@ -11,6 +11,7 @@ import ninja.nonemu.samurai.connection.BotJoinEvent;
import ninja.nonemu.samurai.connection.BotKickEvent;
import ninja.nonemu.samurai.connection.BotLeaveEvent;
import ninja.nonemu.samurai.connection.ChatEvent;
import ninja.nonemu.samurai.connection.Connection;
import ninja.nonemu.samurai.connection.NoticeEvent;
import ninja.nonemu.samurai.connection.UserJoinEvent;
import ninja.nonemu.samurai.connection.UserKickEvent;
......@@ -66,12 +67,14 @@ public class ConsoleManager {
@EventHandler(priority = Priority.MONITOR)
public void onBotConnectEvent(BotConnectEvent event) {
logger.info("Connected to {}:{} as {}", event.getHost(), event.getPort(), event.getNickname());
Connection connection = event.getConnection();
logger.info("Connected to {}:{} as {}", connection.getHost(), connection.getPort(), connection.getNickname());
}
@EventHandler(priority = Priority.MONITOR)
public void onBotDisconnectEvent(BotDisconnectEvent event) {
logger.info("Disconnected from {}:{}", event.getHost(), event.getPort());
Connection connection = event.getConnection();
logger.info("Disconnected from {}:{}", connection.getHost(), connection.getPort());
}
@EventHandler(priority = Priority.MONITOR)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment