Compare commits

..

No commits in common. "e6fa00e55d7cd61d20bc67d8faa9c0fbfb64fbbd" and "8b6da510dc2bf4c81acbed349beca63a30557a74" have entirely different histories.

5 changed files with 24 additions and 28 deletions

View File

@ -36,7 +36,7 @@ public final class DefaultAuthenticators {
// Check if account exists. // Check if account exists.
if(account == null && ACCOUNT.autoCreate) { if(account == null && ACCOUNT.autoCreate) {
// This account has been created AUTOMATICALLY. There will be no permissions added. // This account has been created AUTOMATICALLY. There will be no permissions added.
account = DatabaseHelper.createAccountWithUid(requestData.account, 0); account = DatabaseHelper.createAccountWithId(requestData.account, 0);
// Check if the account was created successfully. // Check if the account was created successfully.
if(account == null) { if(account == null) {

View File

@ -44,7 +44,7 @@ public final class AccountCommand implements CommandHandler {
} }
} }
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid); emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
if (account == null) { if (account == null) {
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists")); CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
return; return;

View File

@ -15,7 +15,7 @@ public final class SendMessageCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, Player targetPlayer, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() == 0) { if (args.size() == 0) {
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMessage.usage")); CommandHandler.sendMessage(null, translate(sender, "commands.sendMessage.usage"));
return; return;
} }

View File

@ -22,28 +22,32 @@ import static com.mongodb.client.model.Filters.eq;
public final class DatabaseHelper { public final class DatabaseHelper {
public static Account createAccount(String username) { public static Account createAccount(String username) {
return createAccountWithUid(username, 0); return createAccountWithId(username, 0);
} }
public static Account createAccountWithUid(String username, int reservedUid) { public static Account createAccountWithId(String username, int reservedId) {
// Unique names only // Unique names only
if (DatabaseHelper.checkIfAccountExists(username)) { Account exists = DatabaseHelper.getAccountByName(username);
if (exists != null) {
return null; return null;
} }
// Make sure there are no id collisions // Make sure there are no id collisions
if (reservedUid > 0) { if (reservedId > 0) {
// Cannot make account with the same uid as the server console // Cannot make account with the same uid as the server console
if (reservedUid == GameConstants.SERVER_CONSOLE_UID) { if (reservedId == GameConstants.SERVER_CONSOLE_UID) {
return null; return null;
} }
if (DatabaseHelper.checkIfAccountExists(reservedUid)) { // Make sure not other accounts has that id as its reservedPlayerId
exists = DatabaseHelper.getAccountByPlayerId(reservedId);
if (exists != null) {
return null; return null;
} }
// Make sure no existing player already has this id. // Make sure no existing player already has this id.
if (DatabaseHelper.checkIfPlayerExists(reservedUid)) { Player existsPlayer = DatabaseHelper.getPlayerByUid(reservedId);
if (existsPlayer != null) {
return null; return null;
} }
} }
@ -53,8 +57,8 @@ public final class DatabaseHelper {
account.setUsername(username); account.setUsername(username);
account.setId(Integer.toString(DatabaseManager.getNextId(account))); account.setId(Integer.toString(DatabaseManager.getNextId(account)));
if (reservedUid > 0) { if (reservedId > 0) {
account.setReservedPlayerUid(reservedUid); account.setReservedPlayerUid(reservedId);
} }
DatabaseHelper.saveAccount(account); DatabaseHelper.saveAccount(account);
@ -104,14 +108,6 @@ public final class DatabaseHelper {
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first(); return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", playerId)).first();
} }
public static boolean checkIfAccountExists(String name) {
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("username", name)).count() > 0;
}
public static boolean checkIfAccountExists(int reservedUid) {
return DatabaseManager.getGameDatastore().find(Account.class).filter(Filters.eq("reservedPlayerId", reservedUid)).count() > 0;
}
public static void deleteAccount(Account target) { public static void deleteAccount(Account target) {
// To delete an account, we need to also delete all the other documents in the database that reference the account. // To delete an account, we need to also delete all the other documents in the database that reference the account.
// This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the // This should optimally be wrapped inside a transaction, to make sure an error thrown mid-way does not leave the
@ -156,21 +152,21 @@ public final class DatabaseHelper {
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first(); return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first();
} }
public static boolean checkIfPlayerExists(int uid) { public static boolean checkPlayerExists(int uid) {
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0; return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0;
} }
public static synchronized Player generatePlayerUid(Player character, int reservedId) { public static synchronized Player generatePlayerUid(Player character, int reservedId) {
// Check if reserved id // Check if reserved id
int id; int id;
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) { if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId; id = reservedId;
character.setUid(id); character.setUid(id);
} else { } else {
do { do {
id = DatabaseManager.getNextId(character); id = DatabaseManager.getNextId(character);
} }
while (checkIfPlayerExists(id)); while (checkPlayerExists(id));
character.setUid(id); character.setUid(id);
} }
// Save to database // Save to database
@ -181,13 +177,13 @@ public final class DatabaseHelper {
public static synchronized int getNextPlayerId(int reservedId) { public static synchronized int getNextPlayerId(int reservedId) {
// Check if reserved id // Check if reserved id
int id; int id;
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) { if (reservedId > 0 && !checkPlayerExists(reservedId)) {
id = reservedId; id = reservedId;
} else { } else {
do { do {
id = DatabaseManager.getNextId(Player.class); id = DatabaseManager.getNextId(Player.class);
} }
while (checkIfPlayerExists(id)); while (checkPlayerExists(id));
} }
return id; return id;
} }

View File

@ -136,7 +136,7 @@
"coop": { "coop": {
"usage": "Usage: coop [host UID]", "usage": "Usage: coop [host UID]",
"success": "Summoned %s to %s's world.", "success": "Summoned %s to %s's world.",
"description": "Forces someone to join the world of others. If no one is targeted, it sends you into co-op mode anyway." "description": "Forces someone to join the world of others"
}, },
"enter_dungeon": { "enter_dungeon": {
"usage": "Usage: enterdungeon <dungeonID>", "usage": "Usage: enterdungeon <dungeonID>",