Skip to content

Commit 615022b

Browse files
imDMKvLuckyyy
andauthored
GH-903 Add seen command feature (#918)
* Add seen feature. * Improvements. * Fix conflict. * Follow coderabbit review * Follow team review. * Fix * Follow review feedback. * Follow other messages files. * Allow use OfflinePlayer. --------- Co-authored-by: Martin Sulikowski <[email protected]>
1 parent 17e9e0c commit 615022b

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.eternalcode.core.feature.seen;
2+
3+
import com.eternalcode.annotations.scan.command.DescriptionDocs;
4+
import com.eternalcode.core.injector.annotations.Inject;
5+
import com.eternalcode.core.notice.NoticeService;
6+
import com.eternalcode.core.user.User;
7+
import com.eternalcode.core.util.DurationUtil;
8+
import com.eternalcode.core.viewer.Viewer;
9+
import dev.rollczi.litecommands.annotations.argument.Arg;
10+
import dev.rollczi.litecommands.annotations.async.Async;
11+
import dev.rollczi.litecommands.annotations.command.Command;
12+
import dev.rollczi.litecommands.annotations.context.Context;
13+
import dev.rollczi.litecommands.annotations.execute.Execute;
14+
import dev.rollczi.litecommands.annotations.permission.Permission;
15+
import org.bukkit.OfflinePlayer;
16+
import org.bukkit.Server;
17+
18+
import java.time.Duration;
19+
import java.time.Instant;
20+
import org.bukkit.entity.Player;
21+
22+
@Command(name = "seen", aliases = { "lastonline" })
23+
@Permission("eternalcore.seen")
24+
class SeenCommand {
25+
26+
public static final int NEVER_JOINED_BEFORE = 0;
27+
private final Server server;
28+
private final NoticeService noticeService;
29+
30+
@Inject
31+
public SeenCommand(Server server, NoticeService noticeService) {
32+
this.server = server;
33+
this.noticeService = noticeService;
34+
}
35+
36+
@Execute
37+
@DescriptionDocs(description = "Shows when the player was last seen on the server")
38+
void execute(@Context Viewer sender, @Arg @Async OfflinePlayer target) {
39+
OfflinePlayer targetPlayer = this.server.getOfflinePlayer(target.getUniqueId());
40+
41+
if (targetPlayer.isOnline()) {
42+
this.noticeService.create()
43+
.viewer(sender)
44+
.notice(translation -> translation.seen().nowOnline())
45+
.placeholder("{PLAYER}", target.getName())
46+
.send();
47+
48+
return;
49+
}
50+
51+
long lastPlayed = targetPlayer.getLastPlayed();
52+
53+
if (lastPlayed == NEVER_JOINED_BEFORE) {
54+
this.noticeService.create()
55+
.viewer(sender)
56+
.notice(translation -> translation.seen().neverPlayedBefore())
57+
.placeholder("{PLAYER}", target.getName())
58+
.send();
59+
60+
return;
61+
}
62+
63+
Duration lastPlayedBetween = Duration.between(Instant.ofEpochMilli(lastPlayed), Instant.now());
64+
String lastPlayedFormatted = DurationUtil.format(lastPlayedBetween, true);
65+
66+
this.noticeService.create()
67+
.viewer(sender)
68+
.notice(translation -> translation.seen().lastSeen())
69+
.placeholder("{PLAYER}", target.getName())
70+
.placeholder("{SEEN}", lastPlayedFormatted)
71+
.send();
72+
}
73+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.eternalcode.core.feature.seen.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
import lombok.Getter;
5+
import lombok.experimental.Accessors;
6+
import net.dzikoysk.cdn.entity.Contextual;
7+
import net.dzikoysk.cdn.entity.Description;
8+
9+
@Getter
10+
@Accessors(fluent = true)
11+
@Contextual
12+
public class ENSeenMessages implements SeenMessages {
13+
14+
@Description("# {PLAYER} - The player who is never played before on the server")
15+
public Notice neverPlayedBefore = Notice.chat("<green>{PLAYER} has not played before on this server.");
16+
17+
@Description("# {PLAYER} - The player who was last seen on the server, {SEEN} - Time since last login")
18+
public Notice lastSeen = Notice.chat("<green>{PLAYER} was last seen {SEEN} ago.");
19+
20+
@Description("# {PLAYER} - The player who is now online")
21+
public Notice nowOnline = Notice.chat("<green>{PLAYER} is now online!");
22+
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.eternalcode.core.feature.seen.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
import lombok.Getter;
5+
import lombok.experimental.Accessors;
6+
import net.dzikoysk.cdn.entity.Contextual;
7+
import net.dzikoysk.cdn.entity.Description;
8+
9+
@Getter
10+
@Accessors(fluent = true)
11+
@Contextual
12+
public class PLSeenMessages implements SeenMessages {
13+
14+
@Description("# {PLAYER} - Gracz który nigdy nie grał na serwerze")
15+
public Notice neverPlayedBefore = Notice.chat("<green>{PLAYER} nie grał nigdy na tym serwerze.");
16+
17+
@Description("# {PLAYER} - Gracz który ostatnio był widziany na serwerze, {SEEN} - Czas od ostatniego logowania")
18+
public Notice lastSeen = Notice.chat("<green>{PLAYER} był ostatnio widziany {SEEN} temu.");
19+
20+
@Description("# {PLAYER} - Gracz który jest aktualnie online")
21+
public Notice nowOnline = Notice.chat("<green>{PLAYER} jest aktualnie online!");
22+
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.eternalcode.core.feature.seen.messages;
2+
3+
import com.eternalcode.multification.notice.Notice;
4+
5+
public interface SeenMessages {
6+
7+
Notice neverPlayedBefore();
8+
Notice lastSeen();
9+
Notice nowOnline();
10+
11+
}

eternalcore-core/src/main/java/com/eternalcode/core/translation/Translation.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.eternalcode.core.feature.language.Language;
1313
import com.eternalcode.core.feature.privatechat.messages.PrivateChatMessages;
1414
import com.eternalcode.core.feature.randomteleport.messages.RandomTeleportMessages;
15+
import com.eternalcode.core.feature.seen.messages.SeenMessages;
1516
import com.eternalcode.core.feature.setslot.messages.SetSlotMessages;
1617
import com.eternalcode.core.feature.spawn.messages.SpawnMessages;
1718
import com.eternalcode.core.feature.sudo.messages.SudoMessages;
@@ -219,6 +220,8 @@ interface ContainerSection {
219220
InventorySection inventory();
220221
// player section
221222
PlayerSection player();
223+
//Seen section
224+
SeenMessages seen();
222225
// spawn section
223226
SpawnMessages spawn();
224227
// set slot section

eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/ENTranslation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.eternalcode.core.feature.home.messages.ENHomeMessages;
1111
import com.eternalcode.core.feature.jail.messages.ENJailMessages;
1212
import com.eternalcode.core.feature.language.Language;
13+
import com.eternalcode.core.feature.seen.messages.ENSeenMessages;
1314
import com.eternalcode.core.feature.setslot.messages.ENSetSlotMessages;
1415
import com.eternalcode.core.feature.privatechat.messages.ENPrivateMessages;
1516
import com.eternalcode.core.feature.randomteleport.messages.ENRandomTeleportMessages;
@@ -131,6 +132,12 @@ public static class ENFormatSection implements Format {
131132
})
132133
public ENHelpOpMessages helpOp = new ENHelpOpMessages();
133134

135+
@Description({
136+
" ",
137+
"# This section is responsible for the messages of the /seen command"
138+
})
139+
public ENSeenMessages seen = new ENSeenMessages();
140+
134141
@Description({
135142
" ",
136143
"# This section is responsible for the communication between administration",

eternalcore-core/src/main/java/com/eternalcode/core/translation/implementation/PLTranslation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.eternalcode.core.feature.home.messages.PLHomeMessages;
1111
import com.eternalcode.core.feature.jail.messages.PLJailMessages;
1212
import com.eternalcode.core.feature.language.Language;
13+
import com.eternalcode.core.feature.seen.messages.PLSeenMessages;
1314
import com.eternalcode.core.feature.setslot.messages.PLSetSlotMessages;
1415
import com.eternalcode.core.feature.privatechat.messages.PLPrivateChatMessages;
1516
import com.eternalcode.core.feature.randomteleport.messages.PLRandomTeleportMessages;
@@ -140,6 +141,12 @@ public static class PLFormatSection implements Format {
140141
})
141142
public PLSudoMessages sudo = new PLSudoMessages();
142143

144+
@Description({
145+
" ",
146+
"# Ta sekcja odpowiada za wiadomości komendy /seen"
147+
})
148+
public PLSeenMessages seen = new PLSeenMessages();
149+
143150
@Description({
144151
" ",
145152
"# Ta sekcja odpowiada za komunikaty związane z teleportacją",

0 commit comments

Comments
 (0)