|
| 1 | +package com.github.imdmk.playtime.core.feature.playtime.command; |
| 2 | + |
| 3 | +import com.github.imdmk.playtime.api.PlayTime; |
| 4 | +import com.github.imdmk.playtime.core.feature.playtime.repository.PlayTimeUserRepository; |
| 5 | +import com.github.imdmk.playtime.core.injector.annotations.lite.LiteCommand; |
| 6 | +import com.github.imdmk.playtime.core.message.MessageService; |
| 7 | +import com.github.imdmk.playtime.core.platform.logger.PluginLogger; |
| 8 | +import com.github.imdmk.playtime.core.platform.playtime.PlayTimeAdapter; |
| 9 | +import com.github.imdmk.playtime.core.platform.scheduler.TaskScheduler; |
| 10 | +import dev.rollczi.litecommands.annotations.command.Command; |
| 11 | +import dev.rollczi.litecommands.annotations.context.Context; |
| 12 | +import dev.rollczi.litecommands.annotations.execute.Execute; |
| 13 | +import dev.rollczi.litecommands.annotations.permission.Permission; |
| 14 | +import org.bukkit.Server; |
| 15 | +import org.bukkit.command.CommandSender; |
| 16 | +import org.bukkit.entity.Player; |
| 17 | + |
| 18 | +@LiteCommand |
| 19 | +@Command(name = "playtime reset-all") |
| 20 | +@Permission(PlayTimeCommandPermissions.PLAYTIME_RESET) |
| 21 | +public final class PlayTimeResetAllCommand { |
| 22 | + |
| 23 | + private final Server server; |
| 24 | + private final PluginLogger logger; |
| 25 | + private final MessageService messageService; |
| 26 | + private final PlayTimeAdapter adapter; |
| 27 | + private final PlayTimeUserRepository repository; |
| 28 | + private final TaskScheduler scheduler; |
| 29 | + |
| 30 | + public PlayTimeResetAllCommand( |
| 31 | + Server server, |
| 32 | + PluginLogger logger, |
| 33 | + MessageService messageService, |
| 34 | + PlayTimeAdapter adapter, |
| 35 | + PlayTimeUserRepository repository, |
| 36 | + TaskScheduler scheduler |
| 37 | + ) { |
| 38 | + this.server = server; |
| 39 | + this.logger = logger; |
| 40 | + this.messageService = messageService; |
| 41 | + this.adapter = adapter; |
| 42 | + this.repository = repository; |
| 43 | + this.scheduler = scheduler; |
| 44 | + } |
| 45 | + |
| 46 | + @Execute |
| 47 | + void resetAll(@Context CommandSender sender) { |
| 48 | + repository.resetAllPlayTimes() |
| 49 | + .thenAccept(v -> { |
| 50 | + scheduler.runSync(this::resetOnlinePlayersPlayTime); |
| 51 | + messageService.send(sender, n -> n.playtimeMessages.playersPlayTimeReset()); |
| 52 | + }) |
| 53 | + .exceptionally(e -> { |
| 54 | + logger.error(e, "Failed to reset all playtimes"); |
| 55 | + return null; |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + private void resetOnlinePlayersPlayTime() { |
| 60 | + for (Player player : server.getOnlinePlayers()) { |
| 61 | + adapter.write(player, PlayTime.ZERO); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments