Skip to content

Commit

Permalink
chore: improve profile UI formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Sep 5, 2024
1 parent ef27685 commit 80be174
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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),
Expand All @@ -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));
Expand All @@ -109,15 +119,15 @@ 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();
}
}

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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 80be174

Please sign in to comment.