diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/ArgumentParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/ArgumentParser.kt index ad6935e43..89f05ae35 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/ArgumentParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/ArgumentParser.kt @@ -17,9 +17,9 @@ import de.bixilon.minosoft.commands.util.CommandReader import de.bixilon.minosoft.data.text.ChatComponent interface ArgumentParser { - val examples: List + val examples: List val placeholder: ChatComponent fun parse(reader: CommandReader): T - fun getSuggestions(reader: CommandReader): List + fun getSuggestions(reader: CommandReader): List } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt b/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt index 3f4eefb35..ae40081ab 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/factory/ArgumentParserFactories.kt @@ -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>( @@ -28,4 +29,6 @@ object ArgumentParserFactories : DefaultFactory>( IntParser, LongParser, StringParser, + + TargetParser, ) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParser.kt new file mode 100644 index 000000000..c0f9c0e12 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParser.kt @@ -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 . + * + * 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 { + override val examples: List = listOf(playerName, "@a", "@p") + override val placeholder = ChatComponent.of("") + + 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 { + return examples // ToDo + } + + companion object : ArgumentParserFactory { + 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) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetProperties.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetProperties.kt new file mode 100644 index 000000000..a02bc1ec6 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetProperties.kt @@ -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 . + * + * 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 +) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetSelectors.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetSelectors.kt new file mode 100644 index 000000000..becd8b7d3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetSelectors.kt @@ -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 . + * + * 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'), + ; +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/DistanceProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/DistanceProperty.kt new file mode 100644 index 000000000..30c9c8b80 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/DistanceProperty.kt @@ -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 . + * + * 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" } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/GamemodeProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/GamemodeProperty.kt new file mode 100644 index 000000000..b1a1c55ef --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/GamemodeProperty.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/NameProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/NameProperty.kt new file mode 100644 index 000000000..d3f0106e3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/NameProperty.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/Sorting.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/Sorting.kt new file mode 100644 index 000000000..a5467c058 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/Sorting.kt @@ -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 . + * + * 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, + ; +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TargetProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TargetProperty.kt new file mode 100644 index 000000000..bcc3bd6b1 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TargetProperty.kt @@ -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 . + * + * 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 +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TypeProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TypeProperty.kt new file mode 100644 index 000000000..be4e07be1 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TypeProperty.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/PitchRotation.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/PitchRotation.kt new file mode 100644 index 000000000..ca010bcf5 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/PitchRotation.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/RotationProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/RotationProperty.kt new file mode 100644 index 000000000..a6a7a1886 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/RotationProperty.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/YawRotation.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/YawRotation.kt new file mode 100644 index 000000000..3260d34d7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/YawRotation.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/EntityTarget.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/EntityTarget.kt new file mode 100644 index 000000000..e8561c60f --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/EntityTarget.kt @@ -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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.parser.minecraft.target.targets + +interface EntityTarget diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/TargetParseError.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/TargetParseError.kt new file mode 100644 index 000000000..219234abe --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/TargetParseError.kt @@ -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 . + * + * 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) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/InvalidNameError.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/InvalidNameError.kt new file mode 100644 index 000000000..60b707a1c --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/InvalidNameError.kt @@ -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 . + * + * 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, +) : ParserError(reader, result) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/NameEntityTarget.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/NameEntityTarget.kt new file mode 100644 index 000000000..83fd3846b --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/name/NameEntityTarget.kt @@ -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 . + * + * 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 + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/InvalidUUIDError.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/InvalidUUIDError.kt new file mode 100644 index 000000000..cf2a1fb75 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/InvalidUUIDError.kt @@ -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 . + * + * 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, +) : ParserError(reader, result) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/UUIDEntityTarget.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/UUIDEntityTarget.kt new file mode 100644 index 000000000..f52f0398a --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/identifier/uuid/UUIDEntityTarget.kt @@ -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 . + * + * 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 + } +} diff --git a/src/test/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParserTest.kt b/src/test/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParserTest.kt new file mode 100644 index 000000000..99c84eabb --- /dev/null +++ b/src/test/java/de/bixilon/minosoft/commands/parser/minecraft/target/TargetParserTest.kt @@ -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 . + * + * 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 { 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 { parser.parse(reader) } + } + + @Test + fun testTooShortUUID() { + val reader = CommandReader("9e6ce7c540d3483e8e5ab63") + val parser = TargetParser() + assertThrows { parser.parse(reader) } + } + + @Test + fun testTooLongUUID() { + val reader = CommandReader("9e6ce7c540d3483e8e5ab6350987d65f123") + val parser = TargetParser() + assertThrows { parser.parse(reader) } + } +}