This is an automated email from the ASF dual-hosted git repository. ifropc pushed a commit to branch NLPCRAFT-91 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit d45d88a7ca988e824db232d3427c33c02544ac98 Author: Ifropc <[email protected]> AuthorDate: Thu Feb 11 00:24:48 2021 -0800 NLPCRAFT-91: Support basic /fill command (different shapes and static position) --- .../org/apache/nplcraft/example/ExampleMod.java | 5 ++- .../apache/nlpcraft/example/FIllMatchProcessor.kt | 52 ++++++++++++++++++++++ .../org/apache/nlpcraft/example/MinecraftModel.kt | 17 +------ .../kotlin/org/apache/nlpcraft/example/Utils.kt | 49 ++++++++++++++++++++ .../src/main/resources/minecraft.yaml | 7 ++- 5 files changed, 109 insertions(+), 21 deletions(-) diff --git a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java index c3c349f..f76ccd8 100644 --- a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java +++ b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/ExampleMod.java @@ -31,6 +31,7 @@ import java.util.Optional; import java.util.stream.Collectors; import net.minecraft.server.MinecraftServer; import net.minecraft.util.FileUtil; +import net.minecraft.util.math.vector.Vector2f; import net.minecraft.util.registry.DefaultedRegistry; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.CommandEvent; @@ -68,7 +69,7 @@ public class ExampleMod { return; } String command = event.getParseResults().getReader().getString(); - LOGGER.debug("Processing command:" + command); + Vector2f rotation = event.getParseResults().getContext().getSource().getRotation(); askProbe(command).map(r -> r.state) .filter(s -> s.errorCode == null) .map(s -> s.resBody) @@ -134,7 +135,7 @@ public class ExampleMod { http.setRequestMethod("POST"); // PUT is another valid option http.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); http.setConnectTimeout(1_000); - http.setReadTimeout(1_000); + http.setReadTimeout(3_000); http.setDoOutput(true); DataOutputStream wr = new DataOutputStream(http.getOutputStream()); diff --git a/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/FIllMatchProcessor.kt b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/FIllMatchProcessor.kt new file mode 100644 index 0000000..b43694b --- /dev/null +++ b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/FIllMatchProcessor.kt @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.nlpcraft.example + +import org.apache.nlpcraft.example.MinecraftObjectValueLoader.Companion.dumps +import org.apache.nlpcraft.model.* + +class FIllMatchProcessor { + companion object { + fun process( + ctx: NCIntentMatch, + @NCIntentTerm("shape") shape: NCToken, + @NCIntentTerm("block") blockToken: NCToken, + @NCIntentTerm("len") length: NCToken, + @NCIntentTerm("position") position: NCToken + ): NCResult { + val (from, to) = resultCoordinates(length = length.toInt(), shape.id) + val block = dumps["item"]!![blockToken.value]!! + + // TODO: Use user rotation + // TODO: handle y coordinate for cube + return NCResult.text("execute at @p positioned ~ ~ ~*1 rotated 0 0 run fill ${from.relativeRotated()} ${to.relativeRotated()} $block") + } + + private fun resultCoordinates(length: Int, shape: String): Pair<Coordinate, Coordinate> { + return when (shape) { + "line" -> Coordinate(-length / 2) to Coordinate((length - 1) / 2) + "square" -> Coordinate(-length / 2,0, -length / 2) to Coordinate((length - 1) / 2, 0,(length - 1) / 2) + "cube" -> Coordinate(-length / 2,-length / 2, -length / 2) to Coordinate((length - 1) / 2, (length - 1) / 2,(length - 1) / 2) + else -> { + throw NCRejection("Unsupported shape") + } + } + } + } +} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/MinecraftModel.kt b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/MinecraftModel.kt index 99a5ad2..d4ed279 100644 --- a/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/MinecraftModel.kt +++ b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/MinecraftModel.kt @@ -25,8 +25,6 @@ import java.util.* @Suppress("unused") class MinecraftModel : NCModelFileAdapter("minecraft.yaml") { - private var firstPersonWords = setOf("me", "my", "i") - @NCIntentRef("weatherIntent") fun onWeatherMatch(ctx: NCIntentMatch, @NCIntentTerm("arg") tok: NCToken): NCResult { if (ctx.isAmbiguous) { @@ -68,10 +66,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") { val itemRegistry = dumps["item"]!![item.value]!! val player = player(target.partTokens[1]) - val itemQuantity = quantity - .flatMap { x -> x.metaOpt<Double>("nlpcraft:num:from") } - .map { x -> x.toLong() } - .orElse(1) + val itemQuantity = quantity.map(NCToken::toInt).orElse(1) return NCResult.text("give $player $itemRegistry $itemQuantity") } @@ -84,14 +79,6 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") { @NCIntentTerm("len") length: NCToken, @NCIntentTerm("position") position: NCToken, ): NCResult { - TODO() - } - - private fun NCToken.normText(): String { - return this.meta("nlpcraft:nlp:normtext") - } - - private fun player(target: NCToken): String { - return if (firstPersonWords.contains(target.normText())) "@p" else target.originalText ?: "@p" + return FIllMatchProcessor.process(ctx, shape, block, length, position) } } diff --git a/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/Utils.kt b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/Utils.kt new file mode 100644 index 0000000..79fc784 --- /dev/null +++ b/nlpcraft-examples/minecraft-model/src/main/kotlin/org/apache/nlpcraft/example/Utils.kt @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.nlpcraft.example + +import org.apache.nlpcraft.model.NCToken + +private var firstPersonWords = setOf("me", "my", "i") + +internal fun NCToken.normText(): String { + return this.meta("nlpcraft:nlp:normtext") +} + +internal fun NCToken.toInt(): Int { + return this.meta<Double>("nlpcraft:num:from").toInt() +} + +internal fun player(target: NCToken): String { + return if (firstPersonWords.contains(target.normText())) "@p" else target.originalText ?: "@p" +} + +internal data class Coordinate(val x: Int = 0, val y: Int = 0, val z: Int = 0) { + override fun toString(): String { + return "$x $y $z" + } + + fun relative(): String { + return "~$x ~$y ~$z" + } + + fun relativeRotated(): String { + return "^$x ^$y ^$z" + } +} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml b/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml index 6d356bd..8ff6b5a 100644 --- a/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml +++ b/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml @@ -24,8 +24,10 @@ description: Minecraft Model. ] macros: +# - name: "<PLAYER_NAME>" +# macro: "{me|I|my|//[a-zA-Z0-9]+//}" - name: "<PLAYER_NAME>" - macro: "{me|I|my|//[a-zA-Z0-9]+//}" + macro: "{me|I|my}" elements: # General synonyms @@ -108,9 +110,6 @@ elements: - id: cube groups: - fill:shape - - id: sphere - groups: - - fill:shape - id: square groups: - fill:shape
