From d188bd50b6df92199ddf6b7a409e68e19af8f2db Mon Sep 17 00:00:00 2001 From: famous1622 <8428080+famous1622@users.noreply.github.com> Date: Fri, 19 Jun 2020 17:36:13 -0400 Subject: [PATCH 1/3] ServerAboutToStart event code --- .../server/FMLServerAboutToStartEvent.java | 38 +++++++++++++++++++ .../fml/server/ServerLifecycleHooks.java | 33 ++++++++++++++++ .../impl/event/lifecycle/LifecycleEvents.java | 22 +++++++++++ .../lifecycle/MixinIntegratedServer.java | 38 +++++++++++++++++++ .../MixinMinecraftDedicatedServer.java | 38 +++++++++++++++++++ .../patchwork-events-lifecycle.mixins.json | 3 ++ 6 files changed, 172 insertions(+) create mode 100644 patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/event/server/FMLServerAboutToStartEvent.java create mode 100644 patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java create mode 100644 patchwork-events-lifecycle/src/main/java/net/patchworkmc/mixin/event/lifecycle/MixinIntegratedServer.java create mode 100644 patchwork-events-lifecycle/src/main/java/net/patchworkmc/mixin/event/lifecycle/MixinMinecraftDedicatedServer.java diff --git a/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/event/server/FMLServerAboutToStartEvent.java b/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/event/server/FMLServerAboutToStartEvent.java new file mode 100644 index 00000000..4aa7eaaa --- /dev/null +++ b/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/event/server/FMLServerAboutToStartEvent.java @@ -0,0 +1,38 @@ +/* + * Minecraft Forge, Patchwork Project + * Copyright (c) 2016-2020, 2019-2020 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.fml.event.server; + +import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent; + +import net.minecraft.server.MinecraftServer; + +/** + * Called before the server begins loading anything. Called after {@link InterModProcessEvent} on the dedicated + * server, and after the player has hit "Play Selected World" in the client. Called before {@link FMLServerStartingEvent}. + * + *
You can obtain a reference to the server with this event.
+ *
+ * @author cpw
+ */
+public class FMLServerAboutToStartEvent extends ServerLifecycleEvent {
+ public FMLServerAboutToStartEvent(MinecraftServer server) {
+ super(server);
+ }
+}
diff --git a/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java b/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java
new file mode 100644
index 00000000..9f194790
--- /dev/null
+++ b/patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/server/ServerLifecycleHooks.java
@@ -0,0 +1,33 @@
+/*
+ * Minecraft Forge, Patchwork Project
+ * Copyright (c) 2016-2020, 2019-2020
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package net.minecraftforge.fml.server;
+
+import net.minecraft.server.MinecraftServer;
+
+/**
+ * This is a stub of the ServerLifecycleHooks class in Forge for mods that use getCurrentServer.
+ */
+public class ServerLifecycleHooks {
+ public static MinecraftServer currentServer;
+
+ public static MinecraftServer getCurrentServer() {
+ return currentServer;
+ }
+}
diff --git a/patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java b/patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java
index 8c360c28..e63af4c4 100644
--- a/patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java
+++ b/patchwork-events-lifecycle/src/main/java/net/patchworkmc/impl/event/lifecycle/LifecycleEvents.java
@@ -19,11 +19,19 @@
package net.patchworkmc.impl.event.lifecycle;
+import java.nio.file.Path;
+
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.fml.LogicalSide;
+import net.minecraftforge.fml.LogicalSidedProvider;
+import net.minecraftforge.fml.config.ConfigTracker;
+import net.minecraftforge.fml.config.ModConfig;
+import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent;
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
+import net.minecraftforge.fml.loading.FileUtils;
+import net.minecraftforge.fml.server.ServerLifecycleHooks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
@@ -61,6 +69,20 @@ public static void handleServerStarted(final MinecraftServer server) {
MinecraftForge.EVENT_BUS.post(new FMLServerStartedEvent(server));
}
+ public static void handleServerAboutToStart(final MinecraftServer server) {
+ ServerLifecycleHooks.currentServer = server;
+ LogicalSidedProvider.setServer(() -> server);
+ ConfigTracker.INSTANCE.loadConfigs(ModConfig.Type.SERVER, getServerConfigPath(server));
+ // TODO: ResourcePackLoader.loadResourcePacks(currentServer.getDataPackManager(), ServerLifecycleHooks::buildPackFinder);
+ MinecraftForge.EVENT_BUS.post(new FMLServerAboutToStartEvent(server));
+ }
+
+ private static Path getServerConfigPath(final MinecraftServer server) {
+ final Path serverConfig = server.getLevelStorage().resolveFile(server.getLevelName(), "serverconfig").toPath();
+ FileUtils.getOrCreateDirectory(serverConfig, "serverconfig");
+ return serverConfig;
+ }
+
@Override
public void onInitialize() {
WorldTickCallback.EVENT.register(world -> fireWorldTickEvent(TickEvent.Phase.END, world));
diff --git a/patchwork-events-lifecycle/src/main/java/net/patchworkmc/mixin/event/lifecycle/MixinIntegratedServer.java b/patchwork-events-lifecycle/src/main/java/net/patchworkmc/mixin/event/lifecycle/MixinIntegratedServer.java
new file mode 100644
index 00000000..7ad7fcd9
--- /dev/null
+++ b/patchwork-events-lifecycle/src/main/java/net/patchworkmc/mixin/event/lifecycle/MixinIntegratedServer.java
@@ -0,0 +1,38 @@
+/*
+ * Minecraft Forge, Patchwork Project
+ * Copyright (c) 2016-2020, 2019-2020
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation version 2.1
+ * of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package net.patchworkmc.mixin.event.lifecycle;
+
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.integrated.IntegratedServer;
+
+import net.patchworkmc.impl.event.lifecycle.LifecycleEvents;
+
+@Mixin(IntegratedServer.class)
+public class MixinIntegratedServer {
+ @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"))
+ private void onServerAboutToStart(CallbackInfoReturnable