diff --git a/README.md b/README.md index 407013a..2d2d5a7 100644 --- a/README.md +++ b/README.md @@ -118,20 +118,20 @@ Reads and outputs the contents of text files located in the "text" folder to pla Performs HTTP requests to specified URLs, supporting both GET and POST requests. Configuration files are used to manage request parameters. Allows you to execute commands depending on the HTTP request response. Each individual configuration file corresponds to a single request. ```yaml # The URL of the API to be queried. -url: "https://domain.com/api/minecraft/link" +# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]% +url: "https://api.mojang.com/users/profiles/minecraft/%player_name%" # HTTP request method. Can be "GET" or "POST". method: "GET" # Query parameters. They will be sent to the API with the request. If the parameters are not used, this field can be deleted. # You can use placeholders that will be automatically replaced with the appropriate values when the query is executed. -# Available placeholders: %player_name%, %player_uuid%, %server%, %arg1%, %arg2%, %arg[n]% +# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]% parameters: - api_key: "******************************" - player_name: "%player_name%" - player_uuid: "%player_uuid%" + secret: "lmksfdjlfjsffsdfjkljklgjkljsieiweiefdls" + player: "%player_name%" server: "%server%" - code: "%arg1%" + user_code: "%arg1%" # Commands that cause the request to be executed. When one of these commands is entered, a request to the API will be executed. triggers: @@ -148,12 +148,15 @@ permission: "account.link" response: # Reply with successful status (200) success: - - "m %player_name% %response%" + - "alert Player %player_name% is uuid %id%" + - "Player %player_name% has successfully linked his account to the site %json_resp_key_1%" + - "msg %player_name% You have successfully linked your account to site. %json_resp_key_2%" # Response in case of failed status (not 200) failure: - - "m %player_name% %response%" + - "msg %player_name% It was not possible to link the account to the site." -debug: false +# If this option is enabled, all response options from the api/site will be sent to the sender +debug: true ``` ### EventsManager: diff --git a/src/main/java/com/vcmi/modules/requests/RequestCommand.java b/src/main/java/com/vcmi/modules/requests/RequestCommand.java index 8a55482..9c77885 100644 --- a/src/main/java/com/vcmi/modules/requests/RequestCommand.java +++ b/src/main/java/com/vcmi/modules/requests/RequestCommand.java @@ -45,11 +45,8 @@ private void runCommand(String[] args, CommandSource sender) throws Exception { config.getConfigurationSection("parameters").getMapValues(true), params ); - HttpRequest req = new HttpRequest( - config.getString("url"), config.getString("method"), - parameters - ); - + String url = parsePlaceholder(config.getString("url"), params); + HttpRequest req = new HttpRequest(url, config.getString("method"), parameters); JsonElement resp = req.send(); @@ -93,6 +90,13 @@ private void runCommand(String[] args, CommandSource sender) throws Exception { } } + /** + * This method prepares the placeholders for the command. + * It collects the necessary information from the command sender and arguments. + * @param args The command arguments. + * @param sender The command sender. + * @return A map of placeholders and their corresponding values. + */ private Map placeholderPrepare(String[] args, CommandSource sender) { Map params = new HashMap<>(); if (sender instanceof Player) { @@ -115,6 +119,13 @@ private Map placeholderPrepare(String[] args, CommandSource send return params; } + /** + * This method parses the placeholders in a map. + * It replaces each placeholder in the map values with its corresponding value from the params map. + * @param map The map containing the placeholders. + * @param params The map containing the placeholder values. + * @return A map with the parsed placeholders. + */ private Map parsePlaceholders(Map map, Map params) { Map stringMap = new HashMap<>(); for (Map.Entry entry : map.entrySet()) { @@ -127,6 +138,34 @@ private Map parsePlaceholders(Map map, Map params) { + if (text == null || params == null) { + return text; + } + String parsedText = text; + for (Map.Entry param : params.entrySet()) { + if (param.getKey() == null || param.getValue() == null) { + continue; + } + parsedText = parsedText.replace("%" + param.getKey() + "%", param.getValue()); + } + return parsedText; + } + + /** + * This method parses the placeholders in a list. + * It replaces each placeholder in the list items with its corresponding value from the params map. + * @param list The list containing the placeholders. + * @param params The map containing the placeholder values. + * @return A list with the parsed placeholders. + */ private List parsePlaceholdersInList(List list, Map params) { if (list == null || params == null) { return Collections.emptyList(); diff --git a/src/main/resources/linkaccount.yml b/src/main/resources/linkaccount.yml index 39c56ed..7cb4193 100644 --- a/src/main/resources/linkaccount.yml +++ b/src/main/resources/linkaccount.yml @@ -1,8 +1,9 @@ # The URL of the API to be queried. -url: "http://example.com/api" +# Available placeholders: %player_name%, %player_uuid%, %player_ip%, %server%, %arg1%, %arg2%, %arg[n]% +url: "https://api.mojang.com/users/profiles/minecraft/%player_name%" # HTTP request method. Can be "GET" or "POST". -method: "POST" +method: "GET" # Query parameters. They will be sent to the API with the request. If the parameters are not used, this field can be deleted. # You can use placeholders that will be automatically replaced with the appropriate values when the query is executed. @@ -28,11 +29,12 @@ permission: "account.link" response: # Reply with successful status (200) success: - - "alert Player %player_name% has successfully linked his account to the site %json_resp_key_1%" + - "alert Player %player_name% is uuid %id%" + - "Player %player_name% has successfully linked his account to the site %json_resp_key_1%" - "msg %player_name% You have successfully linked your account to site. %json_resp_key_2%" # Response in case of failed status (not 200) failure: - "msg %player_name% It was not possible to link the account to the site." # If this option is enabled, all response options from the api/site will be sent to the sender -debug: false \ No newline at end of file +debug: true \ No newline at end of file