Skip to content

Commit

Permalink
👽️ Fix getOffset method signature change
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiome committed Sep 21, 2024
1 parent 16d3941 commit 7253ee0
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/net/gunivers/bookshelf/HitboxExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.lang.reflect.Method;
import java.util.Map;
import net.minecraft.core.BlockPos;
import net.minecraft.data.Main;
Expand All @@ -33,22 +33,22 @@ public static void main(String[] args) {
return;
}

JsonObject blockShapes = extractBlockRegistryShapes();
JsonObject blockShapes = extractBlockShapes();
writeJsonToFile("generated/" + args[0] + "/blocks/shapes.json", blockShapes, true);
writeJsonToFile("generated/" + args[0] + "/blocks/shapes.min.json", blockShapes, false);
}

/**
* Extracts block shapes from the Minecraft Blocks registry.
*/
public static JsonObject extractBlockRegistryShapes() {
public static JsonObject extractBlockShapes() {
JsonObject blocksJson = new JsonObject();

for (Field blockField : Blocks.class.getFields()) {
try {
Block block = (Block) blockField.get(null);
String blockID = block.toString().substring(6, block.toString().length() - 1);
blocksJson.add(blockID, extractBlockShapes(block));
blocksJson.add(blockID, extractSingleBlockShapes(block));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Expand All @@ -59,7 +59,7 @@ public static JsonObject extractBlockRegistryShapes() {
/**
* Extracts shapes from a Minecraft Block.
*/
public static JsonArray extractBlockShapes(Block block) {
private static JsonArray extractSingleBlockShapes(Block block) {
JsonArray states = new JsonArray();

block.getStateDefinition().getPossibleStates().forEach(state -> {
Expand All @@ -68,8 +68,28 @@ public static JsonArray extractBlockShapes(Block block) {
for (Map.Entry<Property<?>, Comparable<?>> entry : state.getValues().entrySet())
properties.addProperty(entry.getKey().getName(), String.valueOf(entry.getValue()).toLowerCase());

Vec3 offset = state.getOffset(EmptyBlockGetter.INSTANCE, BlockPos.ZERO).reverse();
VoxelShape shape = new VoxelShape(state.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO).move(offset.x, offset.y, offset.z));
Vec3 offset = null;
try {
Method method = getMethod(state.getClass(), "getOffset");
if (method != null) {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == 2) {
offset = (Vec3) method.invoke(state, EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
} else if (paramTypes.length == 1) {
offset = (Vec3) method.invoke(state, BlockPos.ZERO);
}
} else {
System.out.println("Method getOffset not found.");
}
} catch (Exception e) {
e.printStackTrace();
}

offset = offset.reverse();
VoxelShape shape = new VoxelShape(state
.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)
.move(offset.x, offset.y, offset.z)
);

JsonObject stateJson = new JsonObject();
stateJson.add("properties", properties);
Expand All @@ -83,7 +103,7 @@ public static JsonArray extractBlockShapes(Block block) {
/**
* Writes a JsonObject to a JSON file.
*/
public static void writeJsonToFile(String fileName, JsonObject data, boolean prettyPrint) {
private static void writeJsonToFile(String fileName, JsonObject data, boolean prettyPrint) {
Gson gson = prettyPrint ? new GsonBuilder().setPrettyPrinting().create() : new Gson();

try (FileWriter writer = new FileWriter(fileName)) {
Expand All @@ -92,4 +112,19 @@ public static void writeJsonToFile(String fileName, JsonObject data, boolean pre
e.printStackTrace();
}
}

/**
* Find a method by its name using reflection.
*/
private static Method getMethod(Class<?> clazz, String methodName) {
while (clazz != null) {
for (Method method : clazz.getDeclaredMethods()) {
if (method.getName().equals(methodName)) {
return method;
}
}
clazz = clazz.getSuperclass();
}
return null;
}
}

0 comments on commit 7253ee0

Please sign in to comment.