diff --git a/spenttime-plugin/src/main/java/com/github/imdmk/spenttime/litecommands/implementation/SpentTimeCommand.java b/spenttime-plugin/src/main/java/com/github/imdmk/spenttime/litecommands/implementation/SpentTimeCommand.java index b6c21ea..13cf461 100644 --- a/spenttime-plugin/src/main/java/com/github/imdmk/spenttime/litecommands/implementation/SpentTimeCommand.java +++ b/spenttime-plugin/src/main/java/com/github/imdmk/spenttime/litecommands/implementation/SpentTimeCommand.java @@ -8,6 +8,7 @@ import com.github.imdmk.spenttime.user.User; import com.github.imdmk.spenttime.util.DurationUtil; import dev.rollczi.litecommands.annotations.argument.Arg; +import dev.rollczi.litecommands.annotations.async.Async; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; import dev.rollczi.litecommands.annotations.execute.Execute; @@ -15,6 +16,8 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.time.Duration; + @Command(name = "spenttime") public class SpentTimeCommand { @@ -41,17 +44,30 @@ void showSpentTime(@Context Player player) { this.notificationSender.send(player, notification); } + @Async @Execute @Permission("command.spenttime.target") void showTarget(@Context CommandSender sender, @Arg User target) { - String userSpentTime = DurationUtil.toHumanReadable(target.getSpentTimeDuration()); + String targetName = target.getName(); + String targetSpentTime = DurationUtil.toHumanReadable(this.updateSpentTime(target)); Notification notification = new NotificationFormatter() .notification(this.notificationSettings.targetSpentTime) - .placeholder("{PLAYER}", target.getName()) - .placeholder("{TIME}", userSpentTime) + .placeholder("{PLAYER}", targetName) + .placeholder("{TIME}", targetSpentTime) .build(); this.notificationSender.send(sender, notification); } + + private Duration updateSpentTime(User target) { + Duration playerSpentTime = this.bukkitPlayerSpentTimeService.getSpentTime(target.getUuid()); + Duration userSpentTime = target.getSpentTimeDuration(); + + if (!playerSpentTime.equals(userSpentTime)) { + target.setSpentTime(playerSpentTime); + } + + return playerSpentTime; + } }