You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by if...@apache.org on 2021/03/06 18:08:35 UTC

[incubator-nlpcraft] 23/26: NLPCRAFT-91: Finish fill command

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 241aa8fd51e411629d1462ad98c6c9ad7aca1260
Author: Ifropc <if...@apache.org>
AuthorDate: Thu Feb 18 19:41:36 2021 -0800

    NLPCRAFT-91: Finish fill command
---
 nlpcraft-examples/minecraft-model/README.md        | 11 +++---
 .../apache/nlpcraft/example/FIllMatchProcessor.kt  | 39 ++++++++++++++++++++--
 .../org/apache/nlpcraft/example/MinecraftModel.kt  |  4 +--
 .../kotlin/org/apache/nlpcraft/example/Utils.kt    |  4 +--
 .../src/main/resources/minecraft.yaml              |  6 ++--
 5 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/nlpcraft-examples/minecraft-model/README.md b/nlpcraft-examples/minecraft-model/README.md
index 348cd79..63dd1fa 100644
--- a/nlpcraft-examples/minecraft-model/README.md
+++ b/nlpcraft-examples/minecraft-model/README.md
@@ -45,11 +45,12 @@ use modded client, so vanilla client could be used. Commands could be either inv
 on client side, prefixed with slash (`/make it sunny`)
 
 ### Supported commands
-| Command | Example of usage |
-| :---: |:---:|
-`/weather` | Make it rain | 
-`/time` | Set current time to evening | 
-`/give` | Give me iron sword | 
+| Command | Example of usage | Commentary
+| :---: |:---:|:---|
+`/weather` | Make it rain | | 
+`/time` | Set current time to evening | |
+`/give` | Give me iron sword | You can specify quanity |
+`/fill` | Make a cube of gold near me | Can specify length (size of 10, length 10), position (10 meters in front of me/other player) |
 
 ### Copyright
 Copyright (C) 2020 Apache Software Foundation
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
index 26d617e..06db335 100644
--- 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
@@ -20,6 +20,7 @@ package org.apache.nlpcraft.example
 
 import org.apache.nlpcraft.example.MinecraftObjectValueLoader.Companion.dumps
 import org.apache.nlpcraft.model.*
+import java.util.*
 
 class FIllMatchProcessor {
     companion object {
@@ -27,15 +28,20 @@ class FIllMatchProcessor {
             ctx: NCIntentMatch,
             @NCIntentTerm("shape") shape: NCToken,
             @NCIntentTerm("block") blockToken: NCToken,
-            @NCIntentTerm("len") length: NCToken,
+            @NCIntentTerm("len") length: Optional<NCToken>,
             @NCIntentTerm("position") position: NCToken
         ): NCResult {
-            val (from, to) = resultCoordinates(length = length.toInt(), shape.id)
+            val (from, to) = resultCoordinates(transformLength(length), shape.id)
             val block = dumps["item"]!![blockToken.value]!!
+            val player = findPlayer(position)
+            val positionCoordinate = positionCoordinate(position)
 
             // TODO: Use user rotation
             // TODO: handle y coordinate for cube
-            return NCResult.text("execute at @p positioned ~ ~ ~ rotated 0 0 run fill ${from.relativeRotated()} ${to.relativeRotated()} $block")
+            return NCResult.text(
+                "execute at $player positioned ${positionCoordinate.relative()} rotated 0 0 run " +
+                        "fill ${from.relativeRotated()} ${to.relativeRotated()} $block"
+            )
         }
 
         private fun resultCoordinates(length: Int, shape: String): Pair<Coordinate, Coordinate> {
@@ -51,5 +57,32 @@ class FIllMatchProcessor {
                 }
             }
         }
+
+        private fun positionCoordinate(position: NCToken): Coordinate {
+            return when (position.id ) {
+                "position:player" -> Coordinate()
+                "position:front" -> Coordinate(0, 0, transformLength(Optional.of(position), 10))
+                else -> {
+                    throw NCRejection("Unsupported position")
+                }
+            }
+        }
+
+        private fun transformLength(length: Optional<NCToken>, default: Int = 5): Int {
+            return length.flatMap { x ->
+                x.partTokens.stream()
+                    .filter { it.id == "nlpcraft:num" }
+                    .findAny()
+                    .map { it.toInt() }
+            }.orElse(default)
+        }
+
+        private fun findPlayer(position: NCToken): String {
+            return position.partTokens.stream()
+                .filter { it.id == "mc:player" }
+                .findAny()
+                .orElseThrow { NCRejection("") }
+                .player()
+        }
     }
 }
\ 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 b92c57c..2cc667c 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
@@ -65,7 +65,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
         }
 
         val itemRegistry = dumps["item"]!![item.value]!!
-        val player = player(target.partTokens[1])
+        val player = target.partTokens[1].player()
         val itemQuantity = quantity.map(NCToken::toInt).orElse(1)
 
         return NCResult.text("give $player $itemRegistry $itemQuantity")
@@ -76,7 +76,7 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") {
         ctx: NCIntentMatch,
         @NCIntentTerm("shape") shape: NCToken,
         @NCIntentTerm("block") block: NCToken,
-        @NCIntentTerm("len") length: NCToken,
+        @NCIntentTerm("len") length: Optional<NCToken>,
         @NCIntentTerm("position") position: NCToken,
     ): NCResult {
         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
index 79fc784..ae52a10 100644
--- 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
@@ -30,8 +30,8 @@ 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 fun NCToken.player(): String {
+    return if (firstPersonWords.contains(this.normText())) "@p" else this.originalText ?: "@p"
 }
 
 internal data class Coordinate(val x: Int = 0, val y: Int = 0, val z: Int = 0) {
diff --git a/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml b/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml
index 31762b1..99cee41 100644
--- a/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml
+++ b/nlpcraft-examples/minecraft-model/src/main/resources/minecraft.yaml
@@ -123,7 +123,7 @@ elements:
       - "wall"
   - id: fill:length
     synonyms:
-      - "{{size|length|diameter} {of|*}}"
+      - "{{size|length|diameter} {of|*} ^^[length](id == 'nlpcraft:num')^^}"
   - id: position:player
     groups:
       - fill:position
@@ -135,7 +135,7 @@ elements:
     groups:
       - fill:position
     synonyms:
-      - "{{{^^[distance](id == 'nlpcraft:num')^^ m|meter|meters|ft|feet}|*} {in|*} front {of|*} ^^[player](id == 'mc:player')^^}"
+      - "{{^^[distance](id == 'nlpcraft:num')^^|*} {in|*} front {of|*} ^^[player](id == 'mc:player')^^}"
     "permutateSynonyms": false
 
 
@@ -148,7 +148,7 @@ intents:
   - "intent=giveIntent term(action)={id == 'give:action'} term(quantity)={id == 'nlpcraft:num'}?
   term(item)={id == 'mc:item'} term={id == 'give:block-word'}?"
   - "intent=fillIntent term={id == 'fill:action'} term(shape)={groups @@ 'fill:shape'} term(block)={id == 'mc:item'}
-  term={id == 'fill:length'}? term(len)={id == 'nlpcraft:num'} term(position)={groups @@ 'fill:position'}"
+  term(len)={id == 'fill:length'}? term(position)={groups @@ 'fill:position'}"
 
 # give me sand
 swearWordsAllowed: true