1 line
12 KiB
Java
1 line
12 KiB
Java
package examples;\n\nimport com.openosrs.client.ModernizedClient;\nimport com.openosrs.client.api.AgentAPI;\nimport com.openosrs.client.api.Position;\nimport com.openosrs.client.api.GameObject;\nimport com.openosrs.client.login.LoginState;\nimport com.openosrs.client.scripting.ScriptingFramework;\nimport com.openosrs.client.plugins.PluginManager;\n\n/**\n * ExampleAgentWithLogin - Demonstrates how an AI agent can use the modernized client\n * with automated login functionality to interact with RuneScape.\n */\npublic class ExampleAgentWithLogin {\n \n public static void main(String[] args) throws Exception {\n System.out.println(\"=== Example AI Agent with Automated Login ===\");\n \n // 1. Initialize the modernized client\n ModernizedClient client = new ModernizedClient();\n client.start().get(); // Wait for startup\n \n // 2. Set up automated login (example credentials)\n demonstrateLoginSetup(client, args);\n \n // 3. Wait for login completion and demonstrate game interaction\n if (waitForLogin(client)) {\n // 4. Basic agent behavior - demonstrate API usage\n demonstrateGameplay(client);\n } else {\n System.out.println(\"Login failed or timed out - running in offline demo mode\");\n demonstrateOfflineCapabilities(client);\n }\n \n // 5. Keep running for a while then shutdown\n System.out.println(\"Agent will run for 60 seconds then shutdown...\");\n Thread.sleep(60000);\n \n client.stop();\n System.out.println(\"Agent shutdown complete\");\n }\n \n /**\n * Demonstrate login setup options.\n */\n private static void demonstrateLoginSetup(ModernizedClient client, String[] args) {\n System.out.println(\"\\n--- Automated Login Setup ---\");\n \n // Option 1: Set credentials directly (for development/testing)\n if (args.length >= 2 && \"--direct\".equals(args[0])) {\n String username = args[1];\n String password = args.length > 2 ? args[2] : \"demo-password\";\n \n System.out.println(\"Setting up direct login credentials...\");\n client.setLoginCredentials(username, password);\n \n // Start automated login\n System.out.println(\"Starting automated login process...\");\n client.login(30).thenAccept(success -> {\n if (success) {\n System.out.println(\"✅ Automated login successful!\");\n System.out.println(\"Login Status: \" + client.getLoginStatus());\n } else {\n System.out.println(\"❌ Automated login failed: \" + \n client.getLoginManager().getLastError());\n }\n });\n }\n // Option 2: Load from encrypted file\n else if (args.length >= 2 && \"--file\".equals(args[0])) {\n String credentialsFile = args[1];\n \n System.out.println(\"Loading credentials from file: \" + credentialsFile);\n if (client.loadLoginCredentials(credentialsFile)) {\n System.out.println(\"Credentials loaded - starting automated login...\");\n client.login().thenAccept(success -> {\n System.out.println(success ? \"✅ Login successful!\" : \n \"❌ Login failed: \" + client.getLoginManager().getLastError());\n });\n } else {\n System.out.println(\"Failed to load credentials from file\");\n }\n }\n // Option 3: Demonstrate credential file creation\n else if (args.length >= 3 && \"--create\".equals(args[0])) {\n String username = args[1];\n String password = args[2];\n String outputFile = args.length > 3 ? args[3] : \"agent-credentials.dat\";\n \n System.out.println(\"Creating encrypted credentials file...\");\n client.setLoginCredentials(username, password);\n \n // Save to file (in real usage, use a secure master password)\n String masterPassword = \"secure-master-password-123\";\n boolean saved = client.getLoginManager().getCredentials()\n .saveToFile(outputFile, masterPassword);\n \n if (saved) {\n System.out.println(\"✅ Credentials saved to: \" + outputFile);\n System.out.println(\"To use: java ExampleAgent --file \" + outputFile);\n } else {\n System.out.println(\"❌ Failed to save credentials\");\n }\n }\n else {\n System.out.println(\"No login credentials provided. Usage options:\");\n System.out.println(\" --direct <username> <password> : Direct login\");\n System.out.println(\" --file <credentials-file> : Load from encrypted file\");\n System.out.println(\" --create <user> <pass> <file> : Create credentials file\");\n System.out.println(\"\\nRunning in demo mode without login...\");\n }\n \n // Enable auto-reconnect for agents\n client.setAutoReconnect(true);\n System.out.println(\"Auto-reconnect enabled for reliable gameplay\");\n }\n \n /**\n * Wait for login completion with timeout.\n */\n private static boolean waitForLogin(ModernizedClient client) throws InterruptedException {\n System.out.println(\"\\n--- Waiting for Login Completion ---\");\n \n // Wait up to 60 seconds for login\n int maxWaitSeconds = 60;\n int waitedSeconds = 0;\n \n while (waitedSeconds < maxWaitSeconds) {\n LoginState state = client.getLoginState();\n \n if (state == LoginState.LOGGED_IN) {\n System.out.println(\"✅ Login completed successfully!\");\n System.out.println(\"Current status: \" + client.getLoginStatus());\n return true;\n }\n \n if (state == LoginState.FAILED) {\n System.out.println(\"❌ Login failed: \" + client.getLoginManager().getLastError());\n return false;\n }\n \n if (state.isInProgress()) {\n System.out.printf(\"⏳ Login in progress: %s (%ds)\\r\", \n state.getDescription(), waitedSeconds);\n }\n \n Thread.sleep(1000);\n waitedSeconds++;\n }\n \n System.out.println(\"\\n⏰ Login timed out after \" + maxWaitSeconds + \" seconds\");\n return false;\n }\n \n /**\n * Demonstrate gameplay with the Agent API.\n */\n private static void demonstrateGameplay(ModernizedClient client) {\n System.out.println(\"\\n--- Agent Gameplay Demonstration ---\");\n \n try {\n // Get the Agent API for game interaction\n AgentAPI api = client.getAgentAPI();\n \n // Display player information\n Position playerPos = api.getPlayerPosition();\n System.out.println(\"Player Position: \" + playerPos);\n \n int hitpoints = api.getHitpoints();\n int maxHp = api.getMaxHitpoints();\n System.out.println(\"Hitpoints: \" + hitpoints + \"/\" + maxHp);\n \n int combatLevel = api.getCombatLevel();\n System.out.println(\"Combat Level: \" + combatLevel);\n \n // Check game state\n boolean inCombat = api.isInCombat();\n System.out.println(\"In Combat: \" + inCombat);\n \n // Get inventory info\n boolean inventoryFull = api.isInventoryFull();\n int emptySlots = api.getEmptySlots();\n System.out.println(\"Inventory Full: \" + inventoryFull + \", Empty Slots: \" + emptySlots);\n \n // Find nearby entities\n var objects = api.getGameObjects();\n var npcs = api.getNPCs();\n System.out.println(\"Nearby Objects: \" + objects.size() + \", NPCs: \" + npcs.size());\n \n // Example agent decision making\n if (hitpoints < maxHp * 0.5) {\n System.out.println(\"🍖 Agent decision: Health is low, should eat food\");\n }\n \n if (inventoryFull) {\n System.out.println(\"🎒 Agent decision: Inventory full, should bank or drop items\");\n }\n \n if (!objects.isEmpty()) {\n GameObject firstObject = objects.get(0);\n System.out.println(\"🎯 Found object: \" + firstObject.getName() + \n \" at \" + firstObject.getPosition());\n System.out.println(\"🤖 Agent could interact with this object\");\n }\n \n // Demonstrate scripting capability\n demonstrateScripting(client.getScriptingFramework());\n \n } catch (Exception e) {\n System.err.println(\"Error in gameplay demo: \" + e.getMessage());\n }\n }\n \n /**\n * Demonstrate capabilities when not logged in.\n */\n private static void demonstrateOfflineCapabilities(ModernizedClient client) {\n System.out.println(\"\\n--- Offline Capabilities Demo ---\");\n \n // Show that agent systems are still functional\n System.out.println(\"✅ Client is running: \" + client.isRunning());\n System.out.println(\"✅ Scripting framework available: \" + \n (client.getScriptingFramework() != null));\n System.out.println(\"✅ Plugin system available: \" + \n (client.getPluginManager() != null));\n System.out.println(\"✅ Login manager available: \" + \n (client.getLoginManager() != null));\n \n // Show available scripts\n System.out.println(\"\\nAvailable automation scripts:\");\n System.out.println(\" • woodcutting - Automated tree cutting\");\n System.out.println(\" • combat-training - NPC combat training\");\n System.out.println(\" • banking - Automated banking\");\n \n // Show available plugins\n System.out.println(\"\\nAvailable plugins:\");\n var plugins = client.getPluginManager().getAllPlugins();\n for (var entry : plugins.entrySet()) {\n System.out.println(\" • \" + entry.getKey() + \": \" + \n (entry.getValue().isEnabled() ? \"enabled\" : \"disabled\"));\n }\n \n System.out.println(\"\\n📋 Agent can prepare automation scripts while offline\");\n System.out.println(\"📋 Agent can configure plugins and settings\");\n System.out.println(\"📋 Agent will be ready to play once login succeeds\");\n }\n \n /**\n * Demonstrate the scripting framework.\n */\n private static void demonstrateScripting(ScriptingFramework scripting) {\n System.out.println(\"\\n--- Scripting Framework Demo ---\");\n \n try {\n System.out.println(\"Available scripts: woodcutting, combat-training, banking\");\n System.out.println(\"Scripting framework enabled: \" + scripting.isEnabled());\n \n var activeScripts = scripting.getActiveScripts();\n System.out.println(\"Active scripts: \" + activeScripts.size());\n \n // In a real scenario, an agent could:\n // String executionId = scripting.startScript(\"woodcutting\");\n // System.out.println(\"Started automated woodcutting: \" + executionId);\n \n System.out.println(\"🤖 Agent can start/stop scripts based on goals and conditions\");\n \n } catch (Exception e) {\n System.err.println(\"Error in scripting demo: \" + e.getMessage());\n }\n }\n} |