11package xaeroplus .feature .highlights ;
22
33import net .lenni0451 .lambdaevents .EventHandler ;
4- import net .minecraft .client .Minecraft ;
54import xaeroplus .XaeroPlus ;
6- import xaeroplus .event .ClientStoppingEvent ;
75import xaeroplus .event .ClientTickEvent ;
86import xaeroplus .event .XaeroWorldChangeEvent ;
97
8+ import java .util .Set ;
9+ import java .util .concurrent .CompletableFuture ;
10+ import java .util .concurrent .ConcurrentHashMap ;
11+
1012public class SavableHighlightCacheInstance {
13+ private static final Set <SavableHighlightCacheInstance > INSTANCES = ConcurrentHashMap .newKeySet ();
1114 private ChunkHighlightCache cache ;
1215 private final String dbName ;
1316
@@ -18,6 +21,10 @@ public SavableHighlightCacheInstance(String dbName) {
1821 this .cache = new ChunkHighlightLocalCache ();
1922 }
2023
24+ public static Set <SavableHighlightCacheInstance > instances () {
25+ return INSTANCES ;
26+ }
27+
2128 public ChunkHighlightCache get () {
2229 return cache ;
2330 }
@@ -29,11 +36,13 @@ public ChunkHighlightCache get() {
2936 public synchronized void onEnable () {
3037 XaeroPlus .EVENT_BUS .register (this );
3138 cache .onEnable ();
39+ INSTANCES .add (this );
3240 }
3341
3442 public synchronized void onDisable () {
3543 cache .onDisable ();
3644 XaeroPlus .EVENT_BUS .unregister (this );
45+ INSTANCES .remove (this );
3746 }
3847
3948 public synchronized void setDiskCache (final boolean disk , final boolean enabled ) {
@@ -57,7 +66,6 @@ public synchronized void setDiskCache(final boolean disk, final boolean enabled)
5766
5867 @ EventHandler
5968 public void onXaeroWorldChange (XaeroWorldChangeEvent event ) {
60- if (!Minecraft .getInstance ().isRunning ()) return ;
6169 try {
6270 cache .handleWorldChange (event );
6371 } catch (final Exception e ) {
@@ -74,15 +82,13 @@ public void onClientTickEvent(ClientTickEvent.Post event) {
7482 }
7583 }
7684
77- @ EventHandler
78- public void onClientStopping (ClientStoppingEvent event ) {
79- try {
80- cache .onShutdown ();
81- if (cache instanceof ChunkHighlightSavingCache savingCache ) {
82- savingCache .closeAndAwaitTermination ();
83- }
84- } catch (final Exception e ) {
85- XaeroPlus .LOGGER .error ("Error closing {}" , dbName , e );
85+ public CompletableFuture <Void > shutdown () {
86+ if (!INSTANCES .contains (this )) return CompletableFuture .completedFuture (null );
87+ XaeroPlus .EVENT_BUS .unregister (this );
88+ INSTANCES .remove (this );
89+ if (cache instanceof ChunkHighlightSavingCache savingCache ) {
90+ return savingCache .onShutdown ();
8691 }
92+ return CompletableFuture .completedFuture (null );
8793 }
8894}
0 commit comments