use of com.djrapitops.plan.storage.database.transactions.commands.RemoveWebUserTransaction in project Plan by plan-player-analytics.
the class RegistrationCommands method onUnregister.
public void onUnregister(String mainCommand, CMDSender sender, Arguments arguments) {
Optional<String> givenUsername = arguments.get(0).filter(arg -> sender.hasPermission(Permissions.UNREGISTER_OTHER));
Database database = dbSystem.getDatabase();
UUID playerUUID = sender.getUUID().orElse(null);
String username;
if (!givenUsername.isPresent() && playerUUID != null) {
Optional<User> found = database.query(WebUserQueries.fetchUser(playerUUID));
if (!found.isPresent()) {
throw new IllegalArgumentException(locale.getString(CommandLang.USER_NOT_LINKED));
}
username = found.get().getUsername();
} else if (!givenUsername.isPresent()) {
throw new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, "<" + locale.getString(HelpLang.ARG_USERNAME) + ">"));
} else {
username = givenUsername.get();
}
Optional<User> found = database.query(WebUserQueries.fetchUser(username));
if (!found.isPresent()) {
throw new IllegalArgumentException(locale.getString(FailReason.USER_DOES_NOT_EXIST));
}
User presentUser = found.get();
boolean ownsTheUser = Objects.equals(playerUUID, presentUser.getLinkedToUUID());
if (!(ownsTheUser || sender.hasPermission(Permissions.UNREGISTER_OTHER.getPerm()))) {
throw new IllegalArgumentException(locale.getString(CommandLang.USER_NOT_LINKED));
}
if (sender.supportsChatEvents()) {
sender.buildMessage().addPart(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_UNREGISTER, presentUser.getUsername(), presentUser.getLinkedTo())).newLine().addPart(colors.getTertiaryColor() + locale.getString(CommandLang.CONFIRM)).addPart("§2§l[\u2714]").command("/" + mainCommand + " accept").hover(locale.getString(CommandLang.CONFIRM_ACCEPT)).addPart(" ").addPart("§4§l[\u2718]").command("/" + mainCommand + " cancel").hover(locale.getString(CommandLang.CONFIRM_DENY)).send();
} else {
sender.buildMessage().addPart(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_UNREGISTER, presentUser.getUsername(), presentUser.getLinkedTo())).newLine().addPart(colors.getTertiaryColor() + locale.getString(CommandLang.CONFIRM)).addPart("§a/" + mainCommand + " accept").addPart(" ").addPart("§c/" + mainCommand + " cancel").send();
}
confirmation.confirm(sender, choice -> {
if (Boolean.TRUE.equals(choice)) {
try {
sender.send(colors.getMainColor() + locale.getString(CommandLang.UNREGISTER, presentUser.getUsername()));
database.executeTransaction(new RemoveWebUserTransaction(username)).get();
ActiveCookieStore.removeUserCookie(username);
sender.send(locale.getString(CommandLang.PROGRESS_SUCCESS));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
errorLogger.warn(e, ErrorContext.builder().related("unregister command", sender, sender.getPlayerName().orElse("console"), arguments).build());
}
} else {
sender.send(colors.getMainColor() + locale.getString(CommandLang.CONFIRM_CANCELLED_UNREGISTER, presentUser.getUsername()));
}
});
}
Aggregations