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 740261a15face065ec2c473601a08089b7a2672f Author: Ifropc <[email protected]> AuthorDate: Sun Dec 27 18:12:06 2020 -0800 NLPCRAFT-91: Settings support for mod and cleanup data files --- .../org/apache/nplcraft/example/ExampleMod.java | 56 +++++++++------------- nlpcraft-examples/minecraft-model/README.md | 2 +- .../src/main/resources/nlpcraft-credentials.json | 4 -- .../src/main/resources/nlpcraft-settings.json | 6 +++ 4 files changed, 30 insertions(+), 38 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 02628e3..577e890 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 @@ -23,6 +23,7 @@ import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; @@ -42,6 +43,7 @@ public class ExampleMod { private static final String MODEL_ID = "nlpcraft.minecraft.ex"; private final Gson gson = new Gson(); private NCSignIn creds; + private String baseUrl; private MinecraftServer server; private Optional<String> token = Optional.empty(); private boolean inRecursion = false; @@ -89,7 +91,7 @@ public class ExampleMod { private Optional<String> getToken() { if (!token.isPresent()) { - obtainCreds(); + loadSettings(); token = post("signin", gson.toJson(creds), NCSignResponse.class).map(x -> x.acsTok); } @@ -99,7 +101,7 @@ public class ExampleMod { private <T> Optional<T> post(String url, String postJson, Class<T> clazz) { try { - String str = "http://0.0.0.0:8081/api/v1/" + url; + String str = baseUrl + url; HttpURLConnection http = (HttpURLConnection) new URL(str).openConnection(); http.setRequestMethod("POST"); // PUT is another valid option @@ -127,52 +129,43 @@ public class ExampleMod { return Optional.empty(); } - private void obtainCreds() { + private void loadSettings() { creds = new NCSignIn(); creds.email = "[email protected]"; creds.passwd = "admin"; + String host = "0.0.0.0"; + String port = "8081"; Path configDir = Paths.get("config"); - Path jsonPath = FileUtil.resolveResourcePath(configDir, "nlpcraft-credentials", ".json"); + Path jsonPath = FileUtil.resolveResourcePath(configDir, "nlpcraft-settings", ".json"); try { Reader reader = Files.newBufferedReader(jsonPath); - creds = gson.fromJson(reader, NCSignIn.class); - } catch (FileNotFoundException e) { + NCSettings settings = gson.fromJson(reader, NCSettings.class); + creds.email = settings.email; + creds.passwd = settings.passwd; + host = settings.host; + port = settings.port; + } catch (NoSuchFileException e) { LOGGER.info("Credentials were not found"); } catch (IOException e) { LOGGER.error(e); } + + baseUrl = "http://" + host + ":" + port + "/api/v1/"; } private class AskParams { private final String mdlId = MODEL_ID; private String acsTok; private String txt; - - @Override - public String toString() { - return "AskParams{" + - "acsTok='" + acsTok + '\'' + - ", mdlId='" + mdlId + '\'' + - ", txt='" + txt + '\'' + - '}'; - } } private class NCResponse { private String status; private NCState state; - - @Override - public String toString() { - return "NCResponse{" + - "status='" + status + '\'' + - ", state=" + state + - '}'; - } } private class NCState { @@ -180,16 +173,6 @@ public class ExampleMod { private String error; private String status; private String resBody; - - @Override - public String toString() { - return "NCState{" + - "errorCode=" + errorCode + - ", error='" + error + '\'' + - ", status='" + status + '\'' + - ", resBody='" + resBody + '\'' + - '}'; - } } private class NCSignIn { @@ -200,4 +183,11 @@ public class ExampleMod { private class NCSignResponse { private String acsTok; } + + private class NCSettings { + private String email; + private String passwd; + private String host; + private String port; + } } diff --git a/nlpcraft-examples/minecraft-model/README.md b/nlpcraft-examples/minecraft-model/README.md index 1fd3605..6b504e3 100644 --- a/nlpcraft-examples/minecraft-model/README.md +++ b/nlpcraft-examples/minecraft-model/README.md @@ -35,7 +35,7 @@ Start server normally. For running probe it's required to use dedicated configur 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. (Optional) If non-default settings are used, put them in `main/resources/nlpcraft-settings.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! diff --git a/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json b/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json deleted file mode 100644 index cb7ceef..0000000 --- a/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-credentials.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "email": "[email protected]", - "passwd": "admin" -} \ No newline at end of file diff --git a/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-settings.json b/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-settings.json new file mode 100644 index 0000000..e1a4eae --- /dev/null +++ b/nlpcraft-examples/minecraft-model/src/main/resources/nlpcraft-settings.json @@ -0,0 +1,6 @@ +{ + "email": "[email protected]", + "passwd": "admin", + "host": "0.0.0.0", + "port": 8081 +} \ No newline at end of file
