Jump to content

Recommended Posts

Posted

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.

Posted

Okay, it's fixed now. Thanks!

glubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglubglub.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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