Skip to content

Commit 62323ed

Browse files
committed
refactor(commands): simplify playtime commands
1 parent 4e2ef4f commit 62323ed

8 files changed

Lines changed: 161 additions & 21 deletions

File tree

playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/command/PlayTimeCommand.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
import dev.rollczi.litecommands.annotations.command.Command;
1111
import dev.rollczi.litecommands.annotations.context.Context;
1212
import dev.rollczi.litecommands.annotations.execute.Execute;
13+
import dev.rollczi.litecommands.annotations.permission.Permission;
1314
import org.bukkit.command.CommandSender;
1415
import org.panda_lang.utilities.inject.annotations.Inject;
1516

16-
import java.time.Duration;
17-
1817
@LiteCommand
1918
@Command(name = "playtime")
2019
final class PlayTimeCommand {
@@ -35,9 +34,9 @@ final class PlayTimeCommand {
3534
}
3635

3736
@Execute
37+
@Permission(PlayTimeCommandPermissions.PLAYTIME)
3838
void playTime(@Context PlayTimeUser user) {
3939
PlayTime playTime = playTimeService.getCurrentPlayTime(user);
40-
4140
messageService.create()
4241
.player(user.getUuid())
4342
.notice(n -> n.playtimeMessages.playerPlayTimeSelf())
@@ -46,25 +45,14 @@ void playTime(@Context PlayTimeUser user) {
4645
}
4746

4847
@Execute
49-
void playTimeOther(@Context CommandSender sender, @Arg PlayTimeUser target) {
48+
@Permission(PlayTimeCommandPermissions.PLAYTIME_TARGET)
49+
void playTimeTarget(@Context CommandSender sender, @Arg PlayTimeUser target) {
5050
PlayTime playTime = playTimeService.getCurrentPlayTime(target);
51-
5251
messageService.create()
5352
.viewer(sender)
5453
.notice(n -> n.playtimeMessages.playerPlayTimeTarget())
5554
.placeholder("{PLAYER_NAME}", target.getName())
5655
.placeholder("{PLAYER_PLAYTIME}", durationService.format(playTime.toDuration()))
5756
.send();
5857
}
59-
60-
@Execute(name = "set")
61-
void setPlayTime(@Context CommandSender sender, @Arg PlayTimeUser target, @Arg Duration time) {
62-
playTimeService.setPlayTime(target, PlayTime.of(time));
63-
messageService.create()
64-
.viewer(sender)
65-
.notice(n -> n.playtimeMessages.playerPlayTimeUpdated())
66-
.placeholder("{PLAYER_NAME}", target.getName())
67-
.placeholder("{PLAYER_PLAYTIME}", durationService.format(time))
68-
.send();
69-
}
7058
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.github.imdmk.playtime.core.feature.playtime.command;
2+
3+
public final class PlayTimeCommandPermissions {
4+
5+
public static final String PLAYTIME = "command.playtime";
6+
public static final String PLAYTIME_TARGET = "command.playtime.target";
7+
8+
public static final String PLAYTIME_TOP = "command.playtime.top";
9+
public static final String PLAYTIME_TOP_INVALIDATE = "command.playtime.top.invalidate";
10+
11+
public static final String PLAYTIME_SET = "command.playtime.set";
12+
public static final String PLAYTIME_RESET = "command.playtime.reset";
13+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.PlayTimeService;
5+
import com.github.imdmk.playtime.core.feature.playtime.PlayTimeUser;
6+
import com.github.imdmk.playtime.core.injector.annotations.lite.LiteCommand;
7+
import com.github.imdmk.playtime.core.message.MessageService;
8+
import com.github.imdmk.playtime.core.time.DurationService;
9+
import dev.rollczi.litecommands.annotations.argument.Arg;
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.command.CommandSender;
15+
import org.panda_lang.utilities.inject.annotations.Inject;
16+
17+
import java.time.Duration;
18+
19+
@LiteCommand
20+
@Command(name = "playtime set")
21+
@Permission(PlayTimeCommandPermissions.PLAYTIME_SET)
22+
public final class PlayTimeSetCommand {
23+
24+
private final MessageService messageService;
25+
private final DurationService durationService;
26+
private final PlayTimeService playTimeService;
27+
28+
@Inject
29+
public PlayTimeSetCommand(
30+
MessageService messageService,
31+
DurationService durationService,
32+
PlayTimeService playTimeService
33+
) {
34+
this.messageService = messageService;
35+
this.durationService = durationService;
36+
this.playTimeService = playTimeService;
37+
}
38+
39+
@Execute
40+
void setPlayTime(@Context CommandSender sender, @Arg PlayTimeUser target, @Arg Duration time) {
41+
playTimeService.setPlayTime(target, PlayTime.of(time));
42+
messageService.create()
43+
.viewer(sender)
44+
.notice(n -> n.playtimeMessages.playerPlayTimeUpdated())
45+
.placeholder("{PLAYER_NAME}", target.getName())
46+
.placeholder("{PLAYER_PLAYTIME}", durationService.format(time))
47+
.send();
48+
}
49+
}

playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/command/PlayTimeUserArgument.java renamed to playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/command/handler/PlayTimeUserArgument.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.imdmk.playtime.core.feature.playtime.command;
1+
package com.github.imdmk.playtime.core.feature.playtime.command.handler;
22

33
import com.github.imdmk.playtime.core.feature.playtime.PlayTimeUser;
44
import com.github.imdmk.playtime.core.feature.playtime.PlayTimeUserService;
@@ -53,12 +53,20 @@ protected ParseResult<PlayTimeUser> parse(
5353
}
5454

5555
@Override
56-
protected boolean match(Invocation<CommandSender> invocation, Argument<PlayTimeUser> context, String argument) {
56+
protected boolean match(
57+
Invocation<CommandSender> invocation,
58+
Argument<PlayTimeUser> context,
59+
String argument
60+
) {
5761
return VALID_USER_PATTERN.matcher(argument).matches();
5862
}
5963

6064
@Override
61-
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<PlayTimeUser> argument, SuggestionContext context) {
65+
public SuggestionResult suggest(
66+
Invocation<CommandSender> invocation,
67+
Argument<PlayTimeUser> argument,
68+
SuggestionContext context
69+
) {
6270
return SuggestionResult.of(userService.cachedNames());
6371
}
6472
}

playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/command/PlayTimeUserContext.java renamed to playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/command/handler/PlayTimeUserContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.imdmk.playtime.core.feature.playtime.command;
1+
package com.github.imdmk.playtime.core.feature.playtime.command.handler;
22

33
import com.github.imdmk.playtime.core.feature.playtime.PlayTimeUser;
44
import com.github.imdmk.playtime.core.feature.playtime.PlayTimeUserService;
@@ -15,6 +15,8 @@
1515
final class PlayTimeUserContext
1616
implements ContextProvider<CommandSender, PlayTimeUser> {
1717

18+
private static final String PLAYER_ONLY_ERROR = "Only player can use this command!";
19+
1820
private final MessageConfig messageConfig;
1921
private final PlayTimeUserService userService;
2022

@@ -30,7 +32,7 @@ final class PlayTimeUserContext
3032
@Override
3133
public ContextResult<PlayTimeUser> provide(Invocation<CommandSender> invocation) {
3234
if (!(invocation.sender() instanceof Player player)) {
33-
return ContextResult.error("Only player can use this command!");
35+
return ContextResult.error(PLAYER_ONLY_ERROR);
3436
}
3537

3638
return ContextResult.completableFuture(

playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/message/ENPlayTimeMessages.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public final class ENPlayTimeMessages
4545
"<dark_gray>• <gray>Updated playtime for player <red>{PLAYER_NAME} <gray>to <red>{PLAYER_PLAYTIME}<dark_gray>."
4646
);
4747

48+
@Comment({
49+
"#",
50+
"# Sent to a command executor after resetting all players playtimes.",
51+
"#",
52+
})
53+
Notice playersPlayTimeReset = Notice.chat(
54+
"<dark_gray>• <gray>All players playtimes have been reset.<dark_gray>"
55+
);
56+
4857
@Comment({
4958
"#",
5059
"# Sent when the playtime top cache is invalidated.",
@@ -69,6 +78,11 @@ public Notice playerPlayTimeUpdated() {
6978
return playerPlayTimeUpdated;
7079
}
7180

81+
@Override
82+
public Notice playersPlayTimeReset() {
83+
return playersPlayTimeReset;
84+
}
85+
7286
@Override
7387
public Notice playTimeTopCacheInvalidated() {
7488
return playTimeTopCacheInvalidated;

playtime-core/src/main/java/com/github/imdmk/playtime/core/feature/playtime/message/PlayTimeMessages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public interface PlayTimeMessages {
88
Notice playerPlayTimeTarget();
99
Notice playerPlayTimeUpdated();
1010

11+
Notice playersPlayTimeReset();
12+
1113
Notice playTimeTopCacheInvalidated();
1214

1315
}

0 commit comments

Comments
 (0)