如何在 Minecraft 1.12.2 模组中制作方向块?
How to make directional block in Minecraft 1.12.2 modding?
我已经开发 Minecraft 1.12.2 mod 几个月了。我想做定向块。方块是黑色和紫色的立方体(放置时,拿在手里是有质感的)。 JSON 文件我已经检查了多次。每次我开始游戏时,所有方向都设置为默认值。感谢您的帮助!
P.S。我骗了 Minecraft 原版 BlockFurnace,但它让我感到困惑而不是帮助。
这是我已有的代码:
public class DirBlock extends BlockHorizontal implements IHasModel {
public DirBlock(String name) {
super(Material.CLOTH);
setSoundType(SoundType.CLOTH);
setCreativeTab(Main.RM_TAB1);
setUnlocalizedName(name);
setRegistryName(name);
setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
ModBlocks.BLOCKS.add(this);
ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
}
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, new IProperty[] { FACING });
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, meta == 0 ? EnumFacing.WEST : EnumFacing.EAST);
}
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing.getHorizontalIndex();
}
@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
return super.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, meta, placer, hand).withProperty(FACING, placer.getHorizontalFacing());
}
@Override
public void registerModels() {
Main.proxy.registerItemRenderer(Item.getItemFromBlock(this), 0, "inventory");
}
}
你的metaFromState
和stateFromMeta
函数不是镜像(它们应该是)
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, meta == 0 ? EnumFacing.WEST : EnumFacing.EAST);
}
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing.getHorizontalIndex();
}
您正在使用面索引对元数据进行编码,但是当您返回时,您确定元数据是否恰好为 0,如果是,则面为 WEST(所有其他值均表示 EAST) .
不幸的是,WEST 不是 0。这来自 getHorizontalIndex()
的代码内 Javadoc:
/**
* Get the index of this horizontal facing (0-3). The order is S-W-N-E
*/
你应该使用这个:
//use existing getMetaFromState
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
}
如果你希望你的方块永远只面向东方和西方(而不是 NORTH/SOUTH),那么你应该在放置的方块上强制执行,然后使用一点元数据进行编码:
//use existing getStateFromMeta
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing == EnumFacing.WEST?0:1;
}
我已经开发 Minecraft 1.12.2 mod 几个月了。我想做定向块。方块是黑色和紫色的立方体(放置时,拿在手里是有质感的)。 JSON 文件我已经检查了多次。每次我开始游戏时,所有方向都设置为默认值。感谢您的帮助!
P.S。我骗了 Minecraft 原版 BlockFurnace,但它让我感到困惑而不是帮助。
这是我已有的代码:
public class DirBlock extends BlockHorizontal implements IHasModel {
public DirBlock(String name) {
super(Material.CLOTH);
setSoundType(SoundType.CLOTH);
setCreativeTab(Main.RM_TAB1);
setUnlocalizedName(name);
setRegistryName(name);
setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
ModBlocks.BLOCKS.add(this);
ModItems.ITEMS.add(new ItemBlock(this).setRegistryName(this.getRegistryName()));
}
public static final PropertyDirection FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, new IProperty[] { FACING });
}
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, meta == 0 ? EnumFacing.WEST : EnumFacing.EAST);
}
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing.getHorizontalIndex();
}
@Override
public IBlockState getStateForPlacement(World world, BlockPos pos, EnumFacing facing, float hitX, float hitY,
float hitZ, int meta, EntityLivingBase placer, EnumHand hand) {
return super.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, meta, placer, hand).withProperty(FACING, placer.getHorizontalFacing());
}
@Override
public void registerModels() {
Main.proxy.registerItemRenderer(Item.getItemFromBlock(this), 0, "inventory");
}
}
你的metaFromState
和stateFromMeta
函数不是镜像(它们应该是)
@Override
public IBlockState getStateFromMeta(int meta) {
return getDefaultState().withProperty(FACING, meta == 0 ? EnumFacing.WEST : EnumFacing.EAST);
}
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing.getHorizontalIndex();
}
您正在使用面索引对元数据进行编码,但是当您返回时,您确定元数据是否恰好为 0,如果是,则面为 WEST(所有其他值均表示 EAST) .
不幸的是,WEST 不是 0。这来自 getHorizontalIndex()
的代码内 Javadoc:
/**
* Get the index of this horizontal facing (0-3). The order is S-W-N-E
*/
你应该使用这个:
//use existing getMetaFromState
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(FACING, EnumFacing.getHorizontal(meta));
}
如果你希望你的方块永远只面向东方和西方(而不是 NORTH/SOUTH),那么你应该在放置的方块上强制执行,然后使用一点元数据进行编码:
//use existing getStateFromMeta
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing == EnumFacing.WEST?0:1;
}