cli: wip target parser

This commit is contained in:
Bixilon 2022-05-19 12:01:50 +02:00
parent fbcbc5d0ee
commit f5f797ff3f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
21 changed files with 657 additions and 2 deletions

View File

@ -17,9 +17,9 @@ import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.data.text.ChatComponent
interface ArgumentParser<T> {
val examples: List<T>
val examples: List<Any?>
val placeholder: ChatComponent
fun parse(reader: CommandReader): T
fun getSuggestions(reader: CommandReader): List<T>
fun getSuggestions(reader: CommandReader): List<Any?>
}

View File

@ -19,6 +19,7 @@ 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.target.TargetParser
import de.bixilon.minosoft.data.registries.factory.DefaultFactory
object ArgumentParserFactories : DefaultFactory<ArgumentParserFactory<*>>(
@ -28,4 +29,6 @@ object ArgumentParserFactories : DefaultFactory<ArgumentParserFactory<*>>(
IntParser,
LongParser,
StringParser,
TargetParser,
)

View File

@ -0,0 +1,94 @@
/*
* 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.target
import de.bixilon.kutil.uuid.UUIDUtil.toUUID
import de.bixilon.minosoft.commands.errors.ExpectedArgumentError
import de.bixilon.minosoft.commands.parser.ArgumentParser
import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.InvalidNameError
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.NameEntityTarget
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.uuid.InvalidUUIDError
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.uuid.UUIDEntityTarget
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.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.BitByte.isBitMask
import de.bixilon.minosoft.util.KUtil.toResourceLocation
class TargetParser(
val single: Boolean = false,
val onlyPlayers: Boolean = false,
val playerName: String = DEFAULT_PLAYER_NAME,
) : ArgumentParser<EntityTarget> {
override val examples: List<Any?> = listOf(playerName, "@a", "@p")
override val placeholder = ChatComponent.of("<target>")
override fun parse(reader: CommandReader): EntityTarget {
if (!reader.canPeekNext()) {
throw ExpectedArgumentError(reader)
}
return if (reader.peek() == '@'.code) {
reader.parseSelector()
} else {
parseEntityIdentifier(reader)
}
}
fun CommandReader.parseSelector(): EntityTarget {
TODO()
}
fun parseEntityIdentifier(reader: CommandReader): EntityTarget {
val result = reader.readResult { reader.readUnquotedString() }
if (result.result == null) {
throw ExpectedArgumentError(reader)
}
if (result.result.length < 16) {
val name = result.result
if (ProtocolDefinition.MINECRAFT_NAME_VALIDATOR.matcher(name).matches()) {
return NameEntityTarget(name)
}
throw InvalidNameError(reader, result)
}
try {
return UUIDEntityTarget(result.result.toUUID())
} catch (ignored: Throwable) {
throw InvalidUUIDError(reader, result)
}
}
override fun getSuggestions(reader: CommandReader): List<Any?> {
return examples // ToDo
}
companion object : ArgumentParserFactory<TargetParser> {
override val RESOURCE_LOCATION: ResourceLocation = "minecraft:entity".toResourceLocation()
const val DEFAULT_PLAYER_NAME = "Bixilon"
override fun build(connection: PlayConnection?) = TargetParser(playerName = connection?.player?.name ?: DEFAULT_PLAYER_NAME)
override fun read(buffer: PlayInByteBuffer): TargetParser {
val flags = buffer.readUnsignedByte()
val single = flags.isBitMask(0x01)
val onlyPlayers = flags.isBitMask(0x02)
return TargetParser(single, onlyPlayers, buffer.connection.player.name)
}
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.target
import de.bixilon.minosoft.commands.parser.minecraft.target.properties.*
import de.bixilon.minosoft.commands.parser.minecraft.target.properties.rotation.PitchRotation
import de.bixilon.minosoft.commands.parser.minecraft.target.properties.rotation.YawRotation
data class TargetProperties(
val selector: TargetSelectors,
var x: Double?,
var y: Double?,
var z: Double?,
var distance: DistanceProperty?,
var volumeX: Double?,
var volumeY: Double?,
var volumeZ: Double?,
var scores: Any?, // ToDo
var tag: Any?, // ToDo
var team: Any?, // ToDo,
var sort: Sorting?,
var limit: Int? = null,
var level: IntRange? = null,
var gamemode: GamemodeProperty? = null,
var name: NameProperty? = null,
var xRotation: PitchRotation? = null,
var yRotation: YawRotation? = null,
var type: TypeProperty? = null,
var nbt: Any? = null, // ToDo
var advancements: Any? = null, // ToDo
var predicate: Any? = null, // ToDo
)

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.target
enum class TargetSelectors(val char: Char) {
NEAREST('p'),
RANDOM('r'),
ALL_PLAYERS('a'),
ALL_ENTITIES('e'),
;
}

View File

@ -0,0 +1,25 @@
/*
* 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.target.properties
class DistanceProperty(
val min: Double = 0.0,
val max: Double = Double.MAX_VALUE,
) {
init {
check(min >= 0.0) { "Minimum distance can not be below 0" }
check(max >= min) { "Maximum distance can not be smaller than minimum distance" }
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.target.properties
import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties
import de.bixilon.minosoft.data.abilities.Gamemodes
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
class GamemodeProperty(
val gamemode: Gamemodes,
val negated: Boolean,
) : TargetProperty {
override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean {
if (entity !is PlayerEntity) {
return false
}
if (negated) {
return entity.gamemode != gamemode
}
return entity.gamemode == gamemode
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.target.properties
import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
class NameProperty(
val name: String,
val negated: Boolean,
) : TargetProperty {
override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean {
// ToDo: Check player name?
if (negated) {
return entity.customName?.message != name
}
return entity.customName?.message == name
}
}

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.target.properties
enum class Sorting {
NEAREST,
FURTHEST,
RANDOM,
ARBITRARY,
;
}

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.target.properties
import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
interface TargetProperty {
fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean
}

View File

@ -0,0 +1,32 @@
/*
* 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.target.properties
import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.data.registries.entities.EntityType
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
class TypeProperty(
val type: EntityType,
val negated: Boolean,
) : TargetProperty {
override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean {
if (negated) {
return entity.type != type
}
return entity.type == type
}
}

View File

@ -0,0 +1,26 @@
/*
* 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.target.properties.rotation
import de.bixilon.minosoft.data.entities.EntityRotation
class PitchRotation(
override val min: Float = -90.0f,
override val max: Float = 90.0f,
) : RotationProperty {
override fun getValue(rotation: EntityRotation): Double {
return rotation.pitch
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.target.properties.rotation
import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties
import de.bixilon.minosoft.commands.parser.minecraft.target.properties.TargetProperty
import de.bixilon.minosoft.data.entities.EntityRotation
import de.bixilon.minosoft.data.entities.entities.Entity
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
interface RotationProperty : TargetProperty {
val min: Float
val max: Float
fun getValue(rotation: EntityRotation): Double
override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean {
val rotation = getValue(entity.rotation)
return rotation in min..max
}
}

View File

@ -0,0 +1,26 @@
/*
* 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.target.properties.rotation
import de.bixilon.minosoft.data.entities.EntityRotation
class YawRotation(
override val min: Float = -180.0f,
override val max: Float = 180.0f,
) : RotationProperty {
override fun getValue(rotation: EntityRotation): Double {
return rotation.yaw
}
}

View File

@ -0,0 +1,16 @@
/*
* 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.target.targets
interface EntityTarget

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.target.targets.identifier
import de.bixilon.minosoft.commands.errors.parser.ParserError
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.commands.util.ReadResult
class TargetParseError(
reader: CommandReader,
result: ReadResult<*>,
) : ParserError(reader, result)

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.target.targets.identifier.name
import de.bixilon.minosoft.commands.errors.parser.ParserError
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.commands.util.ReadResult
class InvalidNameError(
reader: CommandReader,
result: ReadResult<String?>,
) : ParserError(reader, result)

View File

@ -0,0 +1,36 @@
/*
* 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.target.targets.identifier.name
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget
class NameEntityTarget(
val name: String,
) : EntityTarget {
override fun toString(): String {
return "{Bixilon}"
}
override fun hashCode(): Int {
return name.hashCode()
}
override fun equals(other: Any?): Boolean {
if (other !is NameEntityTarget) {
return false
}
return name == other.name
}
}

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.target.targets.identifier.uuid
import de.bixilon.minosoft.commands.errors.parser.ParserError
import de.bixilon.minosoft.commands.util.CommandReader
import de.bixilon.minosoft.commands.util.ReadResult
class InvalidUUIDError(
reader: CommandReader,
result: ReadResult<String?>,
) : ParserError(reader, result)

View File

@ -0,0 +1,37 @@
/*
* 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.target.targets.identifier.uuid
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget
import java.util.*
class UUIDEntityTarget(
val uuid: UUID,
) : EntityTarget {
override fun toString(): String {
return "{$uuid}"
}
override fun hashCode(): Int {
return uuid.hashCode()
}
override fun equals(other: Any?): Boolean {
if (other !is UUIDEntityTarget) {
return false
}
return uuid == other.uuid
}
}

View File

@ -0,0 +1,78 @@
/*
* 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.target
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.InvalidNameError
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name.NameEntityTarget
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.uuid.InvalidUUIDError
import de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.uuid.UUIDEntityTarget
import de.bixilon.minosoft.commands.util.CommandReader
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.util.*
import kotlin.test.assertEquals
internal class TargetParserTest {
@Test
fun testName() {
val reader = CommandReader("Bixilon")
val parser = TargetParser()
assertEquals(parser.parse(reader), NameEntityTarget("Bixilon"))
}
@Test
fun testInvalidName() {
val reader = CommandReader("Bi§ilon")
val parser = TargetParser()
assertThrows<InvalidNameError> { parser.parse(reader) }
}
@Test
fun testUUID() {
val reader = CommandReader("9e6ce7c5-40d3-483e-8e5a-b6350987d65f")
val parser = TargetParser()
assertEquals(parser.parse(reader), UUIDEntityTarget(UUID.fromString("9e6ce7c5-40d3-483e-8e5a-b6350987d65f")))
}
@Test
fun testTrimmedUUID() {
val reader = CommandReader("9e6ce7c540d3483e8e5ab6350987d65f")
val parser = TargetParser()
assertEquals(parser.parse(reader), UUIDEntityTarget(UUID.fromString("9e6ce7c5-40d3-483e-8e5a-b6350987d65f")))
}
@Test
fun testInvalidUUID() {
val reader = CommandReader("9g6ce7c540d3483e8e5ab6350987d65f")
val parser = TargetParser()
assertThrows<InvalidUUIDError> { parser.parse(reader) }
}
@Test
fun testTooShortUUID() {
val reader = CommandReader("9e6ce7c540d3483e8e5ab63")
val parser = TargetParser()
assertThrows<InvalidUUIDError> { parser.parse(reader) }
}
@Test
fun testTooLongUUID() {
val reader = CommandReader("9e6ce7c540d3483e8e5ab6350987d65f123")
val parser = TargetParser()
assertThrows<InvalidUUIDError> { parser.parse(reader) }
}
}