Skip to content

Update ViaVersion facet to VV 5.0.0 #170

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ default boolean isApplicable(final @NotNull V viewer) {
* @since 4.0.0
*/
interface Message<V, M> extends Facet<V> {
int PROTOCOL_HEX_COLOR = 713; // Added 20w17a
String PROTOCOL_HEX_COLOR = "1.16"; // Added 20w17a
int PROTOCOL_JSON = 5; // Added 14w02a

/**
Expand Down Expand Up @@ -242,7 +242,7 @@ interface ActionBar<V, M> extends Message<V, M> {
* @since 4.0.0
*/
interface Title<V, M, C, T> extends Message<V, M> {
int PROTOCOL_ACTION_BAR = 310; // Added 16w40a
String PROTOCOL_ACTION_BAR = "1.11"; // Added 16w40a
long MAX_SECONDS = Long.MAX_VALUE / 20;

/**
Expand Down Expand Up @@ -494,7 +494,7 @@ interface Book<V, M, B> extends Message<V, M> {
* @since 4.0.0
*/
interface BossBar<V> extends net.kyori.adventure.bossbar.BossBar.Listener, Closeable {
int PROTOCOL_BOSS_BAR = 356; // Added 18w05a
String PROTOCOL_BOSS_BAR = "1.13"; // Added 18w05a

/**
* A builder for boss bar facets.
Expand Down
2 changes: 1 addition & 1 deletion platform-viaversion/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies {
compileOnlyApi 'com.viaversion:viaversion-api:4.3.0'
compileOnlyApi 'com.viaversion:viaversion-api:5.0.0-SNAPSHOT'
implementation project(':adventure-platform-facet')
implementation("net.kyori:adventure-text-serializer-gson:${rootProject.adventure}") {
exclude group: "com.google.code.gson"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import com.viaversion.viaversion.api.protocol.Protocol;
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.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonParser;
import java.text.MessageFormat;
Expand Down Expand Up @@ -57,7 +58,7 @@
@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 int SUPPORTED_VIA_MAJOR_VERSION = 5;
private static final boolean SUPPORTED;

static {
Expand All @@ -73,46 +74,48 @@ public class ViaFacet<V> extends FacetBase<V> implements Facet.Message<V, String
}

private final Function<V, UserConnection> connectionFunction;
private final int minProtocol;
private final ProtocolVersion hexColorProtocol;
private final ProtocolVersion minProtocol;

public ViaFacet(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction, final int minProtocol) {
public ViaFacet(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction, final String minProtocol) {
super(viewerClass);
this.connectionFunction = connectionFunction;
this.minProtocol = minProtocol;
this.hexColorProtocol = ProtocolVersion.getClosest(PROTOCOL_HEX_COLOR);
this.minProtocol = ProtocolVersion.getClosest(minProtocol);
}

@Override
public boolean isSupported() {
return super.isSupported()
&& SUPPORTED
&& this.connectionFunction != null
&& this.minProtocol >= 0;
&& this.minProtocol.isKnown();
}

@Override
public boolean isApplicable(final @NotNull V viewer) {
return super.isApplicable(viewer)
&& this.minProtocol > Via.getAPI().getServerVersion().lowestSupportedVersion()
&& this.findProtocol(viewer) >= this.minProtocol;
&& this.minProtocol.newerThan(Via.getAPI().getServerVersion().lowestSupportedProtocolVersion())
&& this.findProtocol(viewer).newerThanOrEqualTo(this.minProtocol);
}

public @Nullable UserConnection findConnection(final @NotNull V viewer) {
return this.connectionFunction.apply(viewer);
}

public int findProtocol(final @NotNull V viewer) {
public ProtocolVersion findProtocol(final @NotNull V viewer) {
final UserConnection connection = this.findConnection(viewer);
if (connection != null) {
return connection.getProtocolInfo().getProtocolVersion();
return connection.getProtocolInfo().protocolVersion();
}
return -1;
return ProtocolVersion.unknown;
}

@NotNull
@Override
public String createMessage(final @NotNull V viewer, final @NotNull Component message) {
final int protocol = this.findProtocol(viewer);
if (protocol >= PROTOCOL_HEX_COLOR) {
final ProtocolVersion protocol = this.findProtocol(viewer);
if (protocol.newerThanOrEqualTo(this.hexColorProtocol)) {
return gson().serialize(message);
} else {
return colorDownsamplingGson().serialize(message);
Expand All @@ -125,11 +128,11 @@ public static class ProtocolBased<V> extends ViaFacet<V> {
private final int packetId;

@SuppressWarnings("unchecked")
protected ProtocolBased(final @NotNull String fromProtocol, final @NotNull String toProtocol, final int minProtocol, final @NotNull String packetName, final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
protected ProtocolBased(final @NotNull String fromProtocol, final @NotNull String toProtocol, final String minProtocol, final @NotNull String packetName, final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
super(viewerClass, connectionFunction, minProtocol);

final String protocolClassName = MessageFormat.format("{0}.protocols.protocol{1}to{2}.Protocol{1}To{2}", PACKAGE, fromProtocol, toProtocol);
final String packetClassName = MessageFormat.format("{0}.protocols.protocol{1}to{2}.ClientboundPackets{1}", PACKAGE, fromProtocol, toProtocol);
final String protocolClassName = MessageFormat.format("{0}.protocols.v{1}to{2}.packet.Protocol{1}To{2}", PACKAGE, fromProtocol, toProtocol);
final String packetClassName = MessageFormat.format("{0}.protocols.v{1}to{2}.packet.ClientboundPackets{1}", PACKAGE, fromProtocol, toProtocol);

Class<? extends Protocol<?, ?, ?, ?>> protocolClass = null;
Class<? extends ClientboundPacketType> packetClass = null;
Expand Down Expand Up @@ -180,15 +183,15 @@ public void sendPacket(final @NotNull PacketWrapper packet) {

public static class Chat<V> extends ProtocolBased<V> implements ChatPacket<V, String> {
public Chat(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
super("1_16", "1_15_2", PROTOCOL_HEX_COLOR, "CHAT_MESSAGE", viewerClass, connectionFunction);
super("1_15_2", "1_16", PROTOCOL_HEX_COLOR, "CHAT", viewerClass, connectionFunction);
}

@Override
public void sendMessage(final @NotNull V viewer, final @NotNull Identity source, final @NotNull String message, final @NotNull Object type) {
final PacketWrapper packet = this.createPacket(viewer);
packet.write(Type.COMPONENT, this.parse(message));
packet.write(Type.BYTE, this.createMessageType(type instanceof MessageType ? (MessageType) type : MessageType.SYSTEM));
packet.write(Type.UUID, source.uuid());
packet.write(Types.COMPONENT, this.parse(message));
packet.write(Types.BYTE, this.createMessageType(type instanceof MessageType ? (MessageType) type : MessageType.SYSTEM));
packet.write(Types.UUID, source.uuid());
this.sendPacket(packet);
}
}
Expand All @@ -211,25 +214,25 @@ public void sendMessage(final @NotNull V viewer, final @NotNull String message)

public static class ActionBarTitle<V> extends ProtocolBased<V> implements Facet.ActionBar<V, String> {
public ActionBarTitle(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
super("1_11", "1_10", TitlePacket.PROTOCOL_ACTION_BAR, "TITLE", viewerClass, connectionFunction);
super("1_10", "1_11", TitlePacket.PROTOCOL_ACTION_BAR, "SET_TITLES", viewerClass, connectionFunction);
}

@Override
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.COMPONENT, this.parse(message));
packet.write(Types.VAR_INT, TitlePacket.ACTION_ACTIONBAR);
packet.write(Types.COMPONENT, this.parse(message));
this.sendPacket(packet);
}
}

public static class Title<V> extends ProtocolBased<V> implements Facet.TitlePacket<V, String, List<Consumer<PacketWrapper>>, Consumer<V>> {
protected Title(final @NotNull String fromProtocol, final @NotNull String toProtocol, final int minProtocol, final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
super(fromProtocol, toProtocol, minProtocol, "TITLE", viewerClass, connectionFunction);
protected Title(final @NotNull String fromProtocol, final @NotNull String toProtocol, final String minProtocol, final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
super(fromProtocol, toProtocol, minProtocol, "SET_TITLES", viewerClass, connectionFunction);
}

public Title(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction) {
this("1_16", "1_15_2", PROTOCOL_HEX_COLOR, viewerClass, connectionFunction);
this("1_15_2", "1_16", PROTOCOL_HEX_COLOR, viewerClass, connectionFunction);
}

@Override
Expand All @@ -240,26 +243,26 @@ public Title(final @NotNull Class<? extends V> viewerClass, final @NotNull Funct
@Override
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.COMPONENT, this.parse(title));
packet.write(Types.VAR_INT, ACTION_TITLE);
packet.write(Types.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.COMPONENT, this.parse(subtitle));
packet.write(Types.VAR_INT, ACTION_SUBTITLE);
packet.write(Types.COMPONENT, this.parse(subtitle));
});
}

@Override
public void contributeTimes(final @NotNull List<Consumer<PacketWrapper>> coll, final int inTicks, final int stayTicks, final int outTicks) {
coll.add(packet -> {
packet.write(Type.VAR_INT, ACTION_TIMES);
packet.write(Type.INT, inTicks);
packet.write(Type.INT, stayTicks);
packet.write(Type.INT, outTicks);
packet.write(Types.VAR_INT, ACTION_TIMES);
packet.write(Types.INT, inTicks);
packet.write(Types.INT, stayTicks);
packet.write(Types.INT, outTicks);
});
}

Expand All @@ -282,14 +285,14 @@ public void showTitle(final @NotNull V viewer, final @NotNull Consumer<V> title)
@Override
public void clearTitle(final @NotNull V viewer) {
final PacketWrapper packet = this.createPacket(viewer);
packet.write(Type.VAR_INT, ACTION_CLEAR);
packet.write(Types.VAR_INT, ACTION_CLEAR);
this.sendPacket(packet);
}

@Override
public void resetTitle(final @NotNull V viewer) {
final PacketWrapper packet = this.createPacket(viewer);
packet.write(Type.VAR_INT, ACTION_RESET);
packet.write(Types.VAR_INT, ACTION_RESET);
this.sendPacket(packet);
}
}
Expand All @@ -304,7 +307,7 @@ public static final class BossBar<V> extends ProtocolBased<V> implements Facet.B
private byte flags;

private BossBar(final @NotNull String fromProtocol, final @NotNull String toProtocol, final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> connectionFunction, final Collection<V> viewers) {
super(fromProtocol, toProtocol, PROTOCOL_BOSS_BAR, "BOSSBAR", viewerClass, connectionFunction);
super(fromProtocol, toProtocol, PROTOCOL_BOSS_BAR, "BOSS_EVENT", viewerClass, connectionFunction);
this.viewers = new CopyOnWriteArraySet<>(viewers);
}

Expand All @@ -315,7 +318,7 @@ public Builder(final @NotNull Class<? extends V> viewerClass, final @NotNull Fun

@Override
public Facet.@NotNull BossBar<V> createBossBar(final @NotNull Collection<V> viewer) {
return new ViaFacet.BossBar<>("1_16", "1_15_2", this.viewerClass, this::findConnection, viewer);
return new ViaFacet.BossBar<>("1_15_2", "1_16", this.viewerClass, this::findConnection, viewer);
}
}

Expand All @@ -326,7 +329,7 @@ public Builder1_9_To_1_15(final @NotNull Class<? extends V> viewerClass, final @

@Override
public Facet.@NotNull BossBar<V> createBossBar(final @NotNull Collection<V> viewer) {
return new ViaFacet.BossBar<>("1_9", "1_8", this.viewerClass, this::findConnection, viewer);
return new ViaFacet.BossBar<>("1_8", "1_9", this.viewerClass, this::findConnection, viewer);
}
}

Expand Down Expand Up @@ -371,20 +374,20 @@ public void bossBarFlagsChanged(final net.kyori.adventure.bossbar.@NotNull BossB

public void sendPacket(final @NotNull V viewer, final int action) {
final PacketWrapper packet = this.createPacket(viewer);
packet.write(Type.UUID, this.id);
packet.write(Type.VAR_INT, action);
packet.write(Types.UUID, this.id);
packet.write(Types.VAR_INT, action);
if (action == ACTION_ADD || action == ACTION_TITLE) {
packet.write(Type.COMPONENT, this.parse(this.title));
packet.write(Types.COMPONENT, this.parse(this.title));
}
if (action == ACTION_ADD || action == ACTION_HEALTH) {
packet.write(Type.FLOAT, this.health);
packet.write(Types.FLOAT, this.health);
}
if (action == ACTION_ADD || action == ACTION_STYLE) {
packet.write(Type.VAR_INT, this.color);
packet.write(Type.VAR_INT, this.overlay);
packet.write(Types.VAR_INT, this.color);
packet.write(Types.VAR_INT, this.overlay);
}
if (action == ACTION_ADD || action == ACTION_FLAG) {
packet.write(Type.BYTE, this.flags);
packet.write(Types.BYTE, this.flags);
}
this.sendPacket(packet);
}
Expand Down Expand Up @@ -425,14 +428,14 @@ public void close() {
public static final class TabList<V> extends ProtocolBased<V> implements Facet.TabList<V, String> {

public TabList(final @NotNull Class<? extends V> viewerClass, final @NotNull Function<V, UserConnection> userConnection) {
super("1_16", "1_15_2", PROTOCOL_HEX_COLOR, "TAB_LIST", viewerClass, userConnection);
super("1_15_2", "1_16", PROTOCOL_HEX_COLOR, "TAB_LIST", viewerClass, userConnection);
}

@Override
public void send(final V viewer, final @Nullable String header, final @Nullable String footer) {
final PacketWrapper packet = this.createPacket(viewer);
packet.write(Type.COMPONENT, this.parse(header));
packet.write(Type.COMPONENT, this.parse(footer));
packet.write(Types.COMPONENT, this.parse(header));
packet.write(Types.COMPONENT, this.parse(footer));
this.sendPacket(packet);
}
}
Expand Down