From 8f679de35255fd2005ff31cd850784b59e75eecc Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Sat, 6 Nov 2021 18:41:38 -0400 Subject: [PATCH] Add offline user support to /playtime (#4619) --- .../essentials/commands/Commandplaytime.java | 37 ++++++++++++------- .../commands/EssentialsCommand.java | 14 +++++-- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandplaytime.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandplaytime.java index 0ecfa8325..70eb470c8 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandplaytime.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandplaytime.java @@ -1,17 +1,17 @@ package com.earth2me.essentials.commands; -import static com.earth2me.essentials.I18n.tl; +import com.earth2me.essentials.CommandSource; +import com.earth2me.essentials.utils.DateUtil; +import com.earth2me.essentials.utils.EnumUtil; +import net.ess3.api.IUser; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.Statistic; import java.util.Collections; import java.util.List; -import org.bukkit.Server; -import org.bukkit.Statistic; - -import com.earth2me.essentials.CommandSource; -import com.earth2me.essentials.IUser; -import com.earth2me.essentials.utils.DateUtil; -import com.earth2me.essentials.utils.EnumUtil; +import static com.earth2me.essentials.I18n.tl; public class Commandplaytime extends EssentialsCommand { // For some reason, in 1.13 PLAY_ONE_MINUTE = ticks played = what used to be PLAY_ONE_TICK @@ -24,21 +24,32 @@ public class Commandplaytime extends EssentialsCommand { @Override protected void run(Server server, CommandSource sender, String commandLabel, String[] args) throws Exception { - final IUser target; + String displayName; + long playtime; final String key; if (args.length > 0 && sender.isAuthorized("essentials.playtime.others", ess)) { - target = getPlayer(server, sender, args, 0); + try { + final IUser user = getPlayer(server, sender, args, 0); + displayName = user.getDisplayName(); + playtime = user.getBase().getStatistic(PLAY_ONE_TICK); + } catch (PlayerNotFoundException ignored) { + final IUser user = getPlayer(server, sender, args, 0, true); + displayName = user.getName(); + playtime = Bukkit.getOfflinePlayer(user.getBase().getUniqueId()).getStatistic(PLAY_ONE_TICK); + } key = "playtimeOther"; } else if (sender.isPlayer()) { - target = sender.getUser(ess); + //noinspection ConstantConditions + displayName = sender.getPlayer().getDisplayName(); + playtime = sender.getPlayer().getStatistic(PLAY_ONE_TICK); key = "playtime"; } else { throw new NotEnoughArgumentsException(); } - final long playtimeMs = System.currentTimeMillis() - (target.getBase().getStatistic(PLAY_ONE_TICK) * 50); - sender.sendMessage(tl(key, DateUtil.formatDateDiff(playtimeMs), target.getDisplayName())); + final long playtimeMs = System.currentTimeMillis() - (playtime * 50L); + sender.sendMessage(tl(key, DateUtil.formatDateDiff(playtimeMs), displayName)); } @Override diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java index db19816f2..7fcfdea88 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/EssentialsCommand.java @@ -112,11 +112,15 @@ public abstract class EssentialsCommand implements IEssentialsCommand { // Get online players - only show vanished if source has permission protected User getPlayer(final Server server, final CommandSource sender, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException { + return getPlayer(server, sender, args, pos, false); + } + + protected User getPlayer(final Server server, final CommandSource sender, final String[] args, final int pos, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException { if (sender.isPlayer()) { final User user = ess.getUser(sender.getPlayer()); - return getPlayer(server, user, args, pos); + return getPlayer(server, user, args, pos, getOffline); } - return getPlayer(server, args, pos, true, false); + return getPlayer(server, args, pos, true, getOffline); } // Get online players - only show vanished if source has permission @@ -130,7 +134,11 @@ public abstract class EssentialsCommand implements IEssentialsCommand { // Get online players - only show vanished if source has permission protected User getPlayer(final Server server, final User user, final String[] args, final int pos) throws PlayerNotFoundException, NotEnoughArgumentsException { - return getPlayer(server, user, args, pos, user.canInteractVanished(), false); + return getPlayer(server, user, args, pos, false); + } + + protected User getPlayer(final Server server, final User user, final String[] args, final int pos, final boolean getOffline) throws PlayerNotFoundException, NotEnoughArgumentsException { + return getPlayer(server, user, args, pos, user.canInteractVanished(), getOffline); } // Get online or offline players, this method allows for raw access