mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-25 03:53:00 +08:00
Add account list command
This commit is contained in:
parent
7c065b33d6
commit
510e6bfa78
@ -1,145 +1,163 @@
|
|||||||
package emu.grasscutter.command.commands;
|
package emu.grasscutter.command.commands;
|
||||||
|
|
||||||
import static emu.grasscutter.utils.Language.translate;
|
import static emu.grasscutter.utils.Language.translate;
|
||||||
|
|
||||||
import at.favre.lib.crypto.bcrypt.BCrypt;
|
import at.favre.lib.crypto.bcrypt.BCrypt;
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.command.Command;
|
import emu.grasscutter.command.Command;
|
||||||
import emu.grasscutter.command.CommandHandler;
|
import emu.grasscutter.command.CommandHandler;
|
||||||
import emu.grasscutter.config.Configuration;
|
import emu.grasscutter.config.Configuration;
|
||||||
import emu.grasscutter.database.DatabaseHelper;
|
import emu.grasscutter.database.DatabaseHelper;
|
||||||
import emu.grasscutter.game.Account;
|
import emu.grasscutter.database.DatabaseManager;
|
||||||
import emu.grasscutter.game.player.Player;
|
import emu.grasscutter.game.Account;
|
||||||
import java.util.List;
|
import emu.grasscutter.game.player.Player;
|
||||||
|
import java.util.List;
|
||||||
@Command(
|
import java.util.stream.Collectors;
|
||||||
label = "account",
|
|
||||||
usage = {
|
@Command(
|
||||||
"create <username> [<UID>]", // Only with EXPERIMENTAL_RealPassword == false
|
label = "account",
|
||||||
"delete <username>",
|
usage = {
|
||||||
"create <username> <password> [<UID>]", // Only with EXPERIMENTAL_RealPassword == true
|
"create <username> [<UID>]", // Only with EXPERIMENTAL_RealPassword == false
|
||||||
"resetpass <username> <password>"
|
"delete <username>",
|
||||||
}, // Only with EXPERIMENTAL_RealPassword == true
|
"create <username> <password> [<UID>]", // Only with EXPERIMENTAL_RealPassword == true
|
||||||
targetRequirement = Command.TargetRequirement.NONE)
|
"resetpass <username> <password>"
|
||||||
public final class AccountCommand implements CommandHandler {
|
}, // Only with EXPERIMENTAL_RealPassword == true
|
||||||
@Override
|
targetRequirement = Command.TargetRequirement.NONE)
|
||||||
public void execute(Player sender, Player targetPlayer, List<String> args) {
|
public final class AccountCommand implements CommandHandler {
|
||||||
if (sender != null) {
|
@Override
|
||||||
CommandHandler.sendTranslatedMessage(sender, "commands.generic.console_execute_error");
|
public void execute(Player sender, Player targetPlayer, List<String> args) {
|
||||||
return;
|
if (sender != null) {
|
||||||
}
|
CommandHandler.sendTranslatedMessage(sender, "commands.generic.console_execute_error");
|
||||||
|
return;
|
||||||
if (args.size() < 2) {
|
}
|
||||||
sendUsageMessage(sender);
|
|
||||||
return;
|
if (args.size() < 2) {
|
||||||
}
|
sendUsageMessage(sender);
|
||||||
|
return;
|
||||||
String action = args.get(0);
|
}
|
||||||
String username = args.get(1);
|
|
||||||
|
String action = args.get(0);
|
||||||
switch (action) {
|
String username = args.get(1);
|
||||||
default:
|
|
||||||
sendUsageMessage(sender);
|
switch (action) {
|
||||||
return;
|
default -> this.sendUsageMessage(sender);
|
||||||
case "create":
|
case "create" -> {
|
||||||
int uid = 0;
|
int uid = 0;
|
||||||
String password = "";
|
String password = "";
|
||||||
|
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
||||||
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
if (args.size() < 3) {
|
||||||
if (args.size() < 3) {
|
CommandHandler.sendMessage(
|
||||||
CommandHandler.sendMessage(
|
sender, "EXPERIMENTAL_RealPassword requires a password argument");
|
||||||
sender, "EXPERIMENTAL_RealPassword requires a password argument");
|
CommandHandler.sendMessage(sender, "Usage: account create <username> <password> [uid]");
|
||||||
CommandHandler.sendMessage(sender, "Usage: account create <username> <password> [uid]");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
password = args.get(2);
|
||||||
password = args.get(2);
|
|
||||||
|
if (args.size() == 4) {
|
||||||
if (args.size() == 4) {
|
try {
|
||||||
try {
|
uid = Integer.parseInt(args.get(3));
|
||||||
uid = Integer.parseInt(args.get(3));
|
} catch (NumberFormatException ignored) {
|
||||||
} catch (NumberFormatException ignored) {
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
|
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
||||||
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
CommandHandler.sendMessage(
|
||||||
CommandHandler.sendMessage(
|
sender,
|
||||||
sender,
|
"EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid");
|
||||||
"EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid");
|
CommandHandler.sendMessage(
|
||||||
CommandHandler.sendMessage(
|
sender, "Usage: account create <username> <password> [uid]");
|
||||||
sender, "Usage: account create <username> <password> [uid]");
|
}
|
||||||
}
|
return;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
if (args.size() > 2) {
|
||||||
if (args.size() > 2) {
|
try {
|
||||||
try {
|
uid = Integer.parseInt(args.get(2));
|
||||||
uid = Integer.parseInt(args.get(2));
|
} catch (NumberFormatException ignored) {
|
||||||
} catch (NumberFormatException ignored) {
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.invalid"));
|
return;
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
Account account = DatabaseHelper.createAccountWithUid(username, uid);
|
||||||
|
if (account == null) {
|
||||||
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists"));
|
||||||
if (account == null) {
|
return;
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.exists"));
|
} else {
|
||||||
return;
|
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
||||||
} else {
|
account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray()));
|
||||||
if (Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
}
|
||||||
account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray()));
|
account.addPermission("*");
|
||||||
}
|
account.save(); // Save account to database.
|
||||||
account.addPermission("*");
|
|
||||||
account.save(); // Save account to database.
|
CommandHandler.sendMessage(
|
||||||
|
sender, translate(sender, "commands.account.create", account.getReservedPlayerUid()));
|
||||||
CommandHandler.sendMessage(
|
}
|
||||||
sender, translate(sender, "commands.account.create", account.getReservedPlayerUid()));
|
}
|
||||||
}
|
case "delete" -> {
|
||||||
return;
|
// Get the account we want to delete.
|
||||||
case "delete":
|
Account toDelete = DatabaseHelper.getAccountByName(username);
|
||||||
// Get the account we want to delete.
|
if (toDelete == null) {
|
||||||
Account toDelete = DatabaseHelper.getAccountByName(username);
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
|
||||||
|
return;
|
||||||
if (toDelete == null) {
|
}
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
|
DatabaseHelper.deleteAccount(toDelete);
|
||||||
return;
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete"));
|
||||||
}
|
}
|
||||||
|
case "resetpass" -> {
|
||||||
DatabaseHelper.deleteAccount(toDelete);
|
if (!Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.delete"));
|
CommandHandler.sendMessage(
|
||||||
return;
|
sender, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
|
||||||
case "resetpass":
|
return;
|
||||||
if (!Configuration.ACCOUNT.EXPERIMENTAL_RealPassword) {
|
}
|
||||||
CommandHandler.sendMessage(
|
if (args.size() != 3) {
|
||||||
sender, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
|
CommandHandler.sendMessage(sender, "Invalid Args");
|
||||||
return;
|
CommandHandler.sendMessage(sender, "Usage: account resetpass <username> <password>");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if (args.size() != 3) {
|
Account toUpdate = DatabaseHelper.getAccountByName(username);
|
||||||
CommandHandler.sendMessage(sender, "Invalid Args");
|
if (toUpdate == null) {
|
||||||
CommandHandler.sendMessage(sender, "Usage: account resetpass <username> <password>");
|
CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Account toUpdate = DatabaseHelper.getAccountByName(username);
|
// Make sure player can't stay logged in with old password.
|
||||||
|
kickAccount(toUpdate);
|
||||||
if (toUpdate == null) {
|
toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
|
||||||
CommandHandler.sendMessage(sender, translate(sender, "commands.account.no_account"));
|
toUpdate.save();
|
||||||
return;
|
CommandHandler.sendMessage(sender, "Password Updated.");
|
||||||
}
|
}
|
||||||
|
case "list" -> {
|
||||||
// Make sure player can't stay logged in with old password.
|
CommandHandler.sendMessage(sender, "Note: This command might take a while to complete.");
|
||||||
kickAccount(toUpdate);
|
CommandHandler.sendMessage(sender,
|
||||||
|
"Accounts: \n" + DatabaseManager.getAccountDatastore()
|
||||||
toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
|
.find(Account.class).stream()
|
||||||
toUpdate.save();
|
.map(acc -> "%s: %s (%s)".formatted(
|
||||||
CommandHandler.sendMessage(sender, "Password Updated.");
|
acc.getId(), acc.getUsername(),
|
||||||
}
|
acc.getReservedPlayerUid() == 0 ?
|
||||||
}
|
this.getPlayerUid(acc) :
|
||||||
|
acc.getReservedPlayerUid()))
|
||||||
private void kickAccount(Account account) {
|
.collect(Collectors.joining("\n"))
|
||||||
Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId());
|
);
|
||||||
if (player != null) {
|
}
|
||||||
player.getSession().close();
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
/**
|
||||||
|
* Returns the UID of the player associated with the given account.
|
||||||
|
* If the player is not found, returns "no UID".
|
||||||
|
*
|
||||||
|
* @param account The account to get the UID of.
|
||||||
|
* @return The UID of the player associated with the given account.
|
||||||
|
*/
|
||||||
|
private String getPlayerUid(Account account) {
|
||||||
|
var player = DatabaseHelper.getPlayerByAccount(account, Player.class);
|
||||||
|
return player == null ? "no UID" : String.valueOf(player.getUid());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void kickAccount(Account account) {
|
||||||
|
Player player = Grasscutter.getGameServer().getPlayerByAccountId(account.getId());
|
||||||
|
if (player != null) {
|
||||||
|
player.getSession().close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user