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

Fix ViaFacet component type #95

Merged
merged 3 commits into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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"
Expand Down
Expand Up @@ -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;
Expand All @@ -55,14 +57,15 @@
@SuppressWarnings({"checkstyle:FilteringWriteTag", "checkstyle:MissingJavadocType", "checkstyle:MissingJavadocMethod"})
public class ViaFacet<V> extends FacetBase<V> implements Facet.Message<V, String> {
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
}
Expand Down Expand Up @@ -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<V> extends ProtocolBased<V> implements ChatPacket<V, String> {
Expand All @@ -179,7 +186,7 @@ public Chat(final @NotNull Class<? extends V> 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);
Expand Down Expand Up @@ -211,7 +218,7 @@ public ActionBarTitle(final @NotNull Class<? extends V> 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);
}
}
Expand All @@ -234,15 +241,15 @@ public Title(final @NotNull Class<? extends V> viewerClass, final @NotNull Funct
public void contributeTitle(final @NotNull List<Consumer<PacketWrapper>> 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));
});
}

@Override
public void contributeSubtitle(final @NotNull List<Consumer<PacketWrapper>> 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));
});
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -424,8 +431,8 @@ public TabList(final @NotNull Class<? extends V> 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);
}
}
Expand Down