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

Add ViaVersion Support #6616

Open
wants to merge 4 commits into
base: dev/feature
Choose a base branch
from
Open
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
Expand Up @@ -24,6 +24,10 @@
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

@Name("Player Protocol Version")
@Description("Player's protocol version. For more information and list of protocol versions <a href='https://wiki.vg/Protocol_version_numbers'>visit wiki.vg</a>.")
@Examples({"command /protocolversion &ltplayer&gt:",
Expand All @@ -33,16 +37,43 @@
@RequiredPlugins("Paper 1.12.2 or newer")
public class ExprPlayerProtocolVersion extends SimplePropertyExpression<Player, Integer> {

@Nullable
private static Object VIA_API;
@Nullable
private static Method VIA_GET_PLAYER_VERSION;

static {
if (Skript.classExists("com.destroystokyo.paper.network.NetworkClient")) {
boolean viaVersionExists = Skript.classExists("com.viaversion.viaversion.api.ViaAPI");
if (Skript.classExists("com.destroystokyo.paper.network.NetworkClient") || viaVersionExists) {
register(ExprPlayerProtocolVersion.class, Integer.class, "protocol version", "players");
}

if (viaVersionExists) {
try {
VIA_API = Class.forName("com.viaversion.viaversion.api.Via")
.getDeclaredMethod("getAPI")
.invoke(null);
assert VIA_API != null;
VIA_GET_PLAYER_VERSION = VIA_API.getClass().getDeclaredMethod("getPlayerVersion", UUID.class);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
assert false: e.getMessage();
}
}
}

@Override
@Nullable
public Integer convert(Player player) {
int version = player.getProtocolVersion();
int version = -1;
if (VIA_API != null && VIA_GET_PLAYER_VERSION != null) {
try {
version = (int) VIA_GET_PLAYER_VERSION.invoke(VIA_API, player.getUniqueId());
} catch (IllegalAccessException | InvocationTargetException e) {
assert false: e.getMessage();
}
} else {
version = player.getProtocolVersion();
}
return version == -1 ? null : version;
}

Expand Down