Skip to content

Commit

Permalink
Ensure backward compatibility when updating bossbar title
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Feb 18, 2024
1 parent d36cd91 commit 7f4dc01
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package net.kyori.adventure.platform.bungeecord;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Set;
import java.util.UUID;
Expand All @@ -47,6 +49,7 @@
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.Connection;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.chat.ComponentSerializer;
import net.md_5.bungee.chat.TranslationRegistry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -255,11 +258,28 @@ public void bossBarInitialized(final net.kyori.adventure.bossbar.@NotNull BossBa
@Override
public void bossBarNameChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final @NotNull Component oldName, final @NotNull Component newName) {
if (!this.viewers.isEmpty()) {
this.bar.setTitle(TextComponent.fromArray(this.createMessage(this.viewers.iterator().next(), newName)));
BaseComponent[] message = this.createMessage(this.viewers.iterator().next(), newName);
updateBarTitle(message);
this.broadcastPacket(ACTION_TITLE);
}
}

private void updateBarTitle(BaseComponent[] message) {
if (!tryUpdateTitleWithMethod("setTitle", String.class, ComponentSerializer.toString(message))) {
tryUpdateTitleWithMethod("setTitle", BaseComponent.class, TextComponent.fromArray(message));
}
}

private boolean tryUpdateTitleWithMethod(String methodName, Class<?> parameterType, Object argument) {
try {
Method setTitleMethod = net.md_5.bungee.protocol.packet.BossBar.class.getMethod(methodName, parameterType);
setTitleMethod.invoke(this.bar, argument);
return true;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
return false;
}
}

@Override
public void bossBarProgressChanged(final net.kyori.adventure.bossbar.@NotNull BossBar bar, final float oldPercent, final float newPercent) {
this.bar.setHealth(newPercent);
Expand Down

0 comments on commit 7f4dc01

Please sign in to comment.