Jump to content

ItemBlock's are sharing data


apuppy120

Recommended Posts

I'm trying to make it so that I can break a block and place it back down and it keeps the TileEntity data from before. I managed to get this working... sorta. When I break a block it drops a ItemBlock of with the data stored from the tileEntity. I can place it back down and it is good, but if I break the block, and then break another of the same block with different tileEntity data, they both end up with the data of the second one broken. This always happens to all of the blocks when they exist as an item in the world, whether it be on the ground or in a chest, they always change their data to the most recently broken block.

 

Here is my code:

 

 

Block

package com.spectrum.block;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFlowerPot;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;

import com.spectrum.item.ASItem;
import com.spectrum.itemblock.PotItemBlock;
import com.spectrum.tileentity.TilePotBlock;

public class PotBlock extends Block implements ITileEntityProvider {

protected PotBlock(Material p_i45394_1_) {
	super(p_i45394_1_);
	// TODO Auto-generated constructor stub
}

ArrayList<ItemStack> items = new ArrayList<ItemStack>();


@Override
    public void onBlockPreDestroy(World world, int x, int y, int z, int meta) {

	TilePotBlock t = (TilePotBlock)world.getTileEntity(x, y, z);

	if (t instanceof TilePotBlock) {
		TilePotBlock tile = (TilePotBlock)t;

		ItemStack stack = new ItemStack(world.getBlock(x, y, z), 1);

		PotItemBlock item = (PotItemBlock)stack.getItem();

		item.isRed = tile.red;
		item.isGreen = tile.green;
		item.isBlue = tile.blue;
		item.isIR = tile.ir;
		item.storedBlock = tile.block;
		item.storedBlockString = tile.blockString;



		ItemStack itemstack = new ItemStack(item, 1);

		items.add(itemstack);
	}

    }


@Override
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {




	return items;


}


/**
@Override
public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune) {
	ArrayList<ItemStack> items = new ArrayList<ItemStack>();

	TilePotBlock t = (TilePotBlock)world.getTileEntity(x, y, z);

	if (t instanceof TilePotBlock) {
		TilePotBlock tile = (TilePotBlock)t;

		ItemStack stack = new ItemStack(world.getBlock(x, y, z), 1, metadata);
		if (!stack.hasTagCompound()) {
			stack.setTagCompound(new NBTTagCompound());
		}
		stack.getTagCompound().setBoolean("potBlockR", tile.red);
		stack.getTagCompound().setBoolean("potBlockG", tile.green);
		stack.getTagCompound().setBoolean("potBlockB", tile.blue);

		items.add(stack);
	}

	return items;

}


@Override
    public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player)
    {
	if(player.getHeldItem() != null)
	{

	if(player.getHeldItem().getItem() == ASItem.waveTuner)
	{
	TilePotBlock pot = (TilePotBlock)world.getTileEntity(x, y, z);

	ItemStack stack = new ItemStack(world.getBlock(x, y, z), 1);

	if (!stack.hasTagCompound()) {
		NBTTagCompound nbt = new NBTTagCompound();
		stack.setTagCompound(nbt);
		nbt.setTag("potBlock", nbt);
	}

	stack.getTagCompound().setBoolean("potBlockR", pot.red);
	stack.getTagCompound().setBoolean("potBlockG", pot.green);
	stack.getTagCompound().setBoolean("potBlockB", pot.blue);
	stack.getTagCompound().setBoolean("potBlockIR", pot.ir);


	EntityItem itementity = new EntityItem(world, x, y, z, stack);


	world.setBlock(x, y, z, Blocks.air);

	if(!world.isRemote)
	{
	world.spawnEntityInWorld(itementity);
	}
	}
	}
    }

*/
@Override
public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
	// TODO Auto-generated method stub
	return new TilePotBlock();
}


@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_)
    {
	if(player.getHeldItem() != null)
	{
	TilePotBlock pot = (TilePotBlock)world.getTileEntity(x, y, z);

	if(player.getHeldItem().getItem() == ASItem.waveMagnifier)
	{


		if(!world.isRemote)
        	{
		if(pot.red==false || pot.blue==false || pot.green==false)
			{
			player.addChatComponentMessage(new ChatComponentText("Contains Red: " + pot.red + ". Contains Green: " + pot.green + ". Contains Blue: " + pot.blue + ". Block: " + pot.blockString + "."));
			}else{

				if(pot.ir)
				{
					player.addChatComponentMessage(new ChatComponentText("Block contains Infrared."));

				}

				player.addChatComponentMessage(new ChatComponentText("Block is fully Potential. Block: " + pot.blockString + "."));

			}
		}
	}

	if(player.getHeldItem().getItem() == ASItem.waveTuner)
	{
		pot.use();
	}
	}

	return true;
    }





}

 

ItemBlock

package com.spectrum.itemblock;

import java.util.List;

import com.spectrum.tileentity.TilePotBlock;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

public class PotItemBlock extends ItemBlock {





public PotItemBlock(Block id) {
	super(id);
	setHasSubtypes(true);
	setUnlocalizedName("potentialBlock");
	setMaxStackSize(1);
}


public boolean isRed = false;
public boolean isGreen = false;
public boolean isBlue = false;
public boolean isIR = false;
public int storedBlock = 0;
public String storedBlockString = "None";

@Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
    {

	if (!world.setBlock(x, y, z, field_150939_a, metadata, 3))
       {
           return false;
       }

       if (world.getBlock(x, y, z) == field_150939_a)
       {
    	   field_150939_a.onBlockPlacedBy(world, x, y, z, player, stack);
           field_150939_a.onPostBlockPlaced(world, x, y, z, metadata);
    	   
    	   TilePotBlock tile = (TilePotBlock)world.getTileEntity(x, y, z);
    	   tile.red = isRed;
    	   tile.green = isGreen;
    	   tile.blue = isBlue;
    	   tile.ir = isIR;
    	   tile.block = storedBlock;

    	   
       }

       return true;
    }





}

 

TileEntity

package com.spectrum.tileentity;

import com.spectrum.block.ASBlock;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TilePotBlock extends TileEntity{

public static String publicName = "tileentitypotentialblock";
public boolean red = false;
public boolean green = false;
public boolean blue = false;
public boolean ir = false;

/**
 * 0 = None,
 * 1 = Diamond Block,
 * 2 = Emitter,
 */
public int block = 0;
public String blockString = "None";

public void updateEntity()
{
	if(block == 0)
	{
		blockString = "None";
	}
	if(block == 1)
	{
		blockString = "Diamond Block";
	}
	if(block == 2)
	{
		blockString = "Emitter";
	}



}





public void use()
{
	if(block == 1)
	{
		if(red && blue && green)
		{
			worldObj.setBlock(xCoord, yCoord, zCoord, ASBlock.specCell);
		}
	}

	if(block == 2)
	{
		if(red && blue && green && ir)
		{
			worldObj.setBlock(xCoord, yCoord, zCoord, ASBlock.reciever);

		}
	}


}






@Override
   public Packet getDescriptionPacket()
   {
       NBTTagCompound syncData = new NBTTagCompound();
       this.writeToNBT(syncData);
       return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, syncData);
   }

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

@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.red = nbt.getBoolean("potBlockR");
this.green = nbt.getBoolean("potBlockG");
this.blue = nbt.getBoolean("potBlockB");
this.ir = nbt.getBoolean("potBlockIR");
this.block = nbt.getInteger("potBlockBlock");


}

@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setBoolean("potBlockR", red);
nbt.setBoolean("potBlockG", green);
nbt.setBoolean("potBlockB", blue);
nbt.setBoolean("potBlockIR", ir);
nbt.setInteger("potBlockBlock", block);




}

}

 

 

glubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglub.

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.