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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • file:///C:/Users/Danie/Downloads/Capture.JPG I keep experiencing this error, does anyone know how to fix it here is the report: file:///C:/Users/Danie/AppData/Roaming/.minecraft/crash-reports/Bug_report1.txt
    • Hi, i am unable to join a multiplayer server my friend and i created. We started it with some mods then decided to add some others and this is when the error appeared. We are using Curseforge, the server is in 1.20.1 . The full error message is "Internal Exception: io.netty.handler.codec.DecoderException: java.lang.ArrayIndexOutOfBoundsException: Index 17196645 out of bounds for length 13" I am the only one with that issue, my friends can log in and out no problem. I have tried switching minecraft account but that doesn't help so i guess the bug is on my side. We use the same mod profile so i know the error doesn't come from there. Here is the latest.log file https://pastebin.com/X6J9Fnnf. If you need more informations to try and investigate, please let me know. Big thanks to anyone that will try to help
    • [00:02:07] [main/ERROR]: Missing or unsupported mandatory dependencies:     Mod ID: 'kotlinforforge', Requested by: 'flightlib', Expected range: '[4.3.0,)', Actual version: '[MISSING]'     Mod ID: 'terrablender', Requested by: 'biomesoplenty', Expected range: '[2.2.0.154,)', Actual version: '[MISSING]'     Mod ID: 'geckolib', Requested by: 'cakescosmetics', Expected range: '[4.4,)', Actual version: '[MISSING]'     Mod ID: 'balm', Requested by: 'waystones', Expected range: '[7.2.0,)', Actual version: '[MISSING]'     Mod ID: 'oculus', Requested by: 'irisflw', Expected range: '[1.6.15,)', Actual version: '[MISSING]'     Mod ID: 'cupboard', Requested by: 'betterfpsdist', Expected range: '[1.20.1-1.5,)', Actual version: '[MISSING]' [00:02:53] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\fmlcore\1.20.1-47.2.32\fmlcore-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\javafmllanguage\1.20.1-47.2.32\javafmllanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\lowcodelanguage\1.20.1-47.2.32\lowcodelanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:54] [main/WARN]: Mod file C:\Users\ezkid\AppData\Roaming\.minecraft\libraries\net\minecraftforge\mclanguage\1.20.1-47.2.32\mclanguage-1.20.1-47.2.32.jar is missing mods.toml file [00:02:65] [main/ERROR]: Skipping early mod setup due to previous error
    • Guns and more cool stuff
    • nvm i did it, turn out i just needed to install a drive or smth
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.