Skip to content

Commit f45da16

Browse files
committed
Mod ID mismatch inspection most basic form, fabric only
1 parent 099e295 commit f45da16

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Minecraft Development for IntelliJ
3+
*
4+
* https://mcdev.io/
5+
*
6+
* Copyright (C) 2025 minecraft-dev
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published
10+
* by the Free Software Foundation, version 3.0 only.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package com.demonwav.mcdev.platform.fabric.inspection
22+
23+
import com.demonwav.mcdev.util.constantStringValue
24+
import com.intellij.codeInspection.LocalInspectionTool
25+
import com.intellij.codeInspection.ProblemsHolder
26+
import com.intellij.json.psi.JsonFile
27+
import com.intellij.json.psi.JsonObject
28+
import com.intellij.json.psi.JsonStringLiteral
29+
import com.intellij.openapi.project.Project
30+
import com.intellij.psi.JavaElementVisitor
31+
import com.intellij.psi.PsiElementVisitor
32+
import com.intellij.psi.PsiField
33+
import com.intellij.psi.PsiManager
34+
import com.intellij.psi.search.FilenameIndex
35+
import com.intellij.psi.search.GlobalSearchScope
36+
37+
class ModIdMismatchInspection : LocalInspectionTool() {
38+
39+
override fun getStaticDescription() = "Checks for a mismatch between the mod id in fabric.mod.json and the java code."
40+
41+
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = Visitor(holder)
42+
43+
private class Visitor(private val holder: ProblemsHolder) : JavaElementVisitor() {
44+
45+
override fun visitField(field: PsiField) {
46+
super.visitField(field)
47+
48+
if (field.name != "MOD_ID" && field.name != "MODID") return
49+
val initializer = field.initializer
50+
val javaModId = initializer?.constantStringValue ?: return
51+
52+
val project = field.project
53+
val jsonModId = getModIdFromJson(project, field.manager) ?: return
54+
55+
if (javaModId != jsonModId) {
56+
holder.registerProblem(
57+
initializer,
58+
"Mod ID '$javaModId' does not match mod id '$jsonModId' from fabric.mod.json"
59+
)
60+
}
61+
}
62+
63+
private fun getModIdFromJson(project: Project, manager: PsiManager): String? {
64+
val files = FilenameIndex.getVirtualFilesByName(
65+
"fabric.mod.json",
66+
GlobalSearchScope.projectScope(project)
67+
)
68+
69+
val file = files.firstOrNull() ?: return null
70+
71+
val jsonFile = manager.findFile(file) as? JsonFile ?: return null
72+
val topLevelObj = jsonFile.topLevelValue as? JsonObject ?: return null
73+
74+
val stringLiteral = topLevelObj.findProperty("id")?.value as? JsonStringLiteral ?: return null
75+
return stringLiteral.value
76+
}
77+
}
78+
}

src/main/resources/META-INF/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,14 @@
991991
level="ERROR"
992992
hasStaticDescription="true"
993993
implementationClass="com.demonwav.mcdev.platform.fabric.inspection.FabricEntrypointsInspection"/>
994+
<localInspection displayName="Fabric mod ID mismatch"
995+
shortName="ModIdMismatch"
996+
groupName="Fabric"
997+
language="JAVA"
998+
enabledByDefault="true"
999+
level="WARNING"
1000+
hasStaticDescription="true"
1001+
implementationClass="com.demonwav.mcdev.platform.fabric.inspection.ModIdMismatchInspection"/>
9941002
<!--endregion-->
9951003

9961004
<!--region MCP INSPECTIONS-->

0 commit comments

Comments
 (0)