From 80be174f33d92aa29e02f6c0c12bb491cbda7f8a Mon Sep 17 00:00:00 2001 From: Zak <34372536+ZakShearman@users.noreply.github.com> Date: Thu, 5 Sep 2024 12:32:05 +0100 Subject: [PATCH] chore: improve profile UI formatting --- build.gradle.kts | 2 +- .../minestom/core/module/core/CoreModule.java | 3 +- .../core/module/core/profile/ProfileGui.java | 30 ++++++++++++------- .../core/utils/DurationFormatter.java | 10 ++++++- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8e54a0c..f3a0a34 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -31,7 +31,7 @@ dependencies { // APIs api("dev.emortal.api:module-system:1.0.0") api("dev.emortal.api:agones-sdk:1.1.0") - api("dev.emortal.api:common-proto-sdk:fee482e") + api("dev.emortal.api:common-proto-sdk:6a9ad1a") api("dev.emortal.api:live-config-parser:f0728b0") api("io.kubernetes:client-java:18.0.1") diff --git a/src/main/java/dev/emortal/minestom/core/module/core/CoreModule.java b/src/main/java/dev/emortal/minestom/core/module/core/CoreModule.java index 91b1527..97d66bf 100644 --- a/src/main/java/dev/emortal/minestom/core/module/core/CoreModule.java +++ b/src/main/java/dev/emortal/minestom/core/module/core/CoreModule.java @@ -31,8 +31,7 @@ public boolean onLoad() { commandManager.register(new PerformanceCommand(this.eventNode)); if (badgeService != null) commandManager.register(new BadgeCommand(playerService, playerResolver, badgeService)); - if (playerService != null) - commandManager.register(new ProfileCommand(playerService, playerResolver)); + if (playerService != null) commandManager.register(new ProfileCommand(playerService, playerResolver)); return true; } diff --git a/src/main/java/dev/emortal/minestom/core/module/core/profile/ProfileGui.java b/src/main/java/dev/emortal/minestom/core/module/core/profile/ProfileGui.java index 7b29d1a..b52f12b 100644 --- a/src/main/java/dev/emortal/minestom/core/module/core/profile/ProfileGui.java +++ b/src/main/java/dev/emortal/minestom/core/module/core/profile/ProfileGui.java @@ -1,5 +1,6 @@ package dev.emortal.minestom.core.module.core.profile; +import dev.emortal.api.model.mcplayer.LoginSession; import dev.emortal.api.model.mcplayer.McPlayer; import dev.emortal.api.utils.EmortalXP; import dev.emortal.api.utils.ProtoDurationConverter; @@ -12,14 +13,16 @@ import net.minestom.server.entity.PlayerSkin; import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.InventoryType; +import net.minestom.server.item.ItemComponent; import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; -import net.minestom.server.item.metadata.PlayerHeadMeta; +import net.minestom.server.item.component.HeadProfile; import org.jetbrains.annotations.NotNull; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Date; public class ProfileGui extends Inventory { @@ -28,7 +31,7 @@ public class ProfileGui extends Inventory { 18, 26, 27, 28, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44}; private static final ItemStack BLOCKED_ITEM = ItemStack.builder(Material.BLACK_STAINED_GLASS_PANE) - .displayName(Component.text(" ")) + .set(ItemComponent.CUSTOM_NAME, Component.text(" ")) .build(); private static final int PLAYER_ICON_SLOT = 4; @@ -55,13 +58,12 @@ public ProfileGui(@NotNull McPlayer targetPlayer) { } private ItemStack createPlayerIcon() { - PlayerHeadMeta.Builder metaBuilder = new PlayerHeadMeta.Builder(); dev.emortal.api.model.common.PlayerSkin skin = this.targetPlayer.getCurrentSkin(); - metaBuilder.playerSkin(new PlayerSkin(skin.getTexture(), skin.getSignature())); + HeadProfile headProfile = new HeadProfile(new PlayerSkin(skin.getTexture(), skin.getSignature())); return ItemStack.builder(Material.PLAYER_HEAD) - .meta(metaBuilder.build()) - .displayName(Component.text(this.targetPlayer.getCurrentUsername(), NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) + .set(ItemComponent.PROFILE, headProfile) + .set(ItemComponent.CUSTOM_NAME, Component.text(this.targetPlayer.getCurrentUsername(), NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) .lore( Component.empty(), this.levelLine().decoration(TextDecoration.ITALIC, false), @@ -87,7 +89,15 @@ private Component levelLine() { private Component playtimeLine() { Duration playtime = ProtoDurationConverter.fromProto(this.targetPlayer.getHistoricPlayTime()); - String playtimeFormatted = DurationFormatter.ofGreatestUnits(playtime, 3); + + LoginSession currentSession = this.targetPlayer.getCurrentSession(); + Duration currentSessionDuration = Duration.between( + ProtoTimestampConverter.fromProto(currentSession.getLoginTime()), + Instant.now() + ); + playtime = playtime.plus(currentSessionDuration); + + String playtimeFormatted = DurationFormatter.ofGreatestUnits(playtime, 3, ChronoUnit.SECONDS); return Component.text("Playtime: ", NamedTextColor.GRAY) .append(Component.text(playtimeFormatted, NamedTextColor.GOLD)); @@ -109,7 +119,7 @@ private Component lastOnlineLine() { } else { Instant lastOnline = ProtoTimestampConverter.fromProto(this.targetPlayer.getLastOnline()); Duration duration = Duration.between(lastOnline, Instant.now()); - String lastOnlineFormatted = DurationFormatter.ofGreatestUnits(duration, 2); + String lastOnlineFormatted = DurationFormatter.ofGreatestUnits(duration, 2, ChronoUnit.SECONDS); return builder.append(Component.text(lastOnlineFormatted + " ago", NamedTextColor.GOLD)).build(); } @@ -117,7 +127,7 @@ private Component lastOnlineLine() { private ItemStack createAchievementsIcon() { return ItemStack.builder(Material.DIAMOND) - .displayName(Component.text("Achievements", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) + .set(ItemComponent.CUSTOM_NAME, Component.text("Achievements", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) .lore( Component.empty(), Component.text("Coming Soon...", NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false) @@ -127,7 +137,7 @@ private ItemStack createAchievementsIcon() { private ItemStack createGameHistoryIcon() { return ItemStack.builder(Material.BOOK) - .displayName(Component.text("Game History", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) + .set(ItemComponent.CUSTOM_NAME, Component.text("Game History", NamedTextColor.LIGHT_PURPLE).decoration(TextDecoration.ITALIC, false)) .lore( Component.empty(), Component.text("Coming Soon...", NamedTextColor.GRAY).decoration(TextDecoration.ITALIC, false) diff --git a/src/main/java/dev/emortal/minestom/core/utils/DurationFormatter.java b/src/main/java/dev/emortal/minestom/core/utils/DurationFormatter.java index 70fbb1a..6249580 100644 --- a/src/main/java/dev/emortal/minestom/core/utils/DurationFormatter.java +++ b/src/main/java/dev/emortal/minestom/core/utils/DurationFormatter.java @@ -3,9 +3,11 @@ import net.minestom.server.utils.time.TimeUnit; import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.StringJoiner; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public final class DurationFormatter { private static final Duration YEAR_DURATION = Duration.ofDays(365); @@ -20,7 +22,7 @@ public final class DurationFormatter { return duration.toNanosPart() + "ns"; } - public static @NotNull String ofGreatestUnits(@NotNull Duration duration, int unitCount) { + public static @NotNull String ofGreatestUnits(@NotNull Duration duration, int unitCount, @Nullable ChronoUnit smallestUnit) { StringJoiner builder = new StringJoiner(", "); long days = duration.toDaysPart(); @@ -31,35 +33,41 @@ public final class DurationFormatter { builder.add(years + "y"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.YEARS) return builder.toString(); if (days > 0) { builder.add(days + "d"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.DAYS) return builder.toString(); long hours = duration.toHoursPart(); if (hours > 0) { builder.add(hours + "hr"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.HOURS) return builder.toString(); long minutes = duration.toMinutesPart(); if (minutes > 0) { builder.add(minutes + "min"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.MINUTES) return builder.toString(); long seconds = duration.toSecondsPart(); if (seconds > 0) { builder.add(seconds + "s"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.SECONDS) return builder.toString(); long millis = duration.toMillisPart(); if (millis > 0) { builder.add(millis + "ms"); if (--unitCount == 0) return builder.toString(); } + if (smallestUnit == ChronoUnit.MILLIS) return builder.toString(); long nanos = duration.toNanosPart(); if (nanos > 0) {