finally add bossbar manager

This commit is contained in:
Chayapak 2023-04-02 16:49:13 +07:00
parent 6849a7e130
commit 48f8013767
7 changed files with 185 additions and 25 deletions

View file

@ -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);

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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<String, BossBar> 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<String, BossBar> _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);
}
}

View file

@ -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 () {

View file

@ -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