Skip to content

Commit a538dc7

Browse files
committed
Detect NBT files automatically based on content rather than extension
1 parent 1d07d88 commit a538dc7

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

src/main/kotlin/com/demonwav/mcdev/nbt/filetype/NbtFileType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import com.intellij.openapi.vfs.VirtualFile
1717
object NbtFileType : FileType {
1818
override fun getDefaultExtension() = "nbt"
1919
override fun getIcon() = PlatformAssets.MINECRAFT_ICON
20-
override fun getCharset(file: VirtualFile, content: ByteArray) = null
20+
override fun getCharset(file: VirtualFile, content: ByteArray): String? = null
2121
override fun getName() = "NBT"
2222
override fun getDescription() = "Named Binary Tag"
2323
override fun isBinary() = true
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Minecraft Dev for IntelliJ
3+
*
4+
* https://minecraftdev.org
5+
*
6+
* Copyright (c) 2017 minecraft-dev
7+
*
8+
* MIT License
9+
*/
10+
11+
package com.demonwav.mcdev.nbt.filetype
12+
13+
import com.demonwav.mcdev.nbt.MalformedNbtFileException
14+
import com.demonwav.mcdev.nbt.Nbt
15+
import com.intellij.openapi.fileTypes.FileType
16+
import com.intellij.openapi.fileTypes.FileTypeRegistry
17+
import com.intellij.openapi.util.io.ByteSequence
18+
import com.intellij.openapi.vfs.VirtualFile
19+
import java.io.IOException
20+
21+
class NbtFileTypeDetector : FileTypeRegistry.FileTypeDetector {
22+
override fun getVersion() = 1
23+
24+
override fun detect(file: VirtualFile, firstBytes: ByteSequence, firstCharsIfText: CharSequence?): FileType? {
25+
return try {
26+
Nbt.buildTagTree(file.inputStream, 100)
27+
NbtFileType
28+
} catch (e: MalformedNbtFileException) {
29+
null
30+
} catch (e: IOException) {
31+
null
32+
}
33+
}
34+
}

src/main/kotlin/com/demonwav/mcdev/nbt/lang/NbttFileType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ object NbttFileType : LanguageFileType(NbttLanguage) {
1717
override fun getIcon() = PlatformAssets.MINECRAFT_ICON
1818
override fun getName() = "NBTT"
1919
override fun getDefaultExtension() = "nbtt"
20-
override fun getDescription() = "NBT Text Representation"
20+
override fun getDescription() = "NBT Text Representation (don't use this one)"
2121
}

src/main/kotlin/com/demonwav/mcdev/util/AbstractProjectComponent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ package com.demonwav.mcdev.util
1313
import com.intellij.openapi.components.ProjectComponent
1414
import com.intellij.openapi.project.Project
1515

16-
abstract class AbstractProjectComponent(var projectRef: Project?) : ProjectComponent {
16+
abstract class AbstractProjectComponent(private var projectRef: Project?) : ProjectComponent {
1717

1818
protected val project
1919
get() = projectRef!!
2020

2121
override fun getComponentName(): String = javaClass.name
2222

23-
override final fun disposeComponent() {
23+
final override fun disposeComponent() {
2424
projectRef = null
2525
}
2626

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@
8181
groupId="language"
8282
instance="com.demonwav.mcdev.MinecraftConfigurable"/>
8383

84+
<!-- NBT -->
8485
<fileTypeFactory implementation="com.demonwav.mcdev.nbt.filetype.NbtFileTypeFactory"/>
86+
<fileTypeDetector implementation="com.demonwav.mcdev.nbt.filetype.NbtFileTypeDetector"/>
8587
<fileEditorProvider implementation="com.demonwav.mcdev.nbt.editor.NbtFileEditorProvider" />
8688

8789
<!-- Text representation of NBT -->

0 commit comments

Comments
 (0)