Simplify mod initializers and prevent extensions from mixing into Carpet just to register themselves (#1396)

This commit is contained in:
altrisi authored and GitHub committed 2022-04-20 13:50:44 +02:00
1 parent 83b0fb1f4d
commit 8c2ded4c92
2 files changed
+16 -13

No files matched your search

+14 -11
View File
@@ -29,8 +29,6 @@ import carpet.utils.MobAI;
import carpet.utils.SpawnReporter; import carpet.utils.SpawnReporter;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
@@ -41,9 +39,6 @@ import net.minecraft.server.level.ServerPlayer;
public class CarpetServer // static for now - easier to handle all around the code, its one anyways public class CarpetServer // static for now - easier to handle all around the code, its one anyways
{ {
public static final ClientModInitializer CLIENT_INITIALIZER = CarpetServer::onGameStarted;
public static final DedicatedServerModInitializer SERVER_INITIALIZER = CarpetServer::onGameStarted;
public static final Random rand = new Random(); public static final Random rand = new Random();
public static MinecraftServer minecraft_server; public static MinecraftServer minecraft_server;
private static CommandDispatcher<CommandSourceStack> currentCommandDispatcher; private static CommandDispatcher<CommandSourceStack> currentCommandDispatcher;
@@ -61,17 +56,25 @@ public class CarpetServer // static for now - easier to handle all around the co
public static void manageExtension(CarpetExtension extension) public static void manageExtension(CarpetExtension extension)
{ {
extensions.add(extension); extensions.add(extension);
// for extensions that come late to the party, after server is created / loaded // Stop the stupid practice of extensions mixing into Carpet just to register themselves
// we will handle them now. if (StackWalker.getInstance().walk(stream -> stream.anyMatch(el ->
// that would handle all extensions, even these that add themselves really late to the party el.getClassName() == CarpetServer.class.getName() && !el.getMethodName().equals("manageExtension")
))) {
CarpetSettings.LOG.warn("""
Extension '%s' is registering itself using a mixin into Carpet instead of a regular ModInitializer!
This is stupid and will crash the game in future versions!""".formatted(extension.getClass().getSimpleName()));
}
// for extensions that come late to the party, we used to handle them, but we've been giving them warnings about
// it for a while. Cause a crash
if (currentCommandDispatcher != null) if (currentCommandDispatcher != null)
{ {
extension.registerCommands(currentCommandDispatcher); throw new IllegalStateException("Extension %s tried to register too late!".formatted(extension.getClass().getSimpleName()));
CarpetSettings.LOG.warn(extension.getClass().getSimpleName() + " extension registered too late!");
CarpetSettings.LOG.warn("This won't be supported in later Carpet versions and may crash the game!");
} }
} }
// Gets called by Fabric Loader from a ServerModInitializer and a ClientModInitializer, in both to allow extensions
// to register before this call in a ModInitializer (declared in fabric.mod.json)
public static void onGameStarted() public static void onGameStarted()
{ {
settingsManager = new SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod"); settingsManager = new SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod");
+2 -2
View File
@@ -20,10 +20,10 @@
"environment": "*", "environment": "*",
"entrypoints": { "entrypoints": {
"client": [ "client": [
"carpet.CarpetServer::CLIENT_INITIALIZER" "carpet.CarpetServer::onGameStarted"
], ],
"server": [ "server": [
"carpet.CarpetServer::SERVER_INITIALIZER" "carpet.CarpetServer::onGameStarted"
] ]
}, },
"mixins": [ "mixins": [