Skip to content

Commit 6c279b1

Browse files
melluhJRoy
andauthored
Add playtime command (EssentialsX#4562)
Co-authored-by: Josh Roy <[email protected]>
1 parent 20dea2a commit 6c279b1

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.earth2me.essentials.commands;
2+
3+
import static com.earth2me.essentials.I18n.tl;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
import org.bukkit.Server;
9+
import org.bukkit.Statistic;
10+
11+
import com.earth2me.essentials.CommandSource;
12+
import com.earth2me.essentials.IUser;
13+
import com.earth2me.essentials.utils.DateUtil;
14+
import com.earth2me.essentials.utils.EnumUtil;
15+
16+
public class Commandplaytime extends EssentialsCommand {
17+
// For some reason, in 1.13 PLAY_ONE_MINUTE = ticks played = what used to be PLAY_ONE_TICK
18+
// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/bukkit/commits/b848d8ce633871b52115247b089029749c02f579
19+
private static final Statistic PLAY_ONE_TICK = EnumUtil.getStatistic("PLAY_ONE_MINUTE", "PLAY_ONE_TICK");
20+
21+
public Commandplaytime() {
22+
super("playtime");
23+
}
24+
25+
@Override
26+
protected void run(Server server, CommandSource sender, String commandLabel, String[] args) throws Exception {
27+
final IUser target;
28+
final String key;
29+
30+
if (args.length > 0 && sender.isAuthorized("essentials.playtime.others", ess)) {
31+
target = getPlayer(server, sender, args, 0);
32+
key = "playtimeOther";
33+
} else if (sender.isPlayer()) {
34+
target = sender.getUser(ess);
35+
key = "playtime";
36+
} else {
37+
throw new NotEnoughArgumentsException();
38+
}
39+
40+
final long playtimeMs = System.currentTimeMillis() - (target.getBase().getStatistic(PLAY_ONE_TICK) * 50);
41+
sender.sendMessage(tl(key, DateUtil.formatDateDiff(playtimeMs), target.getDisplayName()));
42+
}
43+
44+
@Override
45+
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
46+
if (args.length == 1 && sender.isAuthorized("essentials.playtime.others", ess)) {
47+
return getPlayers(server, sender);
48+
} else {
49+
return Collections.emptyList();
50+
}
51+
}
52+
53+
}

Essentials/src/main/resources/messages.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,14 @@ playerTempBanned=\u00a76Player \u00a7c{0}\u00a76 temporarily banned \u00a7c{1}\u
884884
playerUnbanIpAddress=\u00a76Player\u00a7c {0} \u00a76unbanned IP\:\u00a7c {1}
885885
playerUnbanned=\u00a76Player\u00a7c {0} \u00a76unbanned\u00a7c {1}
886886
playerUnmuted=\u00a76You have been unmuted.
887+
playtimeCommandDescription=Shows a player''s time played in game
888+
playtimeCommandUsage=/<command> [player]
889+
playtimeCommandUsage1=/<command>
890+
playtimeCommandUsage1Description=Shows your time played in game
891+
playtimeCommandUsage2=/<command> <player>
892+
playtimeCommandUsage2Description=Shows the specified player''s time played in game
893+
playtime=\u00a76Playtime\:\u00a7c {0}
894+
playtimeOther=\u00a76Playtime of {1}\u00a76\:\u00a7c {0}
887895
pong=Pong\!
888896
posPitch=\u00a76Pitch\: {0} (Head angle)
889897
possibleWorlds=\u00a76Possible worlds are the numbers \u00a7c0\u00a76 through \u00a7c{0}\u00a76.

Essentials/src/main/resources/plugin.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ commands:
344344
description: Pong!
345345
usage: /<command>
346346
aliases: [echo,eecho,eping,pong,epong]
347+
playtime:
348+
description: Shows a player's time played in game
349+
usage: /<command> [player]
350+
aliases: [eplaytime]
347351
potion:
348352
description: Adds custom potion effects to a potion.
349353
usage: /<command> <clear|apply|effect:<effect> power:<power> duration:<duration>>

0 commit comments

Comments
 (0)