Skip to content

Commit

Permalink
Merge pull request #10 from Niels-NTG/v0.7
Browse files Browse the repository at this point in the history
V0.7
  • Loading branch information
Niels-NTG authored Dec 28, 2022
2 parents cf03834 + 1e9c13a commit 96ed305
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 174 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '0.6.5'
version = '0.7.0'
group = 'com.nilsgawlik.gdmchttp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'gdmchttp'

Expand Down
41 changes: 31 additions & 10 deletions src/main/java/com/gdmc/httpinterfacemod/handlers/BiomesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@ public BiomesHandler(MinecraftServer mcServer) {
protected void internalHandle(HttpExchange httpExchange) throws IOException {
// query parameters
Map<String, String> queryParams = parseQueryString(httpExchange.getRequestURI().getRawQuery());

// GET: x, y, z positions
int x;
int y;
int z;

// GET: Ranges in the x, y, z directions (can be negative). Defaults to 1.
int dx;
int dy;
int dz;

String dimension;

try {
x = Integer.parseInt(queryParams.getOrDefault("x", "0"));
y = Integer.parseInt(queryParams.getOrDefault("y", "0"));
z = Integer.parseInt(queryParams.getOrDefault("z", "0"));

dx = Integer.parseInt(queryParams.getOrDefault("dx", "1"));
dy = Integer.parseInt(queryParams.getOrDefault("dy", "1"));
dz = Integer.parseInt(queryParams.getOrDefault("dz", "1"));
Expand All @@ -45,15 +51,19 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException {
throw new HandlerBase.HttpException(message, 400);
}

Headers reqestHeaders = httpExchange.getRequestHeaders();
String contentType = getHeader(reqestHeaders, "Accept", "*/*");
boolean returnJson = contentType.equals("application/json") || contentType.equals("text/json");
// Check if clients wants a response in a JSON format. If not, return response in plain text.
Headers requestHeaders = httpExchange.getRequestHeaders();
String acceptHeader = getHeader(requestHeaders, "Accept", "*/*");
boolean returnJson = hasJsonTypeInHeader(acceptHeader);

String method = httpExchange.getRequestMethod().toLowerCase();

String responseString;

if (method.equals("get")) {
ServerLevel serverLevel = getServerLevel(dimension);

// Calculate boundaries of area of blocks to gather biome information on.
int xOffset = x + dx;
int xMin = Math.min(x, xOffset);
int xMax = Math.max(x, xOffset);
Expand All @@ -65,12 +75,18 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException {
int zOffset = z + dz;
int zMin = Math.min(z, zOffset);
int zMax = Math.max(z, zOffset);

if (returnJson) {
// Create a JsonArray with JsonObject, each contain a key-value pair for
// the x, y, z position and the namespaced biome name.
JsonArray jsonArray = new JsonArray();
for (int rangeX = xMin; rangeX < xMax; rangeX++) {
for (int rangeY = yMin; rangeY < yMax; rangeY++) {
for (int rangeZ = zMin; rangeZ < zMax; rangeZ++) {
BlockPos blockPos = new BlockPos(rangeX, rangeY, rangeZ);
if (serverLevel.getBiome(blockPos).unwrapKey().isEmpty()) {
continue;
}
String biomeName = serverLevel.getBiome(blockPos).unwrapKey().get().location().toString();
JsonObject json = new JsonObject();
json.addProperty("id", biomeName);
Expand All @@ -83,11 +99,16 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException {
}
responseString = new Gson().toJson(jsonArray);
} else {
// Create list of \n-separated strings containing the space-separated
// x, y, z position and the namespaced biome name.
ArrayList<String> biomesList = new ArrayList<>();
for (int rangeX = xMin; rangeX < xMax; rangeX++) {
for (int rangeY = yMin; rangeY < yMax; rangeY++) {
for (int rangeZ = zMin; rangeZ < zMax; rangeZ++) {
BlockPos blockPos = new BlockPos(rangeX, rangeY, rangeZ);
if (serverLevel.getBiome(blockPos).unwrapKey().isEmpty()) {
continue;
}
String biomeName = serverLevel.getBiome(blockPos).unwrapKey().get().location().toString();
biomesList.add(rangeX + " " + rangeY + " " + rangeZ + " " + biomeName);
}
Expand All @@ -96,16 +117,16 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException {
responseString = String.join("\n", biomesList);
}
} else {
throw new HandlerBase.HttpException("Method not allowed. Only GET requests are supported.", 405);
throw new HttpException("Method not allowed. Only GET requests are supported.", 405);
}

Headers headers = httpExchange.getResponseHeaders();
addDefaultHeaders(headers);

if(returnJson) {
headers.add("Content-Type", "application/json; charset=UTF-8");
// Response headers
Headers responseHeaders = httpExchange.getResponseHeaders();
addDefaultResponseHeaders(responseHeaders);
if (returnJson) {
addResponseHeadersContentTypeJson(responseHeaders);
} else {
headers.add("Content-Type", "text/plain; charset=UTF-8");
addResponseHeadersContentTypePlain(responseHeaders);
}

resolveRequest(httpExchange, responseString);
Expand Down
Loading

0 comments on commit 96ed305

Please sign in to comment.