From 6d55337b747f727d7b2ef46acaef955e8bd2e400 Mon Sep 17 00:00:00 2001 From: bivashy Date: Sat, 17 Feb 2024 19:30:57 +0600 Subject: [PATCH 1/5] Add AdventureServerBossbar --- .../adventure/AdventureServerBossbar.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java diff --git a/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java b/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java new file mode 100644 index 00000000..2fc4f107 --- /dev/null +++ b/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java @@ -0,0 +1,74 @@ +package me.mastercapexd.auth.server.adventure; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +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 net.kyori.adventure.bossbar.BossBar; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +public abstract class AdventureServerBossbar extends ServerBossbar { + private static final GsonComponentSerializer GSON_COMPONENT_SERIALIZER = GsonComponentSerializer.gson(); + private final List bossBarPlayers = new ArrayList<>(); + private final BossBar bossBar; + + public AdventureServerBossbar(ServerComponent component) { + title(component); + BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; + BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; + bossBar = BossBar.bossBar(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText()), progress, bossBarColor, bossBarOverlay).progress(progress); + } + + public abstract void showBossBar(ServerPlayer player, BossBar bossBar); + + public abstract void hideBossBar(ServerPlayer player, BossBar bossBar); + + @Override + public ServerBossbar send(ServerPlayer... viewers) { + for (ServerPlayer player : viewers) { + showBossBar(player, bossBar); + bossBarPlayers.add(player); + } + return this; + } + + @Override + public ServerBossbar remove(ServerPlayer... viewers) { + for (ServerPlayer player : viewers) { + hideBossBar(player, bossBar); + bossBarPlayers.remove(player); + } + return this; + } + + @Override + public ServerBossbar update() { + BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; + BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; + + 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; + } + + @Override + public ServerBossbar removeAll() { + remove(bossBarPlayers.toArray(new ServerPlayer[0])); + return this; + } + + @Override + public Collection players() { + return Collections.unmodifiableList(bossBarPlayers); + } +} From 7fb1230070de8fac3a6f6f5c13b70afc6f5825aa Mon Sep 17 00:00:00 2001 From: bivashy Date: Sat, 17 Feb 2024 19:31:24 +0600 Subject: [PATCH 2/5] Use AdventureServerBossbar instead of native implementation --- .../api/bossbar/BungeeServerBossbar.java | 72 ++++--------------- .../api/bossbar/VelocityServerBossbar.java | 59 ++------------- 2 files changed, 20 insertions(+), 111 deletions(-) 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 8d48dd6f..28c5fb22 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 @@ -1,77 +1,31 @@ package me.mastercapexd.auth.bungee.api.bossbar; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -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 me.mastercapexd.auth.bungee.BungeeAuthPluginBootstrap; import me.mastercapexd.auth.bungee.player.BungeeServerPlayer; -import net.md_5.bungee.protocol.packet.BossBar; - -public class BungeeServerBossbar extends ServerBossbar { - private final Set players = new HashSet<>(); - private final UUID uuid = UUID.randomUUID(); - - public BungeeServerBossbar(ServerComponent component) { - this.title(component); - } +import me.mastercapexd.auth.server.adventure.AdventureServerBossbar; +import net.kyori.adventure.bossbar.BossBar; +import net.kyori.adventure.platform.bungeecord.BungeeAudiences; - @Override - public ServerBossbar removeAll() { - this.players.forEach(this::remove); - this.players.clear(); - return this; - } +public class BungeeServerBossbar extends AdventureServerBossbar { - @Override - public ServerBossbar send(ServerPlayer... viewers) { - BossBar bossBar = getAddPacket(); - for (ServerPlayer player : viewers) { - player.as(BungeeServerPlayer.class).getBungeePlayer().unsafe().sendPacket(bossBar); - players.add(player); - } - return this; - } + private final BungeeAudiences audiences = BungeeAuthPluginBootstrap.getInstance().getBungeeAudiences(); - @Override - public ServerBossbar remove(ServerPlayer... viewers) { - BossBar bossBar = getRemovePacket(); - for (ServerPlayer player : viewers) { - player.as(BungeeServerPlayer.class).getBungeePlayer().unsafe().sendPacket(bossBar); - players.remove(player); - } - return this; + public BungeeServerBossbar(ServerComponent component) { + super(component); } @Override - public ServerBossbar update() { - BossBar bossBar = getAddPacket(); - for (ServerPlayer player : players) - player.as(BungeeServerPlayer.class).getBungeePlayer().unsafe().sendPacket(bossBar); - return this; + public void showBossBar(ServerPlayer player, BossBar bossBar) { + audiences.player(player.as(BungeeServerPlayer.class).getBungeePlayer()).showBossBar(bossBar); } @Override - public Collection players() { - return Collections.unmodifiableCollection(players); - } - - private BossBar getAddPacket() { - BossBar packet = new BossBar(uuid, 0); - - packet.setTitle(title.jsonText()); - packet.setColor(this.color.ordinal()); - packet.setDivision(this.segmentStyle.ordinal()); - packet.setHealth(this.progress); - return packet; + public void hideBossBar(ServerPlayer player, BossBar bossBar) { + audiences.player(player.as(BungeeServerPlayer.class).getBungeePlayer()).hideBossBar(bossBar); } - private BossBar getRemovePacket() { - return new BossBar(uuid, 1); - } } + 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 0b30b38f..d0f98d2c 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 @@ -1,71 +1,26 @@ package me.mastercapexd.auth.velocity.api.bossbar; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -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.server.adventure.AdventureServerBossbar; import me.mastercapexd.auth.velocity.player.VelocityServerPlayer; import net.kyori.adventure.bossbar.BossBar; -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 class VelocityServerBossbar extends AdventureServerBossbar { 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(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText()), progress, bossBarColor, bossBarOverlay).progress(progress); - } - - @Override - public ServerBossbar send(ServerPlayer... viewers) { - for (ServerPlayer player : viewers) { - player.as(VelocityServerPlayer.class).getPlayer().showBossBar(bossBar); - bossBarPlayers.add(player); - } - return this; - } - - @Override - public ServerBossbar remove(ServerPlayer... viewers) { - for (ServerPlayer player : viewers) { - player.as(VelocityServerPlayer.class).getPlayer().hideBossBar(bossBar); - bossBarPlayers.remove(player); - } - return this; + super(component); } @Override - public ServerBossbar update() { - BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; - BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; - - 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; + public void showBossBar(ServerPlayer player, BossBar bossBar) { + player.as(VelocityServerPlayer.class).getPlayer().showBossBar(bossBar); } @Override - public ServerBossbar removeAll() { - remove(bossBarPlayers.toArray(new ServerPlayer[0])); - return this; + public void hideBossBar(ServerPlayer player, BossBar bossBar) { + player.as(VelocityServerPlayer.class).getPlayer().hideBossBar(bossBar); } - @Override - public Collection players() { - return Collections.unmodifiableList(bossBarPlayers); - } } From bc9c57e6ca0ea970f3aa08663f2fcb5d281135f9 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 8 Mar 2024 18:19:26 +0500 Subject: [PATCH 3/5] Change bossbar update sequence in progress bar task --- .../mastercapexd/auth/task/AuthenticationProgressBarTask.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 5e637193..ecc38de2 100644 --- a/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java +++ b/core/src/main/java/me/mastercapexd/auth/task/AuthenticationProgressBarTask.java @@ -70,9 +70,8 @@ public AuthenticationProgressBarTask(AuthPlugin plugin) { } String formattedDuration = settings.getDurationPlaceholderFormat().format(new Date(timeoutMillis - accountTimeElapsedFromEntryMillis)); - progressBar.title(ServerComponent.fromJson(settings.getTitle().jsonText().replace("%duration%", formattedDuration))); progressBar.progress(progress); - progressBar.update(); + progressBar.title(ServerComponent.fromJson(settings.getTitle().jsonText().replace("%duration%", formattedDuration))); } }, 0, 1, TimeUnit.SECONDS); plugin.getEventBus().register(this); From 55f52fd2bf8d1ca296f39abc3d46b1a207a1a2bc Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 8 Mar 2024 18:19:38 +0500 Subject: [PATCH 4/5] Make AdventureServerBossbar more dynamic --- .../adventure/AdventureServerBossbar.java | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java b/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java index 2fc4f107..87d9c660 100644 --- a/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java +++ b/core/src/main/java/me/mastercapexd/auth/server/adventure/AdventureServerBossbar.java @@ -19,10 +19,10 @@ public abstract class AdventureServerBossbar extends ServerBossbar { private final BossBar bossBar; public AdventureServerBossbar(ServerComponent component) { - title(component); + this.title = component; BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; - bossBar = BossBar.bossBar(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText()), progress, bossBarColor, bossBarOverlay).progress(progress); + bossBar = BossBar.bossBar(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText()), progress, bossBarColor, bossBarOverlay); } public abstract void showBossBar(ServerPlayer player, BossBar bossBar); @@ -61,6 +61,36 @@ public ServerBossbar update() { return this; } + @Override + public ServerBossbar title(ServerComponent component) { + super.title(component); + bossBar.name(GSON_COMPONENT_SERIALIZER.deserialize(component.jsonText())); + return this; + } + + @Override + public ServerBossbar color(Color color) { + BossBar.Color bossBarColor = BossBar.Color.values()[color.ordinal()]; + super.color(color); + bossBar.color(bossBarColor); + return this; + } + + @Override + public ServerBossbar style(Style segmentStyle) { + BossBar.Overlay bossBarOverlay = BossBar.Overlay.values()[segmentStyle.ordinal()]; + super.style(segmentStyle); + bossBar.overlay(bossBarOverlay); + return this; + } + + @Override + public ServerBossbar progress(float progress) { + super.progress(progress); + bossBar.progress(progress); + return this; + } + @Override public ServerBossbar removeAll() { remove(bossBarPlayers.toArray(new ServerPlayer[0])); From 4c5a260bf1d9ce7d86ec8d3ad8c4d3680039c325 Mon Sep 17 00:00:00 2001 From: bivashy Date: Fri, 8 Mar 2024 18:19:53 +0500 Subject: [PATCH 5/5] Empty bossbar as default progress --- .../java/com/bivashy/auth/api/server/bossbar/ServerBossbar.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1f1c4e26..97902d1e 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 @@ -9,7 +9,7 @@ public abstract class ServerBossbar implements Castable { protected Style segmentStyle = Style.SOLID; protected Color color = Color.BLUE; - protected float progress; + protected float progress = 0; protected ServerComponent title; public ServerBossbar color(Color color) {