Use custom interruption flag for JEI reload thread

Fixes #5
This commit is contained in:
embeddedt 2023-01-08 19:02:09 -05:00
parent cadb95ffb7
commit f4dfd428df
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
4 changed files with 27 additions and 6 deletions

View File

@ -18,7 +18,7 @@ plugins {
apply plugin: 'org.spongepowered.mixin' apply plugin: 'org.spongepowered.mixin'
group = 'org.embeddedt' group = 'org.embeddedt'
version = '1.4.1' version = '1.4.2'
java { java {
archivesBaseName = 'modernfix-mc' + minecraft_version archivesBaseName = 'modernfix-mc' + minecraft_version

View File

@ -2,7 +2,7 @@ package org.embeddedt.modernfix.jei.async;
public interface IAsyncJeiStarter { public interface IAsyncJeiStarter {
static void checkForLoadInterruption() { static void checkForLoadInterruption() {
if(Thread.currentThread().isInterrupted()) if(((JEIReloadThread)Thread.currentThread()).isStopRequested())
throw new JEILoadingInterruptedException(); throw new JEILoadingInterruptedException();
} }
} }

View File

@ -0,0 +1,18 @@
package org.embeddedt.modernfix.jei.async;
public class JEIReloadThread extends Thread {
private volatile boolean stopRequested;
public JEIReloadThread(Runnable runnable, String s) {
super(runnable, s);
this.stopRequested = false;
}
public void requestStop() {
stopRequested = true;
}
public boolean isStopRequested() {
return stopRequested;
}
}

View File

@ -15,6 +15,7 @@ import net.minecraft.client.Minecraft;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent; import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.jei.async.JEILoadingInterruptedException; import org.embeddedt.modernfix.jei.async.JEILoadingInterruptedException;
import org.embeddedt.modernfix.jei.async.JEIReloadThread;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Overwrite;
@ -38,7 +39,7 @@ public class ClientLifecycleHandlerMixin {
@Shadow(remap = false) @Final private IModIdHelper modIdHelper; @Shadow(remap = false) @Final private IModIdHelper modIdHelper;
@Shadow(remap = false) @Final private RecipeCategorySortingConfig recipeCategorySortingConfig; @Shadow(remap = false) @Final private RecipeCategorySortingConfig recipeCategorySortingConfig;
@Shadow(remap = false) @Final private IIngredientSorter ingredientSorter; @Shadow(remap = false) @Final private IIngredientSorter ingredientSorter;
private volatile Thread reloadThread = null; private volatile JEIReloadThread reloadThread = null;
@Inject(method = "setupJEI", at = @At(value = "INVOKE", target = "Lmezz/jei/startup/ClientLifecycleHandler;startJEI()V"), cancellable = true, remap = false) @Inject(method = "setupJEI", at = @At(value = "INVOKE", target = "Lmezz/jei/startup/ClientLifecycleHandler;startJEI()V"), cancellable = true, remap = false)
private void startAsync(CallbackInfo ci) { private void startAsync(CallbackInfo ci) {
ci.cancel(); ci.cancel();
@ -60,9 +61,9 @@ public class ClientLifecycleHandlerMixin {
} }
private void cancelPreviousStart() { private void cancelPreviousStart() {
Thread currentReloadThread = reloadThread; JEIReloadThread currentReloadThread = reloadThread;
if(currentReloadThread != null) { if(currentReloadThread != null) {
currentReloadThread.interrupt(); currentReloadThread.requestStop();
Minecraft.getInstance().managedBlock(currentReloadThread::isAlive); Minecraft.getInstance().managedBlock(currentReloadThread::isAlive);
reloadThread = null; reloadThread = null;
} }
@ -71,8 +72,10 @@ public class ClientLifecycleHandlerMixin {
private static int numReloads = 1; private static int numReloads = 1;
private void startJEIAsync(Runnable whenFinishedCb) { private void startJEIAsync(Runnable whenFinishedCb) {
ModernFix.LOGGER.info("JEI restart triggered. Waiting for previous thread to die.");
cancelPreviousStart(); cancelPreviousStart();
Thread newThread = new Thread(() -> { ModernFix.LOGGER.info("Starting new JEI thread.");
JEIReloadThread newThread = new JEIReloadThread(() -> {
try { try {
starter.start( starter.start(
plugins, plugins,