到目前为止,我已经开发了几个月的Minecraft 1.12.2 mod。我想做方向块。块是黑色和紫色的立方体(放置时,手中有纹理)。我多次检查的JSON文件。每当我开始游戏时,所有方向都设置为默认值。感谢帮助!
附:我嘲笑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
and stateFromMeta
functions are not mirrors (And they should be)@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));
}
如果你希望你的块只面对EAST和WEST(而且从不北/南),那么你应该在放置的块上强制执行,然后使用一点元数据进行编码:
//use existing getStateFromMeta
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing == EnumFacing.WEST?0:1;
}