Jump to content

HarvestDropsEvent bug


jibbity

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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


                        		
                        
                                        
                

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, im kind of a newbie on creating mods, and i been trying to make simples mods so far, Right now, i'm creating a mod which detect an specific type of mob and kill it if reach a certain number, by far the mod is detecting the mob and counting them but at the time it reach the limit number, it don't do anything. The Game don't tweak, don't crash, just i't don't run the method i call, which is kill().   public InteractionResultHolder<ItemStack> use(Level pLevel, Player pPlayer, InteractionHand pUsedHand) { final var TotalCows = pLevel.getNearbyEntities(Cow.class, TargetingConditions.forNonCombat(), pPlayer, pPlayer.getBoundingBox().inflate(5)); if(pLevel.isClientSide){ if(pPlayer.getItemInHand(InteractionHand.MAIN_HAND).getItem() == this.asItem()){ if(!TotalCows.isEmpty()){ if(TotalCows.get(0).isAlive()){ TotalCows.get(0).kill(); } }else{ pPlayer.sendSystemMessage(Component.literal("No hay vacas")); } } } The sendSystemMessage is get triggered, and when i change the method .kill() by any other like send a message it got called, can i get some tips, and a clue of what i'm missing
    • UPD: I DID IT   @Override public void applyEffectTick(LivingEntity livingEntity, int pAmplifier) { Level level = livingEntity.level(); if (!level.isClientSide()) { Holder<DamageType> entropyHolder = level .registryAccess() .registryOrThrow(Registries.DAMAGE_TYPE) .getHolderOrThrow(GTDamageTypes.ENTROPY_KEY); livingEntity.hurt(new DamageSource(entropyHolder), 1); } super.applyEffectTick(livingEntity, pAmplifier); }  
    • So I came to this public class ModDamageTypes{ public static void bootstrap(BootstapContext<DamageType> context) { context.register(GTDamageTypes.ENTROPY_KEY, new DamageType(GrimTales.MOD_ID + "_entropy", 0.1f)); } } public class GTDamageTypes implements DamageTypes { public static final ResourceKey<DamageType> ENTROPY_KEY = register("entropy"); private static ResourceKey<DamageType> register(String name) { return ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(GrimTales.MOD_ID, name)); } } public class ModWorldGenProvider extends DatapackBuiltinEntriesProvider { public static final RegistrySetBuilder BUILDER = new RegistrySetBuilder() .add(Registries.DAMAGE_TYPE, ModDamageTypes::bootstrap); public ModWorldGenProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries) { super(output, registries, BUILDER, Set.of(GrimTales.MOD_ID)); } But! I still have an issue that, I cannot just use Holder.direct, due to it will create error that "Required type:  Holder<DamageType>  Provided : Holder<ResourceKey<DamageType>>" And I still have no clue how to fix it
    • Yea, unless someone else sees this and has a solution in the next 4 or 5 hours, I’m just gonna reinstall the modpack. Thanks for trying to help me out.
    • Sadly, no it didn't work, although the window for minecraft startup stayed longer. do you have anymore recommendations?
  • Topics

×
×
  • Create New...

Important Information

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