Skip to content

Commit

Permalink
Minor optimisations BlocksHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels-NTG committed Dec 16, 2022
1 parent 665bc51 commit 7c818ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 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.0'
version = '0.6.1'
group = 'com.nilsgawlik.gdmchttp' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'gdmchttp'

Expand Down
50 changes: 23 additions & 27 deletions src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Clearable;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -162,14 +160,13 @@ public void internalHandle(HttpExchange httpExchange) throws IOException {
responseString = String.join("\n", returnValues);
}
} else if (method.equals("get")) {
JsonArray jsonArray = new JsonArray();
StringBuilder responseStringBuilder = new StringBuilder();
for (int rangeX = x; rangeX < x + dx; rangeX++) {
for (int rangeY = y; rangeY < y + dy; rangeY++) {
for (int rangeZ = z; rangeZ < z + dz; rangeZ++) {
BlockPos blockPos = new BlockPos(rangeX, rangeY, rangeZ);
String blockId = getBlockAsStr(blockPos);
if (returnJson) {
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++) {
BlockPos blockPos = new BlockPos(rangeX, rangeY, rangeZ);
String blockId = getBlockAsStr(blockPos);
JsonObject json = new JsonObject();
json.addProperty("id", blockId);
json.addProperty("x", rangeX);
Expand All @@ -182,29 +179,28 @@ public void internalHandle(HttpExchange httpExchange) throws IOException {
json.add("data", getBlockDataAsJsonObject(blockPos));
}
jsonArray.add(json);
} else {
responseStringBuilder.append(rangeX);
responseStringBuilder.append(' ');
responseStringBuilder.append(rangeY);
responseStringBuilder.append(' ');
responseStringBuilder.append(rangeZ);
responseStringBuilder.append(' ');
responseStringBuilder.append(getBlockAsStr(blockPos));
}
}
}
responseString = new Gson().toJson(jsonArray);
} else {
ArrayList<String> 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++) {
BlockPos blockPos = new BlockPos(rangeX, rangeY, rangeZ);
String listItem = rangeX + ' ' + rangeY + ' ' + rangeZ + ' ' + getBlockAsStr(blockPos);
if (includeState) {
responseStringBuilder.append(getBlockStateAsStr(blockPos));
listItem += getBlockStateAsStr(blockPos);
}
if (includeData) {
responseStringBuilder.append(getBlockDataAsStr(blockPos));
listItem += getBlockDataAsStr(blockPos);
}
responseStringBuilder.append('\n');
responseList.add(listItem);
}
}
}
}
if (returnJson) {
responseString = new Gson().toJson(jsonArray);
} else {
responseString = responseStringBuilder.deleteCharAt(responseStringBuilder.length() - 1).toString();
responseString = String.join("\n", responseList);
}
} else {
throw new HandlerBase.HttpException("Method not allowed. Only PUT and GET requests are supported.", 405);
Expand Down Expand Up @@ -315,7 +311,7 @@ public static String getBlockRegistryName(BlockState blockState) {
return getBlockRegistryName(blockState.getBlock());
}
public static String getBlockRegistryName(Block block) {
return ForgeRegistries.BLOCKS.getKey(block).toString();
return Objects.requireNonNull(ForgeRegistries.BLOCKS.getKey(block)).toString();
}

// function that converts a bunch of Property/Comparable pairs into strings that look like 'property=value'
Expand Down

0 comments on commit 7c818ff

Please sign in to comment.