Jump to content

Recommended Posts

Posted

Been working on this for hours still cant get it to work, it holds the item and displays the entity fine, just wont save inventory to and from nbt, looked up about 5 examples and most are similar so i don't get what i'm missing.

 

public class TileEntityAltar extends TileEntity  {

	public ItemStack display;
	public EntityItem entity;

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound tag = new NBTTagCompound();
		this.writeToNBT(tag);
		return new SPacketUpdateTileEntity(pos, 0, tag);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
		super.onDataPacket(net, packet);
        readFromNBT(packet.getNbtCompound());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.display.deserializeNBT(compound.getCompoundTag("ItemHandler"));
        if (this.entity == null && this.display != null) this.setDisplayItem(worldObj, pos, null, null, null);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		compound = super.writeToNBT(compound);
		compound.setTag("ItemHandler", this.display.serializeNBT());
		return compound;
	}

	public void change() {this.markDirty();}


public void setDisplayItem(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, ItemStack heldItem) {


	if (!worldObj.isRemote) {
		entity = new EntityItemProxy(worldObj, pos.getX(), pos.getY(), pos.getZ(), display.copy());
		entity.setVelocity(0, 0, 0);
		entity.setNoDespawn();
		entity.setPositionAndRotation(pos.getX()+.5, pos.getY()+1d, pos.getZ()+.5, 0, 1);
		worldObj.spawnEntityInWorld(entity);
	}
}	

public void removeItem(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn) {

	if (!worldIn.isRemote) {
	playerIn.entityDropItem(display, 1);
	if (entity != null)entity.setDead();
	display = null;
	}
}
}

 

from the block activation

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
    {
    	TileEntity te = worldIn.getTileEntity(pos);
    	
        if (((TileEntityAltar) te).display == null)
        {
            if (Ref.altarlist(heldItem)) {
            	((TileEntityAltar) te).display = heldItem.copy();
        		--heldItem.stackSize;
                	((TileEntityAltar) te).setDisplayItem(worldIn, pos, state, playerIn, heldItem);
                	((TileEntityAltar) te).change();
            }
        }
        else {
        	((TileEntityAltar) te).removeItem(worldIn, pos, state, playerIn);
        	((TileEntityAltar) te).change();
        }
        
        return true;
    }

 

error

 

Failed to load data for block entity tileentityaltar
java.lang.NullPointerException
at com.ravvoid.init.tileentity.TileEntityAltar.readFromNBT(TileEntityAltar.java:60) ~[TileEntityAltar.class:?]
at net.minecraft.tileentity.TileEntity.func_190200_a(TileEntity.java:137) [TileEntity.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:515) [AnvilChunkLoader.class:?]
at net.minecraftforge.common.chunkio.ChunkIOProvider.syncCallback(ChunkIOProvider.java:96) [ChunkIOProvider.class:?]
at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:94) [ChunkIOExecutor.class:?]
at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:129) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:92) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.provideChunk(ChunkProviderServer.java:141) [ChunkProviderServer.class:?]
at net.minecraft.server.MinecraftServer.initialWorldChunkLoad(MinecraftServer.java:336) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:107) [integratedServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:124) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:496) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
[11:34:57] [server thread/ERROR] [FML]: A TileEntity tileentityaltar(com.ravvoid.init.tileentity.TileEntityAltar) has thrown an exception during loading, its state cannot be restored. Report this to the mod author
java.lang.NullPointerException
at com.ravvoid.init.tileentity.TileEntityAltar.readFromNBT(TileEntityAltar.java:60) ~[TileEntityAltar.class:?]
at net.minecraft.tileentity.TileEntity.func_190200_a(TileEntity.java:137) [TileEntity.class:?]
at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadEntities(AnvilChunkLoader.java:515) [AnvilChunkLoader.class:?]
at net.minecraftforge.common.chunkio.ChunkIOProvider.syncCallback(ChunkIOProvider.java:96) [ChunkIOProvider.class:?]
at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:94) [ChunkIOExecutor.class:?]
at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:129) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:92) [ChunkProviderServer.class:?]
at net.minecraft.world.gen.ChunkProviderServer.provideChunk(ChunkProviderServer.java:141) [ChunkProviderServer.class:?]
at net.minecraft.server.MinecraftServer.initialWorldChunkLoad(MinecraftServer.java:336) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.loadAllWorlds(IntegratedServer.java:107) [integratedServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.startServer(IntegratedServer.java:124) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:496) [MinecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]

 

 

Posted

that and it didn't like me adding the spawn entity in the nbt code, so i added itickable to check if it has an item and spawn and entity if none exists, all working perfectly now thanks a bunch

 

 

 

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.