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

Commit d260c05

Browse files
Implement FMLServerAboutToStartEvent and hooks related to it. (#90)
* ServerAboutToStart event code * Use normal CallbackInfo * Unforget to fix imports
1 parent b9e9615 commit d260c05

File tree

6 files changed

+172
-0
lines changed

6 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
23+
24+
import net.minecraft.server.MinecraftServer;
25+
26+
/**
27+
* Called before the server begins loading anything. Called after {@link InterModProcessEvent} on the dedicated
28+
* server, and after the player has hit "Play Selected World" in the client. Called before {@link FMLServerStartingEvent}.
29+
*
30+
* <p>You can obtain a reference to the server with this event.
31+
*
32+
* @author cpw
33+
*/
34+
public class FMLServerAboutToStartEvent extends ServerLifecycleEvent {
35+
public FMLServerAboutToStartEvent(MinecraftServer server) {
36+
super(server);
37+
}
38+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.server;
21+
22+
import net.minecraft.server.MinecraftServer;
23+
24+
/**
25+
* This is a stub of the ServerLifecycleHooks class in Forge for mods that use getCurrentServer.
26+
*/
27+
public class ServerLifecycleHooks {
28+
public static MinecraftServer currentServer;
29+
30+
public static MinecraftServer getCurrentServer() {
31+
return currentServer;
32+
}
33+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919

2020
package net.patchworkmc.impl.event.lifecycle;
2121

22+
import java.nio.file.Path;
23+
2224
import net.minecraftforge.common.MinecraftForge;
2325
import net.minecraftforge.event.TickEvent;
2426
import net.minecraftforge.fml.LogicalSide;
27+
import net.minecraftforge.fml.LogicalSidedProvider;
28+
import net.minecraftforge.fml.config.ConfigTracker;
29+
import net.minecraftforge.fml.config.ModConfig;
30+
import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent;
2531
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
2632
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
33+
import net.minecraftforge.fml.loading.FileUtils;
34+
import net.minecraftforge.fml.server.ServerLifecycleHooks;
2735

2836
import net.minecraft.entity.player.PlayerEntity;
2937
import net.minecraft.server.MinecraftServer;
@@ -61,6 +69,20 @@ public static void handleServerStarted(final MinecraftServer server) {
6169
MinecraftForge.EVENT_BUS.post(new FMLServerStartedEvent(server));
6270
}
6371

72+
public static void handleServerAboutToStart(final MinecraftServer server) {
73+
ServerLifecycleHooks.currentServer = server;
74+
LogicalSidedProvider.setServer(() -> server);
75+
ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.SERVER, getServerConfigPath(server));
76+
// TODO: ResourcePackLoader.loadResourcePacks(currentServer.getDataPackManager(), ServerLifecycleHooks::buildPackFinder);
77+
MinecraftForge.EVENT_BUS.post(new FMLServerAboutToStartEvent(server));
78+
}
79+
80+
private static Path getServerConfigPath(final MinecraftServer server) {
81+
final Path serverConfig = server.getLevelStorage().resolveFile(server.getLevelName(), "serverconfig").toPath();
82+
FileUtils.getOrCreateDirectory(serverConfig, "serverconfig");
83+
return serverConfig;
84+
}
85+
6486
@Override
6587
public void onInitialize() {
6688
WorldTickCallback.EVENT.register(world -> fireWorldTickEvent(TickEvent.Phase.END, world));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.CallbackInfoReturnable;
26+
27+
import net.minecraft.server.MinecraftServer;
28+
import net.minecraft.server.integrated.IntegratedServer;
29+
30+
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
31+
32+
@Mixin(IntegratedServer.class)
33+
public class MixinIntegratedServer {
34+
@Inject(method = "setupServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/integrated/IntegratedServer;loadWorld(Ljava/lang/String;Ljava/lang/String;JLnet/minecraft/world/level/LevelGeneratorType;Lcom/google/gson/JsonElement;)V"))
35+
private void onServerAboutToStart(CallbackInfoReturnable<Boolean> cir) {
36+
LifecycleEvents.handleServerAboutToStart((MinecraftServer) (Object) this);
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
29+
30+
import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
31+
32+
@Mixin(MinecraftDedicatedServer.class)
33+
public class MixinMinecraftDedicatedServer {
34+
@Inject(method = "setupServer", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/UserCache;setUseRemote(Z)V", shift = At.Shift.AFTER))
35+
private void onServerAboutToStart(CallbackInfo ci) {
36+
LifecycleEvents.handleServerAboutToStart((MinecraftServer) (Object) this);
37+
}
38+
}

patchwork-events-lifecycle/src/main/resources/patchwork-events-lifecycle.mixins.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"MixinClientWorld",
1212
"MixinMinecraftClient"
1313
],
14+
"server": [
15+
"MixinMinecraftDedicatedServer"
16+
],
1417
"injectors": {
1518
"defaultRequire": 1
1619
}

0 commit comments

Comments
 (0)