FINALLY make the bossbar manager not spammy

Closes #3 (#3)
this took 3 hours to make which is not that much mabe
This commit is contained in:
Chayapak 2023-06-18 20:59:57 +07:00
parent d30de87fcc
commit afaf32ec00
8 changed files with 301 additions and 99 deletions

View file

@ -1,18 +1,19 @@
package land.chipmunk.chayapak.chomens_bot.data;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import java.util.UUID;
@AllArgsConstructor
public class BossBar {
@Getter @Setter private Component name;
@Getter @Setter private String color;
@Getter @Setter private long max;
@Getter @Setter private String players;
@Getter @Setter private String style;
@Getter @Setter private int value;
@Getter @Setter private boolean visible;
public UUID uuid;
public Component title;
public BossBarColor color;
public BossBarDivision division;
public float health;
}

View file

@ -1,11 +0,0 @@
package land.chipmunk.chayapak.chomens_bot.data;
public class BossBarColor {
public static final String BLUE = "blue";
public static final String GREEN = "green";
public static final String PINK = "pink";
public static final String PURPLE = "purple";
public static final String RED = "red";
public static final String WHITE = "white";
public static final String YELLOW = "yellow";
}

View file

@ -1,9 +0,0 @@
package land.chipmunk.chayapak.chomens_bot.data;
public class BossBarStyle {
public static final String NOTCHED_10 = "notched_10";
public static final String NOTCHED_12 = "notched_12";
public static final String NOTCHED_20 = "notched_20";
public static final String NOTCHED_6 = "notched_6";
public static final String PROGRESS = "progress";
}

View file

@ -0,0 +1,120 @@
package land.chipmunk.chayapak.chomens_bot.data;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.UUID;
public class BotBossBar extends BossBar {
public final UUID uuid = UUID.randomUUID();
private final Bot bot;
public String id;
public String players;
public boolean visible;
public long max;
public int value;
public BotBossBar(
Component title,
String players,
BossBarColor color,
BossBarDivision division,
boolean visible,
int max,
int value,
Bot bot
) {
super(null, title, color, division, value);
this.players = players;
this.visible = visible;
this.max = max;
this.bot = bot;
}
public void setTitle (Component title) {
setTitle(title, false);
}
public void setTitle (Component title, boolean force) {
if (ComponentUtilities.isEqual(this.title, title) && !force) return;
this.title = title;
bot.core().run("minecraft:bossbar set " + id + " name " + GsonComponentSerializer.gson().serialize(title));
}
public void setColor (BossBarColor color) {
setColor(color, false);
}
public void setColor (BossBarColor color, boolean force) {
if (this.color == color && !force) return;
this.color = color;
bot.core().run("minecraft:bossbar set " + id + " color " + (color == BossBarColor.LIME ? "green" : color.name().toLowerCase()));
}
public void setPlayers (String players) {
if (this.players.equals(players)) return;
this.players = players;
bot.core().run("minecraft:bossbar set " + id + " players " + players);
}
public void setDivision (BossBarDivision division) {
setDivision(division, false);
}
public void setDivision (BossBarDivision _division, boolean force) {
if (this.division == _division && !force) return;
this.division = _division;
String division = null;
switch (_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 set " + id + " style " + division);
}
public void setValue (int value) {
setValue(value, false);
}
public void setValue (int value, boolean force) {
if (this.value == value && !force) return;
this.value = value;
bot.core().run("minecraft:bossbar set " + id + " value " + value);
}
public void setVisible (boolean visible) {
if (this.visible == visible) return;
this.visible = visible;
bot.core().run("minecraft:bossbar set " + id + " visible " + visible);
}
public void setMax (long max) {
setMax(max, false);
}
public void setMax (long max, boolean force) {
if (this.max == max && !force) return;
this.max = max;
bot.core().run("minecraft:bossbar set " + id + " max " + max);
}
}

View file

@ -1,74 +1,167 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundBossEventPacket;
import com.github.steveice10.packetlib.Session;
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.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
// TODO: players can make the bossbar with the same exact component and fard the bossbar manager so mabe fix it
// yes this has been rewritten to be not spammy
public class BossbarManagerPlugin extends Bot.Listener {
private final Bot bot;
private final Map<String, BossBar> bossBars = new HashMap<>();
@Getter private final Map<UUID, BossBar> serverBossBars = new HashMap<>();
private final Map<UUID, BotBossBar> bossBars = new HashMap<>();
@Getter @Setter private boolean enabled = true;
@Getter @Setter private boolean actionBar = false;
@Getter @Setter private String bossBarPrefix = "chomens_bot:";
@Getter @Setter private String textDisplayPrefix = "chomens_bot_";
public BossbarManagerPlugin (Bot bot) {
this.bot = bot;
bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
tick();
}
});
bot.addListener(this);
}
public void tick () {
if (!enabled) return;
for (Map.Entry<String, BossBar> _bossBar : bossBars.entrySet()) {
final String name = _bossBar.getKey();
final BossBar bossBar = _bossBar.getValue();
@Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundBossEventPacket) packetReceived((ClientboundBossEventPacket) packet);
}
final String stringifiedComponent = GsonComponentSerializer.gson().serialize(bossBar.name());
public void packetReceived(ClientboundBossEventPacket packet) {
switch (packet.getAction()) {
case ADD -> serverBossBars.put(
packet.getUuid(),
new BossBar(
packet.getUuid(),
packet.getTitle(),
packet.getColor(),
packet.getDivision(),
packet.getHealth()
)
);
case REMOVE -> {
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue();
if (!actionBar) {
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 + " color " + bossBar.color());
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " visible " + bossBar.visible());
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " style " + bossBar.style());
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " max " + bossBar.max());
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " name " + stringifiedComponent);
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " value " + bossBar.value());
} else {
bot.chat().actionBar(bossBar.name(), bossBar.players());
if (ComponentUtilities.isEqual(bossBar.title, serverBossBars.get(packet.getUuid()).title)) {
addBossBar(bossBar.id, bossBar);
break;
}
}
serverBossBars.remove(packet.getUuid());
}
case UPDATE_STYLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
// 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("'", "\\'") + "'");
final BotBossBar botBossBar = get(bossBar.title);
if (botBossBar != null && botBossBar.color != packet.getColor()) {
botBossBar.setColor(bossBar.color, true);
} else if (botBossBar != null && botBossBar.division != packet.getDivision()) {
botBossBar.setDivision(bossBar.division, true);
}
bossBar.color = packet.getColor();
bossBar.division = packet.getDivision();
}
case UPDATE_TITLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
final BotBossBar botBossBar = get(bossBar.title);
if (!ComponentUtilities.isEqual(packet.getTitle(), bossBar.title) && botBossBar != null) {
botBossBar.setTitle(bossBar.title, true);
}
bossBar.title = packet.getTitle();
}
case UPDATE_HEALTH -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
final BotBossBar botBossBar = get(bossBar.title);
if (botBossBar != null && botBossBar.value != packet.getHealth() * botBossBar.max) {
botBossBar.setValue(botBossBar.value, true);
botBossBar.setMax(botBossBar.max, true);
}
bossBar.health = packet.getHealth();
}
}
}
public void add (String name, BossBar bossBar) {
bossBars.put(name, bossBar);
public void add (String name, BotBossBar bossBar) {
if (!enabled) return;
bossBar.id = bossBarPrefix + name;
addBossBar(bossBar.id, bossBar);
bossBars.put(bossBar.uuid, bossBar);
}
private void addBossBar (String name, BotBossBar bossBar) {
final String prefix = "minecraft:bossbar set " + name + " ";
final String stringifiedName = GsonComponentSerializer.gson().serialize(bossBar.title);
String division = null;
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.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);
}
public void remove (String name) {
bossBars.remove(name);
for (Map.Entry<UUID, BotBossBar> bossBar : bossBars.entrySet()) {
if (bossBar.getValue().id.equals(bossBarPrefix + name)) bossBars.remove(bossBar.getValue().uuid);
}
bot.core().run("minecraft:bossbar remove " + bossBarPrefix + name);
}
public BossBar get (String name) {
return bossBars.get(name);
public BotBossBar get (String name) {
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue();
if (bossBar.id.equals(bossBarPrefix + name)) return bossBars.get(bossBar.uuid);
}
return null;
}
public BotBossBar get (Component component) {
for (Map.Entry<UUID, BotBossBar> bossBar : bossBars.entrySet()) {
if (ComponentUtilities.isEqual(component, bossBar.getValue().title)) {
return bossBar.getValue();
}
}
return null;
}
}

View file

@ -1,10 +1,10 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
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.data.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.song.*;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
@ -114,17 +114,13 @@ public class MusicPlayerPlugin extends Bot.Listener {
if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return;
else ticksUntilPausedBossbar = 20;
BossBar bossBar = bot.bossbar().get(bossbarName);
BotBossBar bossBar = bot.bossbar().get(bossbarName);
if (bossBar == null) bossBar = addBossBar();
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 * speed));
bossBar.max(currentSong.length);
bossBar.setTitle(generateBossbar());
bossBar.setColor(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.setValue((int) Math.floor(currentSong.time * speed));
if (currentSong.paused) return;
@ -185,15 +181,18 @@ public class MusicPlayerPlugin extends Bot.Listener {
currentSong.play();
}
public BossBar addBossBar () {
final BossBar bossBar = new BossBar(
public BotBossBar addBossBar () {
if (currentSong == null) return null;
final BotBossBar bossBar = new BotBossBar(
Component.empty(),
SELECTOR,
BossBarColor.WHITE,
BossBarDivision.NONE,
true,
(int) currentSong.length,
0,
"",
BossBarStyle.PROGRESS,
0,
false
bot
);
bot.bossbar().add(bossbarName, bossBar);

View file

@ -1,13 +1,14 @@
package land.chipmunk.chayapak.chomens_bot.plugins;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundSetTimePacket;
import com.github.steveice10.packetlib.Session;
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.data.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@ -49,15 +50,19 @@ public class TPSPlugin extends Bot.Listener {
public void on () {
enabled = true;
bot.bossbar().add(bossbarName, new BossBar(
final BotBossBar bossBar = new BotBossBar(
Component.empty(),
BossBarColor.WHITE,
"@a",
getBossBarColor(getTickRate()),
BossBarDivision.NOTCHES_20,
true,
20,
0,
"",
BossBarStyle.PROGRESS,
0,
false
));
bot
);
bot.bossbar().add(bossbarName, bossBar);
}
public void off () {
@ -79,15 +84,13 @@ public class TPSPlugin extends Bot.Listener {
Component.text(formatter.format(tickRate)).color(getColor(tickRate))
).color(NamedTextColor.DARK_GRAY);
final BossBar bossBar = bot.bossbar().get(bossbarName);
final BotBossBar bossBar = bot.bossbar().get(bossbarName);
bossBar.players("@a");
bossBar.name(component);
bossBar.color(getBossBarColor(tickRate));
bossBar.visible(true);
bossBar.style(BossBarStyle.NOTCHED_20);
bossBar.value((int) Math.round(tickRate));
bossBar.max(20);
if (bossBar == null) return;
bossBar.setTitle(component);
bossBar.setColor(getBossBarColor(tickRate));
bossBar.setValue((int) Math.round(tickRate));
} catch (Exception e) {
e.printStackTrace();
}
@ -100,8 +103,8 @@ public class TPSPlugin extends Bot.Listener {
else return NamedTextColor.DARK_RED;
}
private String getBossBarColor (double tickRate) {
if (tickRate > 15) return BossBarColor.GREEN;
private BossBarColor getBossBarColor (double tickRate) {
if (tickRate > 15) return BossBarColor.LIME;
else if (tickRate == 15) return BossBarColor.YELLOW;
else if (tickRate < 15 && tickRate > 10) return BossBarColor.RED;
else return BossBarColor.PURPLE;

View file

@ -11,6 +11,7 @@ import net.kyori.adventure.text.SelectorComponent;
import net.kyori.adventure.text.KeybindComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.io.BufferedReader;
import java.io.InputStream;
@ -22,6 +23,11 @@ import java.util.regex.Pattern;
// totallynotskidded from chipmunkbot and added colors (ignore the ohio code please,..,.)
public class ComponentUtilities {
// is this the best way to check?
public static boolean isEqual (Component component1, Component component2) {
return component1.toString().equals(component2.toString());
}
private static final Map<String, String> language = loadJsonStringMap("language.json");
private static final Map<String, String> keybinds = loadJsonStringMap("keybinds.json");