Naton 1028 Posted August 31, 2021 Share Posted August 31, 2021 Often times you'll want to save/load script settings, especially if you provide a GUI to your users. Nobody wants to have to fill out the same settings over and over. The TRiBot Script SDK provides a utility class to assist with saving and loading settings. It uses Gson internally and therefore saves settings as a json file. See org.tribot.script.sdk.util.ScriptSettings Create an instance via ScriptSettings.builder() // configure here .build() // or ScriptSettings.getDefault() // for default configuration The builder allows you to specify things such as the directory to save in (must be in .tribot/script-config), and a custom gson instance if you need special configuration. These fields will be configured with sensible defaults if not explicitly set. You will need a specific class to hold your settings. This object will be serialized and deserialized to a json file. Example: @Data public class BlastFurnaceSettings { private String barType; // this should really be an enum but for simplicity here we'll use a string private boolean useCoalBag; private boolean useStaminas; private int prefWorld; } Suppose we have an instance of this class. We can save it by creating a ScriptSettings object and saving it. We'll use the default ScriptSettings handler for now. BlastFurnaceSettings settings = new BlastFurnaceSettings(); // These would probably be configured based on some GUI settings but we'll hard code them here settings.setBarType("Iron"); settings.setPrefWorld(350); settings.setUseStaminas(true); ScriptSettings handler = ScriptSettings.getDefault(); handler.save("my-settings-file", settings); By default, this will save to .tribot/script-config/script-name with a file name of my-settings-file.json so the full path would be .tribot/script-config/script-name/my-settings-file.json Note that currently your files must be in the .tribot/script-config folder. Now, we'll load the settings above. ScriptSettings handler = ScriptSettings.getDefault(); Optional<BlastFurnaceSettings> settings = handler.load("my-settings-file", BlastFurnaceSettings.class); // now do whatever with your settings, the optional will be empty if the settings did not exist The script settings class provides a method to list all of the settings files if you want to show all settings to a user to select. ScriptSettings handler = ... // some instance, obtained however you want List<String> allSaveFileNames = handler.getSaveNames(); Alternatively, it also provides a way to get the base directory to provide to some file picker/chooser in a UI, such as https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html ScriptSettings handler = ... // some instance, obtained however you want File settingsDirectory = handler.getDirectory(); You can also programmatically delete a settings file. ScriptSettings handler = ... // some instance, obtained however you want if (handler.delete("my-settings-file")) { // deleted successfully } All together in an example script: package scripts; import lombok.Data; import lombok.val; import org.tribot.script.sdk.Log; import org.tribot.script.sdk.script.TribotScript; import org.tribot.script.sdk.util.ScriptSettings; public class SdkTest implements TribotScript { @Override public void execute(String args) { val settings = new BlastFurnaceSettings(); settings.setBarType("Iron"); settings.setPrefWorld(350); settings.setUseStaminas(true); val settingsHandler = ScriptSettings.getDefault(); settingsHandler.save("my-settings-file", settings); settingsHandler.load("my-settings-file", BlastFurnaceSettings.class) .ifPresent(s -> { Log.log("Loaded settings object: " + s); }); Log.log("All settings names: " + settingsHandler.getSaveNames()); Log.log("Settings dir: " + settingsHandler.getDirectory()); if (settingsHandler.delete("my-settings-file")) { Log.log("Deleted settings successfully"); } } @Data public static class BlastFurnaceSettings { private String barType; // this should really be an enum but for simplicity here we'll use a string private boolean useCoalBag; private boolean useStaminas; private int prefWorld; } } Output: [19:03:34] Script Started: SdkTest. [19:03:34] Loaded settings object: SdkTest.BlastFurnaceSettings(barType=Iron, useCoalBag=false, useStaminas=true, prefWorld=350) [19:03:34] All settings names: [my-settings-file, test] [19:03:34] Settings dir: C:\Users\Nate\AppData\Roaming\.tribot\script-config\SdkTest [19:03:34] Deleted settings successfully [19:03:34] Script Ended: SdkTest. Quote View all my bots on the TRiBot store! (premium) (free) Need assistance with any of my bots? Join the nScript support discord (link below). nScripting.com nScript Support Discord Refund Policy Automate your bot management through nRestocker and the TRiBot Bulk Launcher Link to comment Share on other sites More sharing options...
Polymorphic 116 Posted October 2, 2021 Share Posted October 2, 2021 (edited) [21:53:28] java.lang.RuntimeException: Failed to invoke public scripts.api.Work() with no args [21:53:28] at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212) [21:53:28] at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) [21:53:28] at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:932) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:897) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:846) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:817) [21:53:28] at org.tribot.script.sdk.util.ScriptSettings.load(ScriptSettings.java:156) I know you can't instantiate abstract classes, but how else do i instantiate different types of work such as Mining, Woodcutting and Motherlode Edited October 2, 2021 by Polymorphic Quote Link to comment Share on other sites More sharing options...
Naton 1028 Posted October 2, 2021 Author Share Posted October 2, 2021 10 hours ago, Polymorphic said: [21:53:28] java.lang.RuntimeException: Failed to invoke public scripts.api.Work() with no args [21:53:28] at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212) [21:53:28] at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41) [21:53:28] at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) [21:53:28] at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:932) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:897) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:846) [21:53:28] at com.google.gson.Gson.fromJson(Gson.java:817) [21:53:28] at org.tribot.script.sdk.util.ScriptSettings.load(ScriptSettings.java:156) I know you can't instantiate abstract classes, but how else do i instantiate different types of work such as Mining, Woodcutting and Motherlode You'll have to look into how to save abstract classes with gson. There's a lot of resources on google. I'd recommend looking into this solution provided by gson https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java Quote View all my bots on the TRiBot store! (premium) (free) Need assistance with any of my bots? Join the nScript support discord (link below). nScripting.com nScript Support Discord Refund Policy Automate your bot management through nRestocker and the TRiBot Bulk Launcher Link to comment Share on other sites More sharing options...
Polymorphic 116 Posted October 2, 2021 Share Posted October 2, 2021 21 minutes ago, Naton said: You'll have to look into how to save abstract classes with gson. There's a lot of resources on google. I'd recommend looking into this solution provided by gson https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java Okay thanks, that should solve my issue Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.