Jump to content

Recommended Posts

Posted

Hello,

 

So my TE has some data that I would like to be transferred across breaking/placing. I have some base code, however, it does not work.  When destroyed, no data tags are stored and nothing is saved.

 

Here is my current code:

Spoiler

    @Override
    public void getDrops(NonNullList<ItemStack> drops, IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
    	TileEntity tile = world.getTileEntity(pos);
    	ItemStack drop = new ItemStack(ModBlocks.ENCHANTMENT_FAB);
    	
    	if(!drop.hasTagCompound())
    		drop.setTagCompound(new NBTTagCompound());
    	
    	if(tile != null) //fix
    	{
    		if(tile instanceof TileEntityEnchantmentFab)
    		{
    			if(!((TileEntityEnchantmentFab) tile).getKnownEnchants().isEmpty())
    			{
					NBTTagList enchantList = new NBTTagList();
					for(Enchantment e : ((TileEntityEnchantmentFab) tile).getKnownEnchants()) {
						NBTTagString eString = new NBTTagString(e.getRegistryName().toString());
						enchantList.appendTag(eString);
					}
					drop.getTagCompound().setTag("enchantList", enchantList);
    			}
    			if(((TileEntityEnchantmentFab)tile).getEnchantEssence() > 0 ) 
    			{
    				drop.getTagCompound().setInteger("enchantmentEssence", ((TileEntityEnchantmentFab)tile).getEnchantEssence());
    			}
    		}
    	}
    	
    	drops.clear();
    	drops.add(drop);
    }
    
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
    	TileEntity tile = worldIn.getTileEntity(pos);
    	if(!worldIn.isRemote) {
    		if(stack != null && tile != null)
    		{
    			if(stack.hasTagCompound() && tile instanceof TileEntityEnchantmentFab)
    			{
    				if(stack.getTagCompound().hasKey("enchantList"))
    				{
    					Set<Enchantment> eList = new HashSet<Enchantment>();
    					NBTTagList list = stack.getTagCompound().getTagList("enchantList", Constants.NBT.TAG_STRING);
    					for (int i = 0; i < list.tagCount(); i++) {
    						Enchantment theEnch = Enchantment.getEnchantmentByLocation(list.getStringTagAt(i));
    						eList.add(theEnch);
    					}
    					((TileEntityEnchantmentFab) tile).setEnchants(eList);
    				}
    				if(stack.getTagCompound().hasKey("enchantmentEssence"))
    				{
    					((TileEntityEnchantmentFab) tile).enchantmentEssence = stack.getTagCompound().getInteger("enchantmentEssence");
    				}
    			}
    		}
    	}
    }

 

 

 

Thank you.

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

Posted

Look at BlockShulkerBox.

It sets a tag on the item stack called "BlockEntityTag" which is automatically applied to the TE when the block is placed.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Okay thanks, got it to work based on the shulkerbox. Point out any errors if you see any, but it works perfectly.

 

Spoiler

    @Override
    public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
    	TileEntity tile = worldIn.getTileEntity(pos);
    	if(tile != null)
    	{
    		if(tile instanceof TileEntityEnchantmentFab)
    		{
    			ItemStack item = new ItemStack(Item.getItemFromBlock(this));
    			NBTTagCompound compound = new NBTTagCompound();
    			
    			if(!((TileEntityEnchantmentFab) tile).getKnownEnchants().isEmpty())
    			{
					NBTTagList enchantList = new NBTTagList();
					for(Enchantment e : ((TileEntityEnchantmentFab) tile).getKnownEnchants()) {
						NBTTagString eString = new NBTTagString(e.getRegistryName().toString());
						enchantList.appendTag(eString);
					}
					compound.setTag("enchantList", enchantList);
    			}
    			if(((TileEntityEnchantmentFab)tile).getEnchantEssence() > 0 ) 
    			{
    				compound.setInteger("enchantmentEssence", ((TileEntityEnchantmentFab)tile).getEnchantEssence());
    			}
    			
    			item.setTagCompound(compound);
    			
    			this.spawnAsEntity(worldIn, pos, item);
    		}
    	}
    }
    
    @Override
    public Item getItemDropped(IBlockState state, Random rand, int fortune) {
    	return ItemStack.EMPTY.getItem();
    }
    
    @Override
    public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
    	TileEntity tile = worldIn.getTileEntity(pos);
    	if(!worldIn.isRemote) {
    		if(stack != null && tile != null)
    		{
    			if(stack.hasTagCompound() && tile instanceof TileEntityEnchantmentFab)
    			{
    				if(stack.getTagCompound().hasKey("enchantList"))
    				{
    					Set<Enchantment> eList = new HashSet<Enchantment>();
    					NBTTagList list = stack.getTagCompound().getTagList("enchantList", Constants.NBT.TAG_STRING);
    					for (int i = 0; i < list.tagCount(); i++) {
    						Enchantment theEnch = Enchantment.getEnchantmentByLocation(list.getStringTagAt(i));
    						eList.add(theEnch);
    					}
    					((TileEntityEnchantmentFab) tile).setEnchants(eList);
    				}
    				if(stack.getTagCompound().hasKey("enchantmentEssence"))
    				{
    					((TileEntityEnchantmentFab) tile).enchantmentEssence = stack.getTagCompound().getInteger("enchantmentEssence");
    				}
    			}
    		}
    	}
    }

 

 

 

Relatively new to modding.

Currently developing:

https://github.com/LambdaXV/DynamicGenerators

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.