Skip to content

Commit 6dddbeb

Browse files
committed
Add basic setup for JSON validation through schemas
1 parent f483d38 commit 6dddbeb

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.json
12+
13+
import com.demonwav.mcdev.util.resourceDomain
14+
import com.demonwav.mcdev.util.resourcePath
15+
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.vfs.VirtualFile
17+
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
18+
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
19+
import com.jetbrains.jsonSchema.extension.SchemaType
20+
21+
class SchemaProviderFactory : JsonSchemaProviderFactory {
22+
override fun getProviders(project: Project) =
23+
listOf(SoundsSchemaProvider(), BlockstatesSchemaProvider())
24+
}
25+
26+
class SoundsSchemaProvider : JsonSchemaFileProvider {
27+
companion object {
28+
val FILE = JsonSchemaProviderFactory.getResourceFile(SchemaProviderFactory::class.java, "/jsonSchemas/sounds.schema.json")
29+
}
30+
31+
override fun getName() = "Minecraft Sounds JSON"
32+
33+
override fun isAvailable(project: Project, file: VirtualFile) = file.resourceDomain != null && file.resourcePath == "sounds.json"
34+
35+
override fun getSchemaType(): SchemaType = SchemaType.embeddedSchema
36+
37+
override fun getSchemaFile(): VirtualFile = FILE
38+
}
39+
40+
class BlockstatesSchemaProvider : JsonSchemaFileProvider {
41+
companion object {
42+
val FILE = JsonSchemaProviderFactory.getResourceFile(SchemaProviderFactory::class.java, "/jsonSchemas/blockstates.schema.json")
43+
}
44+
45+
override fun getName() = "Minecraft Blockstates JSON"
46+
47+
override fun isAvailable(project: Project, file: VirtualFile) = file.resourceDomain != null && file.resourcePath?.startsWith("blockstates/") == true
48+
49+
override fun getSchemaType(): SchemaType = SchemaType.embeddedSchema
50+
51+
override fun getSchemaFile(): VirtualFile = FILE
52+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ val VirtualFile.manifest: Manifest?
3030

3131
operator fun Manifest.get(attribute: String): String? = mainAttributes.getValue(attribute)
3232
operator fun Manifest.get(attribute: Attributes.Name): String? = mainAttributes.getValue(attribute)
33+
34+
private val DOMAIN_PATTERN = Regex("^.*?/assets/(.*?)/(.*?)\$")
35+
36+
val VirtualFile.resourceDomain: String?
37+
get() = DOMAIN_PATTERN.matchEntire(this.path)?.groupValues?.get(1)
38+
39+
val VirtualFile.resourcePath: String?
40+
get() = DOMAIN_PATTERN.matchEntire(this.path)?.groupValues?.get(2)

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

+4
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,10 @@
629629
<projectResolve implementation="com.demonwav.mcdev.platform.forge.gradle.ForgePatcherProjectResolverExtension"/>
630630
</extensions>
631631

632+
<extensions defaultExtensionNs="JavaScript.JsonSchema">
633+
<ProviderFactory implementation="com.demonwav.mcdev.json.SchemaProviderFactory"/>
634+
</extensions>
635+
632636
<application-components>
633637
</application-components>
634638

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "Minecraft Blockstates JSON",
4+
"definitions": {
5+
"variantDefinition": {
6+
"type": "object",
7+
"additionalProperties": {
8+
"oneOf": [
9+
{
10+
"type": "object",
11+
"additionalProperties": { "$ref": "#/definitions/variantObject" }
12+
},
13+
{
14+
"type": "array",
15+
"items": { "$ref": "#/definitions/variantObject" }
16+
}
17+
]
18+
}
19+
},
20+
"variantObject": {
21+
"type": "object",
22+
"properties": {
23+
"__comment": {
24+
"type": "string"
25+
},
26+
"model": { "type": "string" },
27+
"textures": {
28+
"type": "object",
29+
"additionalProperties": {
30+
"type": "string"
31+
}
32+
}
33+
},
34+
"additionalProperties": false
35+
}
36+
},
37+
"oneOf": [
38+
{
39+
"type": "object",
40+
"properties": {
41+
"__comment": {
42+
"type": "string"
43+
},
44+
"variants": {
45+
"type": "object",
46+
"additionalProperties": { "$ref": "#/definitions/variantObject" }
47+
}
48+
},
49+
"additionalProperties": false,
50+
"required": [ "variants" ],
51+
"not": { "required": [ "forge_marker" ] }
52+
},
53+
{
54+
"type": "object",
55+
"properties": {
56+
"__comment": {
57+
"type": "string"
58+
},
59+
"forge_marker": {
60+
"type": "integer",
61+
"enum": [ 1 ]
62+
},
63+
"variants": { "$ref": "#/definitions/variantDefinition" },
64+
"defaults": { "$ref": "#/definitions/variantObject" }
65+
},
66+
"additionalProperties": false,
67+
"required": [ "forge_marker", "variants" ]
68+
}
69+
]
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "Minecraft Sounds JSON",
4+
"type": "object",
5+
"additionalProperties": {
6+
"type": "object",
7+
"properties": {
8+
"category": {
9+
"type": "string"
10+
},
11+
"replace": {
12+
"type": "boolean"
13+
},
14+
"subtitle": {
15+
"type": "string"
16+
},
17+
"sounds": {
18+
"type": "array",
19+
"items": {
20+
"oneOf": [
21+
{
22+
"type": "string"
23+
},
24+
{
25+
"type": "object",
26+
"properties": {
27+
"name": {
28+
"type": "string"
29+
},
30+
"volume": {
31+
"type": "number",
32+
"minimum": 0,
33+
"maximum": 1,
34+
"default": 1
35+
},
36+
"pitch": {
37+
"type": "number",
38+
"default": 1
39+
},
40+
"weight": {
41+
"type": "number",
42+
"default": 1
43+
},
44+
"stream": {
45+
"type": "boolean",
46+
"default": false
47+
},
48+
"type": {
49+
"enum": [ "sound", "event" ],
50+
"default": "sound"
51+
}
52+
}
53+
}
54+
]
55+
},
56+
"uniqueItems": true
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)