@@ -19,6 +19,7 @@ import com.demonwav.mcdev.platform.PlatformType
1919import com.demonwav.mcdev.platform.forge.ForgeModuleType
2020import com.demonwav.mcdev.platform.sponge.SpongeModuleType
2121import com.demonwav.mcdev.util.containsAllKeys
22+ import com.demonwav.mcdev.util.filterNotNull
2223import com.demonwav.mcdev.util.mapFirstNotNull
2324import com.demonwav.mcdev.util.runInlineReadAction
2425import com.google.common.collect.HashMultimap
@@ -43,8 +44,8 @@ import javax.swing.Icon
4344class MinecraftFacet (module : Module , name : String , configuration : MinecraftFacetConfiguration , underlyingFacet : Facet <* >? ) :
4445 Facet <MinecraftFacetConfiguration >(facetType, module, name, configuration, underlyingFacet) {
4546
46- private val modules = ConcurrentHashMap <AbstractModuleType <* >, AbstractModule > ()
47- private val roots: HashMultimap <SourceType , VirtualFile > = HashMultimap .create()
47+ private val moduleMap = ConcurrentHashMap <AbstractModuleType <* >, AbstractModule > ()
48+ private val roots: HashMultimap <SourceType , VirtualFile ? > = HashMultimap .create()
4849
4950 init {
5051 configuration.facet = this
@@ -55,10 +56,10 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
5556 }
5657
5758 override fun disposeFacet () {
58- modules .forEach { (_, m) ->
59+ moduleMap .forEach { (_, m) ->
5960 m.dispose()
6061 }
61- modules .clear()
62+ moduleMap .clear()
6263 roots.clear()
6364 }
6465
@@ -78,20 +79,20 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
7879 }
7980
8081 // Remove modules that aren't registered anymore
81- val toBeRemoved = modules .entries.asSequence()
82+ val toBeRemoved = moduleMap .entries.asSequence()
8283 .filter { ! allEnabled.contains(it.key.platformType) }
8384 .onEach { it.value.dispose() }
8485 .map { it.key }
8586 .toHashSet() // CME defense
86- toBeRemoved.forEach { modules .remove(it) }
87+ toBeRemoved.forEach { moduleMap .remove(it) }
8788
8889 // Do this before we register the new modules
8990 updateRoots()
9091
9192 // Add modules which are new
9293 allEnabled
9394 .map { it.type }
94- .filter { ! modules .containsKey(it) }
95+ .filter { ! moduleMap .containsKey(it) }
9596 .forEach(this ::register)
9697
9798 ProjectView .getInstance(module.project).refresh()
@@ -103,7 +104,7 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
103104
104105 rootManager.contentEntries.asSequence()
105106 .flatMap { entry -> entry.sourceFolders.asSequence() }
106- .filter { it.file == null }
107+ .filterNotNull { it.file }
107108 .forEach {
108109 when (it.rootType) {
109110 JavaSourceRootType .SOURCE -> roots.put(SourceType .SOURCE , it.file)
@@ -116,26 +117,24 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
116117
117118 private fun register (type : AbstractModuleType <* >) {
118119 type.performCreationSettingSetup(module.project)
119- modules [type] = type.generateModule(this )
120+ moduleMap [type] = type.generateModule(this )
120121 }
121122
122- @Contract(pure = true )
123- fun getModules (): Collection <AbstractModule > = modules.values
124- @Contract(pure = true )
125- fun getTypes (): Collection <AbstractModuleType <* >> = modules.keys
123+ val modules get() = moduleMap.values
124+ val types get() = moduleMap.keys
126125
127126 @Contract(pure = true )
128- fun isOfType (type : AbstractModuleType <* >) = modules .containsKey(type)
127+ fun isOfType (type : AbstractModuleType <* >) = moduleMap .containsKey(type)
129128
130129 @Contract(pure = true )
131130 fun <T : AbstractModule > getModuleOfType (type : AbstractModuleType <T >): T ? {
132131 @Suppress(" UNCHECKED_CAST" )
133- return modules [type] as ? T
132+ return moduleMap [type] as ? T
134133 }
135134
136135 @Contract(value = " null -> false" , pure = true )
137136 fun isEventClassValidForModule (eventClass : PsiClass ? ) =
138- eventClass != null && modules .values.any { it.isEventClassValid(eventClass, null ) }
137+ eventClass != null && moduleMap .values.any { it.isEventClassValid(eventClass, null ) }
139138
140139 @Contract(pure = true )
141140 fun isEventClassValid (eventClass : PsiClass , method : PsiMethod ): Boolean {
@@ -166,7 +165,7 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
166165 }
167166
168167 private inline fun <T > doIfGood (method : PsiMethod , action : (AbstractModule ) -> T ): T ? {
169- for (abstractModule in modules .values) {
168+ for (abstractModule in moduleMap .values) {
170169 val good = abstractModule.moduleType.listenerAnnotations.any {
171170 method.modifierList.findAnnotation(it) != null
172171 }
@@ -178,28 +177,27 @@ class MinecraftFacet(module: Module, name: String, configuration: MinecraftFacet
178177 return null
179178 }
180179
181- @Contract(pure = true )
182- fun isEventGenAvailable () = modules.keys.any { it.isEventGenAvailable }
180+ val isEventGenAvailable get() = moduleMap.keys.any { it.isEventGenAvailable }
183181
184182 @Contract(pure = true )
185- fun shouldShowPluginIcon (element : PsiElement ? ) = modules.values.any { it.shouldShowPluginIcon(element) }
186-
187- @Contract(pure = true )
188- fun getIcon (): Icon ? {
189- val iconCount = modules.keys.count { it.hasIcon() }
190- return when {
191- iconCount == 0 -> null
192- iconCount == 1 -> modules.keys.firstOrNull { it.hasIcon() }?.icon
193- iconCount == 2 && modules.containsAllKeys(SpongeModuleType , ForgeModuleType ) -> PlatformAssets .SPONGE_FORGE_ICON
194- modules.size > 0 -> PlatformAssets .MINECRAFT_ICON
195- else -> null
183+ fun shouldShowPluginIcon (element : PsiElement ? ) = moduleMap.values.any { it.shouldShowPluginIcon(element) }
184+
185+ val icon: Icon ?
186+ get() {
187+ val iconCount = moduleMap.keys.count { it.hasIcon() }
188+ return when {
189+ iconCount == 0 -> null
190+ iconCount == 1 -> moduleMap.keys.firstOrNull { it.hasIcon() }?.icon
191+ iconCount == 2 && moduleMap.containsAllKeys(SpongeModuleType , ForgeModuleType ) -> PlatformAssets .SPONGE_FORGE_ICON
192+ moduleMap.size > 0 -> PlatformAssets .MINECRAFT_ICON
193+ else -> null
194+ }
196195 }
197- }
198196
199197 fun findFile (path : String , type : SourceType ): VirtualFile ? {
200198 val roots = roots[type]
201199 for (root in roots) {
202- return root.findFileByRelativePath(path) ? : continue
200+ return root? .findFileByRelativePath(path) ? : continue
203201 }
204202 return null
205203 }
0 commit comments