Jump to content

[1.12] how do you check if a mod is loaded?


TheGoldenProof

Recommended Posts

I know how to check if a mod is loaded, Loader.isModLoaded(), but if I want to run code with classes from the mod, I can't if the mod isn't there. 

 

Here's what I have right now that checks if the mod is loaded:

Spoiler

@EventHandler
public static void PostInit(FMLPostInitializationEvent event) {
	simpleRPGLoaded = Loader.isModLoaded("simplerpg");
	info(simpleRPGLoaded ?
			"SimpleRPG's player level will be used as Object Control Authority. This can be disabled in config.":
			"SimpleRPG is not loaded. Minecraft's player level will be used instead.");
}

 

and here's where I use it:

Spoiler

EntityPlayerMP p = (EntityPlayerMP)(entity);
int level;
if (ObjectPriorities.simpleRPGLoaded && ConfigFile.general.useSimpleRPG) {
	try {
		IRpgPlayer rpg = (IRpgPlayer) p.getCapability(RpgPlayerProvider.RPG_PLAYER_CAP, (EnumFacing) null);
		level = rpg.getLevel();
	} catch (Error e) {}
} else {
	level = p.experienceLevel;
}

 

But it wont work because the "IRpgPlayer" and "RpgPlayerProvider" classes don't exist when the mod isn't loaded, so it causes a compiler error ("Unresolved Compilation Problems"), so technically the try-catch doesnt do anything because the problem is before it figures that out.

 

I know that this is possible because so many other mods have cross-mod compatibility.

Link to comment
Share on other sites

When compiling, the other mod must be present on the classpath.

To ensure your code can work at runtime without the other mod present, do something similar to @SidedProxy, like this:

 

interface OtherModInterop {

  // interface for interop with other mod here
  // note that the method signatures cannot use classes from the other mod

}

class OtherModInteropImpl implements OtherModInterop {

  // implementation for when other mod is present, can use classes from other mod

}

class OtherModDummyInterop implements OtherModInterop {

  // implementation for when other mod is not present

}

// in pre-init, etc.:
OtherModInterop interop;
if (Loader.isModLoaded("...")) {
  // use reflection to avoid loading the class if other mod is not present
  interop = Class.forName("...OtherModInteropImpl").asSubclass(OtherModInterop.class).newInstance();
} else {
  interop = new OtherModDummyInterop();
}

 

Now you can interface with the other mod from anywhere, since the question of "is it present or not" is abstracted away into two implementation classes.

Edited by diesieben07
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Use java 17. Mixin does not support java 20 (which only came out last week).
    • From what I understand (which might be incorrect, I am not an expert on this), it doesn't use the local client registry for those registries that have data sent from a multiplayer server. SoundEvents is one of those registries. So in multiplayer your SoundEvent effectively won't exist unless it is also registered on the server?   The logic can be found here (and its related methods): https://github.com/MinecraftForge/MinecraftForge/blob/bb49b9e6dc7525ae0fa935abf6eec18b36091bd6/src/main/java/net/minecraftforge/registries/GameData.java#L656
    • This comes up when i press start server and i have no idea what to do about it   2023-03-25 20:41:34,650 main WARN Advanced terminal features are not available in this environment [20:41:34] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher running: args [--launchTarget, forgeserver, --fml.forgeVersion, 43.2.8, --fml.mcVersion, 1.19.2, --fml.forgeGroup, net.minecraftforge, --fml.mcpVersion, 20220805.130853] [20:41:34] [main/INFO] [cp.mo.mo.Launcher/MODLAUNCHER]: ModLauncher 10.0.8+10.0.8+main.0ef7e830 starting: java version 20 by Oracle Corporation; OS Windows 11 arch amd64 version 10.0 [20:41:35] [main/INFO] [mixin/]: SpongePowered MIXIN Subsystem Version=0.8.5 Source=union:/C:/Users/conor/OneDrive/Desktop/CRezate%20server/libraries/org/spongepowered/mixin/0.8.5/mixin-0.8.5.jar%2363!/ Service=ModLauncher Env=SERVER [20:41:35] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\conor\OneDrive\Desktop\CRezate server\libraries\net\minecraftforge\fmlcore\1.19.2-43.2.8\fmlcore-1.19.2-43.2.8.jar is missing mods.toml file [20:41:35] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\conor\OneDrive\Desktop\CRezate server\libraries\net\minecraftforge\javafmllanguage\1.19.2-43.2.8\javafmllanguage-1.19.2-43.2.8.jar is missing mods.toml file [20:41:35] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\conor\OneDrive\Desktop\CRezate server\libraries\net\minecraftforge\lowcodelanguage\1.19.2-43.2.8\lowcodelanguage-1.19.2-43.2.8.jar is missing mods.toml file [20:41:35] [main/WARN] [ne.mi.fm.lo.mo.ModFileParser/LOADING]: Mod file C:\Users\conor\OneDrive\Desktop\CRezate server\libraries\net\minecraftforge\mclanguage\1.19.2-43.2.8\mclanguage-1.19.2-43.2.8.jar is missing mods.toml file [20:41:35] [main/WARN] [ne.mi.ja.se.JarSelector/]: Attempted to select a dependency jar for JarJar which was passed in as source: flywheel. Using Mod File: C:\Users\conor\OneDrive\Desktop\CRezate server\mods\flywheel-forge-1.19.2-0.6.8.a.jar [20:41:35] [main/INFO] [ne.mi.fm.lo.mo.JarInJarDependencyLocator/]: Found 1 dependencies adding them to mods collection [20:41:36] [main/INFO] [mixin/]: Compatibility level set to JAVA_17 [20:41:36] [main/INFO] [cp.mo.mo.LaunchServiceHandler/MODLAUNCHER]: Launching target 'forgeserver' with arguments [] [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/util/Map$Entry (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/util/Map$Entry (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/lang/invoke/MethodHandles$Lookup (java.lang.IllegalArgumentException: Unsupported class file major version 64) [20:41:36] [main/WARN] [mixin/]: Error loading class: java/util/concurrent/CompletableFuture (java.lang.IllegalArgumentException: Unsupported class file major version 64) Exception in thread "main" java.lang.RuntimeException: org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError: An unexpected critical error was encountered         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:32)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.Launcher.run(Launcher.java:106)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.Launcher.main(Launcher.java:77)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23)         at cpw.mods.bootstraplauncher@1.1.2/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) Caused by: org.spongepowered.asm.mixin.transformer.throwables.MixinTransformerError: An unexpected critical error was encountered         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinProcessor.applyMixins(MixinProcessor.java:392)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinTransformer.transformClass(MixinTransformer.java:250)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.service.modlauncher.MixinTransformationHandler.processClass(MixinTransformationHandler.java:131)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.launch.MixinLaunchPluginLegacy.processClass(MixinLaunchPluginLegacy.java:131)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.serviceapi.ILaunchPluginService.processClassWithFlags(ILaunchPluginService.java:156)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.LaunchPluginHandler.offerClassNodeToPlugins(LaunchPluginHandler.java:88)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.ClassTransformer.transform(ClassTransformer.java:120)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.TransformingClassLoader.maybeTransformClassBytes(TransformingClassLoader.java:50)         at cpw.mods.securejarhandler/cpw.mods.cl.ModuleClassLoader.readerToClass(ModuleClassLoader.java:113)         at cpw.mods.securejarhandler/cpw.mods.cl.ModuleClassLoader.lambda$findClass$15(ModuleClassLoader.java:219)         at cpw.mods.securejarhandler/cpw.mods.cl.ModuleClassLoader.loadFromModule(ModuleClassLoader.java:229)         at cpw.mods.securejarhandler/cpw.mods.cl.ModuleClassLoader.findClass(ModuleClassLoader.java:219)         at cpw.mods.securejarhandler/cpw.mods.cl.ModuleClassLoader.loadClass(ModuleClassLoader.java:135)         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)         at java.base/java.lang.Class.getDeclaredMethods0(Native Method)         at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3502)         at java.base/java.lang.Class.getMethodsRecursive(Class.java:3643)         at java.base/java.lang.Class.getMethod0(Class.java:3629)         at java.base/java.lang.Class.getMethod(Class.java:2319)         at MC-BOOTSTRAP/fmlloader@1.19.2-43.2.8/net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$launchService$0(CommonServerLaunchHandler.java:29)         at MC-BOOTSTRAP/cpw.mods.modlauncher@10.0.8/cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30)         Caused by: org.spongepowered.asm.mixin.throwables.ClassMetadataNotFoundException: java.util.concurrent.CompletableFuture         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.transformMethod(MixinPreProcessorStandard.java:754)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.transform(MixinPreProcessorStandard.java:739)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.attach(MixinPreProcessorStandard.java:310)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinPreProcessorStandard.createContextFor(MixinPreProcessorStandard.java:280)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinInfo.createContextFor(MixinInfo.java:1288)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinApplicatorStandard.apply(MixinApplicatorStandard.java:292)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.TargetClassContext.apply(TargetClassContext.java:383)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.TargetClassContext.applyMixins(TargetClassContext.java:365)         at MC-BOOTSTRAP/org.spongepowered.mixin/org.spongepowered.asm.mixin.transformer.MixinProcessor.applyMixins(MixinProcessor.java:363)
    • Hey, guys. I'm trying to play a sound in a mod, which is client-only. It works fine with single-player saves, but when trying to use in on a real multiplayer server (without installing the mod on server of course), I'm getting a crash: Here's the code: Registration: public class Sounds { public static final DeferredRegister<SoundEvent> SOUNDS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, TrovoGration.MOD_ID); public static final RegistryObject<SoundEvent> SOUND_ACTION_NEGATIVE = SOUNDS.register("negative_action", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"negative_action"))); public static final RegistryObject<SoundEvent> SOUND_ACTION_POSITIVE = SOUNDS.register("positive_action", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"positive_action"))); public static final RegistryObject<SoundEvent> SOUND_COOLDOWN_START = SOUNDS.register("cooldown_start", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"cooldown_start"))); public static final RegistryObject<SoundEvent> SOUND_COOLDOWN_END = SOUNDS.register("cooldown_end", () -> new SoundEvent(new ResourceLocation(TrovoGration.MOD_ID,"cooldown_end"))); } Main class constructor: public TrovoGration(){ Sounds.SOUNDS.register(FMLJavaModLoadingContext.get().getModEventBus()); } Play code: LocalPlayer player ... ; player.playSound(Sounds.SOUND_ACTION_NEGATIVE.get(), 0.7f, 1.0f); sounds.json: { "positive_action": { "category": "record", "subtitle": "trovogration.subtitle.positive_action", "sounds": [ "trovogration:positive_action" ] }, "negative_action": { "category": "record", "subtitle": "trovogration.subtitle.negative_action", "sounds": [ "trovogration:negative_action" ] }, "cooldown_start": { "category": "record", "subtitle": "trovogration.subtitle.cooldown_start", "sounds": [ "trovogration:cooldown_start" ] }, "cooldown_end": { "category": "record", "subtitle": "trovogration.subtitle.cooldown_end", "sounds": [ "trovogration:cooldown_end" ] } } Help!!
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.