FINALLY make the bossbar manager not spammy

Closes #3 (ChomeNS/chomens-bot-java#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; 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.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.UUID;
@AllArgsConstructor @AllArgsConstructor
public class BossBar { public class BossBar {
@Getter @Setter private Component name; public UUID uuid;
@Getter @Setter private String color;
@Getter @Setter private long max; public Component title;
@Getter @Setter private String players; public BossBarColor color;
@Getter @Setter private String style; public BossBarDivision division;
@Getter @Setter private int value; public float health;
@Getter @Setter private boolean visible;
} }

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; 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.Bot;
import land.chipmunk.chayapak.chomens_bot.data.BossBar; 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.Getter;
import lombok.Setter; import lombok.Setter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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 { public class BossbarManagerPlugin extends Bot.Listener {
private final Bot bot; 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 enabled = true;
@Getter @Setter private boolean actionBar = false;
@Getter @Setter private String bossBarPrefix = "chomens_bot:"; @Getter @Setter private String bossBarPrefix = "chomens_bot:";
@Getter @Setter private String textDisplayPrefix = "chomens_bot_";
public BossbarManagerPlugin (Bot bot) { public BossbarManagerPlugin (Bot bot) {
this.bot = bot; this.bot = bot;
bot.tick().addListener(new TickPlugin.Listener() {
@Override
public void onTick() {
tick();
}
});
bot.addListener(this); bot.addListener(this);
} }
public void tick () { @Override
public void packetReceived(Session session, Packet packet) {
if (packet instanceof ClientboundBossEventPacket) packetReceived((ClientboundBossEventPacket) packet);
}
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 (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());
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, BotBossBar bossBar) {
if (!enabled) return; if (!enabled) return;
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()); bossBar.id = bossBarPrefix + name;
if (!actionBar) { addBossBar(bossBar.id, bossBar);
bot.core().run("minecraft:bossbar add " + bossBarPrefix + name + " \"\"");
bot.core().run("minecraft:bossbar set " + bossBarPrefix + name + " players " + bossBar.players()); bossBars.put(bossBar.uuid, bossBar);
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());
} }
// is there any way instead of using random? private void addBossBar (String name, BotBossBar bossBar) {
bot.core().run("minecraft:data modify entity @e[tag=" + textDisplayPrefix + name + ",limit=1,sort=random] text set value '" + stringifiedComponent.replace("'", "\\'") + "'"); 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";
} }
public void add (String name, BossBar bossBar) { bot.core().run("minecraft:bossbar add " + name + " " + stringifiedName);
bossBars.put(name, bossBar); 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) { 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); bot.core().run("minecraft:bossbar remove " + bossBarPrefix + name);
} }
public BossBar get (String name) { public BotBossBar get (String name) {
return bossBars.get(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; 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 com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import land.chipmunk.chayapak.chomens_bot.Bot; 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.data.BossBarColor;
import land.chipmunk.chayapak.chomens_bot.data.BossBarStyle;
import land.chipmunk.chayapak.chomens_bot.song.*; import land.chipmunk.chayapak.chomens_bot.song.*;
import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities;
import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
@ -114,17 +114,13 @@ public class MusicPlayerPlugin extends Bot.Listener {
if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return; if (currentSong.paused && ticksUntilPausedBossbar-- < 0) return;
else ticksUntilPausedBossbar = 20; else ticksUntilPausedBossbar = 20;
BossBar bossBar = bot.bossbar().get(bossbarName); BotBossBar bossBar = bot.bossbar().get(bossbarName);
if (bossBar == null) bossBar = addBossBar(); if (bossBar == null) bossBar = addBossBar();
bossBar.players(SELECTOR); bossBar.setTitle(generateBossbar());
bossBar.name(generateBossbar()); bossBar.setColor(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW);
bossBar.color(pitch > 0 ? BossBarColor.PURPLE : BossBarColor.YELLOW); bossBar.setValue((int) Math.floor(currentSong.time * speed));
bossBar.visible(true);
bossBar.style(BossBarStyle.PROGRESS);
bossBar.value((int) Math.floor(currentSong.time * speed));
bossBar.max(currentSong.length);
if (currentSong.paused) return; if (currentSong.paused) return;
@ -185,15 +181,18 @@ public class MusicPlayerPlugin extends Bot.Listener {
currentSong.play(); currentSong.play();
} }
public BossBar addBossBar () { public BotBossBar addBossBar () {
final BossBar bossBar = new BossBar( if (currentSong == null) return null;
final BotBossBar bossBar = new BotBossBar(
Component.empty(), Component.empty(),
SELECTOR,
BossBarColor.WHITE, BossBarColor.WHITE,
BossBarDivision.NONE,
true,
(int) currentSong.length,
0, 0,
"", bot
BossBarStyle.PROGRESS,
0,
false
); );
bot.bossbar().add(bossbarName, bossBar); bot.bossbar().add(bossbarName, bossBar);

View file

@ -1,13 +1,14 @@
package land.chipmunk.chayapak.chomens_bot.plugins; 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.ClientboundLoginPacket;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundSetTimePacket; import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundSetTimePacket;
import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.Session;
import com.github.steveice10.packetlib.packet.Packet; import com.github.steveice10.packetlib.packet.Packet;
import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Bot;
import land.chipmunk.chayapak.chomens_bot.data.BossBar; import land.chipmunk.chayapak.chomens_bot.data.BossBar;
import land.chipmunk.chayapak.chomens_bot.data.BossBarColor; import land.chipmunk.chayapak.chomens_bot.data.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.data.BossBarStyle;
import land.chipmunk.chayapak.chomens_bot.util.MathUtilities; import land.chipmunk.chayapak.chomens_bot.util.MathUtilities;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -49,15 +50,19 @@ public class TPSPlugin extends Bot.Listener {
public void on () { public void on () {
enabled = true; enabled = true;
bot.bossbar().add(bossbarName, new BossBar(
final BotBossBar bossBar = new BotBossBar(
Component.empty(), Component.empty(),
BossBarColor.WHITE, "@a",
getBossBarColor(getTickRate()),
BossBarDivision.NOTCHES_20,
true,
20,
0, 0,
"", bot
BossBarStyle.PROGRESS, );
0,
false bot.bossbar().add(bossbarName, bossBar);
));
} }
public void off () { public void off () {
@ -79,15 +84,13 @@ public class TPSPlugin extends Bot.Listener {
Component.text(formatter.format(tickRate)).color(getColor(tickRate)) Component.text(formatter.format(tickRate)).color(getColor(tickRate))
).color(NamedTextColor.DARK_GRAY); ).color(NamedTextColor.DARK_GRAY);
final BossBar bossBar = bot.bossbar().get(bossbarName); final BotBossBar bossBar = bot.bossbar().get(bossbarName);
bossBar.players("@a"); if (bossBar == null) return;
bossBar.name(component);
bossBar.color(getBossBarColor(tickRate)); bossBar.setTitle(component);
bossBar.visible(true); bossBar.setColor(getBossBarColor(tickRate));
bossBar.style(BossBarStyle.NOTCHED_20); bossBar.setValue((int) Math.round(tickRate));
bossBar.value((int) Math.round(tickRate));
bossBar.max(20);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -100,8 +103,8 @@ public class TPSPlugin extends Bot.Listener {
else return NamedTextColor.DARK_RED; else return NamedTextColor.DARK_RED;
} }
private String getBossBarColor (double tickRate) { private BossBarColor getBossBarColor (double tickRate) {
if (tickRate > 15) return BossBarColor.GREEN; if (tickRate > 15) return BossBarColor.LIME;
else if (tickRate == 15) return BossBarColor.YELLOW; else if (tickRate == 15) return BossBarColor.YELLOW;
else if (tickRate < 15 && tickRate > 10) return BossBarColor.RED; else if (tickRate < 15 && tickRate > 10) return BossBarColor.RED;
else return BossBarColor.PURPLE; 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.KeybindComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
@ -22,6 +23,11 @@ import java.util.regex.Pattern;
// totallynotskidded from chipmunkbot and added colors (ignore the ohio code please,..,.) // totallynotskidded from chipmunkbot and added colors (ignore the ohio code please,..,.)
public class ComponentUtilities { 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> language = loadJsonStringMap("language.json");
private static final Map<String, String> keybinds = loadJsonStringMap("keybinds.json"); private static final Map<String, String> keybinds = loadJsonStringMap("keybinds.json");