Skip to content

Commit aeaf68a

Browse files
committed
Merge branch '1.21.8' into 1.21.10
2 parents e6f6505 + 97e5a7e commit aeaf68a

9 files changed

Lines changed: 53 additions & 46 deletions

common/src/main/java/xaeroplus/XaeroPlus.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@
1010
import xaero.map.platform.Services;
1111
import xaeroplus.commands.XPClientCommandSource;
1212
import xaeroplus.commands.XPCommandManager;
13+
import xaeroplus.event.ClientStoppingEvent;
1314
import xaeroplus.event.MinimapInitCompletedEvent;
15+
import xaeroplus.feature.highlights.SavableHighlightCacheInstance;
1416
import xaeroplus.feature.keybind.KeybindListener;
1517
import xaeroplus.module.ModuleManager;
1618
import xaeroplus.module.impl.Drawing;
1719
import xaeroplus.settings.Settings;
1820
import xaeroplus.settings.XaeroPlusSetting;
1921

2022
import java.io.File;
23+
import java.util.ArrayList;
24+
import java.util.concurrent.CompletableFuture;
25+
import java.util.concurrent.TimeUnit;
2126
import java.util.concurrent.atomic.AtomicBoolean;
2227

2328
import static xaeroplus.settings.SettingHooks.loadXPSettings;
@@ -36,11 +41,25 @@ public static void initializeSettings() {
3641
Settings.REGISTRY.getAllSettings().forEach(XaeroPlusSetting::init);
3742
Globals.initStickySettings();
3843
ModuleManager.getModule(Drawing.class).enable();
39-
XaeroPlus.EVENT_BUS.registerConsumer((e) -> {
44+
XaeroPlus.EVENT_BUS.registerConsumer(o -> {
4045
if (Globals.minimapSettingsInitialized) return;
4146
Globals.minimapSettingsInitialized = true;
4247
}, MinimapInitCompletedEvent.class);
4348
XaeroPlus.EVENT_BUS.register(KEYBIND_LISTENER);
49+
XaeroPlus.EVENT_BUS.registerConsumer(o -> {
50+
try {
51+
var futures = new ArrayList<CompletableFuture<Void>>();
52+
SavableHighlightCacheInstance.instances().stream()
53+
.map(SavableHighlightCacheInstance::shutdown)
54+
.forEach(futures::add);
55+
futures.add(ModuleManager.getModule(Drawing.class).shutdown());
56+
CompletableFuture
57+
.allOf(futures.toArray(CompletableFuture[]::new))
58+
.get(5L, TimeUnit.SECONDS);
59+
} catch (Exception e) {
60+
LOGGER.warn("Timed out awaiting XaeroPlus cache shutdowns");
61+
}
62+
}, ClientStoppingEvent.class);
4463
}
4564

4665
public static void registerCommands(CommandDispatcher<XPClientCommandSource> dispatcher, CommandBuildContext context) {

common/src/main/java/xaeroplus/feature/drawing/DrawingCache.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -575,26 +575,23 @@ public void onDisable() {
575575
parentExecutor.execute(() -> {
576576
cacheReady.set(false);
577577
try {
578-
long before = System.currentTimeMillis();
579578
List<CompletableFuture<?>> tasks = new ArrayList<>();
580579
tasks.addAll(flushAllChunks());
581580
tasks.addAll(flushAllLines());
582581
tasks.addAll(flushAllTexts());
583582
var future = CompletableFuture.allOf(tasks.toArray(CompletableFuture[]::new));
584-
while (!future.isDone() && mc.isRunning() && TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - before) < 30) {
585-
Wait.wait(1);
586-
}
583+
Wait.waitUntil(() -> !mc.isRunning() || future.isDone(), 30);
587584
} catch (final Exception e) {
588585
XaeroPlus.LOGGER.error("Error saving all drawing data before disabling", e);
589586
}
590587
reset();
591588
});
592589
}
593590

594-
public void onShutdown() {
591+
public CompletableFuture<Void> onShutdown() {
595592
if (!mc.isSameThread()) {
596593
onDisable();
597-
return;
594+
return CompletableFuture.completedFuture(null);
598595
}
599596
cacheReady.set(false);
600597
for (var cache : getAllHighlightCaches()) {
@@ -606,7 +603,7 @@ public void onShutdown() {
606603
for (var cache : getAllTextsCaches()) {
607604
cache.writeStaleTextsToDatabase();
608605
}
609-
reset();
606+
return CompletableFuture.runAsync(this::closeAndAwaitTermination);
610607
}
611608

612609
public int getMinimapRegionWindowSize() {
@@ -709,7 +706,7 @@ public void close() throws IOException {
709706
parentExecutor.shutdown();
710707
}
711708

712-
public void closeAndAwaitTermination() throws IOException {
709+
public void closeAndAwaitTermination() {
713710
parentExecutor.execute(() -> {
714711
var dbExec = this.dbExecutor;
715712
if (dbExec != null && !dbExec.isShutdown()) {
@@ -724,7 +721,7 @@ public void closeAndAwaitTermination() throws IOException {
724721
throw new RuntimeException("Failed to shutdown drawing executor");
725722
}
726723
} catch (Exception e) {
727-
XaeroPlus.LOGGER.error("Error waiting for darwing db executor to shutdown", e);
724+
XaeroPlus.LOGGER.error("Error waiting for drawing db executor to shutdown", e);
728725
}
729726
}
730727
});

common/src/main/java/xaeroplus/feature/drawing/DrawingHighlightCacheDimensionHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,6 @@ public void onEnable() {}
245245
@Override
246246
public void onDisable() {}
247247

248-
@Override
249-
public void onShutdown() {}
250-
251248
private final class WindowDataLoadFutureCallback implements FutureCallback<Long2LongMap> {
252249
@Override
253250
public void onSuccess(Long2LongMap dataBuf) {

common/src/main/java/xaeroplus/feature/highlights/ChunkHighlightCache.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public interface ChunkHighlightCache {
4545
void handleTick();
4646
void onEnable();
4747
void onDisable();
48-
void onShutdown();
4948
default <V> CompletableFuture<V> submitTickTask(final Supplier<V> task) {
5049
return TickTaskExecutor.INSTANCE.submit(task);
5150
}

common/src/main/java/xaeroplus/feature/highlights/ChunkHighlightCacheDimensionHandler.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ public void onEnable() {}
270270
@Override
271271
public void onDisable() {}
272272

273-
@Override
274-
public void onShutdown() {}
275-
276273
private final class WindowDataLoadFutureCallback implements FutureCallback<Long2LongMap> {
277274
@Override
278275
public void onSuccess(Long2LongMap dataBuf) {

common/src/main/java/xaeroplus/feature/highlights/ChunkHighlightLocalCache.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,4 @@ public void onEnable() {}
6565

6666
@Override
6767
public void onDisable() {}
68-
69-
@Override
70-
public void onShutdown() {}
7168
}

common/src/main/java/xaeroplus/feature/highlights/ChunkHighlightSavingCache.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,30 +390,26 @@ public void onDisable() {
390390
parentExecutor.execute(() -> {
391391
cacheReady.set(false);
392392
try {
393-
long before = System.currentTimeMillis();
394393
var future = CompletableFuture.allOf(flushAllChunks().toArray(CompletableFuture[]::new));
395-
while (!future.isDone() && mc.isRunning() && TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - before) < 30) {
396-
Wait.wait(1);
397-
}
394+
Wait.waitUntil(() -> !mc.isRunning() || future.isDone(), 30);
398395
} catch (final Exception e) {
399396
XaeroPlus.LOGGER.error("Error saving all chunks before disabling", e);
400397
}
401398
reset();
402399
});
403400
}
404401

405-
@Override
406-
public void onShutdown() {
402+
public CompletableFuture<Void> onShutdown() {
407403
if (!mc.isSameThread()) {
408404
onDisable();
409-
return;
405+
return CompletableFuture.completedFuture(null);
410406
}
411407
cacheReady.set(false);
412408
for (var cache : getAllCaches()) {
413409
cache.flushStaleToRemoveChunks();
414410
cache.writeStaleHighlightsToDatabase();
415411
}
416-
reset();
412+
return CompletableFuture.runAsync(this::closeAndAwaitTermination);
417413
}
418414

419415
public int getMinimapRegionWindowSize() {
@@ -494,7 +490,7 @@ public void close() throws IOException {
494490
parentExecutor.shutdown();
495491
}
496492

497-
public void closeAndAwaitTermination() throws IOException {
493+
public void closeAndAwaitTermination() {
498494
parentExecutor.execute(() -> {
499495
var dbExec = dbExecutor;
500496
if (dbExec != null && !dbExec.isShutdown()) {

common/src/main/java/xaeroplus/feature/highlights/SavableHighlightCacheInstance.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package xaeroplus.feature.highlights;
22

33
import net.lenni0451.lambdaevents.EventHandler;
4-
import net.minecraft.client.Minecraft;
54
import xaeroplus.XaeroPlus;
6-
import xaeroplus.event.ClientStoppingEvent;
75
import xaeroplus.event.ClientTickEvent;
86
import xaeroplus.event.XaeroWorldChangeEvent;
97

8+
import java.util.Set;
9+
import java.util.concurrent.CompletableFuture;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
1012
public 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
}

common/src/main/java/xaeroplus/module/impl/Drawing.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import net.minecraft.world.level.Level;
1313
import xaeroplus.Globals;
1414
import xaeroplus.XaeroPlus;
15-
import xaeroplus.event.ClientStoppingEvent;
1615
import xaeroplus.event.ClientTickEvent;
1716
import xaeroplus.event.XaeroWorldChangeEvent;
1817
import xaeroplus.feature.drawing.DrawingCache;
@@ -29,6 +28,7 @@
2928
import java.util.Collections;
3029
import java.util.Deque;
3130
import java.util.List;
31+
import java.util.concurrent.CompletableFuture;
3232
import java.util.concurrent.LinkedBlockingDeque;
3333

3434
public class Drawing extends Module {
@@ -123,13 +123,12 @@ public void onWorldChange(final XaeroWorldChangeEvent event) {
123123
drawingCache.handleWorldChange(event);
124124
}
125125

126-
@EventHandler
127-
public void onClientStopping(ClientStoppingEvent event) {
126+
public CompletableFuture<Void> shutdown() {
128127
try {
129-
drawingCache.onShutdown();
130-
drawingCache.closeAndAwaitTermination();
128+
return drawingCache.onShutdown();
131129
} catch (Exception e) {
132130
XaeroPlus.LOGGER.error("Failed to close drawing cache", e);
131+
return CompletableFuture.completedFuture(null);
133132
}
134133
}
135134

0 commit comments

Comments
 (0)