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.

[1.7.10] Make block save data values when block is broken and placed again

Featured Replies

Posted

Hello!

 

I was wondering how you would go about allowing an int value to be saved to a specific block, so when a player breaks the block and places it again the block contains the storage (rf) it had before (and this block can now not be stacked any more with the same blocks). I have saved the rf storage to NBT, and logging out and in again works fine, but I am not sure how to save the storage to an instanced block. Thanks.

 

 

 

 

 

 

  • Author

Thanks for the reply, and yes the data is stored in a tileentity.

 

I had a look at the getDrops method (which I assume is the only one I need from the BlockFlowerPot class?), and this is what I got from it. It does drop an extra block when broken, but did it apply the data to the dropped block like this?

 

    @Override
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {
        ArrayList<ItemStack> ret = super.getDrops(world, x, y, z, metadata, fortune);
        TileEntityRedstoneMacerator te = (TileEntityRedstoneMacerator)world.getTileEntity(x, y, z);
        
        if (te != null && te instanceof TileEntityRedstoneMacerator)
            ret.add(new ItemStack(ModBlocks.blockRedstoneMaceratorIdle, 1, te.getEnergyStored()));
        return ret;
    }

  • Author

Since I was getting 2 items I set the getItemDropped to null, and this is code so far, is it now storing the data in the block that gets dropped? The te.getEnergyStored() is returning the internal storage the tileentity has.

 

    @Override
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {
        ArrayList<ItemStack> ret = super.getDrops(world, x, y, z, metadata, fortune);
        TileEntityRedstoneMacerator te = (TileEntityRedstoneMacerator)world.getTileEntity(x, y, z);
        
        if (te != null && te instanceof TileEntityRedstoneMacerator)
            ret.add(new ItemStack(ModBlocks.blockRedstoneMaceratorIdle, 1, te.getEnergyStored()));
        return ret;
    }
   
    @Override
    public boolean removedByPlayer(World world, EntityPlayer player, int x, int y, int z, boolean willHarvest)
    {
        if (willHarvest){
        	return true; //If it will harvest, delay deletion of the block until after getDrops
        }
        return super.removedByPlayer(world, player, x, y, z, willHarvest);
    }

    @Override
    public void harvestBlock(World world, EntityPlayer player, int x, int y, int z, int meta)
    {
        super.harvestBlock(world, player, x, y, z, meta);
        world.setBlockToAir(x, y, z);
    }

  • Author

Do you mean changing it to ArrayList<ItemStack> ret = new ArrayList<ItemStack>();?

 

I tried that also but when I break the block and pick it back up again it goes back into the same stack it was in. I also tried adding other data into it but it didn't go into its own stack when picked up.

 

Like when I try to add te.cookTime() same thing happens. Does the data need to be done in a special way in the tileentity in order to be stored? For cooktime all I did was make the int, store it in NBT, then change its value depending on what machine was doing.

 

nbt.setShort("CookTime", (short) this.cookTime);

this.cookTime = (int) nbt.getShort("CookTime");

  • Author

I am probably missing something, but I thought the ret.add was adding my block to the itemstack, then adding the data to the block and returning the itemstack as a drop with the data.

 

    @Override
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {
    	ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
        TileEntityRedstoneMacerator te = (TileEntityRedstoneMacerator)world.getTileEntity(x, y, z);
        
        if (te instanceof TileEntityRedstoneMacerator)
            ret.add(new ItemStack(ModBlocks.blockRedstoneMaceratorIdle, 1, te.getEnergyStored()));
        return ret;
    }

  • Author

Really appricate the answers diesieben07.

 

You mean something like this?

 

    @Override
    public ArrayList<ItemStack> getDrops(World world, int x, int y, int z, int metadata, int fortune)
    {
    	ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
    	ItemStack stack = new ItemStack(world.getBlock(x, y, z), 1, metadata);
        
    	TileEntityRedstoneMacerator te = (TileEntityRedstoneMacerator)world.getTileEntity(x, y, z);
        int i = te.cookTime;
        
        if (te instanceof TileEntityRedstoneMacerator){
    		
        	if (!stack.hasTagCompound()) {
    			stack.setTagCompound(new NBTTagCompound());	
    		}
    		stack.getTagCompound().setInteger("CookTime", i);
    		ret.add(stack);
        }

        return ret;
    }

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.