diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java index d00d2213d..3f00ee468 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java @@ -102,8 +102,6 @@ public class VelocityServerHandler { VelocityStorage storage = user.get(VelocityStorage.class); - if (storage.getBossbar() == null) storage.saveServerBossBars(); - if (e.getServer() != null) { if (!e.getServer().getServerInfo().getName().equals(storage.getCurrentServer())) { String serverName = e.getServer().getServerInfo().getName(); diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/storage/VelocityStorage.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/storage/VelocityStorage.java index 27b5b6d3e..51dd9191c 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/storage/VelocityStorage.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/storage/VelocityStorage.java @@ -8,6 +8,7 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.List; import java.util.UUID; @@ -16,7 +17,22 @@ import java.util.UUID; public class VelocityStorage extends StoredObject { private Player player; private String currentServer; - private List bossbar; + private List cachedBossbar; + private static Method getServerBossBars; + private static Class clientPlaySessionHandler; + private static Method getMinecraftConnection; + + static { + try { + clientPlaySessionHandler = Class.forName("com.velocitypowered.proxy.connection.client.ClientPlaySessionHandler"); + getServerBossBars = clientPlaySessionHandler + .getDeclaredMethod("getServerBossBars"); + getMinecraftConnection = Class.forName("com.velocitypowered.proxy.connection.client.ConnectedPlayer") + .getDeclaredMethod("getMinecraftConnection"); + } catch (NoSuchMethodException | ClassNotFoundException e) { + e.printStackTrace(); + } + } public VelocityStorage(UserConnection user, Player player) { super(user); @@ -24,16 +40,22 @@ public class VelocityStorage extends StoredObject { this.currentServer = ""; } - public void saveServerBossBars() { - // Get bossbar list if it's supported - try { - Object connection = ReflectionUtil.invoke(player, "getMinecraftConnection"); - Object sessionHandler = ReflectionUtil.invoke(connection, "getSessionHandler"); - if (sessionHandler.getClass().getSimpleName().contains("Play")) { - bossbar = (List) ReflectionUtil.invoke(sessionHandler, "getServerBossBars"); + public List getBossbar() { + if (cachedBossbar == null) { + if (clientPlaySessionHandler == null) return null; + if (getServerBossBars == null) return null; + if (getMinecraftConnection == null) return null; + // Get bossbar list if it's supported + try { + Object connection = getMinecraftConnection.invoke(player); + Object sessionHandler = ReflectionUtil.invoke(connection, "getSessionHandler"); + if (clientPlaySessionHandler.isInstance(sessionHandler)) { + cachedBossbar = (List) getServerBossBars.invoke(sessionHandler); + } + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); } - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { - e.printStackTrace(); } + return cachedBossbar; } }