I'm modding Minecraft and I imported a mod jar as dependency. However, the compiler prompted error since the class from the dependency mod I invoked method of implemented interfaces from other mods.
In my code, I invoked a method of a class from the dependency jar. There are some simplified code that demonstrate what the code is like.
// From my mod
public class MachineIgniterListener {
public void onPlayerInteract(PlayerInteractEvent event) {
BaseMetaTileEntity tileEntity = getMachineInEventIfItIs(event);
if (tileEntity == null) return;
tileEntity.doEnergyExplosion();
}
}
where BaseMetaTileEntity
comes from the dependency mod, and it implements several interfaces from other mods
// From dependency mod
public class BaseMetaTileEntity extends CommonMetaTileEntity
implements IActionHost, IAlignmentProvider, IConstructableProvider... {
public void doEnergyExplosion() {
// do some explosion
}
}
IActionHost
, IAlignmentProvider
, and other interfaces are from other mods which I haven't include in the classpath, resulting in the compiler complaining
tileEntity.doEnergyExplosion();
^
class file for appeng.api.networking.security.IActionHost not found
I think, in terms of JVM bytecode, the code does not require knowing what interfaces the class implements to compile (which I might be wrong about it), so I'm expecting the code compiles without the depended jars added to the classpath. However, the compiler requires all the jars to be present. Since I'm coding in legacy versions of Minecraft, finding some mod that it requires can be very tidious.
I used Gradle to include the jar file in my project. Since I'm new to Gradle, I tried different kinds of dependency configurations, and none of them solved the problem.
dependencies {
// compile files("dependency_mod.jar")
// implementation files("dependency_mod.jar")
// api files("dependency_mod.jar")
// runtimeOnly files("dependency_mod.jar") I can't even refer to the classes with this one
// Yes I tried lol
compileOnly files("dependency_mod.jar")
}
I wonder if there are ways to make the code compile without adding all these jars to the dependencies. Am I doing wrong with the Gradle settings? Or there's no other ways around than adding the jars?
Also, if this is relevant, I'm using IntelliJ with Gradle 6.9.1, Java 8, and FalsePattern / ExampleMod1.7.10 modding toolchain template
See also: A question described that resulted in expected behavior - Java Compiler transistive type checking behaviour