Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement offset collision data #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/main/java/org/geysermc/generator/MappingsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import net.minecraft.world.item.*;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.EmptyBlockGetter;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.MapColor;
Expand All @@ -46,6 +48,7 @@

import java.awt.*;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.nio.file.FileSystem;
Expand Down Expand Up @@ -862,6 +865,18 @@ public void generateInteractionData() {
}
}

@SuppressWarnings("unchecked")
private static Optional<BlockBehaviour.OffsetFunction> getOffsetFunction(BlockState block) {
try {
Field offsetFunctionField = BlockBehaviour.BlockStateBase.class.getDeclaredField("offsetFunction");
offsetFunctionField.setAccessible(true);

return (Optional<BlockBehaviour.OffsetFunction>) offsetFunctionField.get(block);
Comment on lines +869 to +874
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an access widener please

} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

public JsonObject getRemapBlock(BlockState state, String identifier) {
JsonObject object = new JsonObject();
BlockEntry blockEntry = BLOCK_ENTRIES.get(identifier);
Expand Down Expand Up @@ -959,9 +974,36 @@ public JsonObject getRemapBlock(BlockState state, String identifier) {
object.addProperty("bedrock_identifier", bedrockIdentifier);

object.addProperty("block_hardness", state.getDestroySpeed(null, null));

if (state.hasOffsetFunction()) {
var offsetDataObject = new JsonObject();

offsetDataObject.addProperty("max_horizontal_offset", state.getBlock().getMaxHorizontalOffset());
offsetDataObject.addProperty("max_vertical_offset", state.getBlock().getMaxVerticalOffset());

var offsetFunction = getOffsetFunction(state).orElseThrow();
var vec = offsetFunction.evaluate(Blocks.GRASS.defaultBlockState(), EmptyBlockGetter.INSTANCE, new BlockPos(1, 2, 3));

String offsetType;
if (vec.y == 0.0) {
offsetType = "XZ";
} else {
offsetType = "XYZ";
}

offsetDataObject.addProperty("type", offsetType);

object.add("offset_data", offsetDataObject);
}

List<List<Double>> collisionBoxes = Lists.newArrayList();
try {
state.getCollisionShape(null, null).toAabbs().forEach(item -> {
var collisionShape = state.getCollisionShape(null, BlockPos.ZERO);
var offset = state.getOffset(null, BlockPos.ZERO);
var reversedOffset = offset.reverse();
var collisionShapeWithoutOffset = collisionShape.move(reversedOffset.x, reversedOffset.y, reversedOffset.z);

collisionShapeWithoutOffset.toAabbs().forEach(item -> {
List<Double> coordinateList = Lists.newArrayList();
// Convert Box class to an array of coordinates
// They need to be converted from min/max coordinates to centres and sizes
Expand All @@ -976,7 +1018,7 @@ public JsonObject getRemapBlock(BlockState state, String identifier) {
collisionBoxes.add(coordinateList);
});
} catch (NullPointerException e) {
// Fallback to empty collision when the position is needed to calculate it
// Fallback to empty collision when a block getter is needed to calculate it
}

if (!COLLISION_LIST.contains(collisionBoxes)) {
Expand Down