Skip to content

Commit

Permalink
Added gui confirmation when trying to reset a player's time.
Browse files Browse the repository at this point in the history
Renamed reset-time to reset subcommand.
Renamed reset-time-all to reset-all subcommand.
Command permission fixes.
  • Loading branch information
imDMK committed Aug 8, 2023
1 parent 6a0a34b commit 3fcf7f7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ public SpentTimeResetCommand(Server server, MessageConfiguration messageConfigur
this.taskScheduler = taskScheduler;
}

@Async
@Execute(route = "reset-time", required = 1)
void resetTime(CommandSender sender, @Arg @Name("target") Player player) {
this.userManager.getOrFindUser(player.getUniqueId()).ifPresent(user -> {
user.setSpentTime(0L);
this.userRepository.save(user);
});

player.setStatistic(Statistic.PLAY_ONE_MINUTE, 0);
player.saveData();
@Execute(route = "reset", required = 1)
void resetTime(CommandSender sender, @Arg @Name("target") Player target) {
if (sender instanceof Player player) {
new ConfirmGui(this.taskScheduler, "<red>Reset " + target.getName() + " player spent time?")
.afterConfirm(event -> {
player.closeInventory();

Notification notification = Notification.builder()
.fromNotification(this.messageConfiguration.targetResetTimeNotification)
.placeholder("{PLAYER}", player.getName())
.build();
this.taskScheduler.runAsync(() -> this.resetSpentTime(target));
this.sendTargetResetNotification(sender, target);
})
.afterCancel(event -> player.closeInventory())
.open(player, false);
return;
}

this.notificationSender.sendMessage(sender, notification);
this.taskScheduler.runAsync(() -> this.resetSpentTime(target));
this.sendTargetResetNotification(sender, target);
}

@Execute(route = "reset-time-all")
@Execute(route = "reset-all")
void resetTimeAll(CommandSender sender) {
if (sender instanceof Player player) {
new ConfirmGui(this.taskScheduler, "<red>Reset spent time of all users?")
Expand All @@ -75,16 +75,32 @@ void resetTimeAll(CommandSender sender) {
this.notificationSender.sendMessage(sender, this.messageConfiguration.resetSpentTimeForAllUsersNotification);
}

private void resetSpentTime(Player player) {
this.userManager.getOrFindUser(player.getUniqueId())
.ifPresent(user -> {
user.setSpentTime(0L);
this.userRepository.save(user);
});

player.setStatistic(Statistic.PLAY_ONE_MINUTE, 0);
player.saveData();
}

private void resetTimeAll() {
this.userRepository.dropTable();
this.userRepository.createTable();

for (OfflinePlayer offlinePlayer : this.server.getOfflinePlayers()) {
if (!offlinePlayer.hasPlayedBefore()) {
return;
}

offlinePlayer.setStatistic(Statistic.PLAY_ONE_MINUTE, 0);
}
}

private void sendTargetResetNotification(CommandSender sender, Player target) {
Notification notification = Notification.builder()
.fromNotification(this.messageConfiguration.targetResetTimeNotification)
.placeholder("{PLAYER}", target.getName())
.build();

this.notificationSender.sendMessage(sender, notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public SpentTimeCommandEditor(PluginConfiguration pluginConfiguration) {

@Override
public State edit(State state) {
state.editChild("reset-time", s -> s.permission(List.of(this.pluginConfiguration.spentTimeResetTimeCommandPermission)));
state.editChild("reset-time-for-all", s -> s.permission(List.of(this.pluginConfiguration.spentTimeResetTimeForAllCommandPermission)));
state.editChild("reset", s -> s.permission(List.of(this.pluginConfiguration.spentTimeResetTimeCommandPermission)));
state.editChild("reset-all", s -> s.permission(List.of(this.pluginConfiguration.spentTimeResetTimeForAllCommandPermission)));

return state;
}
Expand Down

0 comments on commit 3fcf7f7

Please sign in to comment.