Skip to content

Commit

Permalink
Fix mixed tabs and spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 6, 2023
1 parent 468faa4 commit 75a1e51
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 98 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = off
94 changes: 48 additions & 46 deletions src/main/java/mcextract/BlockCollisionBoxStorage.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
package mcextract;

import com.google.gson.*;
import com.google.gson.JsonObject;
import net.minecraft.world.phys.AABB;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class BlockCollisionBoxStorage {
private final Map<String, ShapePerBlockState> blocks = new HashMap<>();
private final Map<String, ShapePerBlockState> blocks = new HashMap<>();

public BlockCollisionBoxStorage(JsonObject jsonRoot) {
var shapes = new HashMap<Integer, Shape>();
for (var shapeEntry : jsonRoot.getAsJsonObject("shapes").entrySet()) {
var boxesJson = shapeEntry.getValue().getAsJsonArray();
var boxes = new ArrayList<AABB>(boxesJson.size());
for (var element : boxesJson) {
var a = element.getAsJsonArray();
int i = 0;
boxes.add(new AABB(
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble()));
}
shapes.put(Integer.parseInt(shapeEntry.getKey()), new Shape(boxes));
}
public BlockCollisionBoxStorage(JsonObject jsonRoot) {
var shapes = new HashMap<Integer, Shape>();
for (var shapeEntry : jsonRoot.getAsJsonObject("shapes").entrySet()) {
var boxesJson = shapeEntry.getValue().getAsJsonArray();
var boxes = new ArrayList<AABB>(boxesJson.size());
for (var element : boxesJson) {
var a = element.getAsJsonArray();
int i = 0;
boxes.add(new AABB(
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble(),
a.get(i++).getAsDouble()));
}
shapes.put(Integer.parseInt(shapeEntry.getKey()), new Shape(boxes));
}

for (var blockEntry : jsonRoot.getAsJsonObject("blocks").entrySet()) {
var value = blockEntry.getValue();
if (value == null) {
blocks.put(blockEntry.getKey(), new SameShapePerBlockState(Shape.EMPTY));
} else if (value.isJsonPrimitive()) {
var shape = shapes.get(value.getAsInt());
blocks.put(blockEntry.getKey(), new SameShapePerBlockState(shape));
} else {
var shapeIds = value.getAsJsonArray();
var blockStatesShapes = new Shape[shapeIds.size()];
for (int i = 0; i < blockStatesShapes.length; i++) {
blockStatesShapes[i] = shapes.get(shapeIds.get(i).getAsInt());
}
blocks.put(blockEntry.getKey(), new OneShapePerBlockState(blockStatesShapes));
}
}
}
for (var blockEntry : jsonRoot.getAsJsonObject("blocks").entrySet()) {
var value = blockEntry.getValue();
if (value == null) {
blocks.put(blockEntry.getKey(), new SameShapePerBlockState(Shape.EMPTY));
} else if (value.isJsonPrimitive()) {
var shape = shapes.get(value.getAsInt());
blocks.put(blockEntry.getKey(), new SameShapePerBlockState(shape));
} else {
var shapeIds = value.getAsJsonArray();
var blockStatesShapes = new Shape[shapeIds.size()];
for (int i = 0; i < blockStatesShapes.length; i++) {
blockStatesShapes[i] = shapes.get(shapeIds.get(i).getAsInt());
}
blocks.put(blockEntry.getKey(), new OneShapePerBlockState(blockStatesShapes));
}
}
}

public Shape getBlockShape(String blockId, int blockStateId) {
final ShapePerBlockState blockStatesShapeIds = blocks.get(blockId);
if (blockStatesShapeIds == null) return null; // unknown block
return blockStatesShapeIds.getShapeIdForBlockState(blockStateId);
}
public Shape getBlockShape(String blockId, int blockStateId) {
final ShapePerBlockState blockStatesShapeIds = blocks.get(blockId);
if (blockStatesShapeIds == null) return null; // unknown block
return blockStatesShapeIds.getShapeIdForBlockState(blockStateId);
}

private interface ShapePerBlockState {
Shape getShapeIdForBlockState(int blockStateId);
}
private interface ShapePerBlockState {
Shape getShapeIdForBlockState(int blockStateId);
}

private record SameShapePerBlockState(Shape shape) implements ShapePerBlockState {
@Override
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/mcextract/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.Bootstrap;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.EmptyBlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -84,7 +77,7 @@ public static void main(String[] args) {

// Blocks
try {
writeJson(outputDir, "blockProperties.json",blockPropertiesJson);
writeJson(outputDir, "blockProperties.json", blockPropertiesJson);
System.err.println("Done with block shapes.");
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -93,7 +86,7 @@ public static void main(String[] args) {

// Attributes
try {
writeJson(outputDir, "attributes.json",attributesJson);
writeJson(outputDir, "attributes.json", attributesJson);
System.err.println("Done with attributes.");
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -277,7 +270,7 @@ private static JsonObject extractBlockProperties() {
var defaultState = block.defaultBlockState();
String offsetType = "NONE";
if (defaultState.hasOffsetFunction()) {
var vec = defaultState.getOffset(EmptyBlockGetter.INSTANCE, new BlockPos(1, 2, 3));;
var vec = defaultState.getOffset(EmptyBlockGetter.INSTANCE, new BlockPos(1, 2, 3));

if (vec.y == 0.0) {
sawZeroYOffset = true;
Expand Down
86 changes: 44 additions & 42 deletions src/main/java/mcextract/Shape.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
package mcextract;

import com.google.gson.JsonArray;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.level.EmptyBlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

/**
* The shape of a {@link BlockState}, composed of one or more axis-aligned {@link Box}es.
* The shape of a {@link BlockState}, composed of one or more axis-aligned AABBs.
*/
public class Shape {
public static final Shape EMPTY = new Shape(Collections.emptyList());
public static final Shape EMPTY = new Shape(Collections.emptyList());

public final Collection<AABB> boxes;
public final Collection<AABB> boxes;

public Shape(Collection<AABB> boxes) {
this.boxes = boxes;
}
public Shape(Collection<AABB> boxes) {
this.boxes = boxes;
}

public Shape(BlockState blockState) {
var collisionShape = blockState.getCollisionShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
public Shape(BlockState blockState) {
var collisionShape = blockState.getCollisionShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
var offset = blockState.getOffset(EmptyBlockGetter.INSTANCE, BlockPos.ZERO);
var reversedOffset = offset.reverse();
var collisionShapeWithoutOffset = collisionShape.move(reversedOffset.x, reversedOffset.y, reversedOffset.z);

this.boxes = collisionShapeWithoutOffset.toAabbs();
}

public JsonArray toJson() {
final JsonArray boxesJson = new JsonArray();
for (AABB box : boxes) {
boxesJson.add(jsonArrayFromBox(box));
}
return boxesJson;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Shape shape = (Shape) o;
return Objects.equals(boxes, shape.boxes);
}

@Override
public int hashCode() {
return Objects.hash(boxes);
}

public static JsonArray jsonArrayFromBox(AABB box) {
}

public static JsonArray jsonArrayFromBox(AABB box) {
final JsonArray boxJson = new JsonArray();
boxJson.add(box.minX);
boxJson.add(box.minY);
boxJson.add(box.minZ);
boxJson.add(box.maxX);
boxJson.add(box.maxY);
boxJson.add(box.maxZ);
return boxJson;
}
boxJson.add(box.minY);
boxJson.add(box.minZ);
boxJson.add(box.maxX);
boxJson.add(box.maxY);
boxJson.add(box.maxZ);
return boxJson;
}

public JsonArray toJson() {
final JsonArray boxesJson = new JsonArray();
for (AABB box : boxes) {
boxesJson.add(jsonArrayFromBox(box));
}
return boxesJson;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Shape shape = (Shape) o;
return Objects.equals(boxes, shape.boxes);
}

@Override
public int hashCode() {
return Objects.hash(boxes);
}
}

0 comments on commit 75a1e51

Please sign in to comment.