Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

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.

  • Author

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.