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 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,49 @@
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:",
"\ttrigger:",
"\t\tsend \"Protocol version of %arg-1%: %protocol version of arg-1%\""})
@Since("2.6.2")
@Since("2.6.2, INSERT VERSION ViaVersion support")
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
@RequiredPlugins("Paper 1.12.2 or newer")
public class ExprPlayerProtocolVersion extends SimplePropertyExpression<Player, Integer> {

private static final boolean VIAVERSION_EXISTS = Skript.classExists("com.viaversion.viaversion.api.ViaAPI");
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
@Nullable
private static Object VIA_API;

static {
if (Skript.classExists("com.destroystokyo.paper.network.NetworkClient")) {
if (Skript.classExists("com.destroystokyo.paper.network.NetworkClient") || VIAVERSION_EXISTS) {
register(ExprPlayerProtocolVersion.class, Integer.class, "protocol version", "players");
}

if (VIAVERSION_EXISTS) {
try {
VIA_API = Class.forName("com.viaversion.viaversion.api.Via")
.getDeclaredMethod("getAPI")
.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {}
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
@Nullable
public Integer convert(Player player) {
int version = player.getProtocolVersion();
int version = -1;
if (VIAVERSION_EXISTS && VIA_API != null) {
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
try {
Method getPlayerVersion = VIA_API.getClass().getDeclaredMethod("getPlayerVersion", UUID.class);
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
version = (int) getPlayerVersion.invoke(VIA_API, player.getUniqueId());
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ignored) {}
erenkarakal marked this conversation as resolved.
Show resolved Hide resolved
} else {
version = player.getProtocolVersion();
}
return version == -1 ? null : version;
}

Expand Down