From c65443f422104528b2697d5d77237f98f74e515b Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Mon, 18 Apr 2022 21:38:19 -0700 Subject: [PATCH] Fix permissions not saving --- .../emu/grasscutter/commands/ServerCommands.java | 14 ++++++++------ src/main/java/emu/grasscutter/game/Account.java | 5 ++++- .../emu/grasscutter/server/game/GameServer.java | 9 +++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/commands/ServerCommands.java b/src/main/java/emu/grasscutter/commands/ServerCommands.java index 3accd3ce8..1262fae97 100644 --- a/src/main/java/emu/grasscutter/commands/ServerCommands.java +++ b/src/main/java/emu/grasscutter/commands/ServerCommands.java @@ -138,7 +138,7 @@ public final class ServerCommands { String username = args.get(1); String permission = args.get(2); - Account account = DatabaseHelper.getAccountByName(username); + Account account = Grasscutter.getGameServer().getAccountByName(username); if(account == null) { CommandHandler.sendMessage(null, "Account not found."); return; } @@ -146,18 +146,20 @@ public final class ServerCommands { switch(action) { default: CommandHandler.sendMessage(null, "Usage: permission "); - return; + break; case "add": if(account.addPermission(permission)) { - CommandHandler.sendMessage(null, "Permission added."); return; + CommandHandler.sendMessage(null, "Permission added."); } else CommandHandler.sendMessage(null, "They already have this permission!"); - return; + break; case "remove": if(account.removePermission(permission)) { - CommandHandler.sendMessage(null, "Permission removed."); return; + CommandHandler.sendMessage(null, "Permission removed."); } else CommandHandler.sendMessage(null, "They don't have this permission!"); - return; + break; } + + account.save(); } } diff --git a/src/main/java/emu/grasscutter/game/Account.java b/src/main/java/emu/grasscutter/game/Account.java index bba03916a..0d206dda1 100644 --- a/src/main/java/emu/grasscutter/game/Account.java +++ b/src/main/java/emu/grasscutter/game/Account.java @@ -9,6 +9,7 @@ import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Utils; import dev.morphia.annotations.IndexOptions; +import java.util.ArrayList; import java.util.List; @Entity(value = "accounts", noClassnameStored = true) @@ -28,7 +29,9 @@ public class Account { private List permissions; @Deprecated - public Account() {} + public Account() { + this.permissions = new ArrayList<>(); + } public String getId() { return id; diff --git a/src/main/java/emu/grasscutter/server/game/GameServer.java b/src/main/java/emu/grasscutter/server/game/GameServer.java index 87a76d400..329a28e61 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServer.java +++ b/src/main/java/emu/grasscutter/server/game/GameServer.java @@ -8,6 +8,7 @@ import emu.grasscutter.GenshinConstants; import emu.grasscutter.Grasscutter; import emu.grasscutter.commands.CommandMap; import emu.grasscutter.database.DatabaseHelper; +import emu.grasscutter.game.Account; import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.dungeons.DungeonManager; import emu.grasscutter.game.gacha.GachaManager; @@ -138,6 +139,14 @@ public final class GameServer extends MihoyoKcpServer { return player.getSocialDetail(); } + public Account getAccountByName(String username) { + Optional playerOpt = getPlayers().values().stream().filter(player -> player.getAccount().getUsername().equals(username)).findFirst(); + if (playerOpt.get() != null) { + return playerOpt.get().getAccount(); + } + return DatabaseHelper.getAccountByName(username); + } + public void onTick() throws Exception { for (GenshinPlayer player : this.getPlayers().values()) { player.onTick();