This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-310 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 5136f5c0062421dcdb87170d89aa959f4123f3fd Author: Sergey Kamov <[email protected]> AuthorDate: Wed Apr 28 12:00:38 2021 +0300 Minecraft example cleaned up. --- nlpcraft-examples/lightswitch/pom.xml | 5 + .../example/minecraft/NCMinecraftExampleMod.java | 12 +- .../minecraft/utils/NCMinecraftFilesDump.java | 136 +++++++++++++++++++++ .../minecraft/utils/NCMinecraftFilesDump.java | 88 ------------- nlpcraft-examples/minecraft/pom.xml | 5 + ...Processor.kt => MinecraftFillMatchProcessor.kt} | 3 +- .../nlpcraft/example/minecraft/MinecraftModel.kt | 6 +- .../example/minecraft/MinecraftValueLoader.kt | 5 +- .../minecraft/src/main/resources/block.json | 15 ++- .../minecraft/src/main/resources/item.json | 15 ++- .../example/minecraft/NCMinecraftModelSpec.kt | 1 + 11 files changed, 178 insertions(+), 113 deletions(-) diff --git a/nlpcraft-examples/lightswitch/pom.xml b/nlpcraft-examples/lightswitch/pom.xml index 470a86d..0557ecf 100644 --- a/nlpcraft-examples/lightswitch/pom.xml +++ b/nlpcraft-examples/lightswitch/pom.xml @@ -53,6 +53,11 @@ <version>${kotlin.ver}</version> </dependency> <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-reflect</artifactId> + <version>${kotlin.ver}</version> + </dependency> + <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy</artifactId> <version>${groovy.ver}</version> diff --git a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCMinecraftExampleMod.java b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/NCMinecraftExampleMod.java similarity index 95% rename from nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCMinecraftExampleMod.java rename to nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/NCMinecraftExampleMod.java index 1adb1c9..280c9d2 100644 --- a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/NCMinecraftExampleMod.java +++ b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/NCMinecraftExampleMod.java @@ -16,7 +16,7 @@ * */ -package org.apache.nplcraft.example.minecraft; +package org.apache.nlpcraft.example.minecraft; import com.google.gson.Gson; import net.minecraft.server.MinecraftServer; @@ -38,8 +38,8 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Paths; -import java.util.HashSet; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; /** * Minecraft example mod for the Minecraft Forge server. @@ -57,7 +57,7 @@ public class NCMinecraftExampleMod { private static final String MODEL_ID = "nlpcraft.minecraft.ex"; private static final Gson GSON = new Gson(); - private final Set<String> convCmds = new HashSet<>(); + private final Set<String> convCmds = ConcurrentHashMap.newKeySet(); private NCSignIn creds; private String baseUrl; @@ -277,12 +277,12 @@ public class NCMinecraftExampleMod { FileUtil.resolveResourcePath(Paths.get("config"), "nlpcraft-settings", ".json") ) ) { - NCSettings settings = GSON.fromJson(reader, NCSettings.class); + NCSettings setts = GSON.fromJson(reader, NCSettings.class); LOGGER.info("Credentials file loaded."); - this.creds = mkSignin(settings.email, settings.passwd); - this.baseUrl = "http://" + settings.host + ":" + settings.port + "/api/v1/"; + this.creds = mkSignin(setts.email, setts.passwd); + this.baseUrl = "http://" + setts.host + ":" + setts.port + "/api/v1/"; return; } diff --git a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/utils/NCMinecraftFilesDump.java b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/utils/NCMinecraftFilesDump.java new file mode 100644 index 0000000..98d35d3 --- /dev/null +++ b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nlpcraft/example/minecraft/utils/NCMinecraftFilesDump.java @@ -0,0 +1,136 @@ +/* + * 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.minecraft.utils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mojang.serialization.Lifecycle; +import net.minecraft.block.Block; +import net.minecraft.util.RegistryKey; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.registry.DefaultedRegistry; +import net.minecraft.util.registry.Registry; +import net.minecraftforge.registries.ForgeRegistryEntry; +import net.minecraftforge.registries.GameData; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Date; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Utility for getting data from minecraft. + * These values are used for preparing synonyms for user defined elements. + */ +public class NCMinecraftFilesDump { + private final static Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + private static class Json { + private String version; + private Map<String, String> data; + } + + private static <T extends ForgeRegistryEntry<?>> void write(String file, DefaultedRegistry<T> reg, String ver) throws IOException { + Json js = new Json(); + + js.version = ver; + + // Regular name -> registry name. + js.data = + reg.stream().filter(x -> x.getRegistryName() != null). + collect(Collectors.toMap( + x -> transformPath(x.getRegistryName().getPath()), + x -> x.getRegistryName().toString()) + ); + // Add matching like grass -> grass_block. + js.data.putAll(reg.stream() + .filter(x -> x.getRegistryName() != null && x.getRegistryName().getPath().endsWith("_block")) + .collect(Collectors.toMap( + x -> transformPath(x.getRegistryName().getPath().replace("_block", "")), + x -> x.getRegistryName().toString()) + ) + ); + + File f = new File(file + ".json"); + + try (BufferedWriter wr = new BufferedWriter(new FileWriter(f))) { + write(wr, "/*"); + write(wr, " *"); + write(wr, " * Licensed to the Apache Software Foundation (ASF) under one or more"); + write(wr, " * contributor license agreements. See the NOTICE file distributed with"); + write(wr, " * this work for additional information regarding copyright ownership."); + write(wr, " * The ASF licenses this file to You under the Apache License, Version 2.0"); + write(wr, " * (the 'License'); you may not use this file except in compliance with"); + write(wr, " * the License. You may obtain a copy of the License at"); + write(wr, " *"); + write(wr, " * http://www.apache.org/licenses/LICENSE-2.0"); + write(wr, " *"); + write(wr, " * Unless required by applicable law or agreed to in writing, software"); + write(wr, " * distributed under the License is distributed on an 'AS IS' BASIS,"); + write(wr, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied."); + write(wr, " * See the License for the specific language governing permissions and"); + write(wr, " * limitations under the License."); + write(wr, " *"); + write(wr, " * Auto-generated on: $" + new Date()); + write(wr, " *"); + write(wr, " * Dump file with minecraft item game objects. Was made for specified game version (see below)"); + write(wr, " * Generated with dumpItems task in minecraft-mod module"); + write(wr, " *"); + write(wr, " */"); + + wr.newLine(); + + wr.write(GSON.toJson(js)); + } + + System.out.println("File prepared:" + f.getAbsolutePath()); + } + + private static void write(BufferedWriter wr, String line) throws IOException { + wr.write(line); + wr.newLine(); + } + + private static String transformPath(String path) { + return path.replaceAll("_", " "); + } + + /** + * Application entry point. It requires 2 arguments: registry name (`block` and `item`) and version number. + * + * @param args Command line arguments. + */ + public static void main(String[] args) throws IOException { + if (args.length != 2) + throw new IllegalStateException("Unexpected arguments count, 2 expected."); + + String type = args[0]; + String ver = args[1]; + + if (!type.equals("block") && !type.equals("item")) + throw new IllegalStateException("Unexpected type:" + type); + + RegistryKey<Registry<Block>> key = RegistryKey.getOrCreateRootKey(new ResourceLocation(type)); + + write(type, GameData.getWrapper(key, Lifecycle.experimental(), "air"), ver); + } +} diff --git a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/NCMinecraftFilesDump.java b/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/NCMinecraftFilesDump.java deleted file mode 100644 index 8fdacdf..0000000 --- a/nlpcraft-examples/minecraft-mod/src/main/java/org/apache/nplcraft/example/minecraft/utils/NCMinecraftFilesDump.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.nplcraft.example.minecraft.utils; - -import com.google.gson.Gson; -import java.util.Map; -import java.util.stream.Collectors; -import net.minecraft.util.registry.DefaultedRegistry; -import net.minecraft.util.registry.Registry; -import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.ForgeRegistry; -import net.minecraftforge.registries.ForgeRegistryEntry; - -/** - * Utility for getting data from minecraft. These values are used for preparing synonyms for user defined elements. - */ -public class NCMinecraftFilesDump { - private final static Gson GSON = new Gson(); - - private static class Dump { - private String version; - private Map<String, String> data; - } - - private static <T extends ForgeRegistryEntry<?>> void dumpRegistry(DefaultedRegistry<T> registry, String version) { - Dump dump = new Dump(); - - dump.version = version; - - // Regular name -> registry name. - dump.data = - registry.stream().filter(x -> x.getRegistryName() != null). - collect(Collectors.toMap( - x -> transformPath(x.getRegistryName().getPath()), - x -> x.getRegistryName().toString()) - ); - // Add matching like grass -> grass_block. - dump.data.putAll(registry.stream() - .filter(x -> x.getRegistryName() != null && x.getRegistryName().getPath().endsWith("_block")) - .collect(Collectors.toMap( - x -> transformPath(x.getRegistryName().getPath().replace("_block", "")), - x -> x.getRegistryName().toString()) - ) - ); - - System.out.println(GSON.toJson(dump)); - } - - private static String transformPath(String path) { - return path.replaceAll("_", " "); - } - - /** - * App entry point. - * - * @param args Command line arguments. - */ - public static void main(String[] args) { - String type = args[0]; - String version = args[1]; - - if (type.equals("block")) { - dumpRegistry(Registry.BLOCK, version); - } - else if (type.equals("item")) { - dumpRegistry(Registry.ITEM, version); - } - else { - System.err.println("Unknown type"); - } - } -} diff --git a/nlpcraft-examples/minecraft/pom.xml b/nlpcraft-examples/minecraft/pom.xml index f4f7efd..bb955bb 100644 --- a/nlpcraft-examples/minecraft/pom.xml +++ b/nlpcraft-examples/minecraft/pom.xml @@ -54,6 +54,11 @@ <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.ver}</version> </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-reflect</artifactId> + <version>${kotlin.ver}</version> + </dependency> <!-- Test dependencies. --> <dependency> diff --git a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFIllMatchProcessor.kt b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFillMatchProcessor.kt similarity index 98% rename from nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFIllMatchProcessor.kt rename to nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFillMatchProcessor.kt index c1a4f27..f4dfdbe 100644 --- a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFIllMatchProcessor.kt +++ b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftFillMatchProcessor.kt @@ -26,7 +26,7 @@ import java.util.* * Special processor for support 'fillIntent' intent processing. * Is is designed as separated class to simplify main model class. */ -class MinecraftFIllMatchProcessor { +class MinecraftFillMatchProcessor { internal data class Coordinate(val x: Int = 0, val y: Int = 0, val z: Int = 0) { override fun toString(): String { return "$x $y $z" @@ -43,7 +43,6 @@ class MinecraftFIllMatchProcessor { companion object { fun process( - ctx: NCIntentMatch, @NCIntentTerm("shape") shape: NCToken, @NCIntentTerm("block") blockToken: NCToken, @NCIntentTerm("len") length: Optional<NCToken>, diff --git a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt index 5191776..ae41169 100644 --- a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt +++ b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftModel.kt @@ -31,9 +31,8 @@ import java.util.* */ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") { private fun checkAmbiguous(ctx: NCIntentMatch) { - if (ctx.isAmbiguous) { + if (ctx.isAmbiguous) throw NCRejection("Ambiguous request") - } } /** @@ -128,12 +127,11 @@ class MinecraftModel : NCModelFileAdapter("minecraft.yaml") { "make a box of sand with the size of 2 10 meters in front of me" ) fun onFillMatch( - ctx: NCIntentMatch, @NCIntentTerm("shape") shape: NCToken, @NCIntentTerm("block") block: NCToken, @NCIntentTerm("len") length: Optional<NCToken>, @NCIntentTerm("position") position: NCToken, ): NCResult { - return MinecraftFIllMatchProcessor.process(ctx, shape, block, length, position) + return MinecraftFillMatchProcessor.process(shape, block, length, position) } } diff --git a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftValueLoader.kt b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftValueLoader.kt index 665a575..4fa6ffc 100644 --- a/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftValueLoader.kt +++ b/nlpcraft-examples/minecraft/src/main/kotlin/org/apache/nlpcraft/example/minecraft/MinecraftValueLoader.kt @@ -28,7 +28,7 @@ import org.apache.nlpcraft.model.NCValueLoader /** * Data loader from JSON data files. - * These files which prepared via 'minectaft-mod' module org.apache.nplcraft.example.minecraft.utils.GameFilesDump + * These files which prepared via 'minecraft-mod' module, org.apache.nlpcraft.example.minecraft.utils.NCMinecraftFilesDump * for this supported `minecraft` server version. */ class MinecraftValueLoader : NCValueLoader { @@ -73,5 +73,4 @@ class MinecraftValueLoader : NCValueLoader { return dump.data.map { x -> NCMinecraftValue(x.key, x.value) }.toMutableSet() } -} - +} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft/src/main/resources/block.json b/nlpcraft-examples/minecraft/src/main/resources/block.json index b8c392e..c9be25c 100644 --- a/nlpcraft-examples/minecraft/src/main/resources/block.json +++ b/nlpcraft-examples/minecraft/src/main/resources/block.json @@ -1,21 +1,26 @@ /* + * * 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 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, + * 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. + * + * Auto-generated on: $Wed Apr 28 11:38:52 MSK 2021 + * + * Dump file with minecraft item game objects. Was made for specified game version (see below) + * Generated with dumpItems task in minecraft-mod module + * */ -// Dump file with minecraft block game objects. Was made for specified game version (see below) -// Generated with dumpBlocks task in minecraft-mod module { "version": "1.16.4", @@ -808,4 +813,4 @@ "pink shulker box": "minecraft:pink_shulker_box", "crimson button": "minecraft:crimson_button" } -} +} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft/src/main/resources/item.json b/nlpcraft-examples/minecraft/src/main/resources/item.json index 1856f3c..c656457 100644 --- a/nlpcraft-examples/minecraft/src/main/resources/item.json +++ b/nlpcraft-examples/minecraft/src/main/resources/item.json @@ -1,21 +1,26 @@ /* + * * 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 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, + * 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. + * + * Auto-generated on: $Wed Apr 28 11:37:09 MSK 2021 + * + * Dump file with minecraft item game objects. Was made for specified game version (see below) + * Generated with dumpItems task in minecraft-mod module + * */ -// Dump file with minecraft item game objects. Was made for specified game version (see below) -// Generated with dumpItems task in minecraft-mod module { "version": "1.16.4", @@ -1013,4 +1018,4 @@ "lapis lazuli": "minecraft:lapis_lazuli", "carrot": "minecraft:carrot" } -} +} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt b/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt index 811df6b..9d065d3 100644 --- a/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt +++ b/nlpcraft-examples/minecraft/src/test/kotlin/org/apache/nlpcraft/example/minecraft/NCMinecraftModelSpec.kt @@ -31,6 +31,7 @@ class NCMinecraftModelSpec : NCTestContext() { @Test fun test() { val res = client.ask("make a box of sand with the size of 2 10 meters in front of me") + assertEquals( "execute at @p positioned ~0 ~0 ~10 rotated 0 0 run fill ^-1 ^0 ^-1 ^0 ^0 ^0 minecraft:sand", res.result.get()
