Jump to content

MC1.7.10 Randomize Block Top Texture


Eractnod

Recommended Posts

I have created a customized crafting table that has some internal storage.  What I am trying to do is randomize the top texture when I place the block down.  I can get the top to randomly pick the texture, but the issue is it will also change all like blocks that are already placed.  I can't use metadata as I change the metadata to set direction when placing the block. (I also have a front texture).  Any suggestions would be greatly appreciated.

Link to comment
Share on other sites

You can try and cheat a bit and base the texture on the coordinates of the Block. Then replacing the block will not change the texture, but you don't have to save any additional data.

cunning, I like it.

 

You could avoid any obvious patterns by using a random number generator with a seed based on your [x,y,z], something like

long seed = (x & 255) | ((z & 255) <<  | (y << 16);
Random generator = new Random(seed);
int myRandomTexture = generator.nextInt(MY_MAXIMUM_TEXTURE_NUMBER_PLUS_ONE);

 

-TGG

 

 

 

 

 

 

Link to comment
Share on other sites

Guest Abrynos

But this doesn't work fo me:

@Override
public int onBlockPlaced(World world, int x, int y, int z, int meta, float par1, float par2, float par3, int par4){
long seed = (x & 255) | ((y & 255) <<  | (z << 16);
Random generator = new Random(seed);
int myRandomTexture = generator.nextInt(11);

        this.setBlockTextureName("Abrynos:Rune_" + myRandomTexture);
return par4;
}

Link to comment
Share on other sites

I have an idea that might work:

Create a random number on getIcon and then make the block a tile entity and make it require the random number argument, the tile entity can then save it so it has the same one when the world is loaded back up, doubt this will work as I just thought of it in my head a few seconds ago, but it may give you an idea of how you could do it.

Link to comment
Share on other sites

If the number of textures is less than 5, you can also use metadata with bit-shifting since there are only four states of a block with front texture.

@imadnsn  I had done this.  Works to a point, but like you said can only work with 4 textures.  Best result so far.

 

I have an idea that might work:

Create a random number on getIcon and then make the block a tile entity and make it require the random number argument, the tile entity can then save it so it has the same one when the world is loaded back up, doubt this will work as I just thought of it in my head a few seconds ago, but it may give you an idea of how you could do it.

@kenoba10

It is a tile entity.  I tried adding block updates in the tile, but still had to same effect of changing all the like tables.

I have been playing around with something like the way the furnace changes the block with a burning and idle block to do this from the TE, but have not hit on the right way yet.

I am also thinking about using a special renderer.  Should be able to use forge direction for the front then and be able use meta for the tops.  Only in idea form at the moment.  Might cause some lag issues.

 

 

Link to comment
Share on other sites

Hi

 

easiest way = override Block.getBlockTexture(), which provides [x,y,z], to return one of your textures based on the [x,y,z] as per DieSieben's suggestion.

 

next easiest way = store the texture information in your TileEntity, and render the sides in the TileEntityRenderer not the Block.

 

The bottom line is - if you want each placed container to render a different texture, and you don't want to use the [x,y,z] method, you have to store that texture somewhere for each container.  You can't use metadata, so you must use something else.  Your texture is changing for all containers at once because you are not storing the necessary information for each placed container.

 

-TGG

 

Link to comment
Share on other sites

I am going to have to admit temporary defeat on this one.  I have tried every which way I know to manipulate this method to only change one block, but they still all change.  I am going to continue to gain knowledge and revisit later, but for now, I am just going to leave the top side of the block as one texture.

 

 @SideOnly(Side.CLIENT)
    public IIcon getIcon(IBlockAccess block, int x, int y,  int z,  int side)
    {
        return this.getIcon(side,  block.getBlockMetadata(x, y, z));
    }

 

 

Link to comment
Share on other sites

This is what I have tried in my block class.  When I use the variable [top] in tableIconTop, it changes all the blocks top icons still.  As a temporary setup, I change top to meta and at least get 4 different tops based on direction.

 

 @SideOnly(Side.CLIENT)
    public IIcon getIcon(IBlockAccess block, int x, int y, int z, int side)
    {
    	TECraftingTableClass block1 = (TECraftingTableClass) block.getBlock(x, y, z);
    	int meta = block.getBlockMetadata(x, y, z);
   
    	return side == 1 ? block1.tableIconTop[top] : (side == 0 ? block1.tableIconTop[0] : (side != meta ? block1.tableIconSide : block1.tableIconFront));
    }

    @SideOnly(Side.CLIENT)
     //Not used at the moment
    public IIcon getIcon(int side, int meta, IIcon icon)
    {
    	return side == 1 ? icon : (side == 0 ? this.tableIconTop[0] : (side != meta ? this.tableIconSide : this.tableIconFront));
    }

    /**
     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
     * This is needed to show the textures while in the tool bar
     */
    public IIcon getIcon(int side, int meta)
    {
    	return side == 1 ? this.tableIconTop[0] : (side == 0 ? this.tableIconTop[0] : (side != meta ? this.tableIconSide : this.tableIconFront));
    }

public void onBlockAdded(World world, int x, int y, int z) {
    //Random number 	
     top = rand.nextInt(10);
    }

Link to comment
Share on other sites

Hi

 

The problem is that you're still trying to store information about each block in places that aren't appropriate.  There is only one Block.  So when you change top in that block, all the blocks change.  You have to store the top information in the metadata, in the tile entity, or cheat with the [x,y,z] like dieSieben suggested.

 

If you're having trouble understanding this, these two links might help

http://greyminecraftcoder.blogspot.com/2013/10/the-most-important-minecraft-classes_9.html

http://greyminecraftcoder.blogspot.com.au/2013/07/blocks.html

 

To show you a proof of concept - try this

@SideOnly(Side.CLIENT)
    public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side)
    {
    	int meta = world.getBlockMetadata(x, y, z);   
        final int NUMBER_OF_TOP_ICONS = 10;
        int topIconIndex = (x+y+z) % NUMBER_OF_TOP_ICONS;                   // "randomly" select a texture for the table top.
    	return side == 1 ? tableIconTop[topIconIndex]  : (side == 0 ? tableIconTop[0] : (side != meta ? tableIconSide : tableIconFront));
    }

-TGG

 

 

 

Link to comment
Share on other sites

Now I understand what Diesieben07 meant by cheat a bit.  I was still trying to make the block have a random top.  When placing the block at the same location, it could have 10 different tops.  What this does is make the location generate the top.  Place the block at the same location 10 times, you get the same top.  Each location could give a number between 0 and 9, but always give the same number at that given x, y, z location.  This gives the illusion of random tops.

 

I was closer to the right way in one of my earlier attempts that I didn't post.  I generated a unique number by adding x + y + z and storing it in the TE.  Also saved the random number in the TE when the block was placed.  My conditional statement must have been not quite right as one or two blocks would not change, but the rest would.

 

Thank you all for the help.  This is why when I am bored at work, I try and read every post.  I gain new knowledge from each question asked.

My goal is to learn something new each day, or each week until the day I die (Like my Dad).  Java and Minecraft now, with something new always around the corner.

 

EDIT: This is what I put into my block class.  Worked fine in my test mod, but crashed in my real mod.  For some strange reason, it just didn't like negative block coordinates. (Joke)

 

 @SideOnly(Side.CLIENT)
    //Many thanks goes to TheGreyGhost and DieSieBen07 for the help on this method
    public IIcon getIcon(IBlockAccess world, int x, int y, int z, int side)
    {
    	int meta = world.getBlockMetadata(x, y, z); 
    	
        final int NUMBER_OF_TOP_ICONS = 11;
        int topIconIndex = (x + y + z) % NUMBER_OF_TOP_ICONS;                   // "randomly" select a texture for the table top.
        //Sometimes the coordinates are negative
        if (topIconIndex < 0) {
        	topIconIndex *= -1;
        }
        
    	return side == 1 ? tableIconTop[topIconIndex]  : (side == 0 ? tableIconTop[0] : (side != meta ? tableIconSide : tableIconFront));
    }

Link to comment
Share on other sites

Thank you all for the help.  This is why when I am bored at work, I try and read every post.  I gain new knowledge from each question asked.

My goal is to learn something new each day, or each week until the day I die (Like my Dad).  Java and Minecraft now, with something new always around the corner.

no worries, you're welcome.  That's the joy of programming, every day I find out that I didn't understand something as much as I thought :)

 

EDIT: This is what I put into my block class.  Worked fine in my test mod, but crashed in my real mod.  For some strange reason, it just didn't like negative block coordinates. (Joke)

Ah yeah (facepalm), I always forget that taking % of a negative number gives a negative number.

 

-TGG

 

 

 

Link to comment
Share on other sites

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.