From 4c60b452efda24794c0faa5f300fdd830edcd383 Mon Sep 17 00:00:00 2001 From: nextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com> Date: Sat, 3 Sep 2022 12:50:40 +0100 Subject: deprecator + Stack trace utilities (#121) * deprecator * check for more system packages because iterators n stuff * add LogScanner, new loader platform features, spam protection * oop~ * move to Preferences * diamond review * add back the exception * try-catch things and associate OneConfig mods with an ActiveMod * ok maybe not + api my beloved --- .../platform/impl/LoaderPlatformImpl.java | 73 +++++++++++++++++----- .../oneconfig/platform/impl/PlatformImpl.java | 16 +++++ 2 files changed, 72 insertions(+), 17 deletions(-) (limited to 'versions/src/main/java') diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/LoaderPlatformImpl.java b/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/LoaderPlatformImpl.java index 68065fd..4c10997 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/LoaderPlatformImpl.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/LoaderPlatformImpl.java @@ -28,26 +28,33 @@ package cc.polyfrost.oneconfig.platform.impl; import cc.polyfrost.oneconfig.platform.LoaderPlatform; //#if MC>=11600 - //#if FORGE==1 - //$$ import net.minecraftforge.fml.ModList; - //#else - //$$ import net.fabricmc.loader.api.FabricLoader; - //#endif +//#if FORGE==1 +//$$ import net.minecraftforge.fml.ModList; +//#else +//$$ import net.fabricmc.loader.api.FabricLoader; +//$$ import net.fabricmc.loader.api.ModContainer; +//#endif //#endif //#if FORGE==1 import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.ModContainer; //#endif +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; public class LoaderPlatformImpl implements LoaderPlatform { @Override public boolean isModLoaded(String id) { //#if MC>=11600 - //#if FORGE==1 - //$$ return ModList.get().isLoaded(id); - //#else - //$$ return FabricLoader.getInstance().isModLoaded(id); - //#endif + //#if FORGE==1 + //$$ return ModList.get().isLoaded(id); + //#else + //$$ return FabricLoader.getInstance().isModLoaded(id); + //#endif //#else return Loader.isModLoaded(id); //#endif @@ -62,18 +69,50 @@ public class LoaderPlatformImpl implements LoaderPlatform { //#endif } + @Override + public @NotNull List getLoadedMods() { + try { + return + //#if FORGE==1 + //#if MC<=11202 + Loader.instance().getActiveModList().stream().map + //#else + //$$ ModList.get().applyForEachModContainer + //#endif + //#else + //$$ FabricLoader.getInstance().getAllMods().stream().map + //#endif + (this::toActiveMod).collect(Collectors.toList()); + } catch (Exception e) { + return Collections.emptyList(); + } + } + @Override public ActiveMod getActiveModContainer() { //#if FORGE==1 - ModContainer container = Loader.instance().activeModContainer(); - if (container == null) return null; - //#if MC==11202 - return new ActiveMod(container.getName(), container.getModId(), container.getVersion()); - //#else - //$$ return new ActiveMod(container.getModInfo().getDisplayName(), container.getModId(), container.getModInfo().getVersion().getQualifier()); - //#endif + return toActiveMod(Loader.instance().activeModContainer()); //#else //$$ return null; //#endif } + + @Override + public ActiveMod toActiveMod(@Nullable Object in) { + try { + ModContainer container = (ModContainer) in; + if (container == null) return null; + //#if FORGE==1 + //#if MC==11202 + return new ActiveMod(container.getName(), container.getModId(), container.getVersion(), container.getSource()); + //#else + //$$ return new ActiveMod(container.getModInfo().getDisplayName(), container.getModId(), container.getModInfo().getVersion().getQualifier(), ModList.get().getModFileById(container.getModId()).getFile().getFilePath().toFile()); + //#endif + //#else + //$$ return new ActiveMod(container.getMetadata().getName(), container.getMetadata().getId(), container.getMetadata().getVersion().getFriendlyString(), container.getRootPaths().get(0).toFile()); + //#endif + } catch (Exception e) { + return null; + } + } } diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/PlatformImpl.java b/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/PlatformImpl.java index 85bf795..e6e96f0 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/PlatformImpl.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/platform/impl/PlatformImpl.java @@ -64,6 +64,22 @@ public class PlatformImpl implements Platform { //#endif } + @Override + public boolean isDevelopmentEnvironment() { + //#if FORGE==1 && MC<=11202 + try { + Class.forName("net.minecraft.block.BlockDirt"); + return true; + } catch (Exception ignored) { + return false; + } + //#elseif FABRIC==1 + //$$ return net.fabricmc.loader.api.FabricLoader.getInstance().isDevelopmentEnvironment(); + //#else + //$$ return !net.minecraftforge.fml.loading.FMLLoader.isProduction(); + //#endif + } + @Override public Loader getLoader() { //#if FORGE==1 -- cgit