Skip to content

Commit

Permalink
Fix version checker freezing the game when my server is being slow
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed May 13, 2015
1 parent fac6216 commit 9ab8391
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions build.properties
Original file line number Diff line number Diff line change
@@ -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
30 changes: 24 additions & 6 deletions src/main/java/evilcraft/VersionStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -65,16 +67,29 @@ 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() {

@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));
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9ab8391

Please sign in to comment.