diff --git a/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt b/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt index 40fbaac57..bba8a6df5 100644 --- a/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt +++ b/src/main/java/de/bixilon/minosoft/commands/nodes/LiteralNode.kt @@ -65,6 +65,6 @@ class LiteralNode : ExecutableNode { stack.push(name, name) return super.getSuggestions(reader, stack) } - return suggester.suggest(literalName) ?: listOf() + return suggester.suggest(literalName) ?: emptyList() } } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_double/DoubleParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_double/DoubleParser.kt index 5e5f840db..f8bb8d3cb 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_double/DoubleParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_double/DoubleParser.kt @@ -44,7 +44,7 @@ class DoubleParser( if (reader.readString()?.isBlank() != false) { return examples } - return listOf() + return emptyList() } companion object : ArgumentParserFactory { diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_float/FloatParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_float/FloatParser.kt index b473f2304..ba854d527 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_float/FloatParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_float/FloatParser.kt @@ -44,7 +44,7 @@ class FloatParser( if (reader.readString()?.isBlank() != false) { return examples } - return listOf() + return emptyList() } companion object : ArgumentParserFactory { diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_int/IntParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_int/IntParser.kt index 925b98b29..cfebc048d 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_int/IntParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_int/IntParser.kt @@ -44,7 +44,7 @@ class IntParser( if (reader.readString()?.isBlank() != false) { return examples } - return listOf() + return emptyList() } companion object : ArgumentParserFactory { diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_long/LongParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_long/LongParser.kt index 8c838db85..a4f5d3a39 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_long/LongParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/_long/LongParser.kt @@ -44,7 +44,7 @@ class LongParser( if (reader.readString()?.isBlank() != false) { return examples } - return listOf() + return emptyList() } companion object : ArgumentParserFactory { diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/string/StringParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/string/StringParser.kt index 0967da872..76b0046e4 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/string/StringParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/brigadier/string/StringParser.kt @@ -27,7 +27,7 @@ import de.bixilon.minosoft.util.KUtil.toResourceLocation class StringParser( val mode: StringModes = StringModes.SINGLE, ) : BrigadierParser { - override val examples: List = listOf() + override val examples: List = emptyList() override val placeholder = ChatComponent.of("") override fun parse(reader: CommandReader): String { 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 ae40081ab..6beca7234 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 @@ -20,6 +20,9 @@ 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.commands.parser.minosoft.range.RangeParserFactory +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRangeParser +import de.bixilon.minosoft.commands.parser.minosoft.range._int.IntRangeParser import de.bixilon.minosoft.data.registries.factory.DefaultFactory object ArgumentParserFactories : DefaultFactory>( @@ -31,4 +34,8 @@ object ArgumentParserFactories : DefaultFactory>( StringParser, TargetParser, + + RangeParserFactory, + FloatRangeParser, + IntRangeParser, ) 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 index a02bc1ec6..385341880 100644 --- 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 @@ -13,9 +13,13 @@ 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 +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.DistanceProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.GamemodeProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.NameProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TypeProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.rotation.PitchRotation +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.rotation.YawRotation +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort.Sorting data class TargetProperties( val selector: TargetSelectors, 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 index becd8b7d3..c3125bce4 100644 --- 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 @@ -13,10 +13,21 @@ package de.bixilon.minosoft.commands.parser.minecraft.target -enum class TargetSelectors(val char: Char) { - NEAREST('p'), - RANDOM('r'), - ALL_PLAYERS('a'), - ALL_ENTITIES('e'), +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort.Sorting +import de.bixilon.minosoft.data.entities.entities.Entity + +enum class TargetSelectors( + val char: Char, + val sorting: Sorting, +) { + NEAREST('p', Sorting.NEAREST), + RANDOM('r', Sorting.RANDOM), + ALL_PLAYERS('a', Sorting.ARBITRARY), + ALL_ENTITIES('e', Sorting.ARBITRARY), ; + + + fun sort(selected: MutableList) { + sorting.sort(selected) + } } 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 index e8561c60f..c715d89cd 100644 --- 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 @@ -13,4 +13,10 @@ package de.bixilon.minosoft.commands.parser.minecraft.target.targets -interface EntityTarget +import de.bixilon.minosoft.data.entities.entities.Entity +import de.bixilon.minosoft.data.world.WorldEntities + +interface EntityTarget { + + fun getEntities(entities: WorldEntities): List +} 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 index 83fd3846b..008ae6729 100644 --- 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 @@ -14,11 +14,30 @@ package de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.name import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget +import de.bixilon.minosoft.data.entities.entities.Entity +import de.bixilon.minosoft.data.world.WorldEntities class NameEntityTarget( val name: String, ) : EntityTarget { + override fun getEntities(entities: WorldEntities): List { + var entity: Entity? = null + entities.lock.acquire() + for (entry in entities) { + if (entry.customName?.message == name) { + entity = entry + break + } + } + entities.lock.release() + + if (entity == null) { + return emptyList() + } + return listOf(entity) + } + override fun toString(): String { return "{Bixilon}" } 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 index f52f0398a..9bf40634d 100644 --- 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 @@ -14,12 +14,19 @@ package de.bixilon.minosoft.commands.parser.minecraft.target.targets.identifier.uuid import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget +import de.bixilon.minosoft.data.entities.entities.Entity +import de.bixilon.minosoft.data.world.WorldEntities import java.util.* class UUIDEntityTarget( val uuid: UUID, ) : EntityTarget { + override fun getEntities(entities: WorldEntities): List { + val entity = entities[uuid] ?: return emptyList() + return listOf(entity) + } + override fun toString(): String { return "{$uuid}" } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/SelectorEntityTarget.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/SelectorEntityTarget.kt new file mode 100644 index 000000000..31e80d57b --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/SelectorEntityTarget.kt @@ -0,0 +1,51 @@ +/* + * 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.selector + +import de.bixilon.kutil.cast.CastUtil.nullCast +import de.bixilon.minosoft.commands.parser.minecraft.target.TargetSelectors +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.EntityTarget +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TargetProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort.SortProperty +import de.bixilon.minosoft.data.entities.entities.Entity +import de.bixilon.minosoft.data.world.WorldEntities + +class SelectorEntityTarget( + val selector: TargetSelectors, + val properties: Map, +) : EntityTarget { + + override fun getEntities(entities: WorldEntities): List { + val selected: MutableList = mutableListOf() + entities.lock.acquire() + for (entity in entities) { + selected += entity + } + entities.lock.release() + + properties[SortProperty.name]?.nullCast()?.sort(selected) ?: selector.sort(selected) + + val output: MutableList = mutableListOf() + entityLoop@ for (entity in selected) { + for (property in properties.values) { + if (!property.passes(output, entity)) { + continue@entityLoop + } + } + output += entity + } + + return output + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/DistanceProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/DistanceProperty.kt new file mode 100644 index 000000000..a26925068 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/DistanceProperty.kt @@ -0,0 +1,40 @@ +/* + * 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.selector.properties + +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.entities.entities.Entity + +class DistanceProperty( + val min: Double = 0.0, + val max: Double = Double.MAX_VALUE, +) : TargetProperty { + + init { + check(min >= 0.0) { "Minimum distance can not be below 0" } + check(max >= min) { "Maximum distance can not be smaller than minimum distance" } + } + + override fun passes(selected: List, entity: Entity): Boolean { + TODO() + } + + companion object : TargetPropertyFactory { + override val name: String = "distance" + + override fun read(reader: CommandReader): DistanceProperty { + TODO("Not yet implemented") + } + } +} 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/targets/selector/properties/GamemodeProperty.kt similarity index 61% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/GamemodeProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/GamemodeProperty.kt index b1a1c55ef..baba28c71 100644 --- 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/targets/selector/properties/GamemodeProperty.kt @@ -11,20 +11,21 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties -import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties +import de.bixilon.minosoft.commands.errors.ExpectedArgumentError +import de.bixilon.minosoft.commands.parser.minosoft.enums.EnumParser +import de.bixilon.minosoft.commands.util.CommandReader 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 { + override fun passes(selected: List, entity: Entity): Boolean { if (entity !is PlayerEntity) { return false } @@ -33,4 +34,15 @@ class GamemodeProperty( } return entity.gamemode == gamemode } + + + companion object : TargetPropertyFactory { + override val name: String = "gamemode" + private val parser = EnumParser(Gamemodes) + + override fun read(reader: CommandReader): GamemodeProperty { + val (gamemode, negated) = reader.readNegateable { parser.parse(reader) } ?: throw ExpectedArgumentError(reader) + return GamemodeProperty(gamemode, negated) + } + } } 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/targets/selector/properties/NameProperty.kt similarity index 62% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/NameProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/NameProperty.kt index d3f0106e3..bbc3a5817 100644 --- 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/targets/selector/properties/NameProperty.kt @@ -11,22 +11,31 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties -import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties +import de.bixilon.minosoft.commands.errors.ExpectedArgumentError +import de.bixilon.minosoft.commands.util.CommandReader 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 { + override fun passes(selected: List, entity: Entity): Boolean { // ToDo: Check player name? if (negated) { return entity.customName?.message != name } return entity.customName?.message == name } + + companion object : TargetPropertyFactory { + override val name: String = "name" + + override fun read(reader: CommandReader): NameProperty { + val (word, negated) = reader.readNegateable { readWord() ?: throw ExpectedArgumentError(reader) } ?: throw ExpectedArgumentError(reader) + return NameProperty(word, negated) + } + } } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TargetProperties.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TargetProperties.kt new file mode 100644 index 000000000..baf6e4bc7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TargetProperties.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.selector.properties + +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.rotation.PitchRotation +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.rotation.YawRotation +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort.SortProperty + +object TargetProperties { + val properties: MutableMap> = mutableMapOf() + + + init { + register(SortProperty) + register(PitchRotation) + register(YawRotation) + register(DistanceProperty) + register(GamemodeProperty) + register(NameProperty) + register(TypeProperty) + } + + fun register(factory: TargetPropertyFactory<*>) { + properties[factory.name] = factory + } +} 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/targets/selector/properties/TargetProperty.kt similarity index 72% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TargetProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TargetProperty.kt index bcc3bd6b1..dc88a81e9 100644 --- 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/targets/selector/properties/TargetProperty.kt @@ -11,13 +11,11 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.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 + fun passes(selected: List, entity: Entity): Boolean } 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/targets/selector/properties/TargetPropertyFactory.kt similarity index 69% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/DistanceProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TargetPropertyFactory.kt index 30c9c8b80..cb7ea10b4 100644 --- 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/targets/selector/properties/TargetPropertyFactory.kt @@ -11,15 +11,13 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties -class DistanceProperty( - val min: Double = 0.0, - val max: Double = Double.MAX_VALUE, -) { +import de.bixilon.minosoft.commands.util.CommandReader - init { - check(min >= 0.0) { "Minimum distance can not be below 0" } - check(max >= min) { "Maximum distance can not be smaller than minimum distance" } - } +interface TargetPropertyFactory { + val name: String + + + fun read(reader: CommandReader): T } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TypeProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TypeProperty.kt new file mode 100644 index 000000000..099b0c9d8 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/TypeProperty.kt @@ -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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties + +import de.bixilon.minosoft.commands.errors.ExpectedArgumentError +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.entities.entities.Entity +import de.bixilon.minosoft.data.registries.ResourceLocation + +class TypeProperty( + val type: ResourceLocation, + val negated: Boolean, +) : TargetProperty { + + override fun passes(selected: List, entity: Entity): Boolean { + if (negated) { + return entity.type.resourceLocation != type + } + return entity.type.resourceLocation == type + } + + + companion object : TargetPropertyFactory { + override val name: String = "type" + + override fun read(reader: CommandReader): TypeProperty { + val (resourceLocation, negated) = reader.readNegateable { readResourceLocation() ?: throw ExpectedArgumentError(reader) } ?: throw ExpectedArgumentError(reader) + return TypeProperty(resourceLocation, negated) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/PitchRotation.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/PitchRotation.kt new file mode 100644 index 000000000..9a98a3507 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/PitchRotation.kt @@ -0,0 +1,45 @@ +/* + * 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.selector.properties.rotation + +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TargetPropertyFactory +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRange +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRangeParser +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.entities.EntityRotation + +class PitchRotation( + override val range: FloatRange, +) : RotationProperty { + + override fun getValue(rotation: EntityRotation): Double { + return rotation.pitch + } + + + companion object : TargetPropertyFactory { + const val MIN = -90.0f + const val MAX = 90.0f + override val name: String = "x_rotation" + private val parser = FloatRangeParser(false) + + override fun read(reader: CommandReader): YawRotation { + val range = reader.readResult { parser.parse(reader) } + if (range.result.min < MIN || range.result.max > MAX) { + throw RotationOutOfRangeError(reader, range) + } + return YawRotation(range.result) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/RotationOutOfRangeError.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/RotationOutOfRangeError.kt new file mode 100644 index 000000000..67427461a --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/RotationOutOfRangeError.kt @@ -0,0 +1,24 @@ +/* + * 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.selector.properties.rotation + +import de.bixilon.minosoft.commands.errors.parser.ParserError +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRange +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.commands.util.ReadResult + +class RotationOutOfRangeError( + reader: CommandReader, + result: ReadResult, +) : ParserError(reader, result) 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/targets/selector/properties/rotation/RotationProperty.kt similarity index 67% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/RotationProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/RotationProperty.kt index a6a7a1886..cc0be2a13 100644 --- 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/targets/selector/properties/rotation/RotationProperty.kt @@ -11,25 +11,22 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties.rotation +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.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.commands.parser.minecraft.target.targets.selector.properties.TargetProperty +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRange 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 - + val range: FloatRange fun getValue(rotation: EntityRotation): Double - override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean { + override fun passes(selected: List, entity: Entity): Boolean { val rotation = getValue(entity.rotation) - return rotation in min..max + return rotation.toFloat() in range } } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/YawRotation.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/YawRotation.kt new file mode 100644 index 000000000..e0585625f --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/rotation/YawRotation.kt @@ -0,0 +1,44 @@ +/* + * 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.selector.properties.rotation + +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TargetPropertyFactory +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRange +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRangeParser +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.entities.EntityRotation + +class YawRotation( + override val range: FloatRange, +) : RotationProperty { + + override fun getValue(rotation: EntityRotation): Double { + return rotation.yaw + } + + companion object : TargetPropertyFactory { + const val MIN = -180.0f + const val MAX = 180.0f + override val name: String = "y_rotation" + private val parser = FloatRangeParser(false) + + override fun read(reader: CommandReader): YawRotation { + val range = reader.readResult { parser.parse(reader) } + if (range.result.min < MIN || range.result.max > MAX) { + throw RotationOutOfRangeError(reader, range) + } + return YawRotation(range.result) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/sort/SortProperty.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/sort/SortProperty.kt new file mode 100644 index 000000000..9d2fe86ab --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/sort/SortProperty.kt @@ -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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort + +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TargetProperty +import de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.TargetPropertyFactory +import de.bixilon.minosoft.commands.parser.minosoft.enums.EnumParser +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.entities.entities.Entity + +class SortProperty( + val sorting: Sorting, +) : TargetProperty { + + override fun passes(selected: List, entity: Entity): Boolean { + return true + } + + fun sort(selected: MutableList) { + sorting.sort(selected) + } + + companion object : TargetPropertyFactory { + override val name: String = "sort" + private val parser = EnumParser(Sorting) + + override fun read(reader: CommandReader): SortProperty { + return SortProperty(parser.parse(reader)) + } + } +} 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/targets/selector/properties/sort/Sorting.kt similarity index 58% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/TypeProperty.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/targets/selector/properties/sort/Sorting.kt index be4e07be1..d502ee6d9 100644 --- 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/targets/selector/properties/sort/Sorting.kt @@ -11,22 +11,26 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minecraft.target.targets.selector.properties.sort -import de.bixilon.minosoft.commands.parser.minecraft.target.TargetProperties +import de.bixilon.kutil.enums.EnumUtil +import de.bixilon.kutil.enums.ValuesEnum 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 { +enum class Sorting { + NEAREST, + FURTHEST, + RANDOM, + ARBITRARY, + ; - override fun passes(properties: TargetProperties, connection: PlayConnection, entity: Entity): Boolean { - if (negated) { - return entity.type != type - } - return entity.type == type + + fun sort(selected: MutableList) { + sort(selected) + } + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = EnumUtil.getEnumValues(VALUES) } } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/enums/EnumParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/enums/EnumParser.kt index c8b169f10..ca94cf776 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/enums/EnumParser.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/enums/EnumParser.kt @@ -41,7 +41,7 @@ class EnumParser>( } override fun getSuggestions(reader: CommandReader): List { - val text = reader.readResult { reader.readString() } + val text = reader.readResult { reader.readWord() } if (text.result == null) { return examples } 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/minosoft/range/Range.kt similarity index 83% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/Sorting.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/Range.kt index a5467c058..9b998442a 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/Sorting.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/Range.kt @@ -11,12 +11,11 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties +package de.bixilon.minosoft.commands.parser.minosoft.range -enum class Sorting { - NEAREST, - FURTHEST, - RANDOM, - ARBITRARY, - ; +interface Range { + val min: T + val max: T + + fun contains(number: T): Boolean } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/RangeParserFactory.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/RangeParserFactory.kt new file mode 100644 index 000000000..d43b35647 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/RangeParserFactory.kt @@ -0,0 +1,60 @@ +/* + * 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.minosoft.range + +import de.bixilon.minosoft.commands.parser.ArgumentParser +import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory +import de.bixilon.minosoft.commands.parser.minosoft.range._float.FloatRangeParser +import de.bixilon.minosoft.commands.parser.minosoft.range._int.IntRangeParser +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection +import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer +import de.bixilon.minosoft.util.KUtil.toResourceLocation + + +object RangeParserFactory : ArgumentParserFactory> { + override val RESOURCE_LOCATION: ResourceLocation = "minosoft:range".toResourceLocation() + + override fun build(connection: PlayConnection?) = TODO("Can not construct range parser!") + + override fun read(buffer: PlayInByteBuffer): ArgumentParser<*> { + val decimals = buffer.readBoolean() + return if (decimals) { + FloatRangeParser.read(buffer) + } else { + IntRangeParser.read(buffer) + } + } + + + fun CommandReader.readRange(reader: CommandReader.() -> T): Pair? { + if (!canPeek()) { + return null + } + val first = reader(this) ?: return null + if (!canPeek()) { + return Pair(first, null) + } + if (peekNext() != '.'.code || peekNext(pointer + 1) != '.'.code) { + return null + } + readNext() + readNext() + + val second = reader(this) ?: return null + + return Pair(first, second) + } +} 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/minosoft/range/_float/FloatRange.kt similarity index 68% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/YawRotation.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRange.kt index 3260d34d7..5f1d7ef58 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/YawRotation.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRange.kt @@ -11,16 +11,16 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties.rotation +package de.bixilon.minosoft.commands.parser.minosoft.range._float -import de.bixilon.minosoft.data.entities.EntityRotation +import de.bixilon.minosoft.commands.parser.minosoft.range.Range -class YawRotation( - override val min: Float = -180.0f, - override val max: Float = 180.0f, -) : RotationProperty { +class FloatRange( + override val min: Float = -Float.MAX_VALUE, + override val max: Float = Float.MAX_VALUE, +) : Range { - override fun getValue(rotation: EntityRotation): Double { - return rotation.yaw + override operator fun contains(number: Float): Boolean { + return number in min..max } } diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParseError.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParseError.kt new file mode 100644 index 000000000..ef5f49317 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParseError.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.minosoft.range._float + +import de.bixilon.minosoft.commands.errors.parser.ParserError +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.commands.util.ReadResult + +class FloatRangeParseError( + reader: CommandReader, + result: ReadResult, +) : ParserError(reader, result) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParser.kt new file mode 100644 index 000000000..d44f9074e --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_float/FloatRangeParser.kt @@ -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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.parser.minosoft.range._float + +import de.bixilon.minosoft.commands.parser.ArgumentParser +import de.bixilon.minosoft.commands.parser.brigadier._float.FloatParser.Companion.readFloat +import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory +import de.bixilon.minosoft.commands.parser.minosoft.range.RangeParserFactory.readRange +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.util.KUtil.toResourceLocation + +class FloatRangeParser( + val defaultMin: Boolean = true, +) : ArgumentParser { + override val examples: List = listOf(1.0f, "1.0..10") + override val placeholder = ChatComponent.of("") + + override fun parse(reader: CommandReader): FloatRange { + return reader.readResult { reader.readFloatRange(defaultMin) }.let { return@let it.result ?: throw FloatRangeParseError(reader, it) } + } + + override fun getSuggestions(reader: CommandReader): List { + if (reader.readString()?.isBlank() != false) { + return examples + } + return emptyList() + } + + companion object : ArgumentParserFactory { + override val RESOURCE_LOCATION: ResourceLocation = "minecraft:float_range".toResourceLocation() + + override fun build(connection: PlayConnection?) = FloatRangeParser() + + override fun read(buffer: PlayInByteBuffer) = FloatRangeParser() + + fun CommandReader.readFloatRange(defaultMin: Boolean = true): FloatRange? { + val (first, second) = readRange { readFloat() } ?: return null + if (first == null) { + return null + } + if (second == null) { + return FloatRange(min = if (defaultMin) -Float.MAX_VALUE else first, max = first) + } + return FloatRange(min = first, max = second) + } + } +} 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/minosoft/range/_int/IntRangeParseError.kt similarity index 68% rename from src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/PitchRotation.kt rename to src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_int/IntRangeParseError.kt index ca010bcf5..df9ae3ce2 100644 --- a/src/main/java/de/bixilon/minosoft/commands/parser/minecraft/target/properties/rotation/PitchRotation.kt +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_int/IntRangeParseError.kt @@ -11,16 +11,13 @@ * This software is not affiliated with Mojang AB, the original developer of Minecraft. */ -package de.bixilon.minosoft.commands.parser.minecraft.target.properties.rotation +package de.bixilon.minosoft.commands.parser.minosoft.range._int -import de.bixilon.minosoft.data.entities.EntityRotation +import de.bixilon.minosoft.commands.errors.parser.ParserError +import de.bixilon.minosoft.commands.util.CommandReader +import de.bixilon.minosoft.commands.util.ReadResult -class PitchRotation( - override val min: Float = -90.0f, - override val max: Float = 90.0f, -) : RotationProperty { - - override fun getValue(rotation: EntityRotation): Double { - return rotation.pitch - } -} +class IntRangeParseError( + reader: CommandReader, + result: ReadResult, +) : ParserError(reader, result) diff --git a/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_int/IntRangeParser.kt b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_int/IntRangeParser.kt new file mode 100644 index 000000000..c44d0b943 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/parser/minosoft/range/_int/IntRangeParser.kt @@ -0,0 +1,66 @@ +/* + * 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.minosoft.range._int + +import de.bixilon.minosoft.commands.parser.ArgumentParser +import de.bixilon.minosoft.commands.parser.brigadier._int.IntParser.Companion.readInt +import de.bixilon.minosoft.commands.parser.factory.ArgumentParserFactory +import de.bixilon.minosoft.commands.parser.minosoft.range.RangeParserFactory.readRange +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.util.KUtil.toResourceLocation + +class IntRangeParser( + val defaultMin: Boolean = true, +) : ArgumentParser { + override val examples: List = listOf(1, "1..10") + override val placeholder = ChatComponent.of("") + + override fun parse(reader: CommandReader): IntRange { + return reader.readResult { reader.readIntRange(defaultMin) }.let { return@let it.result ?: throw IntRangeParseError(reader, it) } + } + + override fun getSuggestions(reader: CommandReader): List { + if (reader.readString()?.isBlank() != false) { + return examples + } + return emptyList() + } + + companion object : ArgumentParserFactory { + override val RESOURCE_LOCATION: ResourceLocation = "minecraft:float_range".toResourceLocation() + + override fun build(connection: PlayConnection?) = IntRangeParser() + + override fun read(buffer: PlayInByteBuffer) = IntRangeParser() + + fun CommandReader.readIntRange(defaultMin: Boolean = true): IntRange? { + val (first, second) = readRange { readInt() } ?: return null + if (first == null) { + return null + } + if (second == null) { + return if (defaultMin) { + Int.MIN_VALUE..first + } else { + first..first + } + } + return first..second + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/commands/util/CommandReader.kt b/src/main/java/de/bixilon/minosoft/commands/util/CommandReader.kt index c24813805..0645668a3 100644 --- a/src/main/java/de/bixilon/minosoft/commands/util/CommandReader.kt +++ b/src/main/java/de/bixilon/minosoft/commands/util/CommandReader.kt @@ -15,6 +15,8 @@ package de.bixilon.minosoft.commands.util import de.bixilon.minosoft.commands.errors.reader.* import de.bixilon.minosoft.commands.errors.reader.number.NegativeNumberError +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.util.KUtil.toResourceLocation open class CommandReader(val string: String) { var pointer = 0 @@ -228,7 +230,7 @@ open class CommandReader(val string: String) { } fun readNumeric(decimal: Boolean = true, negative: Boolean = true): String? { - if (!canPeekNext()) { + if (!canPeek()) { return null } val builder = StringBuilder() @@ -254,6 +256,49 @@ open class CommandReader(val string: String) { return builder.toString() } + fun readWord(): String? { + if (!canPeek()) { + return null + } + val builder = StringBuilder() + while (true) { + val peek = peek() ?: break + if (peek in '0'.code..'9'.code || peek in 'a'.code..'z'.code || peek in 'A'.code..'Z'.code || peek == '_'.code || peek == '-'.code || peek == '/'.code) { + builder.appendCodePoint(peek) + pointer++ + continue + } else { + break + } + } + return builder.toString() + } + + fun readResourceLocation(): ResourceLocation? { + val namespace = readWord() ?: return null + if (peek() != ':'.code) { + return namespace.toResourceLocation() + } + read() + val path = readWord() ?: return null + + return ResourceLocation(namespace, path) + } + + fun readNegateable(reader: CommandReader.() -> T): Pair? { + if (!canPeek()) { + return null + } + var negated = false + if (peek() == '!'.code) { + read() + negated = true + } + val it = reader(this) ?: return null + + return Pair(it, negated) + } + fun readResult(reader: CommandReader.() -> T): ReadResult { val start = pointer val result = reader(this) diff --git a/src/main/java/de/bixilon/minosoft/data/language/Language.kt b/src/main/java/de/bixilon/minosoft/data/language/Language.kt index dddd9bec7..4dfdda2f6 100644 --- a/src/main/java/de/bixilon/minosoft/data/language/Language.kt +++ b/src/main/java/de/bixilon/minosoft/data/language/Language.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2021 Moritz Zwerger + * 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. * @@ -38,7 +38,7 @@ class Language( val ret = BaseComponent() val arguments: MutableList = mutableListOf() - var splitPlaceholder: List = listOf() + var splitPlaceholder: List = emptyList() // Bring arguments in correct oder FORMATTER_ORDER_REGEX.findAll(placeholder).toList().let { diff --git a/src/main/java/de/bixilon/minosoft/gui/eros/main/account/ErosAccountType.kt b/src/main/java/de/bixilon/minosoft/gui/eros/main/account/ErosAccountType.kt index dd329a414..51f74b900 100644 --- a/src/main/java/de/bixilon/minosoft/gui/eros/main/account/ErosAccountType.kt +++ b/src/main/java/de/bixilon/minosoft/gui/eros/main/account/ErosAccountType.kt @@ -23,7 +23,7 @@ data class ErosAccountType( override val resourceLocation: ResourceLocation, override val translationKey: ResourceLocation? = null, val icon: Ikon, - val additionalDetails: List Any?>> = listOf(), + val additionalDetails: List Any?>> = emptyList(), val addHandler: ((accountController: AccountController) -> Unit)? = null, val refreshHandler: ((accountController: AccountController, account: T) -> Unit)? = null, ) : ResourceLocationAble, Translatable diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextFlowElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextFlowElement.kt index aea9bec83..5f72ca6a3 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextFlowElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/elements/text/TextFlowElement.kt @@ -35,7 +35,7 @@ open class TextFlowElement( var messageExpireTime: Long, ) : Element(guiRenderer), AbstractLayout { private val messages: MutableList = synchronizedListOf() // all messages **from newest to oldest** - private var visibleLines: List = listOf() // all visible lines **from bottom to top** + private var visibleLines: List = emptyList() // all visible lines **from bottom to top** override var activeElement: TextElement? = null override var activeDragElement: TextElement? = null diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/tab/TabListElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/tab/TabListElement.kt index 69c36552d..40e7b1319 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/tab/TabListElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/tab/TabListElement.kt @@ -52,7 +52,7 @@ class TabListElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedE private var entriesSize = Vec2i.EMPTY private val entries: MutableMap = synchronizedMapOf() - private var toRender: List = listOf() + private var toRender: List = emptyList() private val lock = ReentrantLock() var needsApply = false private set diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/instance/SkeletalInstance.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/instance/SkeletalInstance.kt index cfc549900..c77f55e89 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/instance/SkeletalInstance.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/instance/SkeletalInstance.kt @@ -33,7 +33,7 @@ class SkeletalInstance( private var currentAnimation: SkeletalAnimation? = null private var animationTime = 0.0f private var animationLastFrame = -1L - private var transforms: List = listOf() + private var transforms: List = emptyList() var light: Int = 0xFF diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/SkeletalModel.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/SkeletalModel.kt index 4ebaf9d87..5323d7ba0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/SkeletalModel.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/skeletal/model/SkeletalModel.kt @@ -32,10 +32,10 @@ data class SkeletalModel( val geometryName: String = name, val visibleBox: Vec3 = Vec3.EMPTY, val resolution: SkeletalResolution = SkeletalResolution(), - val elements: List = listOf(), - val outliner: List = listOf(), - val textures: List = listOf(), - val animations: List = listOf(), + val elements: List = emptyList(), + val outliner: List = emptyList(), + val textures: List = emptyList(), + val animations: List = emptyList(), ) { fun bake(renderWindow: RenderWindow, textureOverride: MutableMap): BakedSkeletalModel { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/textures/properties/AnimationProperties.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/properties/AnimationProperties.kt index 21a9959bf..9c8182565 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/textures/properties/AnimationProperties.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/properties/AnimationProperties.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2021 Moritz Zwerger + * 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. * @@ -23,7 +23,7 @@ data class AnimationProperties( var width: Int = -1, var height: Int = -1, @JsonProperty("frametime") private val frameTime: Int = 1, - @JsonProperty("frames") private val _frames: List = listOf(), + @JsonProperty("frames") private val _frames: List = emptyList(), ) { @JsonIgnore private var initialized = false diff --git a/src/main/java/de/bixilon/minosoft/terminal/RunConfiguration.kt b/src/main/java/de/bixilon/minosoft/terminal/RunConfiguration.kt index 900a2da94..f0c9dd717 100644 --- a/src/main/java/de/bixilon/minosoft/terminal/RunConfiguration.kt +++ b/src/main/java/de/bixilon/minosoft/terminal/RunConfiguration.kt @@ -65,7 +65,7 @@ object RunConfiguration { var VERSION_STRING = "Minosoft ${StaticConfiguration.VERSION}" - var SKIP_RENDERERS: List = listOf() + var SKIP_RENDERERS: List = emptyList() var OPEN_Gl_ON_FIRST_THREAD = PlatformInfo.OS == OSTypes.MAC var VERBOSE_LOGGING = false