Jump to content

Recommended Posts

Posted

I've created a block that stores the rotation data inside the tileentity.

 

The problem I'm having is when I right click the custom block I want it to create the same block 4 blocks away.

When I right click the block it places the block but facing North not any of the other directions.

 

The code I've tried to use inside the block code:

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) 
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getStateFromMeta(getFabricType(state));

        //attempts to get the block placed with right property
        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));

        worldIn.setBlockState(new BlockPos(pos.getX() + 4, pos.getY(), pos.getZ()), ModBlocks.testBlock.getDefaultState().withProperty(FACING, EnumFacing.WEST));


        return true;
    }

 

The tileEntity code: I think the problem is caused by this line "private EnumFacing facing = EnumFacing.NORTH" not sure how I could alter this code to not change a placed block to North rotation.

 

 

public class TileEntityTest extends TileEntity
{
    [color=red]private EnumFacing facing = EnumFacing.NORTH;[/color]

    public EnumFacing getFacing()
    {
        return facing;
    }

    public void setFacing(EnumFacing facing)
    {
        this.facing = facing;
        markDirty();
    }

    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        super.readFromNBT(compound);
        facing = EnumFacing.getFront(compound.getInteger("facing"));
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        super.writeToNBT(compound);
        compound.setInteger("facing", facing.getIndex());
    }

    @Override
    public Packet getDescriptionPacket()
    {
        NBTTagCompound nbttagcompound = new NBTTagCompound();
        this.writeToNBT(nbttagcompound);
        return new S35PacketUpdateTileEntity(this.getPos(), this.getBlockMetadata(), nbttagcompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
    {
        readFromNBT(packet.getNbtCompound());
        getWorld().markBlockForUpdate(getPos());
    }

    @Override
    public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
    {
        return oldState.getBlock() != newSate.getBlock();
    }
}

Posted

If you are storing property in TileEntity - what sets that property? Fields don't get magically assigned, you need to use them somewhere. Does your Block class do this?

1.7.10 is no longer supported by forge, you are on your own.

Posted

this is the block code, the facing property gets assigned when the block is placed.

public class BlockTest extends Block
{
    public static final IProperty<EnumFacing> FACING = PropertyDirection.create("facing", EnumFacing.Plane.HORIZONTAL);
    public static final PropertyEnum BLOCKTYPE = PropertyEnum.create("block", EnumBlockType.class);


    public BlockTest(String unlocalizedName)
    {
        super(Material.stone);
    }

    @Override @SideOnly(Side.CLIENT)
    public void getSubBlocks(Item item, CreativeTabs tab, List<ItemStack> list)
    {
        for(EnumBlockType EnumBlockType : EnumBlockType.values())
        {
            list.add(new ItemStack(item, 1, EnumBlockType.getMetadata()));
        }
    }

    @Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(BLOCKTYPE, EnumBlockType.byMetaData(meta));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        return ((EnumBlockType)state.getValue(BLOCKTYPE)).getMetadata();
    }

    @Override
    protected BlockState createBlockState()
    {
        return new BlockState(this, BLOCKTYPE, FACING);
    }

    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        return state.withProperty(FACING, getFacing(worldIn, pos));
    }

    @Override
    public boolean hasTileEntity(IBlockState state)
    {
        return true;
    }

    @Override
    public TileEntity createTileEntity(World world, IBlockState state)
    {
        return new TileEntityBlockTest();
    }

    public TileEntityBlockTest getTileEntity(IBlockAccess world, BlockPos pos)
    {
        return (TileEntityBlockTest) world.getTileEntity(pos);
    }

    public EnumFacing getFacing(IBlockAccess world, BlockPos pos)
    {
        return getTileEntity(world, pos).getFacing();
    }

    public void setFacing(IBlockAccess world, BlockPos pos, EnumFacing facing)
    {
        getTileEntity(world, pos).setFacing(facing);
    }

    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
    {
        setFacing(worldIn, pos, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer));
    }

    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getDefaultState();

        //attempts to get the block placed with right property
        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));

        worldIn.setBlockState(new BlockPos(pos.getX() + 4, pos.getY(), pos.getZ()), ModBlocks.testBlock.getDefaultState().withProperty(FACING, EnumFacing.WEST));


        return true;
    }
}

Posted

The tileEntity code: I think the problem is caused by this line "private EnumFacing facing = EnumFacing.NORTH" not sure how I could alter this code to not change a placed block to North rotation.

 

That line is likely the problem.  Do what Diesieben07 says and get rid of the TE.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I've removed the tileEntity and managed to place blocks with the correct blockType property but having few problems merging the facing property together.

 

Believe I need to combine these two lines together but having trouble merging both returns into one

 

Method I'm having trouble getting both property to work together.

@Override
    public int getMetaFromState(IBlockState state)
    {
        return  state.getValue(FACING).getIndex();
        //return state.getValue(BLOCKTYPE).getMetadata();
    }

Posted

Metadata is only 4 bits worth of data.  Facing takes (minimum) 2 of those bits, leaving you 2 (four possible values) for BLOCKTYPE.

 

Show your BLOCKTYPE property declaration/definition.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

That could be the problem then as BLOCKTYPE uses 10 values :/

Thought facing might of taken 1 bit, as I'm just using horizontal properties with 4 values, my mistake xD

 

I could go back to storing the block face in the tileEntity and figure a way to fix my first post problem.

 

 

Posted

Should I still use getStateFromMeta/getMetaFromState to store the "BLOCKTYPE"?

@Override
    public IBlockState getStateFromMeta(int meta)
    {
        return this.getDefaultState().withProperty(BLOCKTYPE, EnumBlockType.byMetaData(meta));
    }

    @Override
    public int getMetaFromState(IBlockState state)
    {
        return ((EnumBlockType)state.getValue(BLOCKTYPE)).getMetadata();
    }

    @Override
    protected BlockState createBlockState()
    {
        return new BlockState(this, BLOCKTYPE, FACING);
    }

    @Override
    public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
    {
        return state.withProperty(FACING, getFacing(worldIn, pos));
    }

 

When I place to block ingame it has the correct block type / facing n/e/s/w,

but when I right click the block it's placing the block in the north face not west.

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
        Block testBlock = ModBlocks.testBlock;
        IBlockState testState = testBlock.getDefaultState();

        worldIn.setBlockState(new BlockPos(pos.getX() + 2, pos.getY(), pos.getZ()), testState.withProperty(FACING, EnumFacing.WEST));
return true;
}

 

 

 

 

Posted

That could be the problem then as BLOCKTYPE uses 10 values :/

Thought facing might of taken 1 bit, as I'm just using horizontal properties with 4 values, my mistake xD

 

I could go back to storing the block face in the tileEntity and figure a way to fix my first post problem.

 

I would store the FACING in the metadata (so many blocks already do, if a mod comes along with a tool that can rotate blocks, your block would be automatically rotatable with no effort if you do) and the BLOCKTYPE in the TileEntity.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

If I did store the BlockType inside the tileEntity is it possible to create recipes that uses each blockType?

Was main reason I stored the BlockType inside the metadata so I could create recipes for each metadata version.

 

I've not looked into 1.8/1.9 recipes wasn't sure if it was possible.

 

I have all code working now will keep it how it is :)

Posted

Is there any forge documentations on custom ItemBlock or examples of basic ItemBlocks?

 

never used, best if i did some research before I break my mod for the tenth time this week :D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.