Jump to content

Dropping another item/block when a block is destroyed with a tool


Robertof

Recommended Posts

Hello, I'm pretty new to Forge programming (but not to programming in general), and I need to ask a thing.

I already tried to ask that in IRC but I got no response.

Anyways, I have a class 'ItemMyPickaxe' which should replace some blocks when they break.

However I didn't find anything to do this.

I tried 2 different solutions:

  • Using onBlockStartBreak() and then running playerEntity.worldObj.setBlock (blockX, blockY, blockZ, newblock)
    That would've work perfectly, but then I realized that I need to drop even items, not just blocks. So, nope.
  • Then I tried again with onBlockStartBreak(), this time dropping the block manually using the code in Block.dropBlockAsItem_do().
    But using that leads to 1) the drop entity not being placed correctly, for a reason unknown to me 2) the need to add the code in ItemInWorldManager.tryHarvestBlock(x,y,z). But that's too much, and also dangerous for future updates.

Can anyone help me on this?

(to make an example - a thing like Azanor's TC2 Pickaxe of the core is what I would need: it cooked the items on the fly and dropped them, but I still didn't understand how to do that)

Thank you very much,

Robertof

Link to comment
Share on other sites

//This tells it what to drop

public int idDropped(int par1, Random par2Random, int par3)

    {

      //This is what it drops. DimensionBase is my base mod file,ruby is what i want it to drop and shiftedIndex is for items and BlockID is for blocks.

        return DimensionBase.ruby.shiftedIndex;

    }

//Amount it drops

public int quantityDropped(Random random)

    {

    return 4;

    }

EDIT:this goes in the Block.java

Link to comment
Share on other sites

Yep but as I said - I don't have the access to the original block (and I don't wanna edit the original classes). I just have the access to the tool.

By the way I guess I should use a temporary block which drops what is in the metadata.

Thanks anyways.

//This tells it what to drop

public int idDropped(int par1, Random par2Random, int par3)

    {

      //This is what it drops. DimensionBase is my base mod file,ruby is what i want it to drop and shiftedIndex is for items and BlockID is for blocks.

        return DimensionBase.ruby.shiftedIndex;

    }

//Amount it drops

public int quantityDropped(Random random)

    {

    return 4;

    }

EDIT:this goes in the Block.java

Link to comment
Share on other sites

Sorry for the double post - but I fixed that:

    /**
     * A function which generates an EntityItem
     * from an ItemStack, given World and x, y, z coordinates.
     * @param wObj The world object where the entity will be spawned.
     * @param result The ItemStack to drop. It will be automatically copied.
     * @param x X coordinate
     * @param y Y coordinate
     * @param z Z coordinate
     * @author Robertof
     */
    protected void genBlockDrop (World wObj, ItemStack result, int x, int y, int z)
    {
        if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER)
        {
            float var6 = 0.7F;
            double var7 = (double)(wObj.rand.nextFloat() * var6) + (double)(1.0F - var6) * 0.5D;
            double var9 = (double)(wObj.rand.nextFloat() * var6) + (double)(1.0F - var6) * 0.5D;
            double var11 = (double)(wObj.rand.nextFloat() * var6) + (double)(1.0F - var6) * 0.5D;
            EntityItem ei = new EntityItem(wObj, (double)x + var7, (double)y + var9, (double)z + var11,
                    new ItemStack (result.itemID, result.stackSize, result.getItemDamage()));
            if (result.hasTagCompound())
                ei.item.setTagCompound((NBTTagCompound) result.getTagCompound().copy());
            ei.delayBeforeCanPickup = 10;
            wObj.spawnEntityInWorld(ei);
        }
    }

add that to onBlockStartBreak along with something to add damage to the tool used (if you want), then return true and you are good to go.

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.

Announcements



×
×
  • Create New...

Important Information

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