Jump to content

[1.7.2] [Beginner] Store a block w/in a TileEntity / TileEntity in general


Recommended Posts

Posted

Hello,

sorry if this is trivial.

 

I've a block which is supposed to store meta data, in my case information about a different block. I can't figure out how to store "a block w/in a block", though...

 

My approach:

public class TileEntityBaseScanner extends TileEntity {
public Block bb = Blocks.diamond_ore;

@Override
public void writeToNBT(NBTTagCompound par1) {
	super.writeToNBT(par1);
	try {
		par1.setByteArray("bb", TileEntityHelper.serialize(bb));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

@Override
public void readFromNBT(NBTTagCompound par1) {
	super.readFromNBT(par1);
	try {
		this.bb = (Block) TileEntityHelper.deserialize(par1.getByteArray("bb"));
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public class TileEntityHelper {
public static byte[] serialize(Object obj) throws IOException {
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	ObjectOutputStream os = new ObjectOutputStream(out);
	os.writeObject(obj);
	return out.toByteArray();
}

public static Object deserialize(byte[] data) throws IOException,
		ClassNotFoundException {
	ByteArrayInputStream in = new ByteArrayInputStream(data);
	ObjectInputStream is = new ObjectInputStream(in);
	return is.readObject();
}
}

Unfortunately, the block class can't be serialized and there's no "NBTTagCompound.setBlock()"-method...

 

Also, is there any way to store the TileEntity once a block is destroyed? i.e., the player destroys the block and he has the block including the metadata in his inventory?

Posted

You can store Blocks by id: Block.getIdFromBlock(block).

 

For your TileEntity, you can write it to NBT and append the tag to the ItemStack's NBT tag compound when the block is broken and the Block ItemStack is created, then when the stack is placed as a block, get the TileEntity at that position and read from the ItemStack's NBT. I've found the Block methods 'breakBlock' and 'onBlockPlacedBy' to be most useful for this.

Posted

there is other thing - if you want to have diffrent texture , that changing from TileEntity , you need to have make own ItemRender. Saving and loading stuff is easy - just use alredy made in you TileEntity readFromNBT / writeToNBT .Parameters - ItemStack's NBT tag.

Posted

Hello,

I've successfully stored the blocks's ID w/in the TileEntity - thanks for that!

 

I can't figure out how to store the NBT quite yet. Do you have an example?

 

The breakBlock()-method doesn't seem to interact w/ the player or the ItemStack at all. If I override this, the only benefit would be that I'd be able to prevent removeTileEntity() from being called.

 

So I tried overriding dropBlockAsItem() - but how on earth do I get the NBT from my TileEntity?

 

	@Override
    public void breakBlock(World p_149749_1_, int p_149749_2_, int p_149749_3_, int p_149749_4_, Block p_149749_5_, int p_149749_6_)
    {
        super.breakBlock(p_149749_1_, p_149749_2_, p_149749_3_, p_149749_4_, p_149749_5_, p_149749_6_);
        //p_149749_1_.removeTileEntity(p_149749_2_, p_149749_3_, p_149749_4_);
    }

@Override
protected void dropBlockAsItem(World p_149642_1_, int p_149642_2_, int p_149642_3_, int p_149642_4_, ItemStack p_149642_5_){
	TileEntity tile = TileEntityHelper.getTileEntityBaseScannerFromCoords(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_);
	p_149642_5_.setTagCompound()); // 
	super.dropBlockAsItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, p_149642_5_);
}

Posted

Instead of using ids I would store your block as an itemstack.

In your dropBlockAsItem method you should try simply adding whatever nbt data you need to the itemstack that is dropped. Don't call super.dropBlockAsItem() and spawn an itemstack in the world yourself. The only problem I can foresee is if something has already destroyed the tileentity when dropBlockAsItem is called.

Posted

I've solved it, but it seems awfully complicated for such as basic task.

 

Here's my solution:

@Override
  protected void dropBlockAsItem(World p_149642_1_, int p_149642_2_, int p_149642_3_,
      int p_149642_4_, ItemStack p_149642_5_) {
    System.out.println("dropBlockAsItem");
    // super.dropBlockAsItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, p_149642_5_);
    // TODO: TEST
    try {
      TileEntityBaseScanner tile =
          TileEntityHelper.getTileEntityBaseScannerFromCoords(p_149642_1_, p_149642_2_,
              p_149642_3_, p_149642_4_);
      ItemStack itemstack = new ItemStack(this, 1);
      itemstack.setTagCompound(tile.getNBTTagCompound());
      itemstack.setStackDisplayName("Test: "
          + tile.getNBTTagCompound().getInteger("test"));
      Entity item = new EntityItem(p_149642_1_, p_149642_2_, p_149642_3_, p_149642_4_, itemstack);
      p_149642_1_.spawnEntityInWorld(item);
    } catch (Exception e) {
      System.out.println("NO entity dropped!");
      e.printStackTrace();
    }
  }

  @Override
  public void onBlockPlacedBy(World p_149689_1_, int p_149689_2_, int p_149689_3_, int p_149689_4_,
      EntityLivingBase p_149689_5_, ItemStack p_149689_6_) {
    if (p_149689_1_.isRemote)
      return;

    try {
      NBTTagCompound nbt = p_149689_6_.getTagCompound();
      if(nbt != null){
        TileEntityBaseScanner tile = new TileEntityBaseScanner();
        //tile.writeToNBT(nbt);
        tile.writeToParByNBT(nbt);
        System.out.println(tile.test);
        p_149689_1_.setTileEntity(p_149689_2_, p_149689_3_, p_149689_4_, tile);
      }
      else {
        System.out.println("No NBT");
      }
    } catch (Exception e) {
      System.out.println("NO entity dropped!");
      e.printStackTrace();
    }

  }

// tile-class
  /**
   * Write values from NBT to this
   * bit of an awkward solution, but since we ain't got no pointers making this more flexible is a pain, so I wrote it static for the time being
   * @param par1: NBTTagCompound
   */
  public void writeToParByNBT(NBTTagCompound par1){
    this.test= (par1.hasKey("test")) ? par1.getInteger("test") : test;
  }

Posted

You could jsut override getDrops, also, pro-tip, name your parameters correctly, world, x, y, z makes code a lot easier to understand.

Also, you should store the tile by it's registry name, not ID. just for future compatibility sake.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

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.