2022-04-21 06:48:24 +08:00
|
|
|
package emu.grasscutter.command.commands;
|
|
|
|
|
2022-07-13 10:33:57 +08:00
|
|
|
import at.favre.lib.crypto.bcrypt.BCrypt;
|
|
|
|
import emu.grasscutter.Configuration;
|
2022-05-10 01:03:51 +08:00
|
|
|
import emu.grasscutter.Grasscutter;
|
2022-04-21 06:48:24 +08:00
|
|
|
import emu.grasscutter.command.Command;
|
|
|
|
import emu.grasscutter.command.CommandHandler;
|
|
|
|
import emu.grasscutter.database.DatabaseHelper;
|
2022-05-10 01:03:51 +08:00
|
|
|
import emu.grasscutter.game.Account;
|
2022-04-27 12:24:25 +08:00
|
|
|
import emu.grasscutter.game.player.Player;
|
2022-04-21 06:48:24 +08:00
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-05-06 12:57:45 +08:00
|
|
|
import static emu.grasscutter.utils.Language.translate;
|
|
|
|
|
2022-07-13 10:33:57 +08:00
|
|
|
@Command(label = "account", usage = "account <create|delete|resetpass> <username> [uid|password] [uid] ", description = "commands.account.description", targetRequirement = Command.TargetRequirement.NONE)
|
2022-04-21 06:48:24 +08:00
|
|
|
public final class AccountCommand implements CommandHandler {
|
|
|
|
|
|
|
|
@Override
|
2022-05-04 14:32:09 +08:00
|
|
|
public void execute(Player sender, Player targetPlayer, List<String> args) {
|
2022-04-21 06:48:24 +08:00
|
|
|
if (sender != null) {
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(sender, translate(sender, "commands.generic.console_execute_error"));
|
2022-04-21 06:48:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.size() < 2) {
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.command_usage"));
|
2022-04-21 06:48:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String action = args.get(0);
|
|
|
|
String username = args.get(1);
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
default:
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.command_usage"));
|
2022-04-21 06:48:24 +08:00
|
|
|
return;
|
|
|
|
case "create":
|
|
|
|
int uid = 0;
|
2022-07-13 10:33:57 +08:00
|
|
|
String password = "";
|
|
|
|
|
|
|
|
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
|
|
|
if(args.size() < 3) {
|
|
|
|
CommandHandler.sendMessage(null, "EXPERIMENTAL_RealPassword requires a password argument");
|
|
|
|
CommandHandler.sendMessage(null, "Usage: account create <username> <password> [uid]");
|
|
|
|
|
2022-04-21 06:48:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-07-13 10:33:57 +08:00
|
|
|
password = args.get(2);
|
|
|
|
|
|
|
|
if (args.size() == 4) {
|
|
|
|
try {
|
|
|
|
uid = Integer.parseInt(args.get(3));
|
|
|
|
} catch (NumberFormatException ignored) {
|
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
|
|
|
|
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
|
|
|
CommandHandler.sendMessage(null, "EXPERIMENTAL_RealPassword requires argument 2 to be a password, not a uid");
|
|
|
|
CommandHandler.sendMessage(null, "Usage: account create <username> <password> [uid]");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (args.size() > 2) {
|
|
|
|
try {
|
|
|
|
uid = Integer.parseInt(args.get(2));
|
|
|
|
} catch (NumberFormatException ignored) {
|
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.invalid"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 06:48:24 +08:00
|
|
|
}
|
|
|
|
|
2022-06-04 10:32:10 +08:00
|
|
|
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithUid(username, uid);
|
2022-04-21 06:48:24 +08:00
|
|
|
if (account == null) {
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.exists"));
|
2022-04-21 06:48:24 +08:00
|
|
|
return;
|
|
|
|
} else {
|
2022-07-13 10:33:57 +08:00
|
|
|
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword == true) {
|
|
|
|
account.setPassword(BCrypt.withDefaults().hashToString(12, password.toCharArray()));
|
|
|
|
}
|
2022-04-28 06:30:04 +08:00
|
|
|
account.addPermission("*");
|
2022-04-21 06:48:24 +08:00
|
|
|
account.save(); // Save account to database.
|
2022-04-28 06:28:16 +08:00
|
|
|
|
2022-05-27 15:22:55 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.create", Integer.toString(account.getReservedPlayerUid())));
|
2022-04-21 06:48:24 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
case "delete":
|
2022-05-10 01:03:51 +08:00
|
|
|
// Get the account we want to delete.
|
|
|
|
Account toDelete = DatabaseHelper.getAccountByName(username);
|
|
|
|
|
|
|
|
if (toDelete == null) {
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.no_account"));
|
2022-05-10 01:03:51 +08:00
|
|
|
return;
|
2022-04-21 06:48:24 +08:00
|
|
|
}
|
2022-05-27 15:22:55 +08:00
|
|
|
|
2022-05-10 01:03:51 +08:00
|
|
|
// Get the player for the account.
|
|
|
|
// If that player is currently online, we kick them before proceeding with the deletion.
|
2022-05-27 15:22:55 +08:00
|
|
|
Player player = Grasscutter.getGameServer().getPlayerByAccountId(toDelete.getId());
|
2022-05-10 01:03:51 +08:00
|
|
|
if (player != null) {
|
|
|
|
player.getSession().close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we do the actual deletion.
|
|
|
|
DatabaseHelper.deleteAccount(toDelete);
|
2022-05-10 20:33:45 +08:00
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.delete"));
|
2022-07-13 10:33:57 +08:00
|
|
|
return;
|
|
|
|
case "resetpass":
|
|
|
|
if(Configuration.ACCOUNT.EXPERIMENTAL_RealPassword != true) {
|
|
|
|
CommandHandler.sendMessage(null, "resetpass requires EXPERIMENTAL_RealPassword to be true.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(args.size() != 3) {
|
|
|
|
CommandHandler.sendMessage(null, "Invalid Args");
|
|
|
|
CommandHandler.sendMessage(null, "Usage: account resetpass <username> <password>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Account toUpdate = DatabaseHelper.getAccountByName(username);
|
|
|
|
|
|
|
|
if (toUpdate == null) {
|
|
|
|
CommandHandler.sendMessage(null, translate(sender, "commands.account.no_account"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the player for the account.
|
|
|
|
// If that player is currently online, we kick them before proceeding with the deletion.
|
|
|
|
Player uPlayer = Grasscutter.getGameServer().getPlayerByAccountId(toUpdate.getId());
|
|
|
|
if (uPlayer != null) {
|
|
|
|
uPlayer.getSession().close();
|
|
|
|
}
|
|
|
|
|
|
|
|
toUpdate.setPassword(BCrypt.withDefaults().hashToString(12, args.get(2).toCharArray()));
|
|
|
|
toUpdate.save();
|
|
|
|
CommandHandler.sendMessage(null, "Password Updated.");
|
|
|
|
return;
|
2022-04-21 06:48:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|