From 9ab8391357470a96e392cfa7cc917c12ed38dde8 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Wed, 13 May 2015 14:45:35 +0200 Subject: [PATCH] Fix version checker freezing the game when my server is being slow --- build.properties | 8 +++--- src/main/java/evilcraft/VersionStats.java | 30 ++++++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/build.properties b/build.properties index aa5f3cf6bf..f7da45de4d 100644 --- a/build.properties +++ b/build.properties @@ -1,12 +1,12 @@ mod_version=0.8.5 minecraft_version=1.7.10 -forge_version=10.13.3.1360-1.7.10 +forge_version=10.13.3.1403-1.7.10 tconstruct_version=1.8.3.build927 waila_version=1.5.10_1.7.10 FMP_version=1.1.2.331 forestry_version=3.5.3.11 -NEI_version=1.0.4.95 -CCC_version=1.0.4.35 -CCL_version=1.1.3.127 +NEI_version=1.0.4.107 +CCC_version=1.0.6.43 +CCL_version=1.1.3.136 BM_version=1.3.1-7 diff --git a/src/main/java/evilcraft/VersionStats.java b/src/main/java/evilcraft/VersionStats.java index b9738678d3..de649a58ff 100644 --- a/src/main/java/evilcraft/VersionStats.java +++ b/src/main/java/evilcraft/VersionStats.java @@ -19,7 +19,9 @@ * */ public class VersionStats { - + + private static final Object LOCK = new Object(); + private static VersionStats VERSION_STATS = null; private static boolean CHECKED = false; @@ -48,7 +50,7 @@ public static String getVersion() { /** * Fetch the latest version. Make sure this method is only loaded once! */ - public static synchronized void load() { + public static void load() { new Thread(new Runnable() { @Override @@ -65,7 +67,7 @@ public void run() { * Check the latest version. * @param event The tick event. */ - public static synchronized void check(final PlayerTickEvent event) { + public static void check(final PlayerTickEvent event) { if(!CHECKED) { CHECKED = true; new Thread(new Runnable() { @@ -73,8 +75,21 @@ public static synchronized void check(final PlayerTickEvent event) { @Override public void run() { EntityPlayer player = event.player; - - VersionStats versionStats = getVersionStats(); + + VersionStats versionStats; + synchronized(LOCK) { + versionStats = VERSION_STATS; + } + while(versionStats == null) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + + } + synchronized(LOCK) { + versionStats = VERSION_STATS; + } + } if(GeneralConfig.versionChecker && needsUpdate(versionStats)) { sendMessage(player, L10NHelpers.localize("general.versionUpdate", versionStats.mod_version, Reference.MOD_NAME, Reference.MOD_VERSION, versionStats.update_link)); } @@ -113,7 +128,10 @@ private static VersionStats fetchVersionStats() { private static synchronized VersionStats getVersionStats() { if(VERSION_STATS == null) { - VERSION_STATS = fetchVersionStats(); + VersionStats temp = fetchVersionStats(); + synchronized(LOCK) { + VERSION_STATS = temp; + } } return VERSION_STATS; }