From 1bb4d1713e944be70681ae59f55669aa60e38f55 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Mon, 21 Apr 2025 14:43:44 +0700 Subject: [PATCH] refactor: some improvements and fixes to bossbar manager + fix some NPEs --- build-number.txt | 2 +- .../chomens_bot/data/bossbar/BotBossBar.java | 30 +++----- .../plugins/BossbarManagerPlugin.java | 68 +++++++++---------- 3 files changed, 42 insertions(+), 58 deletions(-) diff --git a/build-number.txt b/build-number.txt index 0ef1ac49..c211c986 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -2881 \ No newline at end of file +2892 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/data/bossbar/BotBossBar.java b/src/main/java/me/chayapak1/chomens_bot/data/bossbar/BotBossBar.java index ed1a55c8..5f12f090 100644 --- a/src/main/java/me/chayapak1/chomens_bot/data/bossbar/BotBossBar.java +++ b/src/main/java/me/chayapak1/chomens_bot/data/bossbar/BotBossBar.java @@ -55,9 +55,7 @@ public class BotBossBar extends BossBar { } public void setTitle (final Component title, final boolean force) { - if (!gotSecret) return; - - if (this.title.equals(title) && !force) return; + if ((this.title.equals(title) || !gotSecret) && !force) return; if (bot.bossbar.actionBar) { bot.chat.actionBar(title, players); @@ -84,9 +82,7 @@ public class BotBossBar extends BossBar { } public void setColor (final BossBarColor color, final boolean force) { - if (!gotSecret) return; - - if (this.color == color && !force) return; + if ((this.color == color || !gotSecret) && !force) return; this.color = color; @@ -102,9 +98,7 @@ public class BotBossBar extends BossBar { public void setPlayers (final String players) { setPlayers(players, false); } public void setPlayers (final String players, final boolean force) { - if (!gotSecret) return; - - if (this.players.equals(players) && !force) return; + if ((this.players.equals(players) || !gotSecret) && !force) return; this.players = players; @@ -120,9 +114,7 @@ public class BotBossBar extends BossBar { } public void setDivision (final BossBarDivision _division, final boolean force) { - if (!gotSecret) return; - - if (this.division == _division && !force) return; + if ((this.division == _division || !gotSecret) && !force) return; this.division = _division; @@ -148,9 +140,7 @@ public class BotBossBar extends BossBar { } public void setValue (final int value, final boolean force) { - if (!gotSecret) return; - - if (this.value == value && !force) return; + if ((this.value == value || !gotSecret) && !force) return; this.value = value; @@ -162,9 +152,11 @@ public class BotBossBar extends BossBar { public boolean visible () { return visible; } public void setVisible (final boolean visible) { - if (!gotSecret) return; + setVisible(visible, false); + } - if (this.visible == visible) return; + public void setVisible (final boolean visible, final boolean force) { + if ((this.visible == visible || !gotSecret) && !force) return; this.visible = visible; @@ -180,9 +172,7 @@ public class BotBossBar extends BossBar { } public void setMax (final long max, final boolean force) { - if (!gotSecret) return; - - if (this.max == max && !force) return; + if ((this.max == max || !gotSecret) && !force) return; this.max = max; diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/BossbarManagerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/BossbarManagerPlugin.java index a37b7733..a965cc4d 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/BossbarManagerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/BossbarManagerPlugin.java @@ -5,11 +5,11 @@ import me.chayapak1.chomens_bot.data.bossbar.BossBar; import me.chayapak1.chomens_bot.data.bossbar.BotBossBar; import me.chayapak1.chomens_bot.data.listener.Listener; import me.chayapak1.chomens_bot.data.player.PlayerEntry; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import org.geysermc.mcprotocollib.network.Session; import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent; import org.geysermc.mcprotocollib.network.packet.Packet; -import org.geysermc.mcprotocollib.protocol.data.game.BossBarColor; import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.ClientboundBossEventPacket; import java.util.HashMap; @@ -46,6 +46,17 @@ public class BossbarManagerPlugin implements Listener { try { switch (packet.getAction()) { case ADD -> { + serverBossBars.put( + packet.getUuid(), + new BossBar( + packet.getUuid(), + packet.getTitle(), + packet.getColor(), + packet.getDivision(), + packet.getHealth() + ) + ); + final Map mapCopy = new HashMap<>(bossBars); for (final Map.Entry _bossBar : mapCopy.entrySet()) { @@ -79,28 +90,21 @@ public class BossbarManagerPlugin implements Listener { newBossBar.setTitle(bossBar.title); } } - - serverBossBars.put( - packet.getUuid(), - new BossBar( - packet.getUuid(), - packet.getTitle(), - packet.getColor(), - packet.getDivision(), - packet.getHealth() - ) - ); } case REMOVE -> serverBossBars.remove(packet.getUuid()); // self care is at the check function case UPDATE_STYLE -> { final BossBar bossBar = serverBossBars.get(packet.getUuid()); + if (bossBar == null) return; + bossBar.color = packet.getColor(); bossBar.division = packet.getDivision(); } case UPDATE_TITLE -> { final BossBar bossBar = serverBossBars.get(packet.getUuid()); + if (bossBar == null) return; + final BotBossBar botBossBar = get(bossBar.uuid); if (botBossBar != null && botBossBar.secret.equals(packet.getTitle())) { @@ -114,6 +118,8 @@ public class BossbarManagerPlugin implements Listener { case UPDATE_HEALTH -> { final BossBar bossBar = serverBossBars.get(packet.getUuid()); + if (bossBar == null) return; + bossBar.health = packet.getHealth(); } } @@ -133,7 +139,7 @@ public class BossbarManagerPlugin implements Listener { if (serverBossBar == null) { bossBar.gotSecret = false; - addBossBar(bossBar.id, bossBar, true); + addBossBar(bossBar.id, bossBar); } else if (!serverBossBar.title.equals(bossBar.title)) { bossBar.setTitle(bossBar.title, true); } else if (bossBar.value() != serverBossBar.health * bossBar.max()) { @@ -152,7 +158,7 @@ public class BossbarManagerPlugin implements Listener { for (final Map.Entry _bossBar : bossBars.entrySet()) { final BotBossBar bossBar = _bossBar.getValue(); - addBossBar(bossBar.id, bossBar, true); + addBossBar(bossBar.id, bossBar); } } @@ -181,37 +187,25 @@ public class BossbarManagerPlugin implements Listener { bossBars.put(bossBar.uuid, bossBar); - addBossBar(bossBar.id, bossBar, true); + addBossBar(bossBar.id, bossBar); } private void addBossBar (final String name, final BotBossBar bossBar) { - addBossBar(name, bossBar, false); - } - - private void addBossBar (final String name, final BotBossBar bossBar, final boolean secret) { if (!enabled || actionBar) return; - final String prefix = "minecraft:bossbar set " + name + " "; + final Component title = bossBar.secret; - final String stringifiedName = GsonComponentSerializer.gson().serialize(secret ? bossBar.secret : bossBar.title); + final String stringTitle = GsonComponentSerializer.gson().serialize(title); - String division = null; + bot.core.run("minecraft:bossbar add " + name + " " + stringTitle); - switch (bossBar.division) { - case NONE -> division = "progress"; - case NOTCHES_20 -> division = "notched_20"; - case NOTCHES_6 -> division = "notched_6"; - case NOTCHES_12 -> division = "notched_12"; - case NOTCHES_10 -> division = "notched_10"; - } - - bot.core.run("minecraft:bossbar add " + name + " " + stringifiedName); - bot.core.run(prefix + "players " + bossBar.players()); - bot.core.run(prefix + "color " + (bossBar.color == BossBarColor.LIME ? "green" : (bossBar.color == BossBarColor.CYAN ? "blue" : bossBar.color.name().toLowerCase()))); - bot.core.run(prefix + "visible " + bossBar.visible()); - bot.core.run(prefix + "style " + division); - bot.core.run(prefix + "max " + bossBar.max()); - bot.core.run(prefix + "value " + bossBar.value()); + bossBar.setTitle(title, true); + bossBar.setPlayers(bossBar.players(), true); + bossBar.setVisible(bossBar.visible(), true); + bossBar.setColor(bossBar.color, true); + bossBar.setDivision(bossBar.division(), true); + bossBar.setMax(bossBar.max(), true); + bossBar.setValue(bossBar.value(), true); } public void remove (final String name) {