Skip to content

Commit

Permalink
Merge pull request #58 from bivashy/57-duration-placeholder-in-bossbar
Browse files Browse the repository at this point in the history
[Suggestion] Add duration placeholder into bossbar title
  • Loading branch information
bivashy authored Aug 4, 2023
2 parents f2023a0 + 292eacb commit 79e662d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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;

public abstract class ServerBossbar implements Castable<ServerBossbar> {
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;
Expand All @@ -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;
}

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

Expand All @@ -16,8 +17,8 @@ public class BungeeServerBossbar extends ServerBossbar {
private final Set<ServerPlayer> players = Sets.newHashSet();
private final UUID uuid = UUID.randomUUID();

public BungeeServerBossbar(String title) {
this.title(title);
public BungeeServerBossbar(ServerComponent component) {
this.title(component);
}

@Override
Expand Down Expand Up @@ -63,7 +64,7 @@ public Collection<ServerPlayer> 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);
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/me/mastercapexd/auth/BaseAuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,23 +10,26 @@

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;

import io.github.revxrsal.eventbus.SubscribeEvent;

public class AuthenticationProgressBarTask implements AuthenticationTask {
private final Map<String, ServerBossbar> 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();
Expand Down Expand Up @@ -65,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();
}
Expand All @@ -88,7 +94,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);
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/resources/configurations/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@
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;
import com.bivashy.auth.api.server.player.ServerPlayer;

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<ServerPlayer> 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
Expand All @@ -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;
}
Expand Down

0 comments on commit 79e662d

Please sign in to comment.