Code refactor

This commit is contained in:
Carlos
2020-06-25 16:53:10 +02:00
parent 45c3584061
commit f177d1fe2d
7 changed files with 168 additions and 85 deletions

View File

@ -10,7 +10,6 @@ import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import es.mesacarlos.webconsole.WebConsole;
import es.mesacarlos.webconsole.auth.LoginManager;
import es.mesacarlos.webconsole.util.DateTimeUtils;
import es.mesacarlos.webconsole.util.Internationalization;
@ -24,11 +23,9 @@ import es.mesacarlos.webconsole.websocket.response.UnknownCommand;
public class WSServer extends WebSocketServer {
private HashMap<String, WSCommand> commands = WSCommandFactory.getCommandsHashMap();
private WebConsole plugin;
public WSServer(WebConsole plugin, InetSocketAddress address) {
public WSServer(InetSocketAddress address) {
super(address);
this.plugin = plugin;
}
@Override
@ -83,14 +80,6 @@ public class WSServer extends WebSocketServer {
Bukkit.getLogger().info(Internationalization.getPhrase("started-websocket"));
}
/**
* Returns main class
* @return Main plugin class
*/
public WebConsole getMainClass() {
return plugin;
}
/**
* Sends the message to all connected AND logged-in users
*/

View File

@ -6,6 +6,7 @@ import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.java_websocket.WebSocket;
import es.mesacarlos.webconsole.WebConsole;
import es.mesacarlos.webconsole.auth.LoginManager;
import es.mesacarlos.webconsole.auth.User;
import es.mesacarlos.webconsole.auth.UserType;
@ -25,11 +26,11 @@ public class ExecCommand implements WSCommand {
Bukkit.getLogger().info(Internationalization.getPhrase("cmd-executed-console", conn.getRemoteSocketAddress(), Internationalization.utf8ToIso(command)));
ConsoleCommandSender sender = Bukkit.getServer().getConsoleSender();
WebConsole plugin = (WebConsole) Bukkit.getPluginManager().getPlugin("WebConsole");
try {
@SuppressWarnings("unused")
boolean success = Bukkit.getScheduler()
.callSyncMethod(wsServer.getMainClass(), () -> Bukkit.dispatchCommand(sender, command)).get();
.callSyncMethod(plugin, () -> Bukkit.dispatchCommand(sender, command)).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}

View File

@ -23,10 +23,10 @@ public class LogInCommand implements WSCommand {
//Check user type and login is password is valid
switch(PasswordManager.isValidUser(password)) {
case ADMIN:
login(wsServer, conn, PasswordManager.isValidAdminPassword(password), UserType.ADMIN);
login(wsServer, conn, PasswordManager.getAdminUsernameFromPassword(password), UserType.ADMIN);
break;
case VIEWER:
login(wsServer, conn, PasswordManager.isValidViewerPassword(password), UserType.VIEWER);
login(wsServer, conn, PasswordManager.getViewerUsernameFromPassword(password), UserType.VIEWER);
break;
default:
wsServer.sendToClient(conn, new LoginRequired(Internationalization.getPhrase("login-failed-message")));