From 42d6cb37a3cf859144c9e30986c251c91052f0fc Mon Sep 17 00:00:00 2001 From: Niels NTG Date: Sat, 17 Dec 2022 15:07:42 +0100 Subject: [PATCH] Allow offset coordinates to be negative --- build.gradle | 2 +- .../handlers/BiomesHandler.java | 23 +++++++++++++----- .../handlers/BlocksHandler.java | 24 ++++++++++++++----- .../handlers/ChunkHandler.java | 12 +++++++--- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 83f8ecd..7adfd22 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '0.6.2' +version = '0.6.3' group = 'com.nilsgawlik.gdmchttp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'gdmchttp' diff --git a/src/main/java/com/gdmc/httpinterfacemod/handlers/BiomesHandler.java b/src/main/java/com/gdmc/httpinterfacemod/handlers/BiomesHandler.java index 45a329c..bdcd78d 100644 --- a/src/main/java/com/gdmc/httpinterfacemod/handlers/BiomesHandler.java +++ b/src/main/java/com/gdmc/httpinterfacemod/handlers/BiomesHandler.java @@ -54,11 +54,22 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException { if (method.equals("get")) { ServerLevel serverLevel = getServerLevel(dimension); + int xOffset = x + dx; + int xMin = Math.min(x, xOffset); + int xMax = Math.max(x, xOffset); + + int yOffset = y + dy; + int yMin = Math.min(y, yOffset); + int yMax = Math.max(y, yOffset); + + int zOffset = z + dz; + int zMin = Math.min(z, zOffset); + int zMax = Math.max(z, zOffset); if (returnJson) { JsonArray jsonArray = new JsonArray(); - for (int rangeX = x; rangeX < x + dx; rangeX++) { - for (int rangeY = y; rangeY < y + dy; rangeY++) { - for (int rangeZ = z; rangeZ < z + dz; rangeZ++) { + 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); String biomeName = serverLevel.getBiome(blockPos).unwrapKey().get().location().toString(); JsonObject json = new JsonObject(); @@ -73,9 +84,9 @@ protected void internalHandle(HttpExchange httpExchange) throws IOException { responseString = new Gson().toJson(jsonArray); } else { ArrayList biomesList = new ArrayList<>(); - for (int rangeX = x; rangeX < x + dx; rangeX++) { - for (int rangeY = y; rangeY < y + dy; rangeY++) { - for (int rangeZ = z; rangeZ < z + dz; rangeZ++) { + 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); String biomeName = serverLevel.getBiome(blockPos).unwrapKey().get().location().toString(); biomesList.add(rangeX + " " + rangeY + " " + rangeZ + " " + biomeName); diff --git a/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java b/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java index f2a2d58..a2ae5e2 100644 --- a/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java +++ b/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java @@ -160,11 +160,23 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { responseString = String.join("\n", returnValues); } } else if (method.equals("get")) { + int xOffset = x + dx; + int xMin = Math.min(x, xOffset); + int xMax = Math.max(x, xOffset); + + int yOffset = y + dy; + int yMin = Math.min(y, yOffset); + int yMax = Math.max(y, yOffset); + + int zOffset = z + dz; + int zMin = Math.min(z, zOffset); + int zMax = Math.max(z, zOffset); + if (returnJson) { JsonArray jsonArray = new JsonArray(); - for (int rangeX = x; rangeX < x + dx; rangeX++) { - for (int rangeY = y; rangeY < y + dy; rangeY++) { - for (int rangeZ = z; rangeZ < z + dz; rangeZ++) { + 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); String blockId = getBlockAsStr(blockPos); JsonObject json = new JsonObject(); @@ -185,9 +197,9 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { responseString = new Gson().toJson(jsonArray); } else { ArrayList responseList = new ArrayList<>(); - for (int rangeX = x; rangeX < x + dx; rangeX++) { - for (int rangeY = y; rangeY < y + dy; rangeY++) { - for (int rangeZ = z; rangeZ < z + dz; rangeZ++) { + 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); String listItem = rangeX + " " + rangeY + " " + rangeZ + " " + getBlockAsStr(blockPos); if (includeState) { diff --git a/src/main/java/com/gdmc/httpinterfacemod/handlers/ChunkHandler.java b/src/main/java/com/gdmc/httpinterfacemod/handlers/ChunkHandler.java index 7fb026c..13941fd 100644 --- a/src/main/java/com/gdmc/httpinterfacemod/handlers/ChunkHandler.java +++ b/src/main/java/com/gdmc/httpinterfacemod/handlers/ChunkHandler.java @@ -58,11 +58,17 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { ServerLevel serverLevel = getServerLevel(dimension); CompletableFuture cfs = CompletableFuture.supplyAsync(() -> { + int xOffset = chunkX + chunkDX; + int xMin = Math.min(chunkX, xOffset); + int xMax = Math.max(chunkX, xOffset); + + int zOffset = chunkZ + chunkDZ; + int zMin = Math.min(chunkZ, zOffset); + int zMax = Math.max(chunkZ, zOffset); ListTag returnList = new ListTag(); - for(int z = chunkZ; z < chunkZ + chunkDZ; z++) - for(int x = chunkX; x < chunkX + chunkDX; x++) { + for(int z = zMin; z < zMax; z++) + for(int x = xMin; x < xMax; x++) { LevelChunk chunk = serverLevel.getChunk(x, z); - CompoundTag chunkNBT = ChunkSerializer.write(serverLevel, chunk); returnList.add(chunkNBT); }