added playertime command

This commit is contained in:
Elias Müller 2024-10-19 15:46:34 +02:00
parent 918ee5ed00
commit b1427ac90e
4 changed files with 78 additions and 1 deletions

View File

@ -0,0 +1,31 @@
package eu.mhsl.craftattack.spawn.appliances.playtime;
import eu.mhsl.craftattack.spawn.appliance.Appliance;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import eu.mhsl.craftattack.spawn.util.text.DataSizeConverter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.Ticks;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Objects;
public class Playtime extends Appliance {
public Component getFormattedPlayerTime(OfflinePlayer player){
int playtimeInTicks = player.getStatistic(Statistic.PLAY_ONE_MINUTE);
String playtime = DataSizeConverter.formatSecondsToHumanReadable(playtimeInTicks / Ticks.TICKS_PER_SECOND);
return Component.text()
.append(Component.text("Der Spieler ", NamedTextColor.GRAY))
.append(Component.text(Objects.requireNonNull(player.getName())))
.append(Component.text(" hat eine Spielzeit von ", NamedTextColor.GRAY))
.append(Component.text(playtime, NamedTextColor.GOLD))
.build();
}
@Override
protected @NotNull List<ApplianceCommand<?>> commands() {
return List.of(new PlaytimeCommand());
}
}

View File

@ -0,0 +1,31 @@
package eu.mhsl.craftattack.spawn.appliances.playtime;
import eu.mhsl.craftattack.spawn.appliance.ApplianceCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class PlaytimeCommand extends ApplianceCommand.PlayerChecked<Playtime> {
public PlaytimeCommand() {
super("playtime");
}
@Override
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
String playerName = getPlayer().getName();
if(args.length == 1) {
playerName = args[0];
}
OfflinePlayer player = Bukkit.getOfflinePlayer(playerName);
if (!player.hasPlayedBefore()) {
sender.sendMessage(Component.text("Der Spieler existiert nicht!", NamedTextColor.RED));
return;
}
sender.sendMessage(getAppliance().getFormattedPlayerTime(player));
}
}

View File

@ -38,4 +38,18 @@ public class DataSizeConverter {
hours %= 60;
return String.format("%dd%dh%dm%ds", days, hours, minutes, seconds);
}
public static String formatSecondsToHumanReadable(int seconds) {
if (seconds < 0) return "unsupported";
int minutes = seconds / 60;
int hours = minutes / 60;
int days = hours / 24;
seconds %= 60;
minutes %= 60;
hours %= 60;
return String.format("%dd %dh %dm %ds", days, hours, minutes, seconds);
}
}

View File

@ -42,3 +42,4 @@ commands:
yearRank:
msg:
r:
playtime: