Jump to content

Recommended Posts

Posted

Im having a bug with the harvest drop event apparently it's this line (line 34) within , can anyone spot what I have done wrong.

Crash Report http://pastebin.com/LJihSaRS

 

Git Hub https://github.com/PandaTeam/HydroBlocks/blob/master/src/hydroblocks/lib/EventHooks.java

                        ItemStack heldItem = player.inventory.getCurrentItem();

 

package hydroblocks.lib;

import java.util.Random;

import hydroblocks.items.Items;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.BlockEvent;

/**
* Name and cast of this class are irrelevant
*/

public class EventHooks {

Random random;


        /**
        * The key is the @ForgeSubscribe annotation and the cast of the Event you put in as argument.
        * The method name you pick does not matter. Method signature is public void, always.
        */
        @ForgeSubscribe
        public void onHarvestDrops(BlockEvent.HarvestDropsEvent event)
  
                {
            random = new Random();
                        /*
                        * You can then proceed to read and change the Event's fields where possible
                        */
                        EntityPlayer player = event.harvester;
                        ItemStack heldItem = player.inventory.getCurrentItem();
                        Block block = event.block;        
                        
                        if(heldItem.itemID == Items.ironsledgehammer.itemID)
                        {   
                        	if(block.blockID == Block.oreIron.blockID);
                        	{
                                event.drops.clear();
                                event.drops.add(new ItemStack(Block.blockGold, random.nextInt(2) + 1));
                                event.dropChance = 1.0F;
                        	}
                        }
                        
                                        
                
        }
        
}

 

Posted

Hi

 

Well it seems pretty clear that either player is null or inventory is null.

 

Perhaps event.harvester is sometimes null?

 

Checking the Javadocs:

    /**
     * Fired when a block is about to drop it's harvested items. The {@link #drops} array can be amended, as can the {@link #dropChance}.
     * <strong>Note well:</strong> the {@link #harvester} player field is null in a variety of scenarios. Code expecting null.

and

        public final EntityPlayer harvester; // May be null for non-player harvesting such as explosions or machines

 

 

-TGG

 

Posted

Ok so I know that it is

 

                        EntityPlayer player = event.harvester;
                        ItemStack heldItem = player.inventory.getCurrentItem();

 

section, by removing it the error goes away, however now any block that is destroyed will drop gold blocks.

 

Posted

A few more changes, not ever block is dropping gold, however any block broken with ironsledgehammer still drops gold

public class EventHooks {

        @ForgeSubscribe
        public void onHarvestDrops(BlockEvent.HarvestDropsEvent event)
        {
        	Block block = event.block;
        	
            EntityPlayer player = event.harvester;
            if(player!=null)
            {
                    ItemStack heldItemStack = player.getCurrentEquippedItem();
                    if(heldItemStack != null && player != null)
                    {
                            int heldItem = heldItemStack.itemID;
                    
                                    if(heldItem == Items.ironsledgehammer.itemID)
                                    {
                                    	if(block.blockID == Block.oreIron.blockID);
                                    	{                               	
                                            event.drops.clear();
                                            event.drops.add(new ItemStack(Block.blockGold, 2));
                                            event.dropChance = 1.0F;
                                    	}
                                    }
                    }               
            }
        }
        
}

Posted

Oh for poops sake that sneaky mother trucker

 

Thanks for your help

Final working code

 

package hydroblocks.lib;

import java.util.Random;

import hydroblocks.items.Items;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.BlockEvent;



public class EventHooks {

Random random;

        @ForgeSubscribe
        public void onHarvestDrops(BlockEvent.HarvestDropsEvent event)
        {
            random = new Random();
        	Block block = event.block;
        	
            EntityPlayer player = event.harvester;
            if(player!=null)
            {
                    ItemStack heldItemStack = player.getCurrentEquippedItem();
                    if(heldItemStack != null && player != null)
                    {
                            int heldItem = heldItemStack.itemID;
                    
                                    if(heldItem == Items.ironsledgehammer.itemID)
                                    {
                                    	if(block.blockID == Block.oreIron.blockID)
                                    	{                               	
                                            event.drops.clear();
                                            event.drops.add(new ItemStack(Block.blockGold, random.nextInt(2) + 1));
                                            event.dropChance = 1.0F;
                                    	}
                                    }
                    }               
            }
        }
        
}


                        		
                        
                                        
                

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.