some fixes (but not all)

we still have the title self care problem which is prob the most noticeable
This commit is contained in:
Chayapak 2023-06-22 18:18:45 +07:00
parent 69713502e4
commit 0bf87c008a

View file

@ -7,6 +7,7 @@ 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.data.BotBossBar;
import land.chipmunk.chayapak.chomens_bot.data.chat.MutablePlayerListEntry;
import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -32,6 +33,13 @@ public class BossbarManagerPlugin extends Bot.Listener {
this.bot = bot; this.bot = bot;
bot.addListener(this); bot.addListener(this);
bot.players().addListener(new PlayersPlugin.Listener() {
@Override
public void playerJoined(MutablePlayerListEntry target) {
BossbarManagerPlugin.this.playerJoined();
}
});
} }
@Override @Override
@ -40,98 +48,110 @@ public class BossbarManagerPlugin extends Bot.Listener {
} }
public void packetReceived(ClientboundBossEventPacket packet) { public void packetReceived(ClientboundBossEventPacket packet) {
switch (packet.getAction()) { try {
case ADD -> { switch (packet.getAction()) {
final Map<UUID, BotBossBar> mapCopy = new HashMap<>(bossBars); case ADD -> {
final Map<UUID, BotBossBar> mapCopy = new HashMap<>(bossBars);
for (Map.Entry<UUID, BotBossBar> _bossBar : mapCopy.entrySet()) { for (Map.Entry<UUID, BotBossBar> _bossBar : mapCopy.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue(); final BotBossBar bossBar = _bossBar.getValue();
if (ComponentUtilities.isEqual(bossBar.secret, packet.getTitle())) { if (ComponentUtilities.isEqual(bossBar.secret, packet.getTitle())) {
bossBars.remove(_bossBar.getKey()); bossBars.remove(_bossBar.getKey());
bossBars.put( bossBars.put(
packet.getUuid(), packet.getUuid(),
new BotBossBar( new BotBossBar(
bossBar.title, bossBar.title,
bossBar.players, bossBar.players,
bossBar.color, bossBar.color,
bossBar.division, bossBar.division,
bossBar.visible, bossBar.visible,
bossBar.max, bossBar.max,
bossBar.value, bossBar.value,
bot bot
) )
); );
bossBars.get(packet.getUuid()).id = bossBar.id; bossBars.get(packet.getUuid()).id = bossBar.id;
bossBars.get(packet.getUuid()).uuid = packet.getUuid(); bossBars.get(packet.getUuid()).uuid = packet.getUuid();
}
} }
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();
serverBossBars.put( if (bossBar.uuid.equals(packet.getUuid())) {
packet.getUuid(), addBossBar(bossBar.id, bossBar);
new BossBar( break;
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 (bossBar.uuid.equals(packet.getUuid())) {
addBossBar(bossBar.id, bossBar);
break;
} }
serverBossBars.remove(packet.getUuid());
} }
case UPDATE_STYLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
serverBossBars.remove(packet.getUuid()); final BotBossBar botBossBar = get(bossBar.uuid);
}
case UPDATE_STYLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
final BotBossBar botBossBar = get(bossBar.uuid); if (botBossBar != null && botBossBar.color != packet.getColor()) {
botBossBar.setColor(botBossBar.color, true);
} else if (botBossBar != null && botBossBar.division != packet.getDivision()) {
botBossBar.setDivision(botBossBar.division, true);
}
if (botBossBar != null && botBossBar.color != packet.getColor()) { bossBar.color = packet.getColor();
botBossBar.setColor(botBossBar.color, true); bossBar.division = packet.getDivision();
} else if (botBossBar != null && botBossBar.division != packet.getDivision()) {
botBossBar.setDivision(botBossBar.division, true);
} }
case UPDATE_TITLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
bossBar.color = packet.getColor(); final BotBossBar botBossBar = get(bossBar.uuid);
bossBar.division = packet.getDivision();
}
case UPDATE_TITLE -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
final BotBossBar botBossBar = get(bossBar.uuid); if (botBossBar != null && !ComponentUtilities.isEqual(botBossBar.title, packet.getTitle())) {
botBossBar.setTitle(bossBar.title, true);
}
if (botBossBar != null && !ComponentUtilities.isEqual(botBossBar.title, packet.getTitle())) { bossBar.title = packet.getTitle();
botBossBar.setTitle(bossBar.title, true);
} }
case UPDATE_HEALTH -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
bossBar.title = packet.getTitle(); final BotBossBar botBossBar = get(bossBar.uuid);
}
case UPDATE_HEALTH -> {
final BossBar bossBar = serverBossBars.get(packet.getUuid());
final BotBossBar botBossBar = get(bossBar.uuid); if (
botBossBar != null &&
botBossBar.value != packet.getHealth() * botBossBar.max
) {
botBossBar.setValue(botBossBar.value, true);
botBossBar.setMax(botBossBar.max, true);
}
if ( bossBar.health = packet.getHealth();
botBossBar != null &&
botBossBar.value != packet.getHealth() * botBossBar.max
) {
botBossBar.setValue(botBossBar.value, true);
botBossBar.setMax(botBossBar.max, true);
} }
bossBar.health = packet.getHealth();
} }
} catch (Exception e) {
e.printStackTrace();
}
}
private void playerJoined () {
for (Map.Entry<UUID, BotBossBar> _bossBar : bossBars.entrySet()) {
final BotBossBar bossBar = _bossBar.getValue();
bossBar.setPlayers(bossBar.players);
} }
} }