-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from KevinDaGame/feat/litematic_support
Feat/litematic support
- Loading branch information
Showing
13 changed files
with
341 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...iperCore/src/main/java/com/github/kevindagame/util/schematic/DataFolderSchematicReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.github.kevindagame.util.schematic | ||
|
||
import com.github.kevindagame.VoxelSniper | ||
import net.sandrohc.schematic4j.SchematicFormat | ||
import net.sandrohc.schematic4j.SchematicLoader | ||
import net.sandrohc.schematic4j.schematic.Schematic | ||
import java.io.File | ||
import java.util.* | ||
|
||
class DataFolderSchematicReader : ISchematicReader { | ||
override fun getSchematicFile(name: String): File? { | ||
//check for exact name | ||
val exactSchematic = VoxelSniper.voxelsniper.fileHandler.getDataFile(SCHEMATIC_FILE_ROOT_PATH + name) | ||
if (exactSchematic.isFile) { | ||
return exactSchematic | ||
} | ||
|
||
//if no, check for name with any extension | ||
val schematicFiles = VoxelSniper.voxelsniper.fileHandler.getDataFile(SCHEMATIC_FILE_ROOT_PATH).listFiles() | ||
if (schematicFiles != null) { | ||
for (schematic in schematicFiles) { | ||
if (schematic.isFile) { | ||
if (schematic.nameWithoutExtension == name) { | ||
return schematic | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
override fun getSchematicFolder(name: String): File? { | ||
val path = SCHEMATIC_FILE_ROOT_PATH + name | ||
val schematicFolder = VoxelSniper.voxelsniper.fileHandler.getDataFile(path) | ||
if (schematicFolder.isDirectory) { | ||
return schematicFolder | ||
} | ||
return null | ||
} | ||
|
||
override fun readSchematicFile(file: File): VoxelSchematic { | ||
val schematic: Schematic | ||
try { | ||
schematic = SchematicLoader.load(file) | ||
} catch (e: Exception) { | ||
throw IllegalArgumentException("Schematic ${file.name} is not valid") | ||
} | ||
|
||
val voxelSchematicBuilder = VoxelSchematicBuilder() | ||
|
||
voxelSchematicBuilder.name = file.nameWithoutExtension | ||
|
||
schematic.blocks().forEach { block -> | ||
voxelSchematicBuilder.addBlock( | ||
block.left.x.toDouble(), | ||
block.left.y.toDouble(), | ||
block.left.z.toDouble(), | ||
block.right | ||
) | ||
} | ||
return voxelSchematicBuilder.build() | ||
} | ||
|
||
override fun readSchematicFolder(folder: File): List<VoxelSchematic> { | ||
val schematics = mutableListOf<VoxelSchematic>() | ||
|
||
val schematicFiles = folder.listFiles() ?: throw IllegalStateException("Folder $folder does not exist") | ||
|
||
for (schematicFile in schematicFiles) { | ||
if (schematicFile.isFile) { | ||
|
||
schematics.add(readSchematicFile(schematicFile)) | ||
} | ||
|
||
} | ||
return schematics | ||
} | ||
|
||
override fun getPossibleSchematicNames(): List<String> { | ||
val schematics = mutableListOf<String>() | ||
val schematicsFolder = VoxelSniper.voxelsniper.fileHandler.getDataFile(SCHEMATIC_FILE_ROOT_PATH) | ||
if (schematicsFolder.exists()) { | ||
val files = schematicsFolder.listFiles() | ||
if (files != null) { | ||
for (file in files) { | ||
if (file.isFile) { | ||
schematics.add(file.nameWithoutExtension) | ||
} else if (file.isDirectory) { | ||
if (file.listFiles()?.any { SCHEMATIC_VALID_EXTENSIONS.contains(it.extension) } == true) { | ||
schematics.add(file.name) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return schematics | ||
} | ||
|
||
companion object { | ||
const val SCHEMATIC_FILE_ROOT_PATH = "schematics/" | ||
|
||
val SCHEMATIC_VALID_EXTENSIONS: List<String> = Arrays.stream(SchematicFormat.values()) | ||
.map { f: SchematicFormat -> f.fileExtension } | ||
.distinct() | ||
.toList() | ||
|
||
fun initialize() { | ||
val schematicsFolder = VoxelSniper.voxelsniper.fileHandler.getDataFile("schematics") | ||
if (!schematicsFolder.exists()) { | ||
schematicsFolder.mkdir() | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
VoxelSniperCore/src/main/java/com/github/kevindagame/util/schematic/ISchematicReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.github.kevindagame.util.schematic | ||
|
||
import java.io.File | ||
|
||
interface ISchematicReader { | ||
fun getSchematicFile(name: String): File? | ||
fun getSchematicFolder(name: String): File? | ||
|
||
fun readSchematicFile(file: File): VoxelSchematic? | ||
fun readSchematicFolder(folder: File): List<VoxelSchematic> | ||
fun getPossibleSchematicNames(): List<String> | ||
} |
122 changes: 0 additions & 122 deletions
122
VoxelSniperCore/src/main/java/com/github/kevindagame/util/schematic/SchematicReader.kt
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
VoxelSniperCore/src/main/java/com/github/kevindagame/util/schematic/VoxelSchematicLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.kevindagame.util.schematic | ||
|
||
class VoxelSchematicLoader(private val schematicReader: ISchematicReader) { | ||
/** | ||
* Reads a schematic from a file or a folder. | ||
* check if there is a folder with the name. | ||
* If there is a folder with that name, then it will read all schematics in that folder. | ||
* If there are no schematics in that folder, then it will throw an IllegalArgumentException. | ||
* If there is no folder with that name, then it will check if there is a file with the <name>.schem. | ||
* If there is a file with the <name>.schem, then it will read the file. | ||
* If there is no file with the <name>.schem, then it will throw an IllegalArgumentException. | ||
* | ||
* @param name The name of the schematic or the folder. | ||
* @return A list of schematics. | ||
* @throws IllegalArgumentException If there is no file with the <name>.schem or no folder with that name. | ||
*/ | ||
fun gatherSchematics(name: String): List<VoxelSchematic> { | ||
val schematicFolder = schematicReader.getSchematicFolder(name) | ||
if (schematicFolder != null) { | ||
val schematics = schematicReader.readSchematicFolder(schematicFolder) | ||
if (schematics.isEmpty()) { | ||
throw IllegalArgumentException("Folder $name does not contain any schematics") | ||
} | ||
return schematics | ||
} | ||
|
||
val schematicFile = schematicReader.getSchematicFile(name) | ||
if (schematicFile != null) { | ||
val schematic = schematicReader.readSchematicFile(schematicFile) | ||
?: throw IllegalArgumentException("Schematic $name is not valid") | ||
|
||
return listOf(schematic) | ||
} | ||
|
||
throw IllegalArgumentException("No schematic file or folder with name $name") | ||
} | ||
|
||
fun getSchematicNamesForAutoComplete(): List<String> { | ||
return schematicReader.getPossibleSchematicNames() | ||
} | ||
} |
Oops, something went wrong.