From a3080800b00576a0f30fd95e39fdaaf116dd6914 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 10 Nov 2022 13:32:25 +0100 Subject: [PATCH] More minor refactoring --- .../bukkit/platform/BukkitViaAPI.java | 12 ++++++---- .../viaversion/bukkit/util/NMSUtil.java | 2 +- .../bukkit/util/ProtocolSupportUtil.java | 24 +++++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java index 50fbdcf36..b1251786c 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaAPI.java @@ -43,14 +43,17 @@ public class BukkitViaAPI extends ViaAPIBase { @Override public int getPlayerVersion(UUID uuid) { UserConnection connection = Via.getManager().getConnectionManager().getConnectedClient(uuid); - if (connection == null) { + if (connection != null) { + return connection.getProtocolInfo().getProtocolVersion(); + } + + if (isProtocolSupport()) { Player player = Bukkit.getPlayer(uuid); - if (player != null && isProtocolSupport()) { + if (player != null) { return ProtocolSupportUtil.getProtocolVersion(player); } - return -1; } - return connection.getProtocolInfo().getProtocolVersion(); + return -1; } @Override @@ -66,5 +69,4 @@ public class BukkitViaAPI extends ViaAPIBase { public boolean isProtocolSupport() { return plugin.isProtocolSupport(); } - } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java index 09689dc73..b1cb43faf 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/NMSUtil.java @@ -19,7 +19,7 @@ package com.viaversion.viaversion.bukkit.util; import org.bukkit.Bukkit; -public class NMSUtil { +public final class NMSUtil { private static final String BASE = Bukkit.getServer().getClass().getPackage().getName(); private static final String NMS = BASE.replace("org.bukkit.craftbukkit", "net.minecraft.server"); private static final boolean DEBUG_PROPERTY = loadDebugProperty(); diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java index c96db4522..c64a03fab 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/util/ProtocolSupportUtil.java @@ -22,27 +22,31 @@ import org.bukkit.entity.Player; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -public class ProtocolSupportUtil { - private static Method protocolVersionMethod = null; - private static Method getIdMethod = null; +public final class ProtocolSupportUtil { + private static final Method PROTOCOL_VERSION_METHOD; + private static final Method GET_ID_METHOD; static { + Method protocolVersionMethod = null; + Method getIdMethod = null; try { protocolVersionMethod = Class.forName("protocolsupport.api.ProtocolSupportAPI").getMethod("getProtocolVersion", Player.class); getIdMethod = Class.forName("protocolsupport.api.ProtocolVersion").getMethod("getId"); - } catch (Exception e) { + } catch (ReflectiveOperationException e) { // ProtocolSupport not installed. } + PROTOCOL_VERSION_METHOD = protocolVersionMethod; + GET_ID_METHOD = getIdMethod; } public static int getProtocolVersion(Player player) { - if (protocolVersionMethod == null) return -1; + if (PROTOCOL_VERSION_METHOD == null) { + return -1; + } try { - Object version = protocolVersionMethod.invoke(null, player); - return (int) getIdMethod.invoke(version); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { + Object version = PROTOCOL_VERSION_METHOD.invoke(null, player); + return (int) GET_ID_METHOD.invoke(version); + } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } return -1;