diff --git a/platform-viaversion/build.gradle b/platform-viaversion/build.gradle index 098b6d56..d52e77a5 100644 --- a/platform-viaversion/build.gradle +++ b/platform-viaversion/build.gradle @@ -1,5 +1,5 @@ dependencies { - compileOnlyApi 'com.viaversion:viaversion-api:4.0.0-21w19a' + compileOnlyApi 'com.viaversion:viaversion-api:4.3.0' implementation project(':adventure-platform-facet') implementation("net.kyori:adventure-text-serializer-gson:${rootProject.adventure}") { exclude group: "com.google.code.gson" diff --git a/platform-viaversion/src/main/java/net/kyori/adventure/platform/viaversion/ViaFacet.java b/platform-viaversion/src/main/java/net/kyori/adventure/platform/viaversion/ViaFacet.java index 4234223a..219b7fcf 100644 --- a/platform-viaversion/src/main/java/net/kyori/adventure/platform/viaversion/ViaFacet.java +++ b/platform-viaversion/src/main/java/net/kyori/adventure/platform/viaversion/ViaFacet.java @@ -29,6 +29,8 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.libs.gson.JsonElement; +import com.viaversion.viaversion.libs.gson.JsonParser; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; @@ -55,14 +57,15 @@ @SuppressWarnings({"checkstyle:FilteringWriteTag", "checkstyle:MissingJavadocType", "checkstyle:MissingJavadocMethod"}) public class ViaFacet extends FacetBase implements Facet.Message { private static final String PACKAGE = "com.viaversion.viaversion"; + private static final int SUPPORTED_VIA_MAJOR_VERSION = 4; private static final boolean SUPPORTED; static { boolean supported = false; try { - // Check if the ViaVersion API is present - Class.forName(PACKAGE + ".api.ViaManager"); - supported = true; + // Check if the ViaVersion API is present and is a supported major version + Class.forName(PACKAGE + ".api.ViaAPI").getDeclaredMethod("majorVersion"); + supported = Via.getAPI().majorVersion() == SUPPORTED_VIA_MAJOR_VERSION; } catch (final Throwable error) { // ignore } @@ -164,11 +167,15 @@ public PacketWrapper createPacket(final @NotNull V viewer) { public void sendPacket(final @NotNull PacketWrapper packet) { if (packet.user() == null) return; try { - packet.send(this.protocolClass); + packet.scheduleSend(this.protocolClass); } catch (final Throwable error) { logError(error, "Failed to send ViaVersion packet: %s %s", packet.user(), packet); } } + + public @NotNull JsonElement parse(final @NotNull String message) { + return JsonParser.parseString(message); + } } public static class Chat extends ProtocolBased implements ChatPacket { @@ -179,7 +186,7 @@ public Chat(final @NotNull Class viewerClass, final @NotNull Functi @Override public void sendMessage(final @NotNull V viewer, final @NotNull Identity source, final @NotNull String message, final @NotNull MessageType type) { final PacketWrapper packet = this.createPacket(viewer); - packet.write(Type.STRING, message); + packet.write(Type.COMPONENT, this.parse(message)); packet.write(Type.BYTE, this.createMessageType(type)); packet.write(Type.UUID, source.uuid()); this.sendPacket(packet); @@ -211,7 +218,7 @@ public ActionBarTitle(final @NotNull Class viewerClass, final @NotN public void sendMessage(final @NotNull V viewer, final @NotNull String message) { final PacketWrapper packet = this.createPacket(viewer); packet.write(Type.VAR_INT, TitlePacket.ACTION_ACTIONBAR); - packet.write(Type.STRING, message); + packet.write(Type.COMPONENT, this.parse(message)); this.sendPacket(packet); } } @@ -234,7 +241,7 @@ public Title(final @NotNull Class viewerClass, final @NotNull Funct public void contributeTitle(final @NotNull List> coll, final @NotNull String title) { coll.add(packet -> { packet.write(Type.VAR_INT, ACTION_TITLE); - packet.write(Type.STRING, title); + packet.write(Type.COMPONENT, this.parse(title)); }); } @@ -242,7 +249,7 @@ public void contributeTitle(final @NotNull List> coll, f public void contributeSubtitle(final @NotNull List> coll, final @NotNull String subtitle) { coll.add(packet -> { packet.write(Type.VAR_INT, ACTION_SUBTITLE); - packet.write(Type.STRING, subtitle); + packet.write(Type.COMPONENT, this.parse(subtitle)); }); } @@ -367,7 +374,7 @@ public void sendPacket(final @NotNull V viewer, final int action) { packet.write(Type.UUID, this.id); packet.write(Type.VAR_INT, action); if (action == ACTION_ADD || action == ACTION_TITLE) { - packet.write(Type.STRING, this.title); + packet.write(Type.COMPONENT, this.parse(this.title)); } if (action == ACTION_ADD || action == ACTION_HEALTH) { packet.write(Type.FLOAT, this.health); @@ -424,8 +431,8 @@ public TabList(final @NotNull Class viewerClass, final @NotNull Fun @Override public void send(final V viewer, final @Nullable String header, final @Nullable String footer) { final PacketWrapper packet = this.createPacket(viewer); - packet.write(Type.STRING, header); - packet.write(Type.STRING, footer); + packet.write(Type.COMPONENT, this.parse(header)); + packet.write(Type.COMPONENT, this.parse(footer)); this.sendPacket(packet); } }