forked from UnlegitDqrk/unlegitlibrary
Bug fixes and new network system
This commit is contained in:
@@ -10,7 +10,6 @@ package me.finn.unlegitlibrary.command;
|
||||
|
||||
import javax.management.InstanceAlreadyExistsException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Command {
|
||||
@@ -22,14 +21,6 @@ public abstract class Command {
|
||||
private final List<CommandPermission> permissions;
|
||||
private final List<String> aliases;
|
||||
|
||||
public final List<CommandPermission> getPermissions() {
|
||||
return new ArrayList<>(permissions);
|
||||
}
|
||||
|
||||
public final List<String> getAliases() {
|
||||
return new ArrayList<>(aliases);
|
||||
}
|
||||
|
||||
public Command(CommandManager commandManager, String name, String description, String usage, List<CommandPermission> permissions, List<String> aliases) throws InstanceAlreadyExistsException {
|
||||
this.commandManager = commandManager;
|
||||
this.name = name;
|
||||
@@ -43,5 +34,13 @@ public abstract class Command {
|
||||
if (exists) throw new InstanceAlreadyExistsException("Command with this name or some alias alreadx exists!");
|
||||
}
|
||||
|
||||
public final List<CommandPermission> getPermissions() {
|
||||
return new ArrayList<>(permissions);
|
||||
}
|
||||
|
||||
public final List<String> getAliases() {
|
||||
return new ArrayList<>(aliases);
|
||||
}
|
||||
|
||||
public abstract void execute(CommandExecutor commandExecutor, String label, String[] args);
|
||||
}
|
||||
|
@@ -18,6 +18,13 @@ public abstract class CommandExecutor {
|
||||
public final String name;
|
||||
private final List<CommandPermission> permissions;
|
||||
|
||||
public CommandExecutor(String name, CommandPermission... permissions) {
|
||||
this.name = name;
|
||||
|
||||
this.permissions = new ArrayList<>();
|
||||
this.permissions.addAll(Arrays.asList(permissions));
|
||||
}
|
||||
|
||||
public List<CommandPermission> getPermissions() {
|
||||
return new ArrayList<>(permissions);
|
||||
}
|
||||
@@ -30,12 +37,5 @@ public abstract class CommandExecutor {
|
||||
return new HashSet<>(this.permissions).containsAll(permissions);
|
||||
}
|
||||
|
||||
public CommandExecutor(String name, CommandPermission... permissions) {
|
||||
this.name = name;
|
||||
|
||||
this.permissions = new ArrayList<>();
|
||||
this.permissions.addAll(Arrays.asList(permissions));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@ import me.finn.unlegitlibrary.command.Command;
|
||||
import me.finn.unlegitlibrary.command.CommandExecutor;
|
||||
import me.finn.unlegitlibrary.command.CommandManager;
|
||||
import me.finn.unlegitlibrary.event.impl.CancellableEvent;
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public class CommandExecuteEvent extends CancellableEvent {
|
||||
public final CommandManager commandManager;
|
||||
|
@@ -11,7 +11,6 @@ package me.finn.unlegitlibrary.command.events;
|
||||
import me.finn.unlegitlibrary.command.Command;
|
||||
import me.finn.unlegitlibrary.command.CommandExecutor;
|
||||
import me.finn.unlegitlibrary.command.CommandManager;
|
||||
import me.finn.unlegitlibrary.event.impl.CancellableEvent;
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public class CommandExecutorMissingPermissionEvent extends Event {
|
||||
|
@@ -8,10 +8,8 @@
|
||||
|
||||
package me.finn.unlegitlibrary.command.events;
|
||||
|
||||
import me.finn.unlegitlibrary.command.Command;
|
||||
import me.finn.unlegitlibrary.command.CommandExecutor;
|
||||
import me.finn.unlegitlibrary.command.CommandManager;
|
||||
import me.finn.unlegitlibrary.event.impl.CancellableEvent;
|
||||
import me.finn.unlegitlibrary.event.impl.Event;
|
||||
|
||||
public class CommandNotFoundEvent extends Event {
|
||||
|
@@ -2,7 +2,10 @@ package me.finn.unlegitlibrary.file;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -47,7 +50,7 @@ public class ConfigurationManager extends DefaultMethodsOverrider {
|
||||
}
|
||||
|
||||
public final Object getObject(String key) {
|
||||
return (Object) properties.getProperty(key);
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
public Map<String, String> getMap(String key) {
|
||||
|
@@ -13,7 +13,10 @@ import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
@@ -129,7 +132,7 @@ public class FileUtils extends DefaultMethodsOverrider {
|
||||
|
||||
public static List<String> readFileLines(File file) throws IOException {
|
||||
List<String> lines = new ArrayList<>();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
|
||||
String str;
|
||||
while ((str = in.readLine()) != null) lines.add(str);
|
||||
return lines;
|
||||
|
@@ -9,9 +9,6 @@
|
||||
package me.finn.unlegitlibrary.network.system.client;
|
||||
|
||||
import me.finn.unlegitlibrary.event.EventManager;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.send.C_PacketFailedSendEvent;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.state.C_DisconnectedEvent;
|
||||
import me.finn.unlegitlibrary.network.system.packets.impl.ClientDisconnectPacket;
|
||||
import me.finn.unlegitlibrary.network.system.client.events.*;
|
||||
import me.finn.unlegitlibrary.network.system.packets.Packet;
|
||||
import me.finn.unlegitlibrary.network.system.packets.PacketHandler;
|
||||
@@ -27,86 +24,43 @@ import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
|
||||
public final class NetworkClient {
|
||||
public static class ClientBuilder extends DefaultMethodsOverrider {
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
private int maxReconnectAttempts = 0;
|
||||
private int reconnectDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkClient build() {
|
||||
return new NetworkClient(host, port ,packetHandler, eventManager, logger, maxReconnectAttempts, reconnectDelay, timeout);
|
||||
}
|
||||
|
||||
public final ClientBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setHost(String host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setMaxReconnectAttempts(int maxReconnectAttempts) {
|
||||
this.maxReconnectAttempts = maxReconnectAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
private final EventManager eventManager;
|
||||
private final Logger logger;
|
||||
|
||||
private Socket socket;
|
||||
private int timeout;
|
||||
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
|
||||
private int clientID;
|
||||
|
||||
private int currentAttempts;
|
||||
private final int maxReconnectAttempts;
|
||||
private final int reconnectDelay;
|
||||
private Socket socket;
|
||||
private final int timeout;
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
private int clientID;
|
||||
private int currentAttempts;
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int reconnectAttempts, int reconnectDelay, int timeout) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.clientID = -1;
|
||||
this.timeout = timeout;
|
||||
|
||||
private final Thread receiveThread = new Thread(this::receive);
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxReconnectAttempts = reconnectAttempts;
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setClientInstance(this);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
}
|
||||
|
||||
public int getClientID() {
|
||||
return clientID;
|
||||
} private final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public void setClientID(int clientID) {
|
||||
if (this.clientID == -1) this.clientID = clientID;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
@@ -142,24 +96,6 @@ public final class NetworkClient {
|
||||
return maxReconnectAttempts != 0;
|
||||
}
|
||||
|
||||
private NetworkClient(String host, int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int reconnectAttempts, int reconnectDelay, int timeout) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.clientID = -1;
|
||||
this.timeout = timeout;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxReconnectAttempts = reconnectAttempts;
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setClientInstance(this);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
}
|
||||
|
||||
public synchronized boolean connect() throws ConnectException {
|
||||
if (isConnected()) return false;
|
||||
|
||||
@@ -177,7 +113,8 @@ public final class NetworkClient {
|
||||
receiveThread.start();
|
||||
|
||||
if (currentAttempts == 0) currentAttempts++;
|
||||
if (logger == null) System.out.println("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
if (logger == null)
|
||||
System.out.println("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
else logger.info("Connected to " + host + ":" + port + " (Attempts: " + currentAttempts + ")");
|
||||
|
||||
eventManager.executeEvent(new ClientConnectedEvent(this));
|
||||
@@ -231,11 +168,6 @@ public final class NetworkClient {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setClientID(int clientID) {
|
||||
if (this.clientID == -1) this.clientID = clientID;
|
||||
}
|
||||
|
||||
public boolean sendPacket(Packet packet) throws IOException, ClassNotFoundException {
|
||||
if (!isConnected()) return false;
|
||||
|
||||
@@ -292,4 +224,63 @@ public final class NetworkClient {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static class ClientBuilder extends DefaultMethodsOverrider {
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
private int maxReconnectAttempts = 0;
|
||||
private int reconnectDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkClient build() {
|
||||
return new NetworkClient(host, port, packetHandler, eventManager, logger, maxReconnectAttempts, reconnectDelay, timeout);
|
||||
}
|
||||
|
||||
public final ClientBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setHost(String host) {
|
||||
this.host = host;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setMaxReconnectAttempts(int maxReconnectAttempts) {
|
||||
this.maxReconnectAttempts = maxReconnectAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.reconnectDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ClientBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -25,31 +25,31 @@ public final class PacketHandler extends DefaultMethodsOverrider {
|
||||
private NetworkClient clientInstance;
|
||||
private NetworkServer serverInstance;
|
||||
|
||||
public final NetworkClient getClientInstance() {
|
||||
public NetworkClient getClientInstance() {
|
||||
return clientInstance;
|
||||
}
|
||||
|
||||
public final NetworkServer getServerInstance() {
|
||||
return serverInstance;
|
||||
}
|
||||
|
||||
public void setClientInstance(NetworkClient clientInstance) {
|
||||
if (this.clientInstance == null) this.clientInstance = clientInstance;
|
||||
}
|
||||
|
||||
public NetworkServer getServerInstance() {
|
||||
return serverInstance;
|
||||
}
|
||||
|
||||
public void setServerInstance(NetworkServer serverInstance) {
|
||||
if (this.serverInstance == null) this.serverInstance = serverInstance;
|
||||
}
|
||||
|
||||
public final boolean isPacketIDRegistered(int id) {
|
||||
public boolean isPacketIDRegistered(int id) {
|
||||
return packets.containsKey(id);
|
||||
}
|
||||
|
||||
public final Packet getPacketByID(int id) {
|
||||
public Packet getPacketByID(int id) {
|
||||
return packets.get(id);
|
||||
}
|
||||
|
||||
public final boolean registerPacket(Packet packet) {
|
||||
public boolean registerPacket(Packet packet) {
|
||||
int id = packet.getPacketID();
|
||||
|
||||
if (isPacketIDRegistered(id)) return false;
|
||||
@@ -58,7 +58,7 @@ public final class PacketHandler extends DefaultMethodsOverrider {
|
||||
return true;
|
||||
}
|
||||
|
||||
public final boolean handlePacket(int id, Packet packet, ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
|
||||
public boolean handlePacket(int id, Packet packet, ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
|
||||
if (!isPacketIDRegistered(id) || (packet != null && id != packet.getPacketID()) || (packet != null && !isPacketIDRegistered(packet.getPacketID())))
|
||||
return false;
|
||||
|
||||
@@ -66,7 +66,7 @@ public final class PacketHandler extends DefaultMethodsOverrider {
|
||||
return true;
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
public boolean sendPacket(Packet packet, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException {
|
||||
int id = packet.getPacketID();
|
||||
if (!isPacketIDRegistered(id)) return false;
|
||||
|
||||
|
@@ -16,12 +16,12 @@ import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class ClientIDPacket extends Packet {
|
||||
private int clientID;
|
||||
|
||||
public ClientIDPacket() {
|
||||
super(0);
|
||||
}
|
||||
|
||||
private int clientID;
|
||||
|
||||
public ClientIDPacket(int clientID) {
|
||||
this();
|
||||
this.clientID = clientID;
|
||||
|
@@ -26,17 +26,6 @@ public class ConnectionHandler {
|
||||
|
||||
private ObjectOutputStream outputStream;
|
||||
private ObjectInputStream inputStream;
|
||||
public final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
return networkServer.isRunning() && socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public ConnectionHandler(NetworkServer server, Socket socket, int clientID) throws IOException, ClassNotFoundException {
|
||||
this.networkServer = server;
|
||||
this.socket = socket;
|
||||
@@ -49,6 +38,15 @@ public class ConnectionHandler {
|
||||
|
||||
sendPacket(new ClientIDPacket());
|
||||
networkServer.getEventManager().executeEvent(new ConnectionHandlerConnectedEvent(this));
|
||||
} public final Thread receiveThread = new Thread(this::receive);
|
||||
|
||||
public int getClientID() {
|
||||
return clientID;
|
||||
}
|
||||
|
||||
public final boolean isConnected() {
|
||||
return networkServer.isRunning() && socket != null && socket.isConnected() && !socket.isClosed() && socket.isBound()
|
||||
&& receiveThread.isAlive() && !receiveThread.isInterrupted();
|
||||
}
|
||||
|
||||
public synchronized boolean disconnect() {
|
||||
@@ -68,8 +66,10 @@ public class ConnectionHandler {
|
||||
inputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException exception) {
|
||||
if (networkServer.getLogger() == null) System.err.println("Client ID '" + clientID + "' failed to close socket: " + exception.getMessage());
|
||||
else networkServer.getLogger().exception("Client ID '" + clientID + "' failed to close socket", exception);
|
||||
if (networkServer.getLogger() == null)
|
||||
System.err.println("Client ID '" + clientID + "' failed to close socket: " + exception.getMessage());
|
||||
else
|
||||
networkServer.getLogger().exception("Client ID '" + clientID + "' failed to close socket", exception);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,8 @@ public class ConnectionHandler {
|
||||
networkServer.getEventManager().executeEvent(new S_PacketReceivedEvent(this, packet));
|
||||
else
|
||||
networkServer.getEventManager().executeEvent(new S_PacketReceivedFailedEvent(this, packet));
|
||||
} else networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(received, this));
|
||||
} else
|
||||
networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(received, this));
|
||||
} else networkServer.getEventManager().executeEvent(new S_UnknownObjectReceivedEvent(received, this));
|
||||
} catch (SocketException ignored) {
|
||||
disconnect();
|
||||
@@ -127,4 +128,6 @@ public class ConnectionHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -24,72 +24,31 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class NetworkServer {
|
||||
public static class ServerBuilder extends DefaultMethodsOverrider {
|
||||
private int port;
|
||||
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
private int maxRestartAttempts = 0;
|
||||
private int restartDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkServer build() {
|
||||
return new NetworkServer(port, packetHandler, eventManager, logger, maxRestartAttempts, restartDelay, timeout);
|
||||
}
|
||||
|
||||
public final ServerBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setMaxReconnectAttempts(int maxRestartAttempts) {
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.restartDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private final int port;
|
||||
|
||||
private final PacketHandler packetHandler;
|
||||
private final EventManager eventManager;
|
||||
private final Logger logger;
|
||||
|
||||
private int currentAttempts;
|
||||
private final int timeout;
|
||||
private final int maxRestartAttempts;
|
||||
private final int restartDelay;
|
||||
|
||||
private final List<ConnectionHandler> connectionHandlers = new ArrayList<>();
|
||||
public final Thread incomingConnectionThread = new Thread(this::incomingConnection);
|
||||
|
||||
private int currentAttempts;
|
||||
private ServerSocket serverSocket;
|
||||
private NetworkServer(int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int maxRestartAttempts, int restartDelay, int timeout) {
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
this.restartDelay = restartDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setServerInstance(this);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
} public final Thread incomingConnectionThread = new Thread(this::incomingConnection);
|
||||
|
||||
public Logger getLogger() {
|
||||
return logger;
|
||||
@@ -111,37 +70,22 @@ public final class NetworkServer {
|
||||
return connectionHandlers;
|
||||
}
|
||||
|
||||
public final boolean isAutoRestartEnabled() {
|
||||
public boolean isAutoRestartEnabled() {
|
||||
return maxRestartAttempts != 0;
|
||||
}
|
||||
|
||||
public final boolean isRunning() {
|
||||
public boolean isRunning() {
|
||||
return serverSocket != null && !serverSocket.isClosed() && serverSocket.isBound() &&
|
||||
incomingConnectionThread.isAlive() && !incomingConnectionThread.isInterrupted();
|
||||
}
|
||||
|
||||
public final ConnectionHandler getConnectionHandlerByID(int clientID) {
|
||||
for (ConnectionHandler connectionHandler : connectionHandlers) if (connectionHandler.getClientID() == clientID) return connectionHandler;
|
||||
public ConnectionHandler getConnectionHandlerByID(int clientID) {
|
||||
for (ConnectionHandler connectionHandler : connectionHandlers)
|
||||
if (connectionHandler.getClientID() == clientID) return connectionHandler;
|
||||
return null;
|
||||
}
|
||||
|
||||
private NetworkServer(int port, PacketHandler packetHandler, EventManager eventManager, Logger logger, int maxRestartAttempts, int restartDelay, int timeout) {
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
|
||||
this.packetHandler = packetHandler;
|
||||
this.eventManager = eventManager;
|
||||
this.logger = logger;
|
||||
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
this.restartDelay = restartDelay;
|
||||
this.currentAttempts = 0;
|
||||
|
||||
this.packetHandler.setServerInstance(this);
|
||||
this.packetHandler.registerPacket(new ClientIDPacket());
|
||||
}
|
||||
|
||||
public synchronized final boolean start() {
|
||||
public synchronized boolean start() {
|
||||
if (isRunning()) return false;
|
||||
|
||||
if (logger == null) System.out.println("Trying to start on port " + port + "...");
|
||||
@@ -222,19 +166,64 @@ public final class NetworkServer {
|
||||
}
|
||||
}
|
||||
|
||||
public final boolean sendPacket(int clientID, Packet packet) throws IOException, ClassNotFoundException {
|
||||
public boolean sendPacket(int clientID, Packet packet) throws IOException, ClassNotFoundException {
|
||||
return getConnectionHandlerByID(clientID).sendPacket(packet);
|
||||
}
|
||||
|
||||
public final boolean sendPacket(Packet packet, int clientID) throws IOException, ClassNotFoundException {
|
||||
public boolean sendPacket(Packet packet, int clientID) throws IOException, ClassNotFoundException {
|
||||
return sendPacket(clientID, packet);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
EventManager eventManager = new EventManager();
|
||||
PacketHandler packetHandler = new PacketHandler();
|
||||
public static class ServerBuilder extends DefaultMethodsOverrider {
|
||||
private int port;
|
||||
|
||||
NetworkServer networkServer = new ServerBuilder().setMaxReconnectAttempts(2).setEventManager(eventManager).setPacketHandler(packetHandler).setPort(1918).build();
|
||||
networkServer.start();
|
||||
private PacketHandler packetHandler;
|
||||
private EventManager eventManager;
|
||||
private Logger logger;
|
||||
|
||||
private int maxRestartAttempts = 0;
|
||||
private int restartDelay = 3000;
|
||||
private int timeout = 0;
|
||||
|
||||
public final NetworkServer build() {
|
||||
return new NetworkServer(port, packetHandler, eventManager, logger, maxRestartAttempts, restartDelay, timeout);
|
||||
}
|
||||
|
||||
public final ServerBuilder setEventManager(EventManager eventManager) {
|
||||
this.eventManager = eventManager;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setLogger(Logger logger) {
|
||||
this.logger = logger;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setMaxReconnectAttempts(int maxRestartAttempts) {
|
||||
this.maxRestartAttempts = maxRestartAttempts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setPacketHandler(PacketHandler packetHandler) {
|
||||
this.packetHandler = packetHandler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setPort(int port) {
|
||||
this.port = port;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setReconnectDelay(int reconnectDelay) {
|
||||
this.restartDelay = reconnectDelay;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final ServerBuilder setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ import java.util.Random;
|
||||
public class RandomString extends DefaultMethodsOverrider {
|
||||
public static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
public static final String lower = upper.toLowerCase(Locale.ROOT);
|
||||
public static final String digits = "0123456789";
|
||||
public static final String alphanum = upper + lower + digits;
|
||||
public static final String digits = "0123456789";
|
||||
private final Random random;
|
||||
private final char[] symbols;
|
||||
private final char[] buf;
|
||||
|
@@ -10,9 +10,6 @@ package me.finn.unlegitlibrary.string.color;
|
||||
|
||||
import me.finn.unlegitlibrary.utils.DefaultMethodsOverrider;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ConsoleColor extends DefaultMethodsOverrider {
|
||||
// Reset
|
||||
public static final String RESET = "\033[0m"; // Text Reset
|
||||
|
@@ -4,22 +4,6 @@ import me.finn.unlegitlibrary.number.MathHelper;
|
||||
|
||||
public class Color {
|
||||
|
||||
public float red = 1;
|
||||
public float green = 1;
|
||||
public float blue = 1;
|
||||
public float alpha = 1;
|
||||
|
||||
public Color(float red, float green, float blue, float alpha) {
|
||||
this.red = MathHelper.clamp_float(red, 0f, 1f);
|
||||
this.green = MathHelper.clamp_float(green, 0f, 1f);
|
||||
this.blue = MathHelper.clamp_float(blue, 0f, 1f);
|
||||
this.alpha = MathHelper.clamp_float(alpha, 0f, 1f);
|
||||
}
|
||||
|
||||
public Color(float red, float green, float blue) {
|
||||
this(red, green, blue, 1);
|
||||
}
|
||||
|
||||
public static final Color COLOR_BLACK = new Color(0, 0, 0);
|
||||
public static final Color COLOR_WHITE = new Color(1, 1, 1);
|
||||
public static final Color COLOR_RED = new Color(1, 0, 0);
|
||||
@@ -32,6 +16,23 @@ public class Color {
|
||||
public static final Color COLOR_WINE = new Color(0.5f, 0.5f, 0.5f);
|
||||
public static final Color COLOR_FORREST = new Color(0, 0.5f, 0);
|
||||
public static final Color COLOR_MARINE = new Color(0, 0, 0.5f);
|
||||
public float red = 1;
|
||||
public float green = 1;
|
||||
public float blue = 1;
|
||||
public float alpha = 1;
|
||||
public Color(float red, float green, float blue, float alpha) {
|
||||
this.red = MathHelper.clamp_float(red, 0f, 1f);
|
||||
this.green = MathHelper.clamp_float(green, 0f, 1f);
|
||||
this.blue = MathHelper.clamp_float(blue, 0f, 1f);
|
||||
this.alpha = MathHelper.clamp_float(alpha, 0f, 1f);
|
||||
}
|
||||
public Color(float red, float green, float blue) {
|
||||
this(red, green, blue, 1);
|
||||
}
|
||||
|
||||
public static Color fromAwtColor(java.awt.Color awtColor) {
|
||||
return new Color(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue(), awtColor.getAlpha());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -47,14 +48,10 @@ public class Color {
|
||||
return new java.awt.Color(red, green, blue, alpha);
|
||||
}
|
||||
|
||||
public static Color fromAwtColor(java.awt.Color awtColor) {
|
||||
return new Color(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue(), awtColor.getAlpha());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object obj) {
|
||||
if (!(obj instanceof Color)) return false;
|
||||
Color equalTo = (Color)obj;
|
||||
Color equalTo = (Color) obj;
|
||||
return equalTo.alpha == alpha && equalTo.red == red && equalTo.green == green && equalTo.blue == blue;
|
||||
}
|
||||
|
||||
|
@@ -3,29 +3,24 @@ package me.finn.unlegitlibrary.utils;
|
||||
import me.finn.unlegitlibrary.file.FileUtils;
|
||||
import me.finn.unlegitlibrary.string.color.ConsoleColor;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.Clipboard;
|
||||
import java.awt.datatransfer.DataFlavor;
|
||||
import java.awt.datatransfer.Transferable;
|
||||
import java.awt.datatransfer.UnsupportedFlavorException;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.System;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Logger by shock9 Interactive
|
||||
*/
|
||||
public final class Logger {
|
||||
private File logFolder;
|
||||
private File latestLogFile;
|
||||
private final File logFolder;
|
||||
private final File latestLogFile;
|
||||
|
||||
private boolean isInitialized = false;
|
||||
|
||||
@@ -66,7 +61,7 @@ public final class Logger {
|
||||
}
|
||||
|
||||
// Renaming latest log to current date and yeah
|
||||
public final void shutdown() throws IOException {
|
||||
public void shutdown() throws IOException {
|
||||
if (!isInitialized) return;
|
||||
|
||||
// Get current date and time
|
||||
@@ -84,12 +79,12 @@ public final class Logger {
|
||||
isInitialized = false;
|
||||
}
|
||||
|
||||
private final void writeToLog(String log) throws IOException {
|
||||
private void writeToLog(String log) throws IOException {
|
||||
if (isInitialized)
|
||||
FileUtils.writeFile(latestLogFile, FileUtils.readFileFull(latestLogFile) + System.lineSeparator() + log);
|
||||
}
|
||||
|
||||
public final void log(String string) {
|
||||
public void log(String string) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
@@ -105,7 +100,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void info(String info) {
|
||||
public void info(String info) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
@@ -124,7 +119,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void warn(String warn) {
|
||||
public void warn(String warn) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
@@ -143,7 +138,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void error(String error) {
|
||||
public void error(String error) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
@@ -162,7 +157,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void exception(String infoLine, Exception exception) {
|
||||
public void exception(String infoLine, Exception exception) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
@@ -177,8 +172,8 @@ public final class Logger {
|
||||
// Writing log
|
||||
String log =
|
||||
timeStamp + " [EXCEPTION-INFO] " + infoLine + System.lineSeparator() +
|
||||
timeStamp + " [EXCEPTION-MESSAGE] " + exception.getMessage() + System.lineSeparator() +
|
||||
timeStamp + " [EXCEPTION-STACKTRACE] " + stackTrace;
|
||||
timeStamp + " [EXCEPTION-MESSAGE] " + exception.getMessage() + System.lineSeparator() +
|
||||
timeStamp + " [EXCEPTION-STACKTRACE] " + stackTrace;
|
||||
|
||||
System.out.println(ConsoleColor.RED + log + ConsoleColor.RESET);
|
||||
|
||||
@@ -189,7 +184,7 @@ public final class Logger {
|
||||
}
|
||||
}
|
||||
|
||||
public final void debug(String debug) {
|
||||
public void debug(String debug) {
|
||||
// Get current date and time
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
|
||||
Date date = new Date();
|
||||
|
Reference in New Issue
Block a user