diff --git a/common/src/main/java/dev/architectury/platform/Platform.java b/common/src/main/java/dev/architectury/platform/Platform.java index b78edd8a..5f813b59 100644 --- a/common/src/main/java/dev/architectury/platform/Platform.java +++ b/common/src/main/java/dev/architectury/platform/Platform.java @@ -24,9 +24,11 @@ import dev.architectury.injectables.targets.ArchitecturyTarget; import dev.architectury.utils.Env; import net.fabricmc.api.EnvType; import net.minecraft.SharedConstants; +import org.jetbrains.annotations.NotNull; import java.nio.file.Path; import java.util.Collection; +import java.util.NoSuchElementException; import java.util.Optional; public final class Platform { @@ -60,54 +62,118 @@ public final class Platform { return SharedConstants.getCurrentVersion().getId(); } + /** + * Gets the root directory for the current instance of Minecraft. + *

+ * The returned path is guaranteed to be absolute. + */ @ExpectPlatform public static Path getGameFolder() { throw new AssertionError(); } + /** + * Gets the main config folder for the current instance of Minecraft. + *

+ * The returned path is guaranteed to be absolute. + */ @ExpectPlatform public static Path getConfigFolder() { throw new AssertionError(); } + /** + * Gets the mods folder of the current instance of Minecraft. + *

+ * The returned path is guaranteed to be absolute. + */ @ExpectPlatform public static Path getModsFolder() { throw new AssertionError(); } + /** + * Returns the current Environment the game is running in, + * being one of either CLIENT or SERVER. + *

+ * The class returned is a platform-agnostic wrapper around the + * EnvType and Dist enums, respectively. + * + * @return The current Environment, as an instance of {@link Env} + * @see Env + * @see #getEnv() + */ @ExpectPlatform public static Env getEnvironment() { throw new AssertionError(); } + /** + * Returns the current Environment the game is running in, + * as a member of the {@link EnvType} enum. This is remapped + * on Forge to be the Dist enum, instead. + * + * @return The current Environment, as an instance of {@link EnvType} + * (or Dist on Forge) + */ @ExpectPlatform public static EnvType getEnv() { throw new AssertionError(); } + /** + * Checks whether a mod with the given mod ID is present. + * + * @param id The mod ID to check. + * @return true if the mod is loaded, false otherwise. + */ @ExpectPlatform public static boolean isModLoaded(String id) { throw new AssertionError(); } + /** + * Gets a {@link Mod} container by its mod ID. + * + * @param id The mod ID to look for. + * @return The mod container, if found. + * @throws NoSuchElementException if no mod with the given ID exists + */ @ExpectPlatform public static Mod getMod(String id) { throw new AssertionError(); } + /** + * Optionally gets a {@link Mod} container by its mod ID if it exists. + * + * @param id The mod ID to look for. + * @return An optional representing the mod container, if found, + * or an empty optional otherwise. + */ public static Optional getOptionalMod(String id) { try { return Optional.of(getMod(id)); - } catch (NullPointerException e) { + } catch (NoSuchElementException e) { return Optional.empty(); } } + /** + * Gets a collection of {@link Mod} containers for all currently-loaded mods. + * + * @return A collection of mod containers. + */ @ExpectPlatform public static Collection getMods() { throw new AssertionError(); } + /** + * Gets a collection of Strings representing the mod IDs of all currently-loaded mods. + * + * @return A collection of all loaded mod IDs. + */ @ExpectPlatform public static Collection getModIds() { throw new AssertionError(); diff --git a/fabric/src/main/java/dev/architectury/platform/fabric/PlatformImpl.java b/fabric/src/main/java/dev/architectury/platform/fabric/PlatformImpl.java index 12053ccd..e91e608c 100644 --- a/fabric/src/main/java/dev/architectury/platform/fabric/PlatformImpl.java +++ b/fabric/src/main/java/dev/architectury/platform/fabric/PlatformImpl.java @@ -41,11 +41,11 @@ public class PlatformImpl { private static final Map mods = new ConcurrentHashMap<>(); public static Path getGameFolder() { - return FabricLoader.getInstance().getGameDir(); + return FabricLoader.getInstance().getGameDir().toAbsolutePath(); } public static Path getConfigFolder() { - return FabricLoader.getInstance().getConfigDir(); + return FabricLoader.getInstance().getConfigDir().toAbsolutePath(); } public static Path getModsFolder() { @@ -88,7 +88,7 @@ public class PlatformImpl { private final ModMetadata metadata; public ModImpl(String id) { - this.container = FabricLoader.getInstance().getModContainer(id).get(); + this.container = FabricLoader.getInstance().getModContainer(id).orElseThrow(); this.metadata = this.container.getMetadata(); } diff --git a/forge/src/main/java/dev/architectury/platform/forge/EventBuses.java b/forge/src/main/java/dev/architectury/platform/forge/EventBuses.java index 05d0e11d..a37b694b 100644 --- a/forge/src/main/java/dev/architectury/platform/forge/EventBuses.java +++ b/forge/src/main/java/dev/architectury/platform/forge/EventBuses.java @@ -32,9 +32,7 @@ public final class EventBuses { private static final Map>> ON_REGISTERED = new HashMap<>(); public static void registerModEventBus(String modId, IEventBus bus) { - IEventBus previous = EVENT_BUS_MAP.put(modId, bus); - if (previous != null) { - EVENT_BUS_MAP.put(modId, previous); + if (EVENT_BUS_MAP.putIfAbsent(modId, bus) != bus) { throw new IllegalStateException("Can't register event bus for mod '" + modId + "' because it was previously registered!"); } diff --git a/forge/src/main/java/dev/architectury/platform/forge/PlatformImpl.java b/forge/src/main/java/dev/architectury/platform/forge/PlatformImpl.java index cf79daf3..a281e39f 100644 --- a/forge/src/main/java/dev/architectury/platform/forge/PlatformImpl.java +++ b/forge/src/main/java/dev/architectury/platform/forge/PlatformImpl.java @@ -92,11 +92,11 @@ public class PlatformImpl { private final IModInfo info; public ModImpl(String id) { - this.container = ModList.get().getModContainerById(id).get(); + this.container = ModList.get().getModContainerById(id).orElseThrow(); this.info = ModList.get().getMods().stream() .filter(modInfo -> Objects.equals(modInfo.getModId(), id)) .findAny() - .get(); + .orElseThrow(); } @Override