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.

Featured Replies

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

 

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

 

  • Author

In this case it cant be null, because  the block is being broken by an item held by the player.

  • Author

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.

 

  • Author

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

Hi

 

:-) look at this line very carefully to spot the unwanted guest at the end

 

if(block.blockID == Block.oreIron.blockID);

 

-TGG

  • Author

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...

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.