From 1ca18be1a0c5f92c1a5af9f40685b4ceb0994934 Mon Sep 17 00:00:00 2001 From: Stewart Borle Date: Wed, 20 Nov 2024 00:17:11 +0800 Subject: [PATCH] Resolve some NeoForge FIXMEs ModLoadingContext can be supplied to the Mod's constructor, so using it. --- .../mod/neoforge/ForgeClientProvider.java | 7 +++-- .../mod/neoforge/ForgeCommonProvider.java | 29 +++++++++---------- .../mod/neoforge/ForgeInitializer.java | 10 ++++--- .../mod/neoforge/Slf4jSystemLogger.java | 4 +-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeClientProvider.java b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeClientProvider.java index bc9d8410..afcef930 100644 --- a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeClientProvider.java +++ b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeClientProvider.java @@ -1,5 +1,6 @@ package net.pcal.fastback.mod.neoforge; +import net.neoforged.fml.ModContainer; import net.neoforged.neoforge.client.event.CustomizeGuiOverlayEvent; import net.neoforged.neoforge.client.event.ScreenEvent; import net.neoforged.neoforge.common.NeoForge; @@ -38,9 +39,9 @@ final class ForgeClientProvider extends ForgeCommonProvider { private long hudTextTime; private final Minecraft client; - public ForgeClientProvider() { - final IEventBus modEventBus = ModLoadingContext.get().getActiveContainer().getEventBus(); - modEventBus.addListener(this::onClientStartupEvent); + public ForgeClientProvider(ModContainer container) { + super(container); + container.getEventBus().addListener(this::onClientStartupEvent); NeoForge.EVENT_BUS.addListener(this::onGuiOverlayEvent); NeoForge.EVENT_BUS.addListener(this::onScreenRenderEvent); this.client = requireNonNull(Minecraft.getInstance(), "MinecraftClient.getInstance() returned null"); diff --git a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeCommonProvider.java b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeCommonProvider.java index 1aa8408a..24c18e16 100644 --- a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeCommonProvider.java +++ b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeCommonProvider.java @@ -7,9 +7,10 @@ import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.storage.LevelResource; +import net.neoforged.fml.ModContainer; +import net.neoforged.fml.loading.FMLPaths; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.RegisterCommandsEvent; -import net.neoforged.neoforge.event.level.LevelEvent; import net.neoforged.neoforge.event.server.ServerStartedEvent; import net.neoforged.neoforge.event.server.ServerStoppingEvent; import net.neoforged.bus.api.IEventBus; @@ -40,13 +41,15 @@ class ForgeCommonProvider implements MinecraftProvider, MixinGateway { static final String MOD_ID = "fastback"; private MinecraftServer logicalServer; + private ModContainer container; private LifecycleListener lifecycleListener = null; private Runnable autoSaveListener; private boolean isWorldSaveEnabled = true; - ForgeCommonProvider() { - final IEventBus modEventBus = ModLoadingContext.get().getActiveContainer().getEventBus(); - modEventBus.addListener(this::onDedicatedServerStartupEvent); + ForgeCommonProvider(ModContainer container) { + this.container = container; + + container.getEventBus().addListener(this::onDedicatedServerStartupEvent); NeoForge.EVENT_BUS.addListener(this::onServerStartupEvent); NeoForge.EVENT_BUS.addListener(this::onServerStoppingEvent); NeoForge.EVENT_BUS.addListener(this::onRegisterCommandEvent); @@ -89,9 +92,9 @@ void onInitialize() { this.lifecycleListener = MinecraftProvider.register(this); syslog().debug("registered backup command"); this.lifecycleListener.onInitialize(); - syslog().info("Fastback initialized"); + syslog().info("Fastback " + getModVersion() + " initialized"); syslog().warn("------------------------------------------------------------------------------------"); - syslog().warn("Thanks for trying the new Forge version of Fastback. For help, go to:"); + syslog().warn("Thanks for trying the new NeoForge version of Fastback. For help, go to:"); syslog().warn("https://pcal43.github.io/fastback/"); syslog().warn("Please note that this is an alpha release. A list of known issues is available here:"); syslog().warn("https://github.com/pcal43/fastback/issues?q=is%3Aissue+is%3Aopen+label%3Aforge"); @@ -125,7 +128,7 @@ void renderOverlayText(GuiGraphics drawContext) { @Override public String getModVersion() { - return "0.15.3+1.20.1-alpha"; //FIXME + return container.getModInfo().getVersion().toString(); } @Override @@ -205,14 +208,10 @@ public void addBackupProperties(Map props) { @Override public Collection getModsBackupPaths() { final List out = new ArrayList<>(); - /** - final FabricLoader fl = FabricLoader.getInstance(); - final Path gameDir = fl.getGameDir(); - out.add(gameDir.resolve("options.txt´")); - out.add(gameDir.resolve("mods")); - out.add(gameDir.resolve("config")); - out.add(gameDir.resolve("resourcepacks")); - **/ + out.add(FMLPaths.GAMEDIR.get().resolve("options.txt")); + out.add(FMLPaths.MODSDIR.get()); + out.add(FMLPaths.CONFIGDIR.get()); + out.add(FMLPaths.GAMEDIR.get().resolve("resourcepacks")); return out; } } diff --git a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeInitializer.java b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeInitializer.java index df89eaac..34903d06 100644 --- a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeInitializer.java +++ b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/ForgeInitializer.java @@ -1,5 +1,7 @@ package net.pcal.fastback.mod.neoforge; +import net.neoforged.fml.ModContainer; +import net.neoforged.fml.ModLoadingContext; import net.neoforged.fml.common.Mod; import net.neoforged.fml.loading.FMLEnvironment; @@ -12,13 +14,13 @@ @Mod("fastback") final public class ForgeInitializer { - public ForgeInitializer() { + public ForgeInitializer(ModContainer container) { try { if (FMLEnvironment.dist.isDedicatedServer()) { - new ForgeCommonProvider(); + new ForgeCommonProvider(container); } else if (FMLEnvironment.dist.isClient()) { // Forge yells at us if we touch any client classes in a server. So, - Class.forName("net.pcal.fastback.mod.neoforge.ForgeClientProvider").getConstructor().newInstance(); + Class.forName("net.pcal.fastback.mod.neoforge.ForgeClientProvider").getConstructor(ModContainer.class).newInstance(container); } else { throw new IllegalStateException("where am i? server or client?"); } @@ -27,4 +29,4 @@ public ForgeInitializer() { throw new RuntimeException(e); } } -} \ No newline at end of file +} diff --git a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/Slf4jSystemLogger.java b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/Slf4jSystemLogger.java index f388be5b..080d05a7 100644 --- a/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/Slf4jSystemLogger.java +++ b/neoforge/src/main/java/net/pcal/fastback/mod/neoforge/Slf4jSystemLogger.java @@ -39,9 +39,7 @@ public void warn(String message) { } @Override - public void warn(String message, Throwable t) { - this.slf4j.warn(message, t); - } + public void warn(String message, Throwable t) {this.slf4j.warn(message, t);} @Override public void info(String message) {