Jump to content

[1.8] Help with real time block coloring


dev909

Recommended Posts

Hello possible saviors, I've been looking all over the forum, google, and src code for an answer but now I'm here. Ok here's what I'm trying to do: I want to be able to right click a block and render a new color on the block. So far I found two ways to possibly do this. The first way I found the method Block.recolorBlock(), which I think is the best way to go as I'm hoping that it will allow me to color per side (as its arg suggests) and that I can hopefully do it on any block. The second way was to create a custom block, override the color methods (getBlockColor(), getRenderColor(), colorMultiplier()), create a custom model that allows tinting (much like grass), and then when right click, of course change color. Preferably I would rather be able to set a custom color instead of using an enum for the recolorBlock() arg. I tried both ways and nothing has worked so far. Here's the codes:

 

Method 1 (recolorBlock()):

 

 

Item (foo):

public class foo {

    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    		worldIn.getBlockState(pos).getBlock().recolorBlock(worldIn, pos, EnumFacing.UP, EnumDyeColor.RED);
    		return true;
    }
}

Also tried the opposite way, using onBlockActivated() instead of onItemUse()

 

Block (foobarBlock):

public class foobarBlock extends Block {

public foobarBlock() {
	super(Material.wood);
	// TODO Auto-generated constructor stub
}
public void onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer player) {
		BlockPos pos = new BlockPos(par2,par3,par4);
		this.recolorBlock(par1World, pos, EnumFacing.UP, EnumDyeColor.RED);
}
        
}

 

 

 

Method 2 (Custom blocks):

 

 

Block (foorbarBlock):

public class foobarBlock extends Block {

private int blockColor;

public foobarBlock() {
	super(Material.wood);
                //set blockColor to a sample color (teal, cyanish)
	blockColor = 65535;
	// TODO Auto-generated constructor stub
}

public void setBlockColor(int color) {
	this.blockColor = color;
}

@SideOnly(Side.CLIENT)
public int getBlockColor() {
    return this.blockColor;
}

@SideOnly(Side.CLIENT)
public int getRenderColor(IBlockState state) {
    return this.getBlockColor();
}

@SideOnly(Side.CLIENT)
public int colorMultiplier(IBlockAccess worldIn, BlockPos pos, int renderPass) {
    return this.getBlockColor();
}

        
}

Item (foo):

public class foo {

    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	if (worldIn.getBlockState(pos).getBlock() == Blocks.foobar) {
    		foobarBlock block = (foobarBlock) worldIn.getBlockState(pos).getBlock();
    		//set block to a red
    		block.setBlockColor(16711680);
    		return true;
    	}
    	else {
    	return false;
    	}
    }
}

 

 

Link to comment
Share on other sites

Blocks are singletons, so you cannot store any state information in the block class - if you change the color of one block, you've changed the color for ALL of those blocks. To have independent block information, you need to use metadata (if possible values are between 0 and 15) or a TileEntity. In your case, since you are storing color, you will want a TileEntity.

 

Then you can return the color value from the TE for the Block using getColorMultiplier:

@Override
@SideOnly(Side.CLIENT)
public int colorMultiplier(IBlockAccess world, BlockPos pos, int renderPass) {
   TileEntity te = world.getTileEntity(te);
   if (te instanceof YourTileEntity) {
       return ((YourTileEntity) te).getCustomColorMultiplier(renderPass);
   }
   return super.colorMultiplier(world, pos, renderPass);
}

Doesn't have a side argument, but you can look around in the Block class and see if you can find any color methods that take both a BlockPos (to get the TileEntity) and the EnumFacing (for the side).

Link to comment
Share on other sites

Your method with new customblock and tileentity may and will work, but it will not support some modded blocks/blocks with tile entities.

---

So yeah, it depends on what you want to achieve. New block and tile entity method is most simple one in both coding, and using. It will be compatible with ~95% modded blocks. One thing i may reccomend is following: when your block is rendered, render "original" block on it's place. Because recoloring will happen before model is rendered. For that, i think, you will have to use ISmartBlockModel. Like that your block will be able to take model of torches, fences, stairs, even other "smart" models...

As far as i know, that is only color method (one suggested by coolAlias) that has at least world and block position. I don't know any with EnumFacing... Again - I... Maybe you will find them...

Good luck!

Link to comment
Share on other sites

  • 8 months later...

Sorry for dragging up an old thread, but I haven't really had much time to code within the last couple months or so.

 

Anyways I got a tile entity working for what I need, such as that I can right click a block and change its color in real time. Yay! I'm using just one int variable in the TE (color) and for the nbt compound and it works great. Blocks get updated right away with the right color, and only blocks that are clicked. However if I want to expand functionality from just one color, to three values (as in red, green, and blue variables) I get problems. I have tried two different methods that are not really that different from each other. I tried created an int array in the TE, and use int array for the nbt data, and I have tried storing three ints in the nbt data. I have updated the relevant classes for both approaches. No matter which one I choose, whenever I'm in game, if I place multiple blocks down, and right click just ONE, all the blocks get updated. Almost as if the block is acting as a singleton again and not having their own separate entities. Any clues?

 

Link to comment
Share on other sites

Post your new code.

 

Right...forgot about that..

 

Here's the relevant classes. The approach is to use an int array. Code flow is paintcan: set paintbrush color -> block: get color from paintbrush -> tileentity: get color from block, update -> block: render new color.

 

COBlock.java

 

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	System.out.println("> onblockactivated");
    	if (ItemStack.areItemsEqual(playerIn.getCurrentEquippedItem(), new ItemStack(ColoramaReg.COPaintbrush))) {
    		System.out.println("> objects are equal");
    		TileEntity te = worldIn.getTileEntity(pos);
    		System.out.println("> got tileentity");
    		if (te instanceof COBlock_TE) {
    			COBlock_TE tile = (COBlock_TE)te;
    			NBTTagCompound nbtTagCompound = playerIn.getCurrentEquippedItem().getTagCompound();
    			if (nbtTagCompound != null && nbtTagCompound.hasKey("rgb")) {
    				int[] rgb = nbtTagCompound.getIntArray("rgb");
        			tile.setColor(rgb[0], rgb[1], rgb[2]);
        			tile.getWorld().markBlockForUpdate(pos);
        			System.out.println("> color changed");
        			return true;
    			}
    			else {
    				return false;
    			}
    		}
    		
    	}
    	return false;
    }

 

 

COBlock_TE.java

 

public class COBlock_TE extends TileEntity {

private static final int[] DEFAULT_RGB = {255,255,255};
private int[] RGB = DEFAULT_RGB;

public int getColor() {return convertToInt(RGB[0],RGB[1],RGB[2]);}
public void setColor(int r, int g, int b) {
	this.RGB[0] = r;
	this.RGB[1] = g;
	this.RGB[2] = b;
}

public static int convertToInt(int r, int g, int b) {return (r*65536)+(g*256)+b;}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound nbtTagCompound = new NBTTagCompound();
	writeToNBT(nbtTagCompound);
	int metadata = getBlockMetadata();
	return new S35PacketUpdateTileEntity(this.pos, metadata, nbtTagCompound);
}

@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
	readFromNBT(pkt.getNbtCompound());
}

@Override
public void writeToNBT(NBTTagCompound parentNBTTagCompound) {
	super.writeToNBT(parentNBTTagCompound);
	parentNBTTagCompound.setIntArray("rgb", RGB);
}

@Override
public void readFromNBT(NBTTagCompound parentNBTTagCompound) {
	super.readFromNBT(parentNBTTagCompound);
	int[] rgbIn = DEFAULT_RGB;
	if (parentNBTTagCompound.hasKey("rgb")) { 
		rgbIn = parentNBTTagCompound.getIntArray("rgb");
	}
	RGB = rgbIn;
}

}

 

 

COBlockPaintcan.java

 

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	System.out.println("> onblockactivated");
    	if (ItemStack.areItemStacksEqual(playerIn.getCurrentEquippedItem(), new ItemStack(ColoramaReg.COPaintbrush))) {
    		System.out.println("> objects are equal");
    		NBTTagCompound nbtTagCompound = playerIn.getCurrentEquippedItem().getTagCompound();
    		System.out.println("> nbt tag....");
		if (nbtTagCompound == null) {
			nbtTagCompound = new NBTTagCompound();
			playerIn.getCurrentEquippedItem().setTagCompound(nbtTagCompound);
    		System.out.println("> nbt tag created");
		}
		nbtTagCompound.setIntArray("rgb", new int[]{255,0,0});
    		System.out.println("> nbt tag COLOR set to 255,0,0");
    	}
    	return false;
    }

 

 

 

Link to comment
Share on other sites

private static final int[] DEFAULT_RGB = {255,255,255};
private int[] RGB = DEFAULT_RGB;

 

You haven't created a new array for

RGB

here, it's pointing to the same array as

DEFAULT_RGB

. Any changes made through

RGB

will be visible from

DEFAULT_RGB

and the

RGB

field of all other instances of this class.

 

Either create the default array in the

RGB

field's initialiser or use

Arrays.copyOf

to create a copy of the

DEFAULT_RGB

array and assign this to

RGB

.

 

Edit: Add missing closing tag.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Ah, works now. Thank you so much! Out of curiosity the missing closing tag you edited about...is that something I should know for nbt, or is it just something that was missing like /code] in your post?

 

It was a missing [nobbc][/quote][/nobbc] tag in my post, nothing to do with the code.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.