From 50e630b1fa41ebca206f37bd34944539ae50f9a2 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 20 Apr 2022 00:03:01 -0400 Subject: [PATCH 1/5] Automatically create account on login screen --- .../emu/grasscutter/commands/CommandMap.java | 4 ++-- .../emu/grasscutter/commands/ServerCommands.java | 1 + .../server/dispatch/DispatchServer.java | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/emu/grasscutter/commands/CommandMap.java b/src/main/java/emu/grasscutter/commands/CommandMap.java index a802551ce..aee080912 100644 --- a/src/main/java/emu/grasscutter/commands/CommandMap.java +++ b/src/main/java/emu/grasscutter/commands/CommandMap.java @@ -91,7 +91,7 @@ public final class CommandMap { public void invoke(GenshinPlayer player, String rawMessage) { rawMessage = rawMessage.trim(); if(rawMessage.length() == 0) { - CommandHandler.sendMessage(player, "No command specified."); + CommandHandler.sendMessage(player, "No command specified."); return; } // Remove prefix if present. @@ -113,7 +113,7 @@ public final class CommandMap { if(player != null) { String permissionNode = this.annotations.get(label).permission(); Account account = player.getAccount(); - if(permissionNode != "" && !account.hasPermission(permissionNode)) { + if(!Objects.equals(permissionNode, "") && !account.hasPermission(permissionNode)) { CommandHandler.sendMessage(player, "You do not have permission to run this command."); return; } } diff --git a/src/main/java/emu/grasscutter/commands/ServerCommands.java b/src/main/java/emu/grasscutter/commands/ServerCommands.java index edeac6ca3..8f679581d 100644 --- a/src/main/java/emu/grasscutter/commands/ServerCommands.java +++ b/src/main/java/emu/grasscutter/commands/ServerCommands.java @@ -188,6 +188,7 @@ public final class ServerCommands { } else { CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + "."); account.addPermission("*"); // Grant the player superuser permissions. + account.save(); // Save account to database. } return; case "delete": diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java index 0ac4c4fc8..a3c73d465 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java @@ -225,16 +225,16 @@ public final class DispatchServer { // Login Account account = DatabaseHelper.getAccountByName(requestData.account); - // Test + // Check if account exists, else create a new one. if (account == null) { - responseData.retcode = -201; - responseData.message = "Username not found."; - } else { - responseData.message = "OK"; - responseData.data.account.uid = account.getId(); - responseData.data.account.token = account.generateSessionKey(); - responseData.data.account.email = account.getEmail(); + account = DatabaseHelper.createAccountWithId(requestData.account, 0); + // This account has been created AUTOMATICALLY. There will be no permissions added. } + + responseData.message = "OK"; + responseData.data.account.uid = account.getId(); + responseData.data.account.token = account.generateSessionKey(); + responseData.data.account.email = account.getEmail(); // Create a response String response = getGsonFactory().toJson(responseData); From c0155b8dcc636d36dd4467261f026dd53f2ed06a Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 20 Apr 2022 00:10:32 -0400 Subject: [PATCH 2/5] Add config option --- src/main/java/emu/grasscutter/Config.java | 1 + .../server/dispatch/DispatchServer.java | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/Config.java b/src/main/java/emu/grasscutter/Config.java index 983524223..8573c83ba 100644 --- a/src/main/java/emu/grasscutter/Config.java +++ b/src/main/java/emu/grasscutter/Config.java @@ -53,5 +53,6 @@ public final class Config { public int MaxEntityLimit = 1000; // Max entity limit per world. // TODO: Enforce later. public int[] WelcomeEmotes = {2007, 1002, 4010}; public String WelcomeMotd = "Welcome to Grasscutter emu"; + public boolean AutomaticallyCreateAccounts = false; } } diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java index a3c73d465..dab6ba0a4 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java @@ -226,15 +226,23 @@ public final class DispatchServer { Account account = DatabaseHelper.getAccountByName(requestData.account); // Check if account exists, else create a new one. - if (account == null) { - account = DatabaseHelper.createAccountWithId(requestData.account, 0); + if (account == null && Grasscutter.getConfig().ServerOptions.AutomaticallyCreateAccounts) { // This account has been created AUTOMATICALLY. There will be no permissions added. + account = DatabaseHelper.createAccountWithId(requestData.account, 0); + + responseData.message = "OK"; + responseData.data.account.uid = account.getId(); + responseData.data.account.token = account.generateSessionKey(); + responseData.data.account.email = account.getEmail(); + } else if (!Grasscutter.getConfig().ServerOptions.AutomaticallyCreateAccounts) { + responseData.retcode = -201; + responseData.message = "Username not found."; + } else { + responseData.message = "OK"; + responseData.data.account.uid = account.getId(); + responseData.data.account.token = account.generateSessionKey(); + responseData.data.account.email = account.getEmail(); } - - responseData.message = "OK"; - responseData.data.account.uid = account.getId(); - responseData.data.account.token = account.generateSessionKey(); - responseData.data.account.email = account.getEmail(); // Create a response String response = getGsonFactory().toJson(responseData); From 7485e9fd4ec822ae1518bef196dd20517c99e471 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 20 Apr 2022 00:10:43 -0400 Subject: [PATCH 3/5] Change to `String#equals` --- src/main/java/emu/grasscutter/commands/CommandMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/commands/CommandMap.java b/src/main/java/emu/grasscutter/commands/CommandMap.java index aee080912..ba3fbb7c0 100644 --- a/src/main/java/emu/grasscutter/commands/CommandMap.java +++ b/src/main/java/emu/grasscutter/commands/CommandMap.java @@ -113,7 +113,7 @@ public final class CommandMap { if(player != null) { String permissionNode = this.annotations.get(label).permission(); Account account = player.getAccount(); - if(!Objects.equals(permissionNode, "") && !account.hasPermission(permissionNode)) { + if(!permissionNode.equals("*") && !account.hasPermission(permissionNode)) { CommandHandler.sendMessage(player, "You do not have permission to run this command."); return; } } From 9f2cf00b95b50123689af3eae6d2a97a6789ca10 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 20 Apr 2022 00:15:37 -0400 Subject: [PATCH 4/5] totally not me forgetting how this works --- src/main/java/emu/grasscutter/commands/CommandMap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/commands/CommandMap.java b/src/main/java/emu/grasscutter/commands/CommandMap.java index ba3fbb7c0..5d7d1110b 100644 --- a/src/main/java/emu/grasscutter/commands/CommandMap.java +++ b/src/main/java/emu/grasscutter/commands/CommandMap.java @@ -113,7 +113,7 @@ public final class CommandMap { if(player != null) { String permissionNode = this.annotations.get(label).permission(); Account account = player.getAccount(); - if(!permissionNode.equals("*") && !account.hasPermission(permissionNode)) { + if(!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) { CommandHandler.sendMessage(player, "You do not have permission to run this command."); return; } } From dee3afcaf88ca2351d9b328ab16d6bdbc833bdc4 Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Wed, 20 Apr 2022 03:28:54 -0700 Subject: [PATCH 5/5] Fix account not found issue when logging in --- .../server/dispatch/DispatchServer.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java index dab6ba0a4..f13b9dc2f 100644 --- a/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java +++ b/src/main/java/emu/grasscutter/server/dispatch/DispatchServer.java @@ -226,18 +226,22 @@ public final class DispatchServer { Account account = DatabaseHelper.getAccountByName(requestData.account); // Check if account exists, else create a new one. - if (account == null && Grasscutter.getConfig().ServerOptions.AutomaticallyCreateAccounts) { - // This account has been created AUTOMATICALLY. There will be no permissions added. - account = DatabaseHelper.createAccountWithId(requestData.account, 0); - - responseData.message = "OK"; - responseData.data.account.uid = account.getId(); - responseData.data.account.token = account.generateSessionKey(); - responseData.data.account.email = account.getEmail(); - } else if (!Grasscutter.getConfig().ServerOptions.AutomaticallyCreateAccounts) { - responseData.retcode = -201; - responseData.message = "Username not found."; + if (account == null) { + // Account doesnt exist, so we can either auto create it if the config value is set + if (Grasscutter.getConfig().ServerOptions.AutomaticallyCreateAccounts) { + // This account has been created AUTOMATICALLY. There will be no permissions added. + account = DatabaseHelper.createAccountWithId(requestData.account, 0); + + responseData.message = "OK"; + responseData.data.account.uid = account.getId(); + responseData.data.account.token = account.generateSessionKey(); + responseData.data.account.email = account.getEmail(); + } else { + responseData.retcode = -201; + responseData.message = "Username not found."; + } } else { + // Account was found, log the player in responseData.message = "OK"; responseData.data.account.uid = account.getId(); responseData.data.account.token = account.generateSessionKey();