Jump to content

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


IvanTune

Recommended Posts

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.

 

 

 

 

 

 

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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);
    }

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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