Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/litematic support #233

Merged
merged 14 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import com.github.kevindagame.snipe.SnipeData
import com.github.kevindagame.util.Messages
import com.github.kevindagame.util.VoxelMessage
import com.github.kevindagame.util.brushOperation.BlockOperation
import com.github.kevindagame.util.schematic.SchematicReader
import com.github.kevindagame.util.schematic.VoxelSchematic
import com.github.kevindagame.util.schematic.VoxelSchematicBlock
import com.github.kevindagame.util.schematic.*
import com.github.kevindagame.voxelsniper.location.BaseLocation
import com.github.kevindagame.voxelsniper.material.VoxelMaterial

class SchematicBrush : AbstractBrush() {
private val schematicLoader: VoxelSchematicLoader
private lateinit var schematicName: String
private lateinit var schematics: List<VoxelSchematic>
private var mode = PasteMode.FULL
private var rotation = RotateMode.DEGREES_0
private var flip = FlipMode.NONE

init {
val schematicReader: ISchematicReader = DataFolderSchematicReader()
this.schematicLoader = VoxelSchematicLoader(schematicReader)
}
override fun info(vm: VoxelMessage) {
vm.brushName(this.name)
vm.custom(Messages.CHOSEN_SCHEMATIC.replace("%schematics%", if(this::schematics.isInitialized) schematics.joinToString(", ") { it.name } else "None"))
Expand Down Expand Up @@ -148,13 +151,13 @@ class SchematicBrush : AbstractBrush() {
if (params.isNotEmpty()) {
when (params[0]) {
"list" -> {
v.sendMessage(Messages.SCHEMATIC_LIST.replace("%schematics%", SchematicReader.getPossibleNames().joinToString(", ")))
v.sendMessage(Messages.SCHEMATIC_LIST.replace("%schematics%", schematicLoader.getSchematicNamesForAutoComplete().joinToString(", ")))
}

"schem" -> {
if (params.size > 1) {
try {
val schematics = SchematicReader.read(params[1])
val schematics = schematicLoader.gatherSchematics(params[1])
this.schematicName = params[1]
this.schematics = schematics
if (schematics.size == 1) {
Expand All @@ -163,7 +166,7 @@ class SchematicBrush : AbstractBrush() {
v.sendMessage(Messages.SCHEMATIC_LOADED_MULTIPLE.replace("%schematics%", this.schematics.joinToString(", ") { it.name }))
}
} catch (e: IllegalArgumentException) {
v.sendMessage(Messages.INVALID_BRUSH_PARAM)
v.sendMessage(Messages.FILE_LOAD_FAIL.replace("%exception.getMessage%", e.message ?: "Unknown error"))
KevinDaGame marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -202,6 +205,10 @@ class SchematicBrush : AbstractBrush() {
}
}

else -> {
v.sendMessage(Messages.INVALID_BRUSH_PARAM)
}

}
}
}
Expand All @@ -212,7 +219,7 @@ class SchematicBrush : AbstractBrush() {

override fun registerArgumentValues(): HashMap<String, List<String>> {
return hashMapOf(
"schem" to SchematicReader.getPossibleNames(),
"schem" to schematicLoader.getSchematicNamesForAutoComplete(),
"rotate" to RotateMode.values().map { it.name.lowercase().replace("degrees_", "") },
"flip" to FlipMode.values().map { it.name.lowercase() },
"mode" to PasteMode.values().map { it.name.lowercase() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public VoxelCommand(String name) {

public boolean execute(IPlayer player, String[] args) {
if (getPermission() == null || getPermission().isEmpty() || player.hasPermission(getPermission())) {
return doCommand(player, args);
try {
return doCommand(player, args);
} catch (Exception e) {
Lennart99 marked this conversation as resolved.
Show resolved Hide resolved
e.printStackTrace();
player.sendMessage(Messages.COMMAND_ERROR);
return true;
}
} else {
player.sendMessage(Messages.NO_PERMISSION_MESSAGE.replace("%permission%", getPermission()));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public enum Messages implements ComponentLike {
SCHEMATIC_SET_ROTATION,
SCHEMATIC_SET_FLIP,
SCHEMATIC_SET_MODE,
;
COMMAND_ERROR;
//</editor-fold>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.github.kevindagame.util.schematic

import com.github.kevindagame.VoxelSniper
import net.sandrohc.schematic4j.SchematicLoader
import net.sandrohc.schematic4j.schematic.Schematic
import java.io.File

class DataFolderSchematicReader : ISchematicReader {
override fun getSchematicFile(name: String): File? {
//check for exact name
val exactSchematic = VoxelSniper.voxelsniper.fileHandler.getDataFile(SCHEMATIC_FILE_ROOT_PATH + name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to use new File(baseDirectory, file); or simular.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. Until now we have used the file handler everywhere. Making a file directly would require getting the full path to the plugin root, which is extra work

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I am not sure if using '/' is platform dependent or not..

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't https://github.com/KevinDaGame/VoxelSniper-Reimagined/blob/master/VoxelSniperCore/src/main/java/com/github/kevindagame/util/Messages.java#L383C92-L383C92

That's more an issue on that part. I prefer to leave this file logic to the main file handler. It might be a good idea to refactor this in other places

Copy link
Collaborator

@Lennart99 Lennart99 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this would be a nice option, we could add/change a method in filehandler File getDataFile(String... path), which could take all path 'parts', without having to concatenate the strings, which I think is not the way it is meant to be done in Java (could be wrong here, but most examples either use File or Paths).

    default File getDataFile(String... path) {
        File f = getDataFolder();
        for (String p : path) {
            f = new File(f, p);
        }
        return f;
    }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

INFO: Why don't you like the way I'm using it? I'd like to stay away from the File constructor wherever possible, to prevent divergence later on.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think though that the way we access the filehandler isn't nice. Perhabs this could be solved using dependency injection

Copy link
Collaborator

@Lennart99 Lennart99 Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will approve the PR, since the same issue was already present before I think, but we should indeed look into improving this.
I feel like we should, if we want to be able to change the implementation, supply every part of the path to the filehandler separately, so that it can traverse the filesystem in whatever way it needs to.

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
Lennart99 marked this conversation as resolved.
Show resolved Hide resolved
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

for (y in 0 until schematic.height()) {
KevinDaGame marked this conversation as resolved.
Show resolved Hide resolved
for (x in 0 until schematic.width()) {
for (z in 0 until schematic.length()) {

val block = schematic.block(x, y, z)
voxelSchematicBuilder.addBlock(x.toDouble(), y.toDouble(), z.toDouble(), block)
}
}
}
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 { it.extension == "schem" } == true) {
KevinDaGame marked this conversation as resolved.
Show resolved Hide resolved
schematics.add(file.name)
}
}
}
}
}
return schematics
}

companion object {
const val SCHEMATIC_FILE_ROOT_PATH = "schematics/"

fun initialize() {
val schematicsFolder = VoxelSniper.voxelsniper.fileHandler.getDataFile("schematics")
if (!schematicsFolder.exists()) {
schematicsFolder.mkdir()
}
}
}
}
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>
}

This file was deleted.

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()
Lennart99 marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 2 additions & 1 deletion VoxelSniperCore/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -603,4 +603,5 @@ SCHEMATIC_LOADED_ONE: "<aqua>Loaded schematic:</aqua> %schematic%"
SCHEMATIC_LOADED_MULTIPLE: "<aqua>Loaded schematics:</aqua> %schematics%"
SCHEMATIC_SET_ROTATION: "<aqua>Rotation set to:</aqua> %rotation% degrees"
SCHEMATIC_SET_FLIP: "<aqua>Flip set to:</aqua> %flip%"
SCHEMATIC_SET_MODE: "<aqua>Paste mode set to:</aqua> %mode%"
SCHEMATIC_SET_MODE: "<aqua>Paste mode set to:</aqua> %mode%"
COMMAND_ERROR: "<red>Something went wrong while executing this command</red>"
Loading