mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-06-26 01:54:51 +08:00
Compare commits
2 Commits
8b6da510dc
...
e6fa00e55d
Author | SHA1 | Date | |
---|---|---|---|
|
e6fa00e55d | ||
|
40bb4daa38 |
@ -36,7 +36,7 @@ public final class DefaultAuthenticators {
|
||||
// Check if account exists.
|
||||
if(account == null && ACCOUNT.autoCreate) {
|
||||
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
||||
account = DatabaseHelper.createAccountWithId(requestData.account, 0);
|
||||
account = DatabaseHelper.createAccountWithUid(requestData.account, 0);
|
||||
|
||||
// Check if the account was created successfully.
|
||||
if(account == null) {
|
||||
|
@ -44,7 +44,7 @@ public final class AccountCommand implements CommandHandler {
|
||||
}
|
||||
}
|
||||
|
||||
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
|
||||
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
|
||||
if (account == null) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
|
||||
return;
|
||||
|
@ -15,7 +15,7 @@ public final class SendMessageCommand implements CommandHandler {
|
||||
@Override
|
||||
public void execute(Player sender, Player targetPlayer, List<String> args) {
|
||||
if (args.size() == 0) {
|
||||
CommandHandler.sendMessage(null, translate(sender, "commands.sendMessage.usage"));
|
||||
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMessage.usage"));
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -22,32 +22,28 @@ import static com.mongodb.client.model.Filters.eq;
|
||||
|
||||
public final class DatabaseHelper {
|
||||
public static Account createAccount(String username) {
|
||||
return createAccountWithId(username, 0);
|
||||
return createAccountWithUid(username, 0);
|
||||
}
|
||||
|
||||
public static Account createAccountWithId(String username, int reservedId) {
|
||||
public static Account createAccountWithUid(String username, int reservedUid) {
|
||||
// Unique names only
|
||||
Account exists = DatabaseHelper.getAccountByName(username);
|
||||
if (exists != null) {
|
||||
if (DatabaseHelper.checkIfAccountExists(username)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make sure there are no id collisions
|
||||
if (reservedId > 0) {
|
||||
if (reservedUid > 0) {
|
||||
// Cannot make account with the same uid as the server console
|
||||
if (reservedId == GameConstants.SERVER_CONSOLE_UID) {
|
||||
if (reservedUid == GameConstants.SERVER_CONSOLE_UID) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Make sure not other accounts has that id as its reservedPlayerId
|
||||
exists = DatabaseHelper.getAccountByPlayerId(reservedId);
|
||||
if (exists != null) {
|
||||
|
||||
if (DatabaseHelper.checkIfAccountExists(reservedUid)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Make sure no existing player already has this id.
|
||||
Player existsPlayer = DatabaseHelper.getPlayerByUid(reservedId);
|
||||
if (existsPlayer != null) {
|
||||
if (DatabaseHelper.checkIfPlayerExists(reservedUid)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -57,8 +53,8 @@ public final class DatabaseHelper {
|
||||
account.setUsername(username);
|
||||
account.setId(Integer.toString(DatabaseManager.getNextId(account)));
|
||||
|
||||
if (reservedId > 0) {
|
||||
account.setReservedPlayerUid(reservedId);
|
||||
if (reservedUid > 0) {
|
||||
account.setReservedPlayerUid(reservedUid);
|
||||
}
|
||||
|
||||
DatabaseHelper.saveAccount(account);
|
||||
@ -107,6 +103,14 @@ public final class DatabaseHelper {
|
||||
public static Account getAccountByPlayerId(int playerId) {
|
||||
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) {
|
||||
// To delete an account, we need to also delete all the other documents in the database that reference the account.
|
||||
@ -152,21 +156,21 @@ public final class DatabaseHelper {
|
||||
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("accountId", account.getId())).first();
|
||||
}
|
||||
|
||||
public static boolean checkPlayerExists(int uid) {
|
||||
public static boolean checkIfPlayerExists(int uid) {
|
||||
return DatabaseManager.getGameDatastore().find(Player.class).filter(Filters.eq("_id", uid)).count() > 0;
|
||||
}
|
||||
|
||||
public static synchronized Player generatePlayerUid(Player character, int reservedId) {
|
||||
// Check if reserved id
|
||||
int id;
|
||||
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
|
||||
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
|
||||
id = reservedId;
|
||||
character.setUid(id);
|
||||
} else {
|
||||
do {
|
||||
id = DatabaseManager.getNextId(character);
|
||||
}
|
||||
while (checkPlayerExists(id));
|
||||
while (checkIfPlayerExists(id));
|
||||
character.setUid(id);
|
||||
}
|
||||
// Save to database
|
||||
@ -177,13 +181,13 @@ public final class DatabaseHelper {
|
||||
public static synchronized int getNextPlayerId(int reservedId) {
|
||||
// Check if reserved id
|
||||
int id;
|
||||
if (reservedId > 0 && !checkPlayerExists(reservedId)) {
|
||||
if (reservedId > 0 && !checkIfPlayerExists(reservedId)) {
|
||||
id = reservedId;
|
||||
} else {
|
||||
do {
|
||||
id = DatabaseManager.getNextId(Player.class);
|
||||
}
|
||||
while (checkPlayerExists(id));
|
||||
while (checkIfPlayerExists(id));
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
@ -136,7 +136,7 @@
|
||||
"coop": {
|
||||
"usage": "Usage: coop [host UID]",
|
||||
"success": "Summoned %s to %s's world.",
|
||||
"description": "Forces someone to join the world of others"
|
||||
"description": "Forces someone to join the world of others. If no one is targeted, it sends you into co-op mode anyway."
|
||||
},
|
||||
"enter_dungeon": {
|
||||
"usage": "Usage: enterdungeon <dungeonID>",
|
||||
|
Loading…
x
Reference in New Issue
Block a user