Posted December 20, 201311 yr 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; } } } }
December 20, 201311 yr 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
December 20, 201311 yr Author In this case it cant be null, because the block is being broken by an item held by the player.
December 20, 201311 yr Hi Have you checked? eg using a breakpoint or System.out.println("player is null:" + (player == null)); -TGG
December 20, 201311 yr 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.
December 20, 201311 yr 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; } } } } } }
December 20, 201311 yr Hi :-) look at this line very carefully to spot the unwanted guest at the end if(block.blockID == Block.oreIron.blockID); -TGG
December 20, 201311 yr 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.