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:22 UTC

[incubator-nlpcraft] 10/26: NLPCRAFT-91: Improve credentials handling

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 c687773c075e8f5be0bc6bfff8ef1e481642b684
Author: Ifropc <if...@apache.org>
AuthorDate: Sun Dec 27 17:49:37 2020 -0800

    NLPCRAFT-91: Improve credentials handling
---
 .../org/apache/nplcraft/example/ExampleMod.java    | 50 +++++++++++++++-------
 nlpcraft-examples/minecraft-model/README.md        | 13 +++---
 .../src/main/resources/nlpcraft-credentials.json   |  4 ++
 3 files changed, 45 insertions(+), 22 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 12b5039..02628e3 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
@@ -19,7 +19,15 @@
 package org.apache.nplcraft.example;
 
 import com.google.gson.Gson;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Optional;
 import net.minecraft.server.MinecraftServer;
+import net.minecraft.util.FileUtil;
 import net.minecraftforge.common.MinecraftForge;
 import net.minecraftforge.event.CommandEvent;
 import net.minecraftforge.eventbus.api.SubscribeEvent;
@@ -28,17 +36,12 @@ import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
-import java.io.BufferedReader;
-import java.io.DataOutputStream;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.Optional;
-
 @Mod("nlpcraft_mod")
 public class ExampleMod {
     private static final Logger LOGGER = LogManager.getLogger();
+    private static final String MODEL_ID = "nlpcraft.minecraft.ex";
     private final Gson gson = new Gson();
+    private NCSignIn creds;
     private MinecraftServer server;
     private Optional<String> token = Optional.empty();
     private boolean inRecursion = false;
@@ -86,12 +89,9 @@ public class ExampleMod {
 
     private Optional<String> getToken() {
         if (!token.isPresent()) {
-            // TODO
-            NCSignIn sign = new NCSignIn();
-            sign.email = "admin@admin.com";
-            sign.passwd = "admin";
+            obtainCreds();
 
-            token = post("signin", gson.toJson(sign), NCSignResponse.class).map(x -> x.acsTok);
+            token = post("signin", gson.toJson(creds), NCSignResponse.class).map(x -> x.acsTok);
         }
 
         return token;
@@ -127,8 +127,28 @@ public class ExampleMod {
         return Optional.empty();
     }
 
+    private void obtainCreds() {
+        creds = new NCSignIn();
+        creds.email = "admin@admin.com";
+        creds.passwd = "admin";
+
+        Path configDir = Paths.get("config");
+
+        Path jsonPath = FileUtil.resolveResourcePath(configDir, "nlpcraft-credentials", ".json");
+
+        try {
+            Reader reader = Files.newBufferedReader(jsonPath);
+
+            creds = gson.fromJson(reader, NCSignIn.class);
+        } catch (FileNotFoundException e) {
+            LOGGER.info("Credentials were not found");
+        } catch (IOException e) {
+            LOGGER.error(e);
+        }
+    }
+
     private class AskParams {
-        private final String mdlId = "nlpcraft.minecraft.ex";
+        private final String mdlId = MODEL_ID;
         private String acsTok;
         private String txt;
 
@@ -173,8 +193,8 @@ public class ExampleMod {
     }
 
     private class NCSignIn {
-        String email;
-        String passwd;
+        private String email;
+        private String passwd;
     }
 
     private class NCSignResponse {
diff --git a/nlpcraft-examples/minecraft-model/README.md b/nlpcraft-examples/minecraft-model/README.md
index 4b1dd2a..1fd3605 100644
--- a/nlpcraft-examples/minecraft-model/README.md
+++ b/nlpcraft-examples/minecraft-model/README.md
@@ -32,13 +32,12 @@ Start server normally. For running probe it's required to use dedicated configur
 
 ### Installation
 1. Download [Minecraft client](https://www.minecraft.net/en-us/download)
-2. Download [Forge server installer](https://files.minecraftforge.net/) and follow instructions
-3. Build mod (`cd ../minecraft-mod && ./gradlew clean build`)
-4. Copy mod to mods folder of your forge server folder (`cp build/libs/nlpcraft-mod-*.jar <forge-server-location>/mods`)
-5. Start server (`java -jar forge.jar`). For detailed instructions refer to [wiki](https://minecraft.gamepedia.com/Tutorials/Setting_up_a_server)
-6. Server commands could be invoked directly from server CLI.
-7. Using Forge client is not required, vanilla client from step 1 could be used
-8. Connect to the server from client and play!
+1. Download [Forge server installer](https://files.minecraftforge.net/) and follow instructions
+1. Build mod (`cd ../minecraft-mod && ./gradlew clean build`)
+1. Copy mod to mods folder of your forge server folder (`cp build/libs/nlpcraft-mod-*.jar <forge-server-location>/mods`)
+1. (Optional) if non-default credentials are used, put them in `main/resources/nlpcraft-credentials.json` and copy file to `<forge-server-location>/config`
+1. Start server (`java -jar forge.jar`). For detailed instructions refer to [wiki](https://minecraft.gamepedia.com/Tutorials/Setting_up_a_server)
+1. Connect to the server from client and play!
 
 ### Usage
 After starting Minecraft server with mod, you can use natural language to invoke certain commands. It's not required to
diff --git a/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json b/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json
new file mode 100644
index 0000000..cb7ceef
--- /dev/null
+++ b/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json
@@ -0,0 +1,4 @@
+{
+  "email": "admin@admin.com",
+  "passwd": "admin"
+}
\ No newline at end of file