[Editor] Enable generating constant with UID in MapHandler classes

This commit is contained in:
2021-11-23 23:40:15 +01:00
parent 2337dc98bf
commit f576d8fc1b
3 changed files with 15 additions and 4 deletions

View File

@@ -30,7 +30,12 @@ class JavaClassService {
fun ofPath(path: String): String = ofPath(Path.of(path))
fun ofPath(path: Path): String = path.joinToString(".")
fun createClassFile(name: String, directory: FileSystemNode, classTemplate: String) {
fun createClassFile(
name: String,
directory: FileSystemNode,
classTemplate: String,
inflate: (MutableMap<String, String>) -> Unit = {}
) {
projectContext.project?.let { project ->
val template = config.getTemplate(classTemplate)
@@ -42,7 +47,7 @@ class JavaClassService {
project.codeFSNode.createNode(classFile.toRelativeString(project.codeFSNode.file))
val model = mapOf("className" to className, "package" to classPackage)
val model = mutableMapOf("className" to className, "package" to classPackage).apply(inflate).toMap()
template.process(model, classFile.writer())
}
}

View File

@@ -39,6 +39,7 @@ import tornadofx.setValue
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
@Component
class DefaultProjectContext : ProjectContext {
@@ -102,7 +103,11 @@ class DefaultProjectContext : ProjectContext {
it.maps += asset
save()
javaClassService.createClassFile(map.handler, it.codeFSNode, "map_handler.ftl")
javaClassService.createClassFile(map.handler, it.codeFSNode, "map_handler.ftl") { model ->
model["mapUid"] = uid
}
File(it.mapsDirectory, asset.source).outputStream().use { fos -> mapSerializer.serialize(map, fos) }
}
}
@@ -250,7 +255,7 @@ class DefaultProjectContext : ProjectContext {
override fun loadScript(fileNode: FileNode, execute: ((String) -> Unit)?, saveable: Boolean): Code {
val typeProperty = SimpleObjectProperty<CodeType>().apply {
bind(createObjectBinding({
when (fileNode.extension.toLowerCase()) {
when (fileNode.extension.lowercase(Locale.getDefault())) {
"java" -> CodeType.JAVA
"xml" -> CodeType.XML
"sql" -> CodeType.SQL

View File

@@ -7,6 +7,7 @@ import com.bartlomiejpluta.base.api.map.handler.MapHandler;
import com.bartlomiejpluta.base.api.screen.Screen;
public class ${className} implements MapHandler {
public static final String UID = "${mapUid}";
@Override
public void onCreate(Context context, GameMap map) {