到目前为止,我已经在开发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");
}
}
答案 0 :(得分:0)
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)。 / p>
不幸的是,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(而不面对NORTH / SOUTH),则应在放置的块上强制实施,然后使用一点元数据进行编码:
//use existing getStateFromMeta
@Override
public int getMetaFromState(IBlockState state) {
EnumFacing facing = (EnumFacing) state.getValue(FACING);
return facing == EnumFacing.WEST?0:1;
}