Jump to content

Recommended Posts

Posted

I recently made a multi-textured block. All the sides are different, but no matter what direction I'm facing when I place the block, the side stay the same. I want to make it so that my block is like a chest/furnace/etc, in the way that it faces whichever way you place it. What's the code for doing that?

Posted

I'm using

 

public int getBlockTextureFromSide(int i)
{
	switch (i)
	{
	case 0: return 16;			//case 0 = Bottom
	case 1: return 16;			//case 1 = Top
	case 2: return 17;			//case 2 = Front
	case 3: return 16;			//case 3 = Back Side
	case 4: return 18;			//case 4 = Right Side
	case 5: return 19;			//case 5 = Left Side
	default: return 16;			//default = default if a case is not assigned
	}

 

I tried copy pasting from dispenser, but it didn't help

Posted

I recently made a multi-textured block. All the sides are different, but no matter what direction I'm facing when I place the block, the side stay the same. I want to make it so that my block is like a chest/furnace/etc, in the way that it faces whichever way you place it. What's the code for doing that?

 

What you need to do is 2 things... (I know this because I recently had the exact same problem)

1. when the block gets placed, get the orientation of the player and store that in the block's metadata to indicate which way it should face.

2. then, use that metadata to change which texture is shown on which side

 

Sounds easy right? :-\.... well it's not :-P

My best advice to you is to look at the

 

ItemSign.java

BlockSign.java

TileEntitySign.java

 

which you can find in common\net.minecraft.src\

 

They show examples of how to store the rotation angle, just like signs do! :-)

Although, for signs, they have an item class for when you hold it, and a block class for when it's found in the world.  In your case, those will be the same things. :-P

 

Also, do NOT forget to do the following when your mod loads....

Make SURE you register your tileentity like this....

 

GameRegistry.registerTileEntity(TileEntityYourBlock.class, "direction");

 

Here's some code that you can put into your block class so when it gets placed it can store this value:

int myAng = MathHelper.floor_double((double)((par2EntityPlayer.rotationYaw + 180.0F) * 16.0F / 360.0F) + 0.5D) & 15;

(it just gets the angle from the player's angle)

 

Although you'll want to convert it to an integer, since you want it to face N, S, W, or E

 

Don't feel bad if it takes you a bit to sort through all this junk, it took me about a week! :-P

Posted

To do it purely with metadata you only need to make changes in your block's class

 

Store the different directions as different metadata

@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityliving)
{
    int metadata = world.getBlockMetadata(x, y, z);
    int playerDirection = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
    if(metadata == 0)
    {
        switch(playerDirection)
        {
            case 0: metadata += 2; break; //South Facing Block = meta 2
            case 1: metadata += 3; break; //West Facing Block = meta 3
            case 2: metadata += 0; break; //North Facing Block = meta 0
            case 3: metadata += 1; break; //East Facing Block = meta 1
        }
    }
    world.setBlockMetadataWithNotify(x, y, z, metadata);
}

 

Now you can texture your block

@SideOnly(Side.CLIENT)
@Override
public int getBlockTextureFromSideAndMetadata(int side, int metadata)
{
    if (side == 0 || side = 1) return 16; //Top and bottom always look the same
    switch (metadata)
    {
    case 0: //North Facing Block 
        if (side == 2) return 16; //North Side of Block
        if (side == 3) return 17; //South Side of Block
        if (side == 4) return 19; //West Side of Block
        if (side == 5) return 18; //East Side of Block
    case 1: //East Facing Block 
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    case 2: //South Facing Block
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    case 3: //West Facing Block
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    }
    return 16; //Fall back if meta is out of range
}

Posted

To do it purely with metadata you only need to make changes in your block's class

 

Store the different directions as different metadata

@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityliving)
{
    int metadata = world.getBlockMetadata(x, y, z);
    int playerDirection = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
    if(metadata == 0)
    {
        switch(playerDirection)
        {
            case 0: metadata += 2; break; //South Facing Block = meta 2
            case 1: metadata += 3; break; //West Facing Block = meta 3
            case 2: metadata += 0; break; //North Facing Block = meta 0
            case 3: metadata += 1; break; //East Facing Block = meta 1
        }
    }
    world.setBlockMetadataWithNotify(x, y, z, metadata);
}

 

Now you can texture your block

@SideOnly(Side.CLIENT)
@Override
public int getBlockTextureFromSideAndMetadata(int side, int metadata)
{
    if (side == 0 || side = 1) return 16; //Top and bottom always look the same
    switch (metadata)
    {
    case 0: //North Facing Block 
        if (side == 2) return 16; //North Side of Block
        if (side == 3) return 17; //South Side of Block
        if (side == 4) return 19; //West Side of Block
        if (side == 5) return 18; //East Side of Block
    case 1: //East Facing Block 
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    case 2: //South Facing Block
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    case 3: //West Facing Block
        if (side == 2) return 0; //North Side of Block
        if (side == 3) return 0; //South Side of Block
        if (side == 4) return 0; //West Side of Block
        if (side == 5) return 0; //East Side of Block
    }
    return 16; //Fall back if meta is out of range
}

 

Thank you very much. A lot easier than figuring the code by myself

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.