### SQL Handler # By Mwthorn (Discord: Mwthorn#2843) (Mail: mwthorn@gmail.com) # Ask for help at any time on the Official Denizen Script Discord: https://discord.gg/Q6pZGSR # This handler takes care of the SQL connection and should be used whenever you make SQL tasks. # This only handles a single connection for simplicity reasons. ## Instructions: # - Place the script in your script folder. # - Restart server or reload scripts # - Configure the file: plugins/denizen/data/sql/config.yml (path is the address with the SQL database) # - Place `- inject SQL_Handler_Connect` right before you have SQL commands; use the SQL ID definition: <[id]> # - Done! # Incase you have multiple SQL connections, you can control the unique ID here: Sql_Handler_Config: type: yaml data config: id: "denizen_unique_sql_id" # After you have injected the connection script, the following definitions are available: # "user" and "path" ("pass" returns "none" for security reasons) # Example use-case, standard. SQL_Handler_Run: type: task script: - inject SQL_Handler_Connect # Now you can do whatever you want to do with SQL using the ID: <[id]> # If the connection fails, the queue is cleared and won't run the SQL command below - ~sql id:<[id]> "update:UPDATE `servers_status` SET `TPS=,`Players`=,`Max`=,`Update`= WHERE `Server Name` = <&sq>lobby<&sq>" # Example use-case so you can do something if the connection fails. SQL_Handler_Run_Two: type: task script: - ~run SQL_Handler_Connect context:check save:feedback - if : - narrate "Connection established with ID<&co> <[id]>" # - ~sql id:<[id]> "update:UPDATE `servers_status` SET `TPS`=20,`Players`=0,`Max`=5,`Status`=1 WHERE `Server Name` = 'Lobby'" - else: - narrate "Connection failed" ### ### Handler code below. No need to configure. ### Sql_Handler_Events: type: world events: on server start: - inject Sql_Handler_Update on reload scripts: - inject Sql_Handler_Update Sql_Handler_Update: type: task script: - if : - define id: - yaml create id:<[id]> - yaml id:<[id]> set Username:User - yaml id:<[id]> set Password:MyPassword - yaml id:<[id]> set path:localhost:3306/test - yaml id:<[id]> "savefile:/data/sql/config.yml" - yaml unload id:<[id]> SQL_Handler_Disconnect: type: task script: - define id: - sql disconnect id:<[id]> SQL_Handler_Connect: type: task definitions: sql_return script: - define id: - if ].not> { - yaml "load:/data/sql/config.yml" id:<[id]> - define pass:].read[Password]> - define user:].read[Username]> - define path:].read[path]> - yaml unload id:<[id]> - ~sql id:<[id]> "connect:<[path]>" "username:<[user]>" "password:<[pass]>" - define pass:"none" - if ].not> { - if <[sql_return].exists>: - determine "false" passively - stop - if <[sql_return].exists>: - determine "true" passively