Jump to content

[1.7.10] How to get harvest level from Tool? [RESOLVED]


WeiseGuy

Recommended Posts

Hey guys,

 

I'm having a small issue (brain fart if you will) on how the heck I can check the harvest level of a tool used in the OnBlockDropItems event. I feel I'm missing just one little thing, and I can't seem to find a method that would be along the lines of .getHarvestLevel() which grabs the harvest level of a tool if its a shovel. This is what I'm working with so far.

 

@SubscribeEvent
    public void onBlockDropItems(BlockEvent.HarvestDropsEvent event) //This is overriding drop tables of vanilla blocks.
    {
        if (event.block == Blocks.gravel)
        {
            World world = event.world;
            EntityPlayer player = event.harvester;

            if(player.getCurrentEquippedItem() != null)
            {
                Item itemUsed = player.getCurrentEquippedItem().getItem(); // Checks what tool was that player used to break item.
                if (itemUsed == (Items.iron_shovel) || itemUsed == (Items.diamond_shovel)) //TODO check for material harvest level iron or better (2 or better)
                {
                    if (world.rand.nextFloat() < 0.25f) //25% Chance to drop a RoughCrystalFragment if using the right shovel!
                    {
                        event.drops.add(new ItemStack(ModItems.itemRoughCrystalFragment, world.rand.nextInt(2) + 1)); //drops RoughCrystalFragment 1-2
                    }
                }
            }
        }

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Link to comment
Share on other sites

Well,

 

I resolved it myself. Easy enough, as soon as I posted I realized another way to test this. I'll leave topic open in case people have input on a better way or anything, but this works for me to have a harvest level 2 or above, which is iron level or better. I think any mod would at some point use ItemSpade to extend from, so this should cover any mod shovel I believe.

 

    @SubscribeEvent
    public void onBlockDropItems(BlockEvent.HarvestDropsEvent event) //This is overriding drop tables of vanilla blocks.
    {
        if (event.block == Blocks.gravel)
        {
            World world = event.world;
            EntityPlayer player = event.harvester;

            if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemSpade) //If its a shovel
            {
                Item toolUsed = player.getCurrentEquippedItem().getItem(); //Gets item used
                int toolUsedHarvestLevel = player.getCurrentEquippedItem().getItem().getHarvestLevel(new ItemStack(toolUsed), "shovel"); // Gets harvest level of said tool
                if (toolUsedHarvestLevel >= 2) //Iron or better, aka 2 or better.
                {
                    if (world.rand.nextFloat() < 0.25f) //25% Chance to drop a RoughCrystalFragment if using the right shovel!
                    {
                        event.drops.add(new ItemStack(ModItems.itemRoughCrystalFragment, world.rand.nextInt(2) + 1)); //drops RoughCrystalFragment 1-2
                    }
                }
            }
        }

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Link to comment
Share on other sites

Don't check if the item extends

ItemSpade

, that's not a reliable indicator that the tool functions as a shovel (Tinkers' Construct tools extend

ItemTool

directly, bypassing

ItemSpade

and the other subclasses); some tools may not even extend

ItemTool

. Just use the harvest level check, any tool that can't act as a shovel will simply return -1 from

Item#getHarvestLevel

.

 

Don't create a new

ItemStack

to pass to

Item#getHarvestLevel

, use the player's held

ItemStack

. Creating a new

ItemStack

will break any tool that uses metadata or NBT to determine the harvest level (e.g. Tinkers' Construct).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Don't check if the item extends

ItemSpade

, that's not a reliable indicator that the tool functions as a shovel (Tinkers' Construct tools extend

ItemTool

directly, bypassing

ItemSpade

and the other subclasses); some tools may not even extend

ItemTool

. Just use the harvest level check, any tool that can't act as a shovel will simply return -1 from

Item#getHarvestLevel

.

 

Don't create a new

ItemStack

to pass to

Item#getHarvestLevel

, use the player's held

ItemStack

. Creating a new

ItemStack

will break any tool that uses metadata or NBT to determine the harvest level (e.g. Tinkers' Construct).

 

You make great points, thanks. I removed the check for ItemSpade, but how do I get the harvest level of the held item without making it an itemstack? getHarvestLevel requires (itemStack. toolClass) parameters. This is where I am at so far, is that what you meant? Code functions fine, just want to ensure I don't break other mods (well, at least try not to break tinkers since its pretty widely used).

 

    @SubscribeEvent
    public void onBlockDropItems(BlockEvent.HarvestDropsEvent event) //This is overriding drop tables of vanilla blocks.
    {
        if (event.block == Blocks.gravel)
        {
            World world = event.world;
            EntityPlayer player = event.harvester;

            if(player.getHeldItem() != null) //If used an item.
            {
                ItemStack toolUsed = player.getHeldItem();
                int toolUsedHarvestLevel = player.getHeldItem().getItem().getHarvestLevel(toolUsed, "shovel"); // Gets harvest level of said tool
                if (toolUsedHarvestLevel >= 2) //Iron or better, aka 2 or better.
                {
                    if (world.rand.nextFloat() < 0.25f) //25% Chance to drop a RoughCrystalFragment if using the right shovel!
                    {
                        event.drops.add(new ItemStack(ModItems.itemRoughCrystalFragment, world.rand.nextInt(2) + 1)); //drops RoughCrystalFragment 1-2
                    }
                }
            }
        }

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Link to comment
Share on other sites

I meant pass the player's held

ItemStack

to

getHarvestLevel

instead of creating a new one with metadata 0 and no NBT.

 

What you've done looks correct.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.