Jump to content

Recommended Posts

Posted

I have used NBT before for entities but I don't know whether it is different for blocks. I have read something about tileEntities, but I don't know if they are relevant. If someone could explain the basics of what I need to do because the only tutorials I can find are outdated.

Posted

If you want to store data about blocks in Minecraft, you can

a) use metadata

b) use a TileEntity

 

Now you say you want to use NBT to store data, so you have to use a TileEntity. If you only want to store data, you need to override

canUpdate()

to return false, so it doesn't tick constantly. In a TileEntity, there are methods called

readFromNBT(NBTTagCompound)

and

writeToNBT(NBTTagCompound)

. In those methods, you can save any data to the NBTTagCompound passed in.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Okay, thanks. I am currently using metadata to change the texture of my block and am already using the updateTick() method to do so, along with some other code to check the blocks around. Would I need to move that code into my tile entity and do everything in that class rather than the actual block class?

Posted

Then you would need a TileEntity. If you already have stuff stored using metadata, it's up to you if you want to convert it to the TileEntity.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

Almost every method in the Block class has the world,x,y,z coordinates of the block, and use

World#getTileEntity(x,y,z)

to get the TileEntity on that location. You can cast that to your TileEntity.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Posted

I am having a bit of a problem with a null pointer exception. Whenever I try to System.out.println(tile.getCoal()); which I thought should work I just get a null pointer at that line. I don't really know why though, any help would be much appreciated, thanks.

 

Tile Entity

 

package com.phytomining.tileentities;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TilePlant extends TileEntity {

private Random random = new Random();

public int coal = 0;
public int iron = 0;
public int gold = 0;
public int redstone = 0;
public int lapis = 0;
public int diamond = 0;
public int emerald = 0;

public World world;
public int x;
public int y;
public int z;

@Override
public void updateEntity() {

	world = worldObj;
	x = xCoord;
	y = yCoord;
	z = zCoord;

	int meta = world.getBlockMetadata(x, y, z);

	int[][][] blocks = new int[5][5][5];

	if(random.nextInt(100) == 0) {

		for(int xx = 0; xx < 5; xx++) {

			for(int yy = 0; yy < 5; yy++) {

				for(int zz = 0; zz < 5; zz++) {

					Block b = world.getBlock((x - 2) + xx, y - (yy + 1), (z - 2) + zz);
					int block = b.getIdFromBlock(b);

					blocks[xx][yy][zz] = block;
					//System.out.println(xx + ", " + yy + ", " + zz + ": " + blocks[xx][yy][zz]);

					if(block == 16) coal++; 
					if(block == 15) iron++; 
					if(block == 14) gold++; 
					if(block == 73 || block == 74) redstone++; 
					if(block == 21) lapis++; 
					if(block == 56) diamond++; 
					if(block == 129) emerald++; 

					world.markBlockForUpdate(x, y, z);

				}

			}

		}

	}

}

@Override
public void writeToNBT(NBTTagCompound tag) {

	super.writeToNBT(tag);
	tag.setInteger("coal", this.coal);
	tag.setInteger("iron", this.iron);
	tag.setInteger("gold", this.gold);
	tag.setInteger("redstone", this.redstone);
	tag.setInteger("lapis", this.lapis);
	tag.setInteger("diamond", this.diamond);
	tag.setInteger("coal", this.coal);

}

@Override
public void readFromNBT(NBTTagCompound tag) {

	super.readFromNBT(tag);
	this.coal = tag.getInteger("coal");
	this.iron = tag.getInteger("iron");
	this.gold = tag.getInteger("gold");
	this.redstone = tag.getInteger("redstone");
	this.lapis = tag.getInteger("lapis");
	this.diamond = tag.getInteger("diamond");
	this.emerald = tag.getInteger("emerald");

}

public int getCoal() {

	return coal;

}

}

 

Block

 

package com.phytomining.blocks;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;

import com.phytomining.main.Phytomining;
import com.phytomining.tileentities.TilePlant;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockPlant extends BlockBush implements ITileEntityProvider {

private Random random = new Random();

private TilePlant tile;

private IIcon texture0;
private IIcon texture1;
private IIcon texture2;
private IIcon texture3;
private IIcon texture4;
private IIcon texture5;
private IIcon texture6;
private IIcon texture7;

public BlockPlant() {

	super(Material.plants);

	setCreativeTab(Phytomining.tabPhytomining);
	setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
	setTickRandomly(true);
	setBlockTextureName(Phytomining.MODID + ":texture0");
	setBlockName("Jerry");

}

@Override
public void updateTick(World world, int x, int y, int z, Random r) {

	super.updateTick(world, x, y, z, r);

	world.scheduleBlockUpdate(x, y, z, this, 50);

}

@Override
public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int metadata) {

	tile = (TilePlant)world.getTileEntity(x, y, z);
	System.out.println(tile.getCoal());
	if(!world.isRemote) {

		world.spawnEntityInWorld(new EntityItem(world, x + (random.nextInt(2)), y, z + (random.nextInt(2)), new ItemStack(Items.coal, tile.getCoal())));

	}
    
}

@Override
public TileEntity createNewTileEntity(World world, int i) {

 	return new TilePlant(); 

    }

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

	world.scheduleBlockUpdate(x, y, z, this, 50);

}



@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister i) {

	texture0 = i.registerIcon(Phytomining.MODID + ":texture0");
	texture1 = i.registerIcon(Phytomining.MODID + ":texture1");
	texture2 = i.registerIcon(Phytomining.MODID + ":texture2");
	texture3 = i.registerIcon(Phytomining.MODID + ":texture3");
	texture4 = i.registerIcon(Phytomining.MODID + ":texture4");
	texture5 = i.registerIcon(Phytomining.MODID + ":texture5");
	texture6 = i.registerIcon(Phytomining.MODID + ":texture6");
	texture7 = i.registerIcon(Phytomining.MODID + ":texture7");

}

@SideOnly(Side.CLIENT)
public IIcon getIcon(int par1, int par2) {

	if(par2 == 0) return texture0;
	if(par2 == 1) return texture1;
	if(par2 == 2) return texture2;
	if(par2 == 3) return texture3;
	if(par2 == 4) return texture4;
	if(par2 == 5) return texture5;
	if(par2 == 6) return texture6;
	if(par2 == 7) return texture7;

	return texture0;

}

}

Posted

tile is probably null because that methods causes a nullpointerexception.

But I don't know how that is possible because the method you get your tileentity seems to be good.

Try System.out.println(tile);

Maybe this will help to understand the problem.

Posted

I will try that and let you know.

 

This is for two of my custom blocks placed near each other...

 

[18:57:33] [server thread/INFO] [sTDOUT]: [com.phytomining.blocks.BlockPlant:updateTick:54]: com.phytomining.tileentities.TilePlant@303c8bb2

[18:57:41] [server thread/INFO] [sTDOUT]: [com.phytomining.blocks.BlockPlant:updateTick:54]: com.phytomining.tileentities.TilePlant@7b26e0b4

Posted

Here is the entire crash report:

 

 

  Reveal hidden contents

 

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.