From 2e01c7d5ad5f46a6bb504b38d1ad8bc2c9a0ec16 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Mon, 18 Apr 2022 22:23:22 -0400 Subject: [PATCH] Find by player's UID not IID --- .../emu/grasscutter/commands/PlayerCommands.java | 8 ++++---- .../emu/grasscutter/commands/ServerCommands.java | 6 +++--- .../emu/grasscutter/database/DatabaseHelper.java | 1 + .../emu/grasscutter/server/game/GameServer.java | 13 +++++++------ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/emu/grasscutter/commands/PlayerCommands.java b/src/main/java/emu/grasscutter/commands/PlayerCommands.java index f427409c2..e8816a188 100644 --- a/src/main/java/emu/grasscutter/commands/PlayerCommands.java +++ b/src/main/java/emu/grasscutter/commands/PlayerCommands.java @@ -51,7 +51,7 @@ public final class PlayerCommands { case 2: try { target = Integer.parseInt(args.get(0)); - if(Grasscutter.getGameServer().getPlayerById(target) == null) { + if(Grasscutter.getGameServer().getPlayerByUid(target) == null) { target = player.getId(); amount = Integer.parseInt(args.get(1)); item = Integer.parseInt(args.get(0)); } else { @@ -66,7 +66,7 @@ public final class PlayerCommands { case 3: try { target = Integer.parseInt(args.get(0)); - if(Grasscutter.getGameServer().getPlayerById(target) == null) { + if(Grasscutter.getGameServer().getPlayerByUid(target) == null) { CommandHandler.sendMessage(player, "Invalid player ID."); return; } @@ -80,7 +80,7 @@ public final class PlayerCommands { break; } - GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target); + GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if(targetPlayer == null) { CommandHandler.sendMessage(player, "Player not found."); return; } @@ -107,7 +107,7 @@ public final class PlayerCommands { int item = Integer.parseInt(args.get(1)); int amount = 1; if(args.size() > 2) amount = Integer.parseInt(args.get(2)); - GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target); + GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if(targetPlayer == null) { CommandHandler.sendMessage(null, "Player not found."); return; } diff --git a/src/main/java/emu/grasscutter/commands/ServerCommands.java b/src/main/java/emu/grasscutter/commands/ServerCommands.java index 193ec2b50..5b2148b67 100644 --- a/src/main/java/emu/grasscutter/commands/ServerCommands.java +++ b/src/main/java/emu/grasscutter/commands/ServerCommands.java @@ -42,7 +42,7 @@ public final class ServerCommands { int target = Integer.parseInt(args.get(0)); String message = String.join(" ", args.subList(1, args.size())); - GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target); + GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if(targetPlayer == null) { CommandHandler.sendMessage(null, "Player not found."); return; } @@ -64,7 +64,7 @@ public final class ServerCommands { int target = Integer.parseInt(args.get(0)); String message = String.join(" ", args.subList(1, args.size())); - GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerById(target); + GenshinPlayer targetPlayer = Grasscutter.getGameServer().getPlayerByUid(target); if(targetPlayer == null) { CommandHandler.sendMessage(null, "Player not found."); return; } @@ -108,7 +108,7 @@ public final class ServerCommands { Account account = DatabaseHelper.createAccountWithId(username, uid); if(account == null) { CommandHandler.sendMessage(null, "Account already exists."); return; - } else CommandHandler.sendMessage(null, "Account created with UID " + account.getId() + "."); + } else CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerId() + "."); return; case "delete": if(DatabaseHelper.deleteAccount(username)) { diff --git a/src/main/java/emu/grasscutter/database/DatabaseHelper.java b/src/main/java/emu/grasscutter/database/DatabaseHelper.java index d27e47030..905c4ef64 100644 --- a/src/main/java/emu/grasscutter/database/DatabaseHelper.java +++ b/src/main/java/emu/grasscutter/database/DatabaseHelper.java @@ -36,6 +36,7 @@ public class DatabaseHelper { if (reservedId == GenshinConstants.SERVER_CONSOLE_UID) { return null; } + exists = DatabaseHelper.getAccountByPlayerId(reservedId); if (exists != null) { return null; diff --git a/src/main/java/emu/grasscutter/server/game/GameServer.java b/src/main/java/emu/grasscutter/server/game/GameServer.java index 263c0cc72..deb215f0d 100644 --- a/src/main/java/emu/grasscutter/server/game/GameServer.java +++ b/src/main/java/emu/grasscutter/server/game/GameServer.java @@ -1,12 +1,7 @@ package emu.grasscutter.server.game; import java.net.InetSocketAddress; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import emu.grasscutter.GenshinConstants; @@ -115,6 +110,12 @@ public final class GameServer extends MihoyoKcpServer { return this.getPlayers().get(id); } + public GenshinPlayer getPlayerByUid(int uid) { + return this.getPlayers().values().stream() + .filter(player -> player.getAccount().getPlayerId() == uid) + .findFirst().orElse(this.getPlayerById(uid)); + } + public GenshinPlayer forceGetPlayerById(int id) { // Console check if (id == GenshinConstants.SERVER_CONSOLE_UID) {