cli: vec2, vec3 parser

This commit is contained in:
Bixilon 2022-05-21 23:46:44 +02:00
parent 975c3002f1
commit ae7e3082c7
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
14 changed files with 458 additions and 4 deletions

View File

@ -19,6 +19,8 @@ import de.bixilon.minosoft.commands.parser.brigadier._int.IntParser
import de.bixilon.minosoft.commands.parser.brigadier._long.LongParser
import de.bixilon.minosoft.commands.parser.brigadier.bool.BooleanParser
import de.bixilon.minosoft.commands.parser.brigadier.string.StringParser
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec2.Vec2Parser
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec3.Vec3Parser
import de.bixilon.minosoft.commands.parser.minecraft.range.RangeParserFactory
import de.bixilon.minosoft.commands.parser.minecraft.range._float.FloatRangeParser
import de.bixilon.minosoft.commands.parser.minecraft.range._int.IntRangeParser
@ -50,6 +52,9 @@ object ArgumentParserFactories : DefaultFactory<ArgumentParserFactory<*>>(
ResourceLocationParser,
TimeParser,
Vec2Parser,
Vec3Parser,
DummyParser,
ScoreHolderParser,
@ -60,8 +65,6 @@ object ArgumentParserFactories : DefaultFactory<ArgumentParserFactory<*>>(
minecraft:game_profile
minecraft:block_pos
minecraft:column_pos
minecraft:vec3
minecraft:vec2
minecraft:block_state
minecraft:block_predicate
minecraft:item_stack

View File

@ -0,0 +1,19 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate
data class Coordinate(
val relatives: CoordinateRelatives,
val offset: Float,
)

View File

@ -0,0 +1,48 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate
import de.bixilon.minosoft.commands.parser.brigadier._float.FloatParser.Companion.readFloat
import de.bixilon.minosoft.commands.util.CommandReader
object CoordinateParserUtil {
fun CommandReader.readCoordinateOrNull(): Coordinate? {
val peek = peek() ?: return null
val type = when (peek) {
'~'.code -> {
read()
CoordinateRelatives.TILDE
}
'^'.code -> {
read()
CoordinateRelatives.CARET
}
else -> CoordinateRelatives.NONE
}
val offset = if (peekWhitespaces() == 0) readFloat() else null
if (offset == null && type == CoordinateRelatives.NONE) {
return null
}
return Coordinate(type, offset ?: 0.0f)
}
fun CommandReader.readCoordinate(): Coordinate {
readResult { readCoordinateOrNull() }.let { return it.result ?: throw InvalidCoordinateError(this, it) }
}
}

View File

@ -0,0 +1,21 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate
enum class CoordinateRelatives {
NONE,
TILDE,
CARET,
;
}

View File

@ -0,0 +1,23 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate
import de.bixilon.minosoft.commands.errors.parser.ParserError
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.commands.util.ReadResult
class InvalidCoordinateError(
reader: CommandReader,
result: ReadResult<Coordinate?>,
) : ParserError(reader, result)

View File

@ -0,0 +1,21 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec2
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.Coordinate
data class Vec2Coordinate(
val x: Coordinate,
val y: Coordinate,
)

View File

@ -0,0 +1,42 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec2
import de.bixilon.minosoft.commands.parser.ArgumentParser
import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.CoordinateParserUtil.readCoordinate
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.util.KUtil.toResourceLocation
object Vec2Parser : ArgumentParser<Vec2Coordinate>, ArgumentParserFactory<Vec2Parser> {
override val RESOURCE_LOCATION: ResourceLocation = "minecraft:vec2".toResourceLocation()
override val examples: List<Any> = listOf("~ ~ ", "^ ^", "5 5")
override val placeholder = ChatComponent.of("<vec2>")
override fun parse(reader: CommandReader): Vec2Coordinate {
return Vec2Coordinate(reader.readCoordinate(), reader.readCoordinate())
}
override fun getSuggestions(reader: CommandReader): List<Any> {
if (reader.readString()?.isBlank() != false) {
return examples
}
return emptyList()
}
override fun read(buffer: PlayInByteBuffer) = this
}

View File

@ -0,0 +1,22 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec3
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.Coordinate
data class Vec3Coordinate(
val x: Coordinate,
val y: Coordinate,
val z: Coordinate,
)

View File

@ -0,0 +1,42 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec3
import de.bixilon.minosoft.commands.parser.ArgumentParser
import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.CoordinateParserUtil.readCoordinate
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.util.KUtil.toResourceLocation
object Vec3Parser : ArgumentParser<Vec3Coordinate>, ArgumentParserFactory<Vec3Parser> {
override val RESOURCE_LOCATION: ResourceLocation = "minecraft:vec3".toResourceLocation()
override val examples: List<Any> = listOf("~ ~ ~", "^ ^ ^", "5 5 5")
override val placeholder = ChatComponent.of("<vec3>")
override fun parse(reader: CommandReader): Vec3Coordinate {
return Vec3Coordinate(reader.readCoordinate(), reader.readCoordinate(), reader.readCoordinate())
}
override fun getSuggestions(reader: CommandReader): List<Any> {
if (reader.readString()?.isBlank() != false) {
return examples
}
return emptyList()
}
override fun read(buffer: PlayInByteBuffer) = this
}

View File

@ -240,7 +240,7 @@ open class CommandReader(val string: String) {
}
val builder = StringBuilder()
while (true) {
val peek = peek() ?: break
val peek = peekNext() ?: break
if (peek in '0'.code..'9'.code) {
builder.appendCodePoint(peek)
pointer++

View File

@ -25,7 +25,6 @@ class SingularPalette<T>(private val registry: AbstractRegistry<T?>) : Palette<T
item = registry.getOrNull(buffer.readVarInt())
}
@Suppress("UNCHECKED_CAST")
override fun getOrNull(id: Int): T? {
return item
}

View File

@ -0,0 +1,84 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.CoordinateParserUtil.readCoordinate
import de.bixilon.minosoft.commands.util.CommandReader
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
internal class CoordinateParserUtilTest {
@Test
fun readNumericPositive() {
val reader = CommandReader("5")
assertEquals(Coordinate(CoordinateRelatives.NONE, 5.0f), reader.readCoordinate())
}
@Test
fun readNumericNegative() {
val reader = CommandReader("-5")
assertEquals(Coordinate(CoordinateRelatives.NONE, -5.0f), reader.readCoordinate())
}
@Test
fun readTilde() {
val reader = CommandReader("~")
assertEquals(Coordinate(CoordinateRelatives.TILDE, 0.0f), reader.readCoordinate())
}
@Test
fun readTildePositiveOffset() {
val reader = CommandReader("~1")
assertEquals(Coordinate(CoordinateRelatives.TILDE, 1.0f), reader.readCoordinate())
}
@Test
fun readTildeNegativeOffset() {
val reader = CommandReader("~-1")
assertEquals(Coordinate(CoordinateRelatives.TILDE, -1.0f), reader.readCoordinate())
}
@Test
fun readCaret() {
val reader = CommandReader("^")
assertEquals(Coordinate(CoordinateRelatives.CARET, 0.0f), reader.readCoordinate())
}
@Test
fun readCaretPositiveOffset() {
val reader = CommandReader("^1")
assertEquals(Coordinate(CoordinateRelatives.CARET, 1.0f), reader.readCoordinate())
}
@Test
fun readCaretNegativeOffset() {
val reader = CommandReader("^-1")
assertEquals(Coordinate(CoordinateRelatives.CARET, -1.0f), reader.readCoordinate())
}
@Test
fun readInvalidPrefix() {
val reader = CommandReader("&12")
assertThrows<InvalidCoordinateError> { reader.readCoordinate() }
}
@Test
fun read2() {
val reader = CommandReader("~ 12")
assertEquals(Coordinate(CoordinateRelatives.TILDE, 0.0f), reader.readCoordinate())
assertEquals(Coordinate(CoordinateRelatives.NONE, 12.0f), reader.readCoordinate())
}
}

View File

@ -0,0 +1,62 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec2
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.Coordinate
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.CoordinateRelatives
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.InvalidCoordinateError
import de.bixilon.minosoft.commands.util.CommandReader
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
internal class Vec2ParserTest {
@Test
fun readNumeric() {
val reader = CommandReader("5 5")
assertEquals(Vec2Coordinate(Coordinate(CoordinateRelatives.NONE, 5.0f), Coordinate(CoordinateRelatives.NONE, 5.0f)), Vec2Parser.parse(reader))
}
@Test
fun readTilde() {
val reader = CommandReader("~ ~")
assertEquals(Vec2Coordinate(Coordinate(CoordinateRelatives.TILDE, 0.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f)), Vec2Parser.parse(reader))
}
@Test
fun readCaret() {
val reader = CommandReader("^ ^")
assertEquals(Vec2Coordinate(Coordinate(CoordinateRelatives.CARET, 0.0f), Coordinate(CoordinateRelatives.CARET, 0.0f)), Vec2Parser.parse(reader))
}
@Test
fun readNumericTilde() {
val reader = CommandReader("5 ~")
assertEquals(Vec2Coordinate(Coordinate(CoordinateRelatives.NONE, 5.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f)), Vec2Parser.parse(reader))
}
@Test
fun readTildeCaret() {
val reader = CommandReader("~4 ^-9")
assertEquals(Vec2Coordinate(Coordinate(CoordinateRelatives.TILDE, 4.0f), Coordinate(CoordinateRelatives.CARET, -9.0f)), Vec2Parser.parse(reader))
}
@Test
fun readOnlyOne() {
val reader = CommandReader("~4")
assertThrows<InvalidCoordinateError> { Vec2Parser.parse(reader) }
}
}

View File

@ -0,0 +1,68 @@
/*
* Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.commands.parser.minecraft.coordinate.vec3
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.Coordinate
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.CoordinateRelatives
import de.bixilon.minosoft.commands.parser.minecraft.coordinate.InvalidCoordinateError
import de.bixilon.minosoft.commands.util.CommandReader
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
internal class Vec3ParserTest {
@Test
fun readNumeric() {
val reader = CommandReader("5 5 5")
assertEquals(Vec3Coordinate(Coordinate(CoordinateRelatives.NONE, 5.0f), Coordinate(CoordinateRelatives.NONE, 5.0f), Coordinate(CoordinateRelatives.NONE, 5.0f)), Vec3Parser.parse(reader))
}
@Test
fun readTilde() {
val reader = CommandReader("~ ~ ~")
assertEquals(Vec3Coordinate(Coordinate(CoordinateRelatives.TILDE, 0.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f)), Vec3Parser.parse(reader))
}
@Test
fun readCaret() {
val reader = CommandReader("^ ^ ^")
assertEquals(Vec3Coordinate(Coordinate(CoordinateRelatives.CARET, 0.0f), Coordinate(CoordinateRelatives.CARET, 0.0f), Coordinate(CoordinateRelatives.CARET, 0.0f)), Vec3Parser.parse(reader))
}
@Test
fun readNumericTilde() {
val reader = CommandReader("5 ~ ~")
assertEquals(Vec3Coordinate(Coordinate(CoordinateRelatives.NONE, 5.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f), Coordinate(CoordinateRelatives.TILDE, 0.0f)), Vec3Parser.parse(reader))
}
@Test
fun readTildeCaret() {
val reader = CommandReader("~4 ^-9 7")
assertEquals(Vec3Coordinate(Coordinate(CoordinateRelatives.TILDE, 4.0f), Coordinate(CoordinateRelatives.CARET, -9.0f), Coordinate(CoordinateRelatives.NONE, 7.0f)), Vec3Parser.parse(reader))
}
@Test
fun readOnlyOne() {
val reader = CommandReader("~4")
assertThrows<InvalidCoordinateError> { Vec3Parser.parse(reader) }
}
@Test
fun readOnlyTwo() {
val reader = CommandReader("~4 9")
assertThrows<InvalidCoordinateError> { Vec3Parser.parse(reader) }
}
}