Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BungeeCord: BossBar compatibilty with BungeeCord 1.20-R0.2 and newer #153

Merged
merged 8 commits into from
May 7, 2024
2 changes: 1 addition & 1 deletion platform-bungeecord/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ dependencies {
}
implementation project(":adventure-platform-facet")
api project(":adventure-text-serializer-bungeecord")
compileOnly 'net.md-5:bungeecord-api:1.16-R0.4'
compileOnly 'net.md-5:bungeecord-api:1.20-R0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.platform.bungeecord;

import java.lang.invoke.MethodHandle;
import java.util.Collection;
import java.util.Set;
import java.util.UUID;
Expand All @@ -44,13 +45,17 @@
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.BaseComponent;
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;

import static net.kyori.adventure.platform.bungeecord.BungeeReflection.findMethod;
import static net.kyori.adventure.platform.bungeecord.BungeeReflection.hasMethod;
import static net.kyori.adventure.platform.facet.Knob.logError;
import static net.kyori.adventure.platform.facet.Knob.logUnsupported;

class BungeeFacet<V extends CommandSender> extends FacetBase<V> {
Expand Down Expand Up @@ -219,6 +224,18 @@ public void resetTitle(final @NotNull ProxiedPlayer viewer) {
}

static class BossBar extends Message implements BossBarPacket<ProxiedPlayer> {
private static MethodHandle SET_TITLE_STRING;
private static MethodHandle SET_TITLE_COMPONENT;

static {
Class<?> bossBarClass = net.md_5.bungee.protocol.packet.BossBar.class;
if (hasMethod(bossBarClass, "setTitle", String.class)) {
SET_TITLE_STRING = findMethod(bossBarClass, "setTitle", void.class, String.class);
} else {
SET_TITLE_COMPONENT = findMethod(bossBarClass, "setTitle", void.class, BaseComponent.class);
}
}

private final Set<ProxiedPlayer> viewers;
private final net.md_5.bungee.protocol.packet.BossBar bar;
private volatile boolean initialized = false;
Expand Down Expand Up @@ -255,7 +272,8 @@ 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(ComponentSerializer.toString(this.createMessage(this.viewers.iterator().next(), newName)));
BaseComponent[] message = this.createMessage(this.viewers.iterator().next(), newName);
this.updateBarTitle(message);
this.broadcastPacket(ACTION_TITLE);
}
}
Expand Down Expand Up @@ -318,6 +336,18 @@ private void broadcastPacket(final int action) {
}
}

private void updateBarTitle(BaseComponent[] message) {
try {
if (SET_TITLE_STRING != null) {
SET_TITLE_STRING.invoke(ComponentSerializer.toString(message));
} else {
SET_TITLE_COMPONENT.invoke(TextComponent.fromArray(message));
}
} catch (Throwable throwable) {
logError(throwable, "Cannot update the BossBar title");
}
}

private void sendPacket(final int action, final ProxiedPlayer... viewers) {
synchronized (this.bar) {
final int lastAction = this.bar.getAction();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.kyori.adventure.platform.bungeecord;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import org.jetbrains.annotations.Nullable;

/**
* Reflection utilities for accessing legacy BungeeCord methods
*/
public class BungeeReflection {
bivashy marked this conversation as resolved.
Show resolved Hide resolved

bivashy marked this conversation as resolved.
Show resolved Hide resolved
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
bivashy marked this conversation as resolved.
Show resolved Hide resolved

private BungeeReflection() {
}

/**
* Checks if the specified class has a method with the given name and parameter types.
*
* @param holderClass The class to check for the method. Can be {@code null}, in which case the method returns {@code false}.
* @param methodName The name of the method to check for.
* @param parameters The parameter types of the method. The method returns {@code false} if any parameter type is {@code null}.
* @return {@code true} if the method exists in the class; {@code false} otherwise.
*/
public static boolean hasMethod(final @Nullable Class<?> holderClass, final String methodName, final Class<?>... parameters) {
if (holderClass == null) return false;
for (Class<?> parameter : parameters) {
if (parameter == null) return false;
}
try {
holderClass.getMethod(methodName, parameters);
return true;
} catch (NoSuchMethodException ignored) {
}
return false;
}

/**
* Finds and returns a {@link MethodHandle} for the specified method of the given class. This allows for dynamic method invocation.
*
* @param holderClass The class containing the method.
* @param methodName The name of the method to find.
* @param returnType The return type of the method.
* @param parameters The parameter types of the method.
* @return A {@link MethodHandle} for the specified method, or {@code null} if the method cannot be found or if any parameter is {@code null}.
*/
public static MethodHandle findMethod(final @Nullable Class<?> holderClass, final String methodName, final Class<?> returnType, final Class<?>... parameters) {
if (holderClass == null || returnType == null) return null;
for (Class<?> parameter : parameters) {
if (parameter == null) return null;
}
try {
return LOOKUP.findVirtual(holderClass, methodName, MethodType.methodType(returnType, parameters));
} catch (NoSuchMethodException | IllegalAccessException ignored) {
}
return null;
}

}
7 changes: 7 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ dependencyResolutionManagement {
includeGroup "com.destroystokyo.paper"
}
}
maven {
name "minecraft-libraries"
url "https://libraries.minecraft.net"
mavenContent {
includeGroup "com.mojang"
}
}
}
}

Expand Down