From f1702495efd690fe67e34743f4c17c542193be7d Mon Sep 17 00:00:00 2001 From: SizableShrimp Date: Sun, 14 May 2023 15:26:24 -0500 Subject: [PATCH] Fix crash when running server from root directory Fixes #9498 --- .../minecraftforge/fml/loading/FMLConfig.java | 2 +- .../minecraftforge/fml/loading/FMLPaths.java | 30 +++++++++++++++++-- .../minecraftforge/fml/loading/FileUtils.java | 24 +++++---------- .../server/ServerLifecycleHooks.java | 12 ++++++-- .../client/rendering/RenderableTest.java | 6 ++-- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLConfig.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLConfig.java index 7595592825..6f14cc2531 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLConfig.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLConfig.java @@ -69,7 +69,7 @@ public class FMLConfig LOGGER.trace(CORE, "Version check is {}", FMLConfig.runVersionCheck()); LOGGER.trace(CORE, "Default config paths at {}", FMLConfig.defaultConfigPath()); } - FMLPaths.getOrCreateGameRelativePath(Paths.get(FMLConfig.defaultConfigPath()), "default config directory"); + FMLPaths.getOrCreateGameRelativePath(Paths.get(FMLConfig.defaultConfigPath())); } public static boolean splashScreenEnabled() { diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLPaths.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLPaths.java index 91b5128853..1f2b3c5a9c 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLPaths.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FMLPaths.java @@ -9,6 +9,8 @@ import com.mojang.logging.LogUtils; import cpw.mods.modlauncher.api.IEnvironment; import org.slf4j.Logger; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; @@ -57,9 +59,13 @@ public enum FMLPaths for (FMLPaths path : FMLPaths.values()) { path.absolutePath = rootPath.resolve(path.relativePath).toAbsolutePath().normalize(); - if (path.isDirectory) + if (path.isDirectory && !Files.isDirectory(path.absolutePath)) { - FileUtils.getOrCreateDirectory(path.absolutePath, path.name()); + try { + Files.createDirectories(path.absolutePath); + } catch (IOException e) { + throw new RuntimeException(e); + } } if (LOGGER.isDebugEnabled(CORE)) { @@ -68,8 +74,26 @@ public enum FMLPaths } } + /** + * @deprecated Use {@link #getOrCreateGameRelativePath(Path)} instead. + */ + @Deprecated(forRemoval = true, since = "1.19.4") public static Path getOrCreateGameRelativePath(Path path, String name) { - return FileUtils.getOrCreateDirectory(FMLPaths.GAMEDIR.get().resolve(path), name); + return getOrCreateGameRelativePath(path); + } + + public static Path getOrCreateGameRelativePath(Path path) { + Path gameFolderPath = FMLPaths.GAMEDIR.get().resolve(path); + + if (!Files.isDirectory(gameFolderPath)) { + try { + Files.createDirectories(gameFolderPath); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + return gameFolderPath; } public Path relative() { diff --git a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FileUtils.java b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FileUtils.java index fc62ab352d..628c7e457c 100644 --- a/fmlloader/src/main/java/net/minecraftforge/fml/loading/FileUtils.java +++ b/fmlloader/src/main/java/net/minecraftforge/fml/loading/FileUtils.java @@ -10,37 +10,27 @@ import org.slf4j.Logger; import java.io.File; import java.io.IOException; -import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; -import static net.minecraftforge.fml.loading.LogMarkers.CORE; - public class FileUtils { private static final Logger LOGGER = LogUtils.getLogger(); + /** + * @deprecated Use normal Java NIO methods instead + */ + @Deprecated(forRemoval = true, since = "1.19.4") public static Path getOrCreateDirectory(Path dirPath, String dirLabel) { - if (!Files.isDirectory(dirPath.getParent())) { - getOrCreateDirectory(dirPath.getParent(), "parent of "+dirLabel); - } if (!Files.isDirectory(dirPath)) { - LOGGER.debug(CORE, "Making {} directory : {}", dirLabel, dirPath); try { - Files.createDirectory(dirPath); + Files.createDirectories(dirPath); } catch (IOException e) { - if (e instanceof FileAlreadyExistsException) { - LOGGER.error(CORE, "Failed to create {} directory - there is a file in the way", dirLabel); - } else { - LOGGER.error(CORE, "Problem with creating {} directory (Permissions?)", dirLabel, e); - } throw new RuntimeException("Problem creating directory", e); } - LOGGER.debug(CORE, "Created {} directory : {}", dirLabel, dirPath); - } else { - LOGGER.debug(CORE, "Found existing {} directory : {}", dirLabel, dirPath); } + return dirPath; } @@ -54,7 +44,7 @@ public class FileUtils return ""; } } - + public static boolean matchFileName(String path, String... matches) { // Extract file name from path String name = path.substring(Math.min(path.lastIndexOf(File.separatorChar) + 1, path.length())); diff --git a/src/main/java/net/minecraftforge/server/ServerLifecycleHooks.java b/src/main/java/net/minecraftforge/server/ServerLifecycleHooks.java index 995360d89a..db49403d43 100644 --- a/src/main/java/net/minecraftforge/server/ServerLifecycleHooks.java +++ b/src/main/java/net/minecraftforge/server/ServerLifecycleHooks.java @@ -7,6 +7,8 @@ package net.minecraftforge.server; import static net.minecraftforge.fml.Logging.CORE; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.Map; @@ -35,7 +37,6 @@ import net.minecraftforge.fml.ModLoadingStage; import net.minecraftforge.fml.ModLoadingWarning; import net.minecraftforge.network.ConnectionType; import net.minecraftforge.network.NetworkConstants; -import net.minecraftforge.network.ServerStatusPing; import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.forgespi.locating.IModFile; @@ -62,7 +63,6 @@ import net.minecraftforge.event.server.ServerStartedEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.event.server.ServerStoppedEvent; import net.minecraftforge.event.server.ServerStoppingEvent; -import net.minecraftforge.fml.loading.FileUtils; import net.minecraftforge.forgespi.language.IModInfo; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.GameData; @@ -79,7 +79,13 @@ public class ServerLifecycleHooks private static Path getServerConfigPath(final MinecraftServer server) { final Path serverConfig = server.getWorldPath(SERVERCONFIG); - FileUtils.getOrCreateDirectory(serverConfig, "serverconfig"); + if (!Files.isDirectory(serverConfig)) { + try { + Files.createDirectories(serverConfig); + } catch (IOException e) { + throw new RuntimeException(e); + } + } return serverConfig; } diff --git a/src/test/java/net/minecraftforge/debug/client/rendering/RenderableTest.java b/src/test/java/net/minecraftforge/debug/client/rendering/RenderableTest.java index 2ad788b0b3..644b0a2d4c 100644 --- a/src/test/java/net/minecraftforge/debug/client/rendering/RenderableTest.java +++ b/src/test/java/net/minecraftforge/debug/client/rendering/RenderableTest.java @@ -49,10 +49,10 @@ import org.joml.Quaternionf; /** * This mod is testing the use of {@link RenderLevelStageEvent} and is a modifaction of a pre-existing test mod that used the old * {@link RenderLevelLastEvent}. To restore the old behavior, set {@link #USE_LEVEL_RENDERER_STAGE} to false. - * + * * When you enter a world, there should be 6 sugar gliders rendering at (0, 120, 0) that test the various stages in {@link RenderLevelStageEvent}. * From left to right (with the sugar gliders facing you) they represent {@link Stage#AFTER_SKY}, {@link Stage#AFTER_SOLID_BLOCKS}, - * {@link Stage#AFTER_TRANSLUCENT_BLOCKS}, {@link Stage#AFTER_TRIPWIRE_BLOCKS}, {@link Stage#AFTER_PARTICLES}, and {@link Stage#AFTER_WEATHER}. + * {@link Stage#AFTER_TRANSLUCENT_BLOCKS}, {@link Stage#AFTER_TRIPWIRE_BLOCKS}, {@link Stage#AFTER_PARTICLES}, and {@link Stage#AFTER_WEATHER}. * Due to how weather modifies the projection matrix, it's sugar glider will be positioned weirdly. Below each sugar gliders is a render of * blue stained glass to test translucency with fabulous graphics. {@link Stage#AFTER_PARTICLES} will render the stained glass using * {@link ForgeRenderTypes#TRANSLUCENT_ON_PARTICLES_TARGET}. @@ -63,7 +63,7 @@ public class RenderableTest public static final String MODID = "renderable_test"; private static final Logger LOGGER = LogManager.getLogger(); - public static final boolean ENABLED = true; // Renders at (0, 120, 0) + public static final boolean ENABLED = false; // Renders at (0, 120, 0) public static final boolean USE_LEVEL_RENDERER_STAGE = true; // True when using RenderLevelStageEvent. False when using RenderLevelLastEvent public RenderableTest()