1919
2020import java .util .ArrayList ;
2121import java .util .List ;
22+ import java .util .concurrent .Callable ;
2223import java .util .concurrent .Future ;
24+ import java .util .concurrent .TimeUnit ;
2325
2426@ SideOnly (Side .CLIENT )
2527public class ClientProxy extends CommonProxy {
@@ -34,33 +36,46 @@ public void preInit(FMLPreInitializationEvent e) {
3436 @ Override
3537 public void postInit (FMLPostInitializationEvent e ) {
3638 super .postInit (e );
37- chatFuture = Async .asyncWorker .submit (() -> {
38- val updates = updatesFuture .get ();
39- if (updates == null || updates .size () == 0 ) return null ;
40- val updateText = new ArrayList <IChatComponent >(FormattedText .parse (I18n .format ("falsepatternlib.chat.updatesavailable" )).toChatText ());
41- val mods = Loader .instance ().getIndexedModList ();
42- for (val update : updates ) {
43- val mod = mods .get (update .modID );
44- updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.modname" , mod .getName ())).toChatText ());
45- updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.currentversion" , update .currentVersion )).toChatText ());
46- updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.latestversion" , update .latestVersion )).toChatText ());
47- if (!update .updateURL .isEmpty ()) {
48- val pre = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurlpre" )).toChatText ();
49- val link = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurl" )).toChatText ();
50- val post = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurlpost" )).toChatText ();
51- pre .get (pre .size () - 1 ).appendSibling (link .get (0 ));
52- link .get (link .size () - 1 ).appendSibling (post .get (0 ));
53- for (val l : link ) {
54- l .getChatStyle ().setChatClickEvent (new ClickEvent (ClickEvent .Action .OPEN_URL , update .updateURL ));
39+ chatFuture = Async .asyncWorker .submit (new Callable <List <IChatComponent >>() {
40+ @ Override
41+ public List <IChatComponent > call () throws Exception {
42+ //Deadlock avoidance
43+ if (updatesFuture == null || updatesFuture .isCancelled ()) {
44+ chatFuture = null ;
45+ return null ;
46+ }
47+ if (!updatesFuture .isDone ()) {
48+ chatFuture = Async .asyncWorker .submit (this );
49+ return null ;
50+ }
51+ val updates = updatesFuture .get ();
52+ if (updates == null || updates .size () == 0 )
53+ return null ;
54+ val updateText = new ArrayList <IChatComponent >(FormattedText .parse (I18n .format ("falsepatternlib.chat.updatesavailable" )).toChatText ());
55+ val mods = Loader .instance ().getIndexedModList ();
56+ for (val update : updates ) {
57+ val mod = mods .get (update .modID );
58+ updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.modname" , mod .getName ())).toChatText ());
59+ updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.currentversion" , update .currentVersion )).toChatText ());
60+ updateText .addAll (FormattedText .parse (I18n .format ("falsepatternlib.chat.latestversion" , update .latestVersion )).toChatText ());
61+ if (!update .updateURL .isEmpty ()) {
62+ val pre = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurlpre" )).toChatText ();
63+ val link = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurl" )).toChatText ();
64+ val post = FormattedText .parse (I18n .format ("falsepatternlib.chat.updateurlpost" )).toChatText ();
65+ pre .get (pre .size () - 1 ).appendSibling (link .get (0 ));
66+ link .get (link .size () - 1 ).appendSibling (post .get (0 ));
67+ for (val l : link ) {
68+ l .getChatStyle ().setChatClickEvent (new ClickEvent (ClickEvent .Action .OPEN_URL , update .updateURL ));
69+ }
70+ link .remove (0 );
71+ post .remove (0 );
72+ updateText .addAll (pre );
73+ updateText .addAll (link );
74+ updateText .addAll (post );
5575 }
56- link .remove (0 );
57- post .remove (0 );
58- updateText .addAll (pre );
59- updateText .addAll (link );
60- updateText .addAll (post );
6176 }
77+ return updateText ;
6278 }
63- return updateText ;
6479 });
6580 }
6681
@@ -70,7 +85,7 @@ public void onSinglePlayer(EntityJoinWorldEvent e) {
7085 !(e .entity instanceof EntityPlayerSP )) return ;
7186 val player = (EntityPlayerSP ) e .entity ;
7287 try {
73- for (val line : chatFuture .get ()) {
88+ for (val line : chatFuture .get (1 , TimeUnit . SECONDS )) {
7489 player .addChatMessage (line );
7590 }
7691 chatFuture = null ;
0 commit comments