Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 3026e42

Browse files
committed
Implement FMLServerStoppedEvent
1 parent e0f940a commit 3026e42

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.fml.event.server;
21+
22+
import net.minecraft.server.MinecraftServer;
23+
24+
/**
25+
* Called after {@link FMLServerStoppingEvent} when the server has completely shut down.
26+
* Called immediately before shutting down, on the dedicated server, and before returning
27+
* to the main menu on the client.
28+
*
29+
* @author cpw
30+
*/
31+
public class FMLServerStoppedEvent extends ServerLifecycleEvent {
32+
public FMLServerStoppedEvent(MinecraftServer server) {
33+
super(server);
34+
}
35+
}

patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
package net.minecraftforge.fml.server;
2121

22+
import java.util.concurrent.CountDownLatch;
23+
2224
import net.minecraft.server.MinecraftServer;
2325

2426
/**
2527
* This is a stub of the ServerLifecycleHooks class in Forge for mods that use getCurrentServer.
2628
*/
2729
public class ServerLifecycleHooks {
2830
public static MinecraftServer currentServer;
31+
public static volatile CountDownLatch exitLatch = null;
2932

3033
public static MinecraftServer getCurrentServer() {
3134
return currentServer;

patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package net.patchworkmc.impl.event.lifecycle;
2121

2222
import java.nio.file.Path;
23+
import java.util.concurrent.CountDownLatch;
2324

2425
import net.minecraftforge.common.MinecraftForge;
2526
import net.minecraftforge.event.TickEvent;
@@ -30,6 +31,7 @@
3031
import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent;
3132
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
3233
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
34+
import net.minecraftforge.fml.event.server.FMLServerStoppedEvent;
3335
import net.minecraftforge.fml.loading.FileUtils;
3436
import net.minecraftforge.fml.server.ServerLifecycleHooks;
3537

@@ -93,6 +95,18 @@ public static void handleLoadComplete() {
9395
loadCompleteCallback.run();
9496
}
9597

98+
public static void handleServerStopped(final MinecraftServer server) {
99+
MinecraftForge.EVENT_BUS.post(new FMLServerStoppedEvent(server));
100+
ServerLifecycleHooks.currentServer = null;
101+
LogicalSidedProvider.setServer(null);
102+
CountDownLatch latch = ServerLifecycleHooks.exitLatch;
103+
104+
if (latch != null) {
105+
latch.countDown();
106+
ServerLifecycleHooks.exitLatch = null;
107+
}
108+
}
109+
96110
@Override
97111
public void onInitialize() {
98112
WorldTickCallback.EVENT.register(world -> fireWorldTickEvent(TickEvent.Phase.END, world));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.event.lifecycle;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Inject;
25+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
26+
27+
import net.minecraft.server.MinecraftServer;
28+
29+
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
30+
31+
@Mixin(MinecraftServer.class)
32+
public class MixinMinecraftServer {
33+
@Inject(method = "run", at = @At(value = "INVOKE", target = "net/minecraft/server/MinecraftServer.exit ()V"))
34+
void serverStoppedHook(CallbackInfo ci) {
35+
LifecycleEvents.handleServerStopped((MinecraftServer) (Object) this);
36+
}
37+
}

0 commit comments

Comments
 (0)