Primitive static analysis for Forge mods
- Warn when using client-/server-only functionality, when not annotated with
@SideOnly
.
public class ExampleItem extends Item {
@SideOnly(Side.CLIENT)
private RenderItem renderItem = Minecraft.getMinecraft().getRenderItem();
// ^
// warning: Using a CLIENT class, but this could be either server or client
@Nonnull
@Override
public String getUnlocalizedName(ItemStack stack) {
if (hasEffect(stack)) {
// ^
// warning: Using a CLIENT method, but this could be either server or client
return "item." + getUnlocalizedName() + ".effect";
} else {
return "item." + getUnlocalizedName() + ".no_effect";
}
}
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
if (world.isRemote) {
world.spawnParticle(EnumParticleTypes.CRIT_MAGIC, false, player.posX, player.posY, player.posZ, 0, 0, 0);
// No warning: world.isRemote acts as a guard
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
@Override
public boolean shouldRotateAroundWhenRendering() {
// ^
// Note: Overriding a CLIENT method, but no @SideOnly annotation
return true;
}
}
Add this to your build.gradle
:
repositories { maven { url 'https://dl.bintray.com/squiddev/maven' } }
dependencies { provided "org.squiddev:forgelint:0.1.0" }
compileJava { options.compilerArgs += ["-Xplugin:ForgeLint"] }
- Detect non-server-thread manipulation of the world.
- Smarter control flow handling, meaning we don't require
@SideOnly
on private methods.
The MinecraftDev plugin also provides a sidedness checker. The plugin is slightly more lenient than ForgeLint in some of its warnings, but is integrated into the editor which provides a nicer experience.