From 48f8013767abc32cf2b7270685e4180d8f0cb60d Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 2 Apr 2023 16:49:13 +0700 Subject: [PATCH] finally add bossbar manager --- .../chipmunk/chayapak/chomens_bot/Bot.java | 2 + .../chayapak/chomens_bot/data/BossBar.java | 18 +++++ .../chomens_bot/data/BossBarColor.java | 17 ++++ .../chomens_bot/data/BossBarStyle.java | 15 ++++ .../plugins/BossbarManagerPlugin.java | 77 +++++++++++++++++++ .../plugins/MusicPlayerPlugin.java | 38 ++++++--- .../chomens_bot/plugins/TPSPlugin.java | 43 +++++++---- 7 files changed, 185 insertions(+), 25 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBar.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarColor.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarStyle.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 2edab2d..4a5ff80 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -46,6 +46,7 @@ public class Bot { @Getter private final CommandHandlerPlugin commandHandler; @Getter private final ChatCommandHandlerPlugin chatCommandHandler; @Getter private final HashingPlugin hashing; + @Getter private final BossbarManagerPlugin bossbar; @Getter private final MusicPlayerPlugin music; @Getter private final TPSPlugin tps; @Getter private final EvalRunnerPlugin eval; @@ -79,6 +80,7 @@ public class Bot { this.commandHandler = new CommandHandlerPlugin(); this.chatCommandHandler = new ChatCommandHandlerPlugin(this); this.hashing = new HashingPlugin(this); + this.bossbar = new BossbarManagerPlugin(this); this.music = new MusicPlayerPlugin(this); this.tps = new TPSPlugin(this); this.eval = new EvalRunnerPlugin(this); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBar.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBar.java new file mode 100644 index 0000000..b201996 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBar.java @@ -0,0 +1,18 @@ +package land.chipmunk.chayapak.chomens_bot.data; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import net.kyori.adventure.text.Component; + +@AllArgsConstructor +public class BossBar { + @Getter @Setter private Component name; + @Getter @Setter private BossBarColor color; + @Getter @Setter private long max; + @Getter @Setter private String players; + @Getter @Setter private BossBarStyle style; + @Getter @Setter private int value; + @Getter @Setter private boolean visible; +} + diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarColor.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarColor.java new file mode 100644 index 0000000..30786a4 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarColor.java @@ -0,0 +1,17 @@ +package land.chipmunk.chayapak.chomens_bot.data; + +public enum BossBarColor { + BLUE("blue"), + GREEN("green"), + PINK("pink"), + PURPLE("purple"), + RED("red"), + WHITE("white"), + YELLOW("yellow"),; + + public final String color; + + BossBarColor (String color) { + this.color = color; + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarStyle.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarStyle.java new file mode 100644 index 0000000..c9477f1 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/data/BossBarStyle.java @@ -0,0 +1,15 @@ +package land.chipmunk.chayapak.chomens_bot.data; + +public enum BossBarStyle { + NOTCHED_10("notched_10"), + NOTCHED_12("notched_12"), + NOTCHED_20("notched_20"), + NOTCHED_6("notched_6"), + PROGRESS("progress"); + + public final String style; + + BossBarStyle (String style) { + this.style = style; + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java new file mode 100644 index 0000000..a58d954 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/BossbarManagerPlugin.java @@ -0,0 +1,77 @@ +package land.chipmunk.chayapak.chomens_bot.plugins; + +import com.github.steveice10.packetlib.event.session.DisconnectedEvent; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.BossBar; +import lombok.Getter; +import lombok.Setter; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class BossbarManagerPlugin extends CorePlugin.Listener { + private final Bot bot; + + private ScheduledFuture tickTask; + + private final Map bossBars = new HashMap<>(); + + @Getter @Setter private String bossBarPrefix = "chomens_bot:"; + @Getter @Setter private String textDisplayPrefix = "chomens_bot_"; + + public BossbarManagerPlugin (Bot bot) { + this.bot = bot; + + bot.core().addListener(this); + + bot.addListener(new SessionAdapter() { + @Override + public void disconnected(DisconnectedEvent event) { + tickTask.cancel(true); + } + }); + } + + @Override + public void ready () { + tickTask = bot.executor().scheduleAtFixedRate(this::tick, 0, 50, TimeUnit.MILLISECONDS); + } + + public void tick () { + for (Map.Entry _bossBar : bossBars.entrySet()) { + final String name = _bossBar.getKey(); + final BossBar bossBar = _bossBar.getValue(); + + final String stringifiedComponent = GsonComponentSerializer.gson().serialize(bossBar.name()); + + bot.core().run("minecraft:bossbar add " + bossBarPrefix + name + " \"\""); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " players " + bossBar.players()); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " name " + stringifiedComponent); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " color " + bossBar.color().color); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " visible " + bossBar.visible()); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " style " + bossBar.style().style); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " value " + bossBar.value()); + bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " max " + bossBar.max()); + + // is there any way instead of using random? + bot.core().run("minecraft:data modify entity @e[tag=" + textDisplayPrefix + name + ",limit=1,sort=random] text set value '" + stringifiedComponent.replace("'", "\\\'") + "'"); + } + } + + public void add (String name, BossBar bossBar) { + bossBars.put(name, bossBar); + } + + public void remove (String name) { + bossBars.remove(name); + bot.core().run("minecraft:bossbar remove " + bossBarPrefix + name); + } + + public BossBar get (String name) { + return bossBars.get(name); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java index 90c06c2..ad1160a 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/MusicPlayerPlugin.java @@ -2,14 +2,16 @@ package land.chipmunk.chayapak.chomens_bot.plugins; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.BossBar; +import land.chipmunk.chayapak.chomens_bot.data.BossBarColor; +import land.chipmunk.chayapak.chomens_bot.data.BossBarStyle; import land.chipmunk.chayapak.chomens_bot.song.*; import land.chipmunk.chayapak.chomens_bot.util.NumberUtilities; import lombok.Getter; import lombok.Setter; -import land.chipmunk.chayapak.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 java.io.File; import java.net.URL; @@ -41,7 +43,8 @@ public class MusicPlayerPlugin extends SessionAdapter { @Getter @Setter private float pitch = 0; private int ticksUntilPausedBossbar = 20; - private final String bossbarName = "chomens_bot:music"; // maybe make this in the config? + + private final String bossbarName = "music"; public MusicPlayerPlugin (Bot bot) { this.bot = bot; @@ -101,6 +104,16 @@ public class MusicPlayerPlugin extends SessionAdapter { if (currentSong == null) { if (songQueue.size() == 0) return; + bot.bossbar().add(bossbarName, new BossBar( + Component.empty(), + BossBarColor.WHITE, + 0, + "", + BossBarStyle.PROGRESS, + 0, + false + )); + currentSong = songQueue.get(0); // songQueue.poll(); bot.chat().tellraw(Component.translatable("Now playing %s", Component.empty().append(currentSong.name).color(NamedTextColor.GOLD))); currentSong.play(); @@ -109,14 +122,15 @@ public class MusicPlayerPlugin extends SessionAdapter { if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; else ticksUntilPausedBossbar = 20; - bot.core().run("minecraft:bossbar add " + bossbarName + " \"\""); - bot.core().run("minecraft:bossbar set " + bossbarName + " players " + SELECTOR); - bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(generateBossbar())); - bot.core().run("minecraft:bossbar set " + bossbarName + " color " + (pitch > 0 ? "purple" : "yellow")); - bot.core().run("minecraft:bossbar set " + bossbarName + " visible true"); - bot.core().run("minecraft:bossbar set " + bossbarName + " style progress"); - bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) Math.floor(currentSong.time)); - bot.core().run("minecraft:bossbar set " + bossbarName + " max " + currentSong.length); + final BossBar bossBar = bot.bossbar().get(bossbarName); + + bossBar.players(SELECTOR); + bossBar.name(generateBossbar()); + bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); + bossBar.visible(true); + bossBar.style(BossBarStyle.PROGRESS); + bossBar.value((int) Math.floor(currentSong.time)); + bossBar.max(currentSong.length); if (currentSong.paused) return; @@ -169,7 +183,7 @@ public class MusicPlayerPlugin extends SessionAdapter { } public void removeBossbar () { - bot.core().run("minecraft:bossbar remove " + bossbarName); + bot.bossbar().remove(bossbarName); } public Component generateBossbar () { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java index f699dfc..11d634b 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/TPSPlugin.java @@ -6,10 +6,12 @@ import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.BossBar; +import land.chipmunk.chayapak.chomens_bot.data.BossBarColor; +import land.chipmunk.chayapak.chomens_bot.data.BossBarStyle; import land.chipmunk.chayapak.chomens_bot.util.NumberUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import java.util.Arrays; import java.util.concurrent.TimeUnit; @@ -24,23 +26,38 @@ public class TPSPlugin extends SessionAdapter { private long timeLastTimeUpdate = -1; private long timeGameJoined; - private final String bossbarName = "chomens_bot:tpsbar"; + private final String bossbarName = "tpsbar"; public TPSPlugin (Bot bot) { this.bot = bot; bot.addListener(this); - bot.executor().scheduleAtFixedRate(this::updateTPSBar, 0, 50, TimeUnit.MILLISECONDS); + bot.core().addListener(new CorePlugin.Listener() { + @Override + public void ready() { + bot.executor().scheduleAtFixedRate(() -> updateTPSBar(), 0, 50, TimeUnit.MILLISECONDS); + } + } + ); } public void on () { enabled = true; + bot.bossbar().add(bossbarName, new BossBar( + Component.empty(), + BossBarColor.WHITE, + 0, + "", + BossBarStyle.PROGRESS, + 0, + false + )); } public void off () { enabled = false; - bot.core().run("minecraft:bossbar remove " + bossbarName); + bot.bossbar().remove(bossbarName); } private void updateTPSBar () { @@ -54,15 +71,15 @@ public class TPSPlugin extends SessionAdapter { Component.text(tickRate).color(NamedTextColor.GREEN) ).color(NamedTextColor.GRAY); - // TODO: move these to like a bossbar manager of some sort - bot.core().run("minecraft:bossbar add " + bossbarName + " \"\""); - bot.core().run("minecraft:bossbar set " + bossbarName + " players @a"); - bot.core().run("minecraft:bossbar set " + bossbarName + " name " + GsonComponentSerializer.gson().serialize(component)); - bot.core().run("minecraft:bossbar set " + bossbarName + " color yellow"); - bot.core().run("minecraft:bossbar set " + bossbarName + " visible true"); - bot.core().run("minecraft:bossbar set " + bossbarName + " style notched_20"); - bot.core().run("minecraft:bossbar set " + bossbarName + " value " + (int) tickRate); - bot.core().run("minecraft:bossbar set " + bossbarName + " max 20"); + final BossBar bossBar = bot.bossbar().get(bossbarName); + + bossBar.players("@a"); + bossBar.name(component); + bossBar.color(BossBarColor.YELLOW); + bossBar.visible(true); + bossBar.style(BossBarStyle.NOTCHED_20); + bossBar.value((int) tickRate); + bossBar.max(20); } @Override