refactor: improve and fix bossbar manager

This commit is contained in:
ChomeNS
2025-04-09 09:04:56 +07:00
parent ffdfcb421d
commit 297b84801f
5 changed files with 44 additions and 30 deletions

View File

@@ -1 +1 @@
2495
2498

View File

@@ -2,7 +2,6 @@ package me.chayapak1.chomens_bot.data.bossbar;
import me.chayapak1.chomens_bot.Bot;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.geysermc.mcprotocollib.protocol.data.game.BossBarColor;
import org.geysermc.mcprotocollib.protocol.data.game.BossBarDivision;
@@ -10,13 +9,13 @@ import org.geysermc.mcprotocollib.protocol.data.game.BossBarDivision;
import java.util.UUID;
public class BotBossBar extends BossBar {
public UUID uuid = UUID.randomUUID(); // the random uuid will be temporary
public UUID uuid;
public final Component secret = Component
.text(UUID.randomUUID().toString())
.color(NamedTextColor.BLACK)
.append(Component.space())
.append(Component.text("(please ignore!)").color(NamedTextColor.GRAY));
.translatable(
"",
Component.text(UUID.randomUUID().toString())
);
public boolean gotSecret = false;

View File

@@ -6,7 +6,6 @@ import me.chayapak1.chomens_bot.data.bossbar.BotBossBar;
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.geysermc.mcprotocollib.network.Session;
import org.geysermc.mcprotocollib.network.event.session.ConnectedEvent;
import org.geysermc.mcprotocollib.network.event.session.DisconnectedEvent;
import org.geysermc.mcprotocollib.network.packet.Packet;
import org.geysermc.mcprotocollib.protocol.data.game.BossBarColor;
@@ -15,10 +14,12 @@ import org.geysermc.mcprotocollib.protocol.packet.ingame.clientbound.Clientbound
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
// yes this has been rewritten to be not spammy
public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.Listener {
public class BossbarManagerPlugin
extends Bot.Listener
implements PlayersPlugin.Listener, TickPlugin.Listener, CorePlugin.Listener
{
private final Bot bot;
public final Map<UUID, BossBar> serverBossBars = new HashMap<>();
@@ -36,7 +37,8 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
bot.addListener(this);
bot.players.addListener(this);
bot.executor.scheduleAtFixedRate(this::check, 0, 600, TimeUnit.MILLISECONDS);
bot.tick.addListener(this);
bot.core.addListener(this);
}
@Override
@@ -126,7 +128,8 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
}
}
private void check () {
@Override
public void onSecondTick () {
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final UUID uuid = _bossBar.getKey();
final BotBossBar bossBar = _bossBar.getValue();
@@ -137,7 +140,7 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
bossBar.gotSecret = false;
addBossBar(bossBar.id, bossBar, true);
} else if (!bossBar.title().equals(serverBossBar.title)) {
} else if (!serverBossBar.title.equals(bossBar.title)) {
bossBar.setTitle(bossBar.title, true);
} else if (bossBar.value() != serverBossBar.health * bossBar.max()) {
bossBar.setValue(bossBar.value(), true);
@@ -151,7 +154,7 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
}
@Override
public void connected (ConnectedEvent event) {
public void coreReady () {
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue();
@@ -192,7 +195,7 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
}
private void addBossBar (String name, BotBossBar bossBar, boolean secret) {
if (actionBar) return;
if (!enabled || actionBar) return;
final String prefix = "minecraft:bossbar set " + name + " ";
@@ -230,6 +233,8 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
}
public BotBossBar get (String name) {
if (!enabled) return null;
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue();
@@ -240,6 +245,8 @@ public class BossbarManagerPlugin extends Bot.Listener implements PlayersPlugin.
}
public BotBossBar get (UUID uuid) {
if (!enabled) return null;
for (Map.Entry<UUID, BotBossBar> bossBar : bossBars.entrySet()) {
if (bossBar.getValue().uuid == uuid) return bossBar.getValue();
}

View File

@@ -33,10 +33,10 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
public static final String CUSTOM_PITCH_SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic,tag=custompitch]";
public static final String BOTH_SELECTOR = "@a[tag=!nomusic,tag=!chomens_bot_nomusic]";
private final Bot bot;
public static final Path SONG_DIR = Path.of("songs");
private static final String BOSS_BAR_NAME = "music";
static {
try {
if (!Files.exists(SONG_DIR)) Files.createDirectory(SONG_DIR);
@@ -45,6 +45,8 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
}
}
private final Bot bot;
public Song currentSong;
public final List<Song> songQueue = Collections.synchronizedList(new LinkedList<>());
public SongLoaderThread loaderThread = null;
@@ -65,7 +67,6 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
public boolean locked = false; // this can be set through servereval
private final String bossBarName = "music";
public BossBarColor bossBarColor;
public String currentLyrics = "";
@@ -159,11 +160,11 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
if (!currentSong.finished()) {
handleLyrics();
BotBossBar bossBar = bot.bossbar.get(bossBarName);
BotBossBar bossBar = bot.bossbar.get(BOSS_BAR_NAME);
if (bossBar == null && bot.bossbar.enabled) bossBar = addBossBar();
if (bossBar == null) bossBar = addBossBar();
if (bot.bossbar.enabled && bot.options.useCore) {
if (bossBar != null && bot.options.useCore) {
bossBar.setTitle(generateBossBar());
bossBar.setColor(bossBarColor);
bossBar.setValue((int) Math.floor(((double) (currentSong.time * speed) / 1000)));
@@ -243,7 +244,7 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
bot
);
bot.bossbar.add(bossBarName, bossBar);
bot.bossbar.add(BOSS_BAR_NAME, bossBar);
return bossBar;
}
@@ -289,11 +290,11 @@ public class MusicPlayerPlugin extends Bot.Listener implements CorePlugin.Listen
}
public void removeBossBar () {
final BotBossBar bossBar = bot.bossbar.get(bossBarName);
final BotBossBar bossBar = bot.bossbar.get(BOSS_BAR_NAME);
if (bossBar != null) bossBar.setTitle(Component.text("No song is currently playing"));
bot.bossbar.remove(bossBarName);
bot.bossbar.remove(BOSS_BAR_NAME);
}
public Component generateBossBar () {

View File

@@ -35,11 +35,7 @@ public class TPSPlugin extends Bot.Listener implements TickPlugin.Listener {
bot.tick.addListener(this);
}
public void on () {
if (enabled) return;
enabled = true;
private void createBossBar () {
final BotBossBar bossBar = new BotBossBar(
Component.empty(),
"@a",
@@ -54,6 +50,14 @@ public class TPSPlugin extends Bot.Listener implements TickPlugin.Listener {
bot.bossbar.add(bossbarName, bossBar);
}
public void on () {
if (enabled) return;
enabled = true;
createBossBar();
}
public void off () {
enabled = false;
@@ -85,7 +89,10 @@ public class TPSPlugin extends Bot.Listener implements TickPlugin.Listener {
final BotBossBar bossBar = bot.bossbar.get(bossbarName);
if (bossBar == null) return;
if (bossBar == null) {
createBossBar();
return;
}
bossBar.setTitle(component);
bossBar.setColor(getBossBarColor(tickRate));