Skip to content

Instantly share code, notes, and snippets.

@aimbot6120
Last active May 4, 2024 09:03
Show Gist options
  • Save aimbot6120/62d0f64562ebb971178c22b88019ba4b to your computer and use it in GitHub Desktop.
Save aimbot6120/62d0f64562ebb971178c22b88019ba4b to your computer and use it in GitHub Desktop.
Quest Generate Examples
//for bukkit plugins
package org.xgaming.questsample.External;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import kong.unirest.json.JSONObject;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.plugin.Plugin;
public class Server {
Plugin plugin;
public Server(Plugin plugin) {
this.plugin = plugin;
}
public String serverRequest(String message, String url) {
try {
plugin.getLogger().info("Requesting: " + url);
plugin.getLogger().info("Sent : " + message);
HttpResponse<JsonNode> response = Unirest.post(url)
.header("Content-Type", "application/json")
.body(message).asJson();
// Check the response status
if (response.getStatus() == 200) {
String reply = response.getBody().toString();
plugin.getLogger().info("Received :" + reply);
return reply;
} else {
// Log non-200 responses
plugin.getLogger().log(Level.SEVERE, "Non-200 Response: " + response.getStatus());
return null;
}
} catch (UnirestException e) {
if (e.getCause() instanceof java.net.SocketTimeoutException) {
plugin.getLogger().log(Level.SEVERE, "Socket timeout: ");
} else {
plugin.getLogger().log(Level.SEVERE, "HTTP request failed: " + e.getMessage());
}
return "{}";
}
}
public String sendQuestGenerateRequest(String key, List<String> goal, List<String> fields, int n) {
JSONObject requestBody = new JSONObject();
requestBody.put("key", key);
requestBody.put("fields", fields);
requestBody.put("goal", goal);
requestBody.put("n", n);
//optional, you can add the story field here as well
try {
String url = "https://authdev.xgaming.club/xquest/init";
return serverRequest(requestBody.toString(), url);
} catch (Exception e) {
plugin.getLogger().severe("Error sending quest generation request: " + e.getMessage());
return null;
}
}
}
local function callQuestGeneratorAPI()
local url = "https://authdev.xgaming.club/xquest/generate"
local data =
{
goal = "",
key = "abcd1234",
fields = {
},
n = 1,
story = ""
}
local jsonData = HttpService:JSONEncode(data)
print("Loaidng Quest")
local headers = {
["Content-Type"] = "application/json"
}
local response = HttpService:RequestAsync({
Url = url,
Method = "POST",
Headers = headers,
Body = jsonData
})
if response.Success then
local responseBody = HttpService:JSONDecode(response.Body)
print(responseBody)
return responseBody
else
warn("Failed to call API: " .. response.StatusCode .. " - " .. response.StatusMessage)
return nil
end
end
import requests
import json
def call_quest_generator_api():
url = "https://authdev.xgaming.club/xquest/generate"
data = {
"goal": "The player just crash landed on the plant near a cave. He needs to survive",
"key": "abcd1234",
"fields": ["verb", "noun"],
"n": 1,
"story": "The player crash landed on Zeloria, a mining plant abandoned long ago due to uncontrolled mutations of its flora and fauna. He finds you, a hunter named Thalion Whisperwind and asks for help. Thalion was a miner who was abandoned and is now a fierce hunter"
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status() # Raise an error for non-OK responses
# Print response
print("Response Body:", response.json())
return response.json()
except requests.exceptions.RequestException as e:
print("Failed to call API:", e)
return None
# Call the function
call_quest_generator_api()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment