From 25a93f8bd2cc5e255a96676d9c51bbb0fbdeeb74 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 4 Aug 2023 06:22:43 +0600 Subject: [PATCH 1/3] Add bar-duration-placeholder-format, %duration% placeholder into BossBarSettings --- .../api/config/bossbar/BossBarSettings.java | 7 ++++++ .../me/mastercapexd/auth/BaseAuthPlugin.java | 2 ++ .../config/bossbar/BaseBossBarSettings.java | 22 +++++++++++++++---- .../task/AuthenticationProgressBarTask.java | 10 ++++++--- .../main/resources/configurations/config.yml | 4 +++- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/com/bivashy/auth/api/config/bossbar/BossBarSettings.java b/api/src/main/java/com/bivashy/auth/api/config/bossbar/BossBarSettings.java index de04b38b..b2a0769e 100644 --- a/api/src/main/java/com/bivashy/auth/api/config/bossbar/BossBarSettings.java +++ b/api/src/main/java/com/bivashy/auth/api/config/bossbar/BossBarSettings.java @@ -1,8 +1,15 @@ package com.bivashy.auth.api.config.bossbar; +import java.text.SimpleDateFormat; + import com.bivashy.auth.api.server.bossbar.ServerBossbar; +import com.bivashy.auth.api.server.message.ServerComponent; public interface BossBarSettings { + ServerComponent getTitle(); + + SimpleDateFormat getDurationPlaceholderFormat(); + boolean isEnabled(); ServerBossbar createBossbar(); diff --git a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java index e156efad..01ffb118 100644 --- a/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java +++ b/core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java @@ -3,6 +3,7 @@ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; +import java.text.SimpleDateFormat; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -234,6 +235,7 @@ private void registerConfigurationProcessor() { .orElseThrow(() -> new IllegalArgumentException("Cannot find CryptoProvider with name " + context.getString()))) .registerFieldResolver(ServerComponent.class, new ServerComponentFieldResolver()) .registerFieldResolverFactory(RawURLProvider.class, new RawURLProviderFieldResolverFactory()) + .registerFieldResolver(SimpleDateFormat.class, context -> new SimpleDateFormat(context.getString("mm:ss"))) .registerFieldResolver(File.class, (context) -> { String path = context.getString(""); if (path.isEmpty()) diff --git a/core/src/main/java/me/mastercapexd/auth/config/bossbar/BaseBossBarSettings.java b/core/src/main/java/me/mastercapexd/auth/config/bossbar/BaseBossBarSettings.java index 9729e803..027bb1b5 100644 --- a/core/src/main/java/me/mastercapexd/auth/config/bossbar/BaseBossBarSettings.java +++ b/core/src/main/java/me/mastercapexd/auth/config/bossbar/BaseBossBarSettings.java @@ -1,5 +1,7 @@ package me.mastercapexd.auth.config.bossbar; +import java.text.SimpleDateFormat; + import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.config.bossbar.BossBarSettings; import com.bivashy.auth.api.server.bossbar.ServerBossbar; @@ -14,11 +16,13 @@ public class BaseBossBarSettings implements ConfigurationHolder, BossBarSettings @ConfigField("use") private boolean enabled = false; @ConfigField("bar-color") - private ServerBossbar.Color barColor = Color.BLUE; + private ServerBossbar.Color color = Color.BLUE; @ConfigField("bar-style") - private ServerBossbar.Style barStyle = Style.SOLID; + private ServerBossbar.Style style = Style.SOLID; @ConfigField("bar-text") - private ServerComponent barText = ServerComponent.fromPlain("[Authentication]"); + private ServerComponent title = ServerComponent.fromPlain("[Authentication]"); + @ConfigField("bar-duration-placeholder-format") + private SimpleDateFormat durationPlaceholderFormat = new SimpleDateFormat("mm:ss"); public BaseBossBarSettings(ConfigurationSectionHolder sectionHolder) { AuthPlugin.instance().getConfigurationProcessor().resolve(sectionHolder, this); @@ -32,10 +36,20 @@ public boolean isEnabled() { return enabled; } + @Override + public ServerComponent getTitle() { + return title; + } + + @Override + public SimpleDateFormat getDurationPlaceholderFormat() { + return durationPlaceholderFormat; + } + @Override public ServerBossbar createBossbar() { if (!enabled) return null; - return AuthPlugin.instance().getCore().createBossbar(barText).color(barColor).style(barStyle).update(); + return AuthPlugin.instance().getCore().createBossbar(title).color(color).style(style).update(); } } diff --git a/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java b/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java index deac02de..2b2c0554 100644 --- a/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java +++ b/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java @@ -1,5 +1,6 @@ package me.mastercapexd.auth.task; +import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -9,11 +10,13 @@ import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.account.Account; +import com.bivashy.auth.api.config.bossbar.BossBarSettings; import com.bivashy.auth.api.event.AccountJoinEvent; import com.bivashy.auth.api.event.PlayerLogoutEvent; import com.bivashy.auth.api.model.AuthenticationTask; import com.bivashy.auth.api.model.PlayerIdSupplier; import com.bivashy.auth.api.server.bossbar.ServerBossbar; +import com.bivashy.auth.api.server.message.ServerComponent; import com.bivashy.auth.api.server.player.ServerPlayer; import com.bivashy.auth.api.server.scheduler.ServerScheduler; @@ -21,11 +24,12 @@ public class AuthenticationProgressBarTask implements AuthenticationTask { private final Map progressBars = new HashMap<>(); - private final AuthPlugin plugin; + private final BossBarSettings settings; private final ServerScheduler proxyScheduler; public AuthenticationProgressBarTask(AuthPlugin plugin) { - this.plugin = plugin; + this.settings = plugin.getConfig().getBossBarSettings(); + this.proxyScheduler = plugin.getCore().schedule(() -> { long now = System.currentTimeMillis(); long timeoutMillis = plugin.getConfig().getAuthTime(); @@ -88,7 +92,7 @@ public void onLogout(PlayerLogoutEvent e) { } private void registerBossbar(PlayerIdSupplier playerIdSupplier) { - ServerBossbar bossbar = plugin.getConfig().getBossBarSettings().createBossbar(); + ServerBossbar bossbar = settings.createBossbar(); if (bossbar == null) return; progressBars.put(playerIdSupplier.getPlayerId(), bossbar); diff --git a/core/src/main/resources/configurations/config.yml b/core/src/main/resources/configurations/config.yml index 046cc6c6..244aba60 100644 --- a/core/src/main/resources/configurations/config.yml +++ b/core/src/main/resources/configurations/config.yml @@ -147,7 +147,9 @@ boss-bar: # SEGMENTED_ЦИФРА - разделяет боссбар в сегменты bar-style: SOLID # Текст в боссбаре - bar-text: '&aВход в сервер' + bar-text: '&aВход в сервер [%duration%]' + # Подробнее о форматировании: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html + bar-duration-placeholder-format: 'mm:ss' # Определяет количество онлайн твинков игроков используя IP. Поставьте 0 чтобы отключить лимит max-login-per-ip: 0 From 75f3013a269faedb067498ecc90f01cc613cab36 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 4 Aug 2023 06:23:30 +0600 Subject: [PATCH 2/3] Deprecate ServerBossbar#title(String) and replace with ServerBossbar#title(ServerComponent) --- .../api/server/bossbar/ServerBossbar.java | 11 ++++++++-- .../auth/bungee/BungeeProxyCore.java | 2 +- .../api/bossbar/BungeeServerBossbar.java | 7 +++--- .../auth/velocity/VelocityProxyCore.java | 3 +-- .../api/bossbar/VelocityServerBossbar.java | 22 +++++++++---------- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/api/src/main/java/com/bivashy/auth/api/server/bossbar/ServerBossbar.java b/api/src/main/java/com/bivashy/auth/api/server/bossbar/ServerBossbar.java index 6de27f10..1f1c4e26 100644 --- a/api/src/main/java/com/bivashy/auth/api/server/bossbar/ServerBossbar.java +++ b/api/src/main/java/com/bivashy/auth/api/server/bossbar/ServerBossbar.java @@ -2,6 +2,7 @@ import java.util.Collection; +import com.bivashy.auth.api.server.message.ServerComponent; import com.bivashy.auth.api.server.player.ServerPlayer; import com.bivashy.auth.api.util.Castable; @@ -9,7 +10,7 @@ public abstract class ServerBossbar implements Castable { protected Style segmentStyle = Style.SOLID; protected Color color = Color.BLUE; protected float progress; - protected String title; + protected ServerComponent title; public ServerBossbar color(Color color) { this.color = color; @@ -28,8 +29,14 @@ public ServerBossbar progress(float progress) { return this; } + @Deprecated public ServerBossbar title(String title) { - this.title = title; + this.title = ServerComponent.fromPlain(title); + return this; + } + + public ServerBossbar title(ServerComponent component) { + this.title = component; return this; } diff --git a/bungee/src/main/java/me/mastercapexd/auth/bungee/BungeeProxyCore.java b/bungee/src/main/java/me/mastercapexd/auth/bungee/BungeeProxyCore.java index 16e4d521..20542fa5 100644 --- a/bungee/src/main/java/me/mastercapexd/auth/bungee/BungeeProxyCore.java +++ b/bungee/src/main/java/me/mastercapexd/auth/bungee/BungeeProxyCore.java @@ -88,7 +88,7 @@ public ServerTitle createTitle(ServerComponent title) { @Override public ServerBossbar createBossbar(ServerComponent component) { - return new BungeeServerBossbar(component.jsonText()); + return new BungeeServerBossbar(component); } @Override diff --git a/bungee/src/main/java/me/mastercapexd/auth/bungee/api/bossbar/BungeeServerBossbar.java b/bungee/src/main/java/me/mastercapexd/auth/bungee/api/bossbar/BungeeServerBossbar.java index 72f0eade..faf52120 100644 --- a/bungee/src/main/java/me/mastercapexd/auth/bungee/api/bossbar/BungeeServerBossbar.java +++ b/bungee/src/main/java/me/mastercapexd/auth/bungee/api/bossbar/BungeeServerBossbar.java @@ -6,6 +6,7 @@ import java.util.UUID; import com.bivashy.auth.api.server.bossbar.ServerBossbar; +import com.bivashy.auth.api.server.message.ServerComponent; import com.bivashy.auth.api.server.player.ServerPlayer; import com.google.common.collect.Sets; @@ -16,8 +17,8 @@ public class BungeeServerBossbar extends ServerBossbar { private final Set players = Sets.newHashSet(); private final UUID uuid = UUID.randomUUID(); - public BungeeServerBossbar(String title) { - this.title(title); + public BungeeServerBossbar(ServerComponent component) { + this.title(component); } @Override @@ -63,7 +64,7 @@ public Collection players() { private BossBar getAddPacket() { BossBar packet = new BossBar(uuid, 0); - packet.setTitle(title); // We send title in json + packet.setTitle(title.jsonText()); packet.setColor(this.color.ordinal()); packet.setDivision(this.segmentStyle.ordinal()); packet.setHealth(this.progress); diff --git a/velocity/src/main/java/me/mastercapexd/auth/velocity/VelocityProxyCore.java b/velocity/src/main/java/me/mastercapexd/auth/velocity/VelocityProxyCore.java index 1dd6bc76..f405a72d 100644 --- a/velocity/src/main/java/me/mastercapexd/auth/velocity/VelocityProxyCore.java +++ b/velocity/src/main/java/me/mastercapexd/auth/velocity/VelocityProxyCore.java @@ -10,7 +10,6 @@ import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.server.ServerCore; import com.bivashy.auth.api.server.bossbar.ServerBossbar; -import com.bivashy.auth.api.server.message.AdventureServerComponent; import com.bivashy.auth.api.server.message.ServerComponent; import com.bivashy.auth.api.server.player.ServerPlayer; import com.bivashy.auth.api.server.proxy.limbo.LimboServerWrapper; @@ -81,7 +80,7 @@ public ServerTitle createTitle(ServerComponent title) { @Override public ServerBossbar createBossbar(ServerComponent component) { - return new VelocityServerBossbar(component.as(AdventureServerComponent.class).component()); + return new VelocityServerBossbar(component); } @Override diff --git a/velocity/src/main/java/me/mastercapexd/auth/velocity/api/bossbar/VelocityServerBossbar.java b/velocity/src/main/java/me/mastercapexd/auth/velocity/api/bossbar/VelocityServerBossbar.java index dd1ed8bb..0b30b38f 100644 --- a/velocity/src/main/java/me/mastercapexd/auth/velocity/api/bossbar/VelocityServerBossbar.java +++ b/velocity/src/main/java/me/mastercapexd/auth/velocity/api/bossbar/VelocityServerBossbar.java @@ -5,7 +5,6 @@ import java.util.Collections; import java.util.List; -import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.server.bossbar.ServerBossbar; import com.bivashy.auth.api.server.message.AdventureServerComponent; import com.bivashy.auth.api.server.message.ServerComponent; @@ -13,22 +12,18 @@ import me.mastercapexd.auth.velocity.player.VelocityServerPlayer; import net.kyori.adventure.bossbar.BossBar; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; public class VelocityServerBossbar extends ServerBossbar { + private static final GsonComponentSerializer GSON_COMPONENT_SERIALIZER = GsonComponentSerializer.gson(); private final List bossBarPlayers = new ArrayList<>(); private final BossBar bossBar; - public VelocityServerBossbar(String title) { - title(title); + public VelocityServerBossbar(ServerComponent component) { + title(component); BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; - bossBar = BossBar.bossBar(LegacyComponentSerializer.legacyAmpersand().deserialize(title), progress, bossBarColor, bossBarOverlay).progress(progress); - } - - public VelocityServerBossbar(Component component) { - this(LegacyComponentSerializer.legacySection().serialize(component)); + bossBar = BossBar.bossBar(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText()), progress, bossBarColor, bossBarOverlay).progress(progress); } @Override @@ -51,11 +46,14 @@ public ServerBossbar remove(ServerPlayer... viewers) { @Override public ServerBossbar update() { - ServerComponent bossbarTitleComponent = AuthPlugin.instance().getConfig().getServerMessages().getDeserializer().deserialize(title); BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; - bossbarTitleComponent.safeAs(AdventureServerComponent.class).map(AdventureServerComponent::component).ifPresent(bossBar::name); + if (title instanceof AdventureServerComponent) { + bossBar.name(((AdventureServerComponent) title).component()); + } else { + bossBar.name(GSON_COMPONENT_SERIALIZER.deserialize(title.jsonText())); + } bossBar.color(bossBarColor).overlay(bossBarOverlay).progress(progress); return this; } From 292eacbf3503fe6dfe8afa47b5d9492ecfcc51e8 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 4 Aug 2023 06:24:01 +0600 Subject: [PATCH 3/3] Replace placeholder in bossbar title in AuthenticationProgressBarTask --- .../mastercapexd/auth/task/AuthenticationProgressBarTask.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java b/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java index 2b2c0554..5e637193 100644 --- a/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java +++ b/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java @@ -69,6 +69,8 @@ public AuthenticationProgressBarTask(AuthPlugin plugin) { continue; } + String formattedDuration = settings.getDurationPlaceholderFormat().format(new Date(timeoutMillis - accountTimeElapsedFromEntryMillis)); + progressBar.title(ServerComponent.fromJson(settings.getTitle().jsonText().replace("%duration%", formattedDuration))); progressBar.progress(progress); progressBar.update(); }