Simplify mod initializers and prevent extensions from mixing into Carpet just to register themselves (#1396)
This commit is contained in:
1 parent
83b0fb1f4d
commit
8c2ded4c92
2 files changed
+16
-13
No files matched your search
@@ -29,8 +29,6 @@ import carpet.utils.MobAI;
|
||||
import carpet.utils.SpawnReporter;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.api.DedicatedServerModInitializer;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.commands.CommandBuildContext;
|
||||
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 static final ClientModInitializer CLIENT_INITIALIZER = CarpetServer::onGameStarted;
|
||||
public static final DedicatedServerModInitializer SERVER_INITIALIZER = CarpetServer::onGameStarted;
|
||||
|
||||
public static final Random rand = new Random();
|
||||
public static MinecraftServer minecraft_server;
|
||||
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)
|
||||
{
|
||||
extensions.add(extension);
|
||||
// for extensions that come late to the party, after server is created / loaded
|
||||
// we will handle them now.
|
||||
// that would handle all extensions, even these that add themselves really late to the party
|
||||
// Stop the stupid practice of extensions mixing into Carpet just to register themselves
|
||||
if (StackWalker.getInstance().walk(stream -> stream.anyMatch(el ->
|
||||
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)
|
||||
{
|
||||
extension.registerCommands(currentCommandDispatcher);
|
||||
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!");
|
||||
throw new IllegalStateException("Extension %s tried to register too late!".formatted(extension.getClass().getSimpleName()));
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
{
|
||||
settingsManager = new SettingsManager(CarpetSettings.carpetVersion, "carpet", "Carpet Mod");
|
||||
|
||||
@@ -20,10 +20,10 @@
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"client": [
|
||||
"carpet.CarpetServer::CLIENT_INITIALIZER"
|
||||
"carpet.CarpetServer::onGameStarted"
|
||||
],
|
||||
"server": [
|
||||
"carpet.CarpetServer::SERVER_INITIALIZER"
|
||||
"carpet.CarpetServer::onGameStarted"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
|
||||
Reference in new issue
Block a user