mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-06 15:52:03 +00:00
feat(block): 改善自定义方块形状和碰撞箱
This commit is contained in:
@@ -4,6 +4,8 @@ import net.fabricmc.api.ModInitializer;
|
|||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.shape.VoxelShape;
|
||||||
import net.momirealms.craftengine.fabric.client.config.ModConfig;
|
import net.momirealms.craftengine.fabric.client.config.ModConfig;
|
||||||
import net.momirealms.craftengine.fabric.util.BlockUtils;
|
import net.momirealms.craftengine.fabric.util.BlockUtils;
|
||||||
import net.momirealms.craftengine.fabric.util.LoggerFilter;
|
import net.momirealms.craftengine.fabric.util.LoggerFilter;
|
||||||
@@ -37,7 +39,8 @@ public class CraftEngineFabricMod implements ModInitializer {
|
|||||||
BlockState blockState = YamlUtils.createBlockData("minecraft:" + replacedBlockId.getPath());
|
BlockState blockState = YamlUtils.createBlockData("minecraft:" + replacedBlockId.getPath());
|
||||||
RegisterBlocks.register(
|
RegisterBlocks.register(
|
||||||
replacedBlockId.getPath() + "_" + i,
|
replacedBlockId.getPath() + "_" + i,
|
||||||
BlockUtils.canPassThrough(blockState)
|
BlockUtils.canPassThrough(blockState),
|
||||||
|
BlockUtils.getShape(blockState)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
package net.momirealms.craftengine.fabric.util;
|
package net.momirealms.craftengine.fabric.util;
|
||||||
|
|
||||||
import net.minecraft.block.AbstractBlock;
|
import net.minecraft.block.AbstractBlock;
|
||||||
|
import net.minecraft.block.Block;
|
||||||
import net.minecraft.block.BlockState;
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.shape.VoxelShape;
|
||||||
|
import net.minecraft.util.shape.VoxelShapes;
|
||||||
|
import net.minecraft.world.BlockView;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@@ -43,4 +48,20 @@ public class BlockUtils {
|
|||||||
throw new RuntimeException("Failed to access 'collidable' field", e);
|
throw new RuntimeException("Failed to access 'collidable' field", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static VoxelShape getShape(BlockState state) {
|
||||||
|
if (state == null) return VoxelShapes.fullCube();
|
||||||
|
Block block = state.getBlock();
|
||||||
|
VoxelShape combinedShape = VoxelShapes.empty();
|
||||||
|
try {
|
||||||
|
for (BlockState possibleState : block.getStateManager().getStates()) {
|
||||||
|
VoxelShape currentShape = possibleState.getOutlineShape(null, BlockPos.ORIGIN);
|
||||||
|
combinedShape = VoxelShapes.union(combinedShape, currentShape);
|
||||||
|
}
|
||||||
|
return combinedShape.isEmpty() ? VoxelShapes.fullCube() : combinedShape;
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
return VoxelShapes.fullCube();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,21 +2,33 @@ package net.momirealms.craftengine.fabric.util;
|
|||||||
|
|
||||||
import net.minecraft.block.AbstractBlock;
|
import net.minecraft.block.AbstractBlock;
|
||||||
import net.minecraft.block.Block;
|
import net.minecraft.block.Block;
|
||||||
|
import net.minecraft.block.BlockState;
|
||||||
|
import net.minecraft.block.ShapeContext;
|
||||||
import net.minecraft.registry.Registries;
|
import net.minecraft.registry.Registries;
|
||||||
import net.minecraft.registry.Registry;
|
import net.minecraft.registry.Registry;
|
||||||
import net.minecraft.registry.RegistryKey;
|
import net.minecraft.registry.RegistryKey;
|
||||||
import net.minecraft.registry.RegistryKeys;
|
import net.minecraft.registry.RegistryKeys;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.shape.VoxelShape;
|
||||||
|
import net.minecraft.util.shape.VoxelShapes;
|
||||||
|
import net.minecraft.world.BlockView;
|
||||||
import net.momirealms.craftengine.fabric.CraftEngineFabricMod;
|
import net.momirealms.craftengine.fabric.CraftEngineFabricMod;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class RegisterBlocks {
|
public class RegisterBlocks {
|
||||||
@SuppressWarnings("UnusedReturnValue")
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
public static Block register(String name, boolean canPassThrough) {
|
public static Block register(String name, boolean canPassThrough, VoxelShape outlineShape) {
|
||||||
AbstractBlock.Settings settings = Block.Settings.create().nonOpaque().strength(-1.0F, 3600000.0F);
|
AbstractBlock.Settings settings = Block.Settings.create().nonOpaque().strength(-1.0F, 3600000.0F);
|
||||||
if (canPassThrough) settings.noCollision();
|
VoxelShape collisionShape;
|
||||||
return register(name, Block::new, settings);
|
if (canPassThrough) {
|
||||||
|
collisionShape = VoxelShapes.empty();
|
||||||
|
settings.noCollision();
|
||||||
|
} else {
|
||||||
|
collisionShape = outlineShape;
|
||||||
|
}
|
||||||
|
return register(name, (settingsParam) -> new CustomBlock(settingsParam, outlineShape, collisionShape), settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Block register(String name, Function<AbstractBlock.Settings, Block> blockFactory, AbstractBlock.Settings settings) {
|
public static Block register(String name, Function<AbstractBlock.Settings, Block> blockFactory, AbstractBlock.Settings settings) {
|
||||||
@@ -31,3 +43,24 @@ public class RegisterBlocks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CustomBlock extends Block {
|
||||||
|
private final VoxelShape outlineShape;
|
||||||
|
private final VoxelShape collisionShape;
|
||||||
|
|
||||||
|
public CustomBlock(Settings settings, VoxelShape outlineShape, VoxelShape collisionShape) {
|
||||||
|
super(settings);
|
||||||
|
this.outlineShape = outlineShape;
|
||||||
|
this.collisionShape = collisionShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||||
|
return this.outlineShape;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
||||||
|
return this.collisionShape;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user