Jump to content

Block Rendering Based on NBT


killer380

Recommended Posts

Hi,

 

I was just wondering how I could get a block to reset its icons? I want it so that when the player right clicks with a certain item it will change texture to show that item. I am not sure how I can get the block to re-render since it only renders once on startup. I heard of a TESR but i dont want it to be constantly rendering every tick. Just when a action happens. Anyone know how I could do that?

 

This is what i have for the block info.

 

package com.MagicandMisc.block;

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import com.MagicandMisc.item.ItemManager;
import com.MagicandMisc.lib.StringLibrary;
import com.MagicandMisc.tileentity.TileEntityIce;

public class MysticalIce extends Block {
boolean hasAlloy[] = new boolean[3];{
for(int i=0;i<3;i++){
	hasAlloy[i] = false;
}
}
public IIcon Side0;
public IIcon Side1;
public IIcon Side2;
public IIcon Side3;
public IIcon Side4;
public IIcon Side5;
public int colors = 0;

protected MysticalIce(Material p_i45394_1_) {
	super(p_i45394_1_);
	// TODO Auto-generated constructor stub
}
@Override
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune){
	ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
	TileEntityIce ice = new TileEntityIce();
	hasAlloy = ice.alloyInfo();
	if(hasAlloy[0]){
		drops.add(new ItemStack(ItemManager.lime_alloy_ingot,1));
	}
	if(hasAlloy[1]){
		drops.add(new ItemStack(ItemManager.magenta_alloy_ingot,1));
	}
	if(hasAlloy[2]){
		drops.add(new ItemStack(ItemManager.orange_alloy_ingot,1));
	}
	boolean alloy[] = {false,false,false};
	ice.getGems(alloy, world);
	drops.add(new ItemStack(BlockManager.mystical_ice,1));
	System.out.println("Drops:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
	return drops;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_)
    {
	ItemStack held = player.getHeldItem();
	Item item;
	if(held == null){
		item = Items.apple;
	}
	else{
		item = held.getItem();
	}
	if(item == ItemManager.lime_alloy_ingot && !hasAlloy[0]){
			hasAlloy[0] = true;
			if(held.stackSize == 1){
				player.setItemInUse(null, 0);
			}
			else{
				held.stackSize = held.stackSize-1;
				player.setItemInUse(held, 0);
			}
	}
	if(item == ItemManager.magenta_alloy_ingot && !hasAlloy[1]){
			hasAlloy[1] = true;
			if(held.stackSize == 1){
				player.setItemInUse(null, 0);
			}
			else{
				held.stackSize = held.stackSize-1;
				player.setItemInUse(held, 0);
			}
	}
	if(item == ItemManager.orange_alloy_ingot && !hasAlloy[2]){
			hasAlloy[2] = true;
			if(held.stackSize == 1){
				player.setItemInUse(null, 0);
			}
			else{
				held.stackSize = held.stackSize-1;
				player.setItemInUse(held, 0);
			}
	}
	TileEntityIce ice = new TileEntityIce();
	ice.getGems(hasAlloy, world);
        return true;
    }
public boolean hasTileEntity(int metadata)
    {
        return true;
    }
public TileEntity createTileEntity(World world, int metadata)
    {
            return new TileEntityIce();
    }
@Override
public void registerBlockIcons(IIconRegister icon)
    {
	/* What I'm trying to do here is to use the method of ^2 so 
	 * I can have a unique image for each combination without a
	 * ton of if statements */
	TileEntityIce ice = new TileEntityIce();
	hasAlloy = ice.alloyInfo();
	if(hasAlloy[0]){
		colors += 2;
	}
	if(hasAlloy[1]){
		colors += 4;
	}
	if(hasAlloy[2]){
		colors += 8;
	}
	System.out.println(colors);
	Side0 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+"tb");
	Side1 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+"tb");
	Side2 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+colors);
	Side3 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+colors);
	Side4 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+colors);
	Side5 = icon.registerIcon(StringLibrary.MODID+":"+"mystical_ice_"+colors);

    }
@Override
public IIcon getIcon(int side, int metadata)
    {
	switch(side){
		case 0:
			return Side0;
		case 1:
			return Side1;
		case 2:
			return Side2;
		case 3:
			return Side3;
		case 4:
			return Side4;
		case 5:
			return Side5;
		default:
			return Side0;
	}
    }
}

 

This is the code for my TileEntity in the block. I tried using notifyBlockChange but I'm unsure what that actually does.

package com.MagicandMisc.tileentity;

import net.minecraft.block.material.Material;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

import com.MagicandMisc.block.BlockManager;
import com.MagicandMisc.block.MysticalIce;

public class TileEntityIce extends TileEntity {
static boolean hasAlloy[] = new boolean[3];
public void getGems(boolean[] alloy, World world){
	for(int i=0;i<3;i++){
		hasAlloy[i] = alloy[i];
	}
	System.out.println("Alloys:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
	world.notifyBlockChange(xCoord, yCoord, zCoord, BlockManager.mystical_ice);
	world.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
	System.out.println("Alloys2:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
}
@Override
public void writeToNBT(NBTTagCompound nbt){
	super.writeToNBT(nbt);
	System.out.println("Write:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
	nbt.setBoolean("Lime", hasAlloy[0]);
	nbt.setBoolean("Magenta", hasAlloy[1]);
	nbt.setBoolean("Orange", hasAlloy[2]);
}
@Override
public void readFromNBT(NBTTagCompound nbt){
	super.readFromNBT(nbt);
	hasAlloy[0] = nbt.getBoolean("Lime");
	hasAlloy[1] = nbt.getBoolean("Magenta");
	hasAlloy[2] = nbt.getBoolean("Orange");
	System.out.println("Read:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
}
public boolean[] alloyInfo(){
	System.out.println("Send:"+ hasAlloy[0] + hasAlloy[1]+hasAlloy[2]);
	return hasAlloy;
}
}

Link to comment
Share on other sites

Rendering does not occur once.  Initilaizing the rendering class for your specific object generally occurs once.

 

There are several tutorials on rendering special for tileEntities.  Follow them. 

 

For clicking, set your Tile entity to respond to what clicks it to changes its texture.

Long time Bukkit & Forge Programmer

Happy to try and help

Link to comment
Share on other sites

Hmmm alright... Well i got the rendering based on NBT working. Turns out the getIcon method is accessed constantly so i stuck some things in there and got it to change the textures. Unfortunately new problem

 

When I right click to add a item to the block it changes the texture for ALL of the blocks. So they are all affected. Im really not sure how to make them separate.

Link to comment
Share on other sites

static boolean hasAlloy[] = new boolean[3];

You know what static means, right?

I'm asking because you write/read it in your NBT methods.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

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.