Getting variables/objects from other scripts
-
I'm trying to make an "Enemies" list to integrate with a suite of custom scripts that I'm developing; I have figured out how to create and use a FriendsConfig object to do this with the following:
FriendsConfig = Java.type("net.ccbluex.liquidbounce.file.configs.FriendsConfig"); jFile = Java.type("java.io.File"); dir = new jFile(mc.mcDataDir, LiquidBounce.CLIENT_NAME + "-" + LiquidBounce.MINECRAFT_VERSION); EnemiesConfig = new FriendsConfig(new jFile(dir, "enemies.json")); EnemiesConfig.createConfig(); saveMethod = EnemiesConfig.getClass().getDeclaredMethod("saveConfig"); saveMethod.setAccessible(true); loadMethod = EnemiesConfig.getClass().getDeclaredMethod("loadConfig"); loadMethod.setAccessible(true); loadMethod.invoke(EnemiesConfig);
My problem is that I need to transfer the FriendsConfig object stored in the EnemiesConfig variable to my other scripts to be able to check if a player is an enemy (for example, a killaura script that prioritizes targeting enemies over other players).
I have tried import/export but have not been able to figure out how it works; whenever I reload my script shows an error saying something like "Expected an operand but found export".A possible solution I've thought of is to create a separate FriendsConfig object that reads from enemies.json in each script, but that seems like a messy solution as it would require constant re-reading from the file to check for changes.
(I already have all of the code to add/remove/list enemies working via a command, I just need help getting the object from one script to the other)
If it is of any importance, I am using CzechHek's Core.lib for all my scripts. -
@allison Since you are already using Core, this should work. I've even fixed more stuff with Reflector to make this easy to do.
Export.js script:
///api_version=2 (script = registerScript({ name: "Export", authors: ["CzechHek"], version: "1.0" })).import("Core.lib"); FriendsConfig = Java.type("net.ccbluex.liquidbounce.file.configs.FriendsConfig"); dir = LiquidBounce.fileManager.dir; EnemiesConfig = new Reflector(new FriendsConfig(new File(dir, "enemies.json"))); EnemiesConfig.createConfig(); EnemiesConfig.loadConfig();
Import.js script
///api_version=2 (script = registerScript({ name: "Import", authors: ["CzechHek"], version: "1.0" })).import("Core.lib"); module = { onEnable: function () { targetScript = new Reflector(Java.from(scriptManager.scripts).find(function (script) script.scriptName == "Export")); config = targetScript.scriptEngine.get("EnemiesConfig"); } }
-
@czechhek said in Getting variables/objects from other scripts:
@allison Since you are already using Core, this should work. I've even fixed more stuff with Reflector to make this easy to do.
This is now built into Core since 4.11.
EnemiesConfig = importFromScript("ScriptName", "EnemiesConfig");
-
@idk-my-name Wouldn't that be too intrusive and confusing?
I guess it would be better to do
importProperties("scriptname")
which would add script to list that no such property uses.