mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-25 01:21:20 +01:00
Extensions will be unloaded when server is stopped
This commit is contained in:
parent
a63e9462c2
commit
2a96beb819
@ -784,6 +784,7 @@ public final class MinecraftServer {
|
||||
public static void stopCleanly() {
|
||||
stopping = true;
|
||||
LOGGER.info("Stopping Minestom server.");
|
||||
extensionManager.unloadAllExtensions();
|
||||
updateManager.stop();
|
||||
schedulerManager.shutdown();
|
||||
connectionManager.shutdown();
|
||||
|
@ -24,7 +24,7 @@ public abstract class Extension {
|
||||
|
||||
/**
|
||||
* Observers that will be notified of events related to this extension.
|
||||
* Kept as WeakReference because entities can be observer, but could become candidate to be garbage-collected while
|
||||
* Kept as WeakReference because entities can be observers, but could become candidate to be garbage-collected while
|
||||
* this extension holds a reference to it. A WeakReference makes sure this extension does not prevent the memory
|
||||
* from being cleaned up.
|
||||
*/
|
||||
|
@ -710,4 +710,17 @@ public class ExtensionManager {
|
||||
}
|
||||
LOGGER.info("Early load of code modifiers from extensions done!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Unloads all extensions
|
||||
*/
|
||||
public void unloadAllExtensions() {
|
||||
// copy names, as the extensions map will be modified via the calls to unload
|
||||
Set<String> extensionNames = new HashSet<>(extensions.keySet());
|
||||
for(String ext : extensionNames) {
|
||||
if(extensions.containsKey(ext)) { // is still loaded? Because extensions can depend on one another, it might have already been unloaded
|
||||
unloadExtension(ext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,9 @@ import net.minestom.server.Bootstrap;
|
||||
// -Dminestom.extension.indevfolder.classes=build/classes/java/test/ -Dminestom.extension.indevfolder.resources=build/resources/test/improveextensions
|
||||
// To test disabling early Mixin injections:
|
||||
// -Dminestom.extension.disable_early_load=true -Dminestom.extension.indevfolder.classes=build/classes/java/test/ -Dminestom.extension.indevfolder.resources=build/resources/test/improveextensions/disableearlyload
|
||||
|
||||
// To test extension termination when the server quits:
|
||||
// -Dminestom.extension.indevfolder.classes=build/classes/java/test/ -Dminestom.extension.indevfolder.resources=build/resources/test/improveextensions/unloadonstop
|
||||
public class MixinIntoMinestomCoreLauncher {
|
||||
public static void main(String[] args) {
|
||||
Bootstrap.bootstrap("demo.MainDemo", args);
|
||||
|
@ -0,0 +1,37 @@
|
||||
package improveextensions.unloadextensiononstop;
|
||||
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.type.monster.EntityZombie;
|
||||
import net.minestom.server.event.EventCallback;
|
||||
import net.minestom.server.event.GlobalEventHandler;
|
||||
import net.minestom.server.event.entity.EntityTickEvent;
|
||||
import net.minestom.server.event.instance.InstanceTickEvent;
|
||||
import net.minestom.server.extensions.Extension;
|
||||
import net.minestom.server.extras.selfmodification.MinestomRootClassLoader;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.opentest4j.AssertionFailedError;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class UnloadExtensionOnStop extends Extension {
|
||||
|
||||
private boolean terminated = false;
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> {
|
||||
Assertions.assertTrue(terminated, "Extension should have been terminated on shutdown.");
|
||||
System.out.println("All tests passed.");
|
||||
}).makeTransient().schedule();
|
||||
|
||||
MinecraftServer.getSchedulerManager().buildTask(MinecraftServer::stopCleanly).makeTransient().delay(1L, TimeUnit.SECOND).schedule();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void terminate() {
|
||||
terminated = true;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"entrypoint": "improveextensions.unloadextensiononstop.UnloadExtensionOnStop",
|
||||
"name": "UnloadExtensionOnStop"
|
||||
}
|
Loading…
Reference in New Issue
Block a user