You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2021/05/04 09:47:35 UTC

[incubator-nlpcraft] branch master updated: Minecraft example minor improvements.

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/master by this push:
     new 57a0f49  Minecraft example minor improvements.
57a0f49 is described below

commit 57a0f49b4ceae1d5198871d8231069c517fa3986
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Tue May 4 12:47:26 2021 +0300

    Minecraft example minor improvements.
---
 .../assets/nlpcraft-example-minecraft-mod-1.0.jar  | Bin 15986 -> 17857 bytes
 .../example/minecraft/NCMinecraftExampleMod.java   |   8 +-
 .../minecraft/utils/NCMinecraftFilesDump.java      | 123 +++++++++++++++++----
 .../minecraft/src/main/resources/block.json        |  14 ++-
 .../minecraft/src/main/resources/item.json         |  14 ++-
 5 files changed, 123 insertions(+), 36 deletions(-)

diff --git a/nlpcraft-examples/minecraft-mod/assets/nlpcraft-example-minecraft-mod-1.0.jar b/nlpcraft-examples/minecraft-mod/assets/nlpcraft-example-minecraft-mod-1.0.jar
index b5a7152..a72d54c 100644
Binary files a/nlpcraft-examples/minecraft-mod/assets/nlpcraft-example-minecraft-mod-1.0.jar and b/nlpcraft-examples/minecraft-mod/assets/nlpcraft-example-minecraft-mod-1.0.jar differ
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/nplcraft/example/minecraft/NCMinecraftExampleMod.java
index 1adb1c9..1ab1de4 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/nplcraft/example/minecraft/NCMinecraftExampleMod.java
@@ -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;
@@ -117,7 +117,9 @@ public class NCMinecraftExampleMod {
     }
 
     private static class UnauthorizedException extends Exception {
-        // No-op.
+        public UnauthorizedException() {
+            super("Authorization error");
+        }
     }
 
     /**
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
index 8fdacdf..4b09cf1 100644
--- 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
@@ -19,39 +19,68 @@
 package org.apache.nplcraft.example.minecraft.utils;
 
 import com.google.gson.Gson;
-import java.util.Map;
-import java.util.stream.Collectors;
+import com.google.gson.GsonBuilder;
+import com.mojang.serialization.Lifecycle;
+import net.minecraft.item.Item;
+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.ForgeRegistries;
-import net.minecraftforge.registries.ForgeRegistry;
 import net.minecraftforge.registries.ForgeRegistryEntry;
+import net.minecraftforge.registries.GameData;
+import net.minecraftforge.registries.IForgeRegistryEntry;
+import net.minecraft.block.Block;
+
+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 Gson();
+    private final static Gson GSON = new GsonBuilder().setPrettyPrinting().create();
 
-    private static class Dump {
+    private static class Json {
         private String version;
         private Map<String, String> data;
     }
 
-    private static <T extends ForgeRegistryEntry<?>> void dumpRegistry(DefaultedRegistry<T> registry, String version) {
-        Dump dump = new Dump();
+    /**
+     * @param writer
+     * @param s
+     * @throws IOException
+     */
+    private static void write(BufferedWriter writer, String s) throws IOException {
+        writer.write(s);
+        writer.newLine();
+    }
+
+    /**
+     * @param type
+     * @param reg
+     * @param ver
+     * @param <T>
+     * @throws IOException
+     */
+    private static <T extends ForgeRegistryEntry<?>> void write(String type, Registry<T> reg, String ver) throws IOException {
+        Json js = new Json();
 
-        dump.version = version;
+        js.version = ver;
 
         // Regular name -> registry name.
-        dump.data =
-            registry.stream().filter(x -> x.getRegistryName() != null).
+        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.
-        dump.data.putAll(registry.stream()
+        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", "")),
@@ -59,30 +88,82 @@ public class NCMinecraftFilesDump {
             )
         );
 
-        System.out.println(GSON.toJson(dump));
+        File f = new File(type + ".json");
+
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(f))) {
+            write(writer, "/*");
+            write(writer, " * Licensed to the Apache Software Foundation (ASF) under one or more");
+            write(writer, " * contributor license agreements.  See the NOTICE file distributed with");
+            write(writer, " * this work for additional information regarding copyright ownership.");
+            write(writer, " * The ASF licenses this file to You under the Apache License, Version 2.0");
+            write(writer, " * (the 'License'); you may not use this file except in compliance with");
+            write(writer, " * the License.  You may obtain a copy of the License at");
+            write(writer, " *");
+            write(writer, " *     http://www.apache.org/licenses/LICENSE-2.0");
+            write(writer, " *");
+            write(writer, " * Unless required by applicable law or agreed to in writing, software");
+            write(writer, " * distributed under the License is distributed on an 'AS IS' BASIS,");
+            write(writer, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
+            write(writer, " * See the License for the specific language governing permissions and");
+            write(writer, " * limitations under the License.");
+            write(writer, " *");
+            write(writer, " * Auto-generated on: " + new Date());
+            write(writer, " * Dump file with minecraft '" + type + "' game objects.");
+            write(writer, " * Made for the game version: " + ver);
+            write(writer, " */");
+
+            writer.newLine();
+
+            writer.write(GSON.toJson(js));
+        }
+
+        System.out.println("File prepared: " + f.getAbsolutePath());
     }
 
+    /**
+     * @param path
+     * @return
+     */
     private static String transformPath(String path) {
         return path.replaceAll("_", " ");
     }
 
     /**
-     * App entry point.
+     * @param type
+     * @param <T>
+     * @return
+     */
+    private static <T extends IForgeRegistryEntry<T>> DefaultedRegistry<T> mkRegistry(String type) {
+        RegistryKey<Registry<T>> orCreateRootKey = RegistryKey.getOrCreateRootKey(new ResourceLocation(type));
+
+        return GameData.getWrapper(orCreateRootKey, Lifecycle.experimental(), "air");
+    }
+
+    /**
+     * Application entry point.
      *
      * @param args Command line arguments.
+     * @throws IOException If any IO error occurs.
      */
-    public static void main(String[] args) {
+    public static void main(String[] args) throws IOException {
+        if (args.length != 2)
+            throw new IllegalArgumentException("2 mandatory parameters should be defined: 'type' and 'version'.");
+
         String type = args[0];
-        String version = args[1];
+        String ver = args[1];
+
+        if (!type.equals("block") && !type.equals("item"))
+            throw new IllegalArgumentException("Unsupported type, supported are 'block' and 'item'.");
 
         if (type.equals("block")) {
-            dumpRegistry(Registry.BLOCK, version);
-        }
-        else if (type.equals("item")) {
-            dumpRegistry(Registry.ITEM, version);
+            DefaultedRegistry<Block> reg = mkRegistry("block");
+
+            write(type, reg, ver);
         }
         else {
-            System.err.println("Unknown type");
+            DefaultedRegistry<Item> reg = mkRegistry("item");
+
+            write(type, reg, ver);
         }
     }
 }
diff --git a/nlpcraft-examples/minecraft/src/main/resources/block.json b/nlpcraft-examples/minecraft/src/main/resources/block.json
index b8c392e..ebefc91 100644
--- a/nlpcraft-examples/minecraft/src/main/resources/block.json
+++ b/nlpcraft-examples/minecraft/src/main/resources/block.json
@@ -3,19 +3,21 @@
  * 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
+ *     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: Mon May 03 13:04:01 MSK 2021
+ * Dump file with minecraft 'block' game objects.
+ * Made for the game version: 1.16.4
  */
-// 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 +810,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..bfca93b 100644
--- a/nlpcraft-examples/minecraft/src/main/resources/item.json
+++ b/nlpcraft-examples/minecraft/src/main/resources/item.json
@@ -3,19 +3,21 @@
  * 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
+ *     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: Mon May 03 13:04:39 MSK 2021
+ * Dump file with minecraft 'item' game objects.
+ * Made for the game version: 1.16.4
  */
-// 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 +1015,4 @@
     "lapis lazuli": "minecraft:lapis_lazuli",
     "carrot": "minecraft:carrot"
   }
-}
+}
\ No newline at end of file