Jump to content

[Solved][1.10.2] Need help with HarvestDropsEvent...


Recommended Posts

Posted

I admit, I've never worked with events so it's probably something I'm overlooking but anyhow...

 

I'm trying to get a vanilla block to drop not only it's own item drop but also to have a chance of dropping a custom one.

 

Here's my Event code:

 

package com.holydevils.event;

import java.util.List;

import com.holydevils.items.ModItems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ModHarvestDropsEvent {
@SubscribeEvent
public void onHarvestBlock(BlockEvent.HarvestDropsEvent event)
{
	List<ItemStack> currentDrops = event.getDrops();
	final EntityPlayer PLAYER = event.getHarvester();
	if(null == PLAYER || null == PLAYER.getHeldItemMainhand()) return;

	if(event.getState().getBlock() == Blocks.DIAMOND_ORE && event.getWorld().rand.nextInt(100) < 75)
	{
		currentDrops.add(new ItemStack(ModItems.diamondDust, 1));
	}

	if(event.getState().getBlock() == Blocks.OBSIDIAN && event.getWorld().rand.nextInt(100) < 75)
	{
		currentDrops.add(new ItemStack(ModItems.obsidianDust, 1));
	}
	event.getDrops().clear();
	event.getDrops().addAll(currentDrops);
}
}

 

 

And here's my main registry:

 

package com.holydevils.main;

import com.holydevils.blocks.ModBlocks;
import com.holydevils.client.render.items.ItemRenderRegistry;
import com.holydevils.event.ModHarvestDropsEvent;
import com.holydevils.items.ModItems;
import com.holydevils.lib.StringReferences;
import com.holydevils.world.ModWorld;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;

@Mod(modid = StringReferences.MODID, name =StringReferences.NAME, version =StringReferences.VERSION)
public class MainRegistry {
@SidedProxy(clientSide = StringReferences.CLIENTPROXY, serverSide = StringReferences.SERVERPROXY)
public static ServerProxy proxyServer;
public static ModWorld mWorld = new ModWorld();

@EventHandler
public static void preLoad(FMLPreInitializationEvent PreEvent){
	ModItems.createItems();
	ModBlocks.manageBlocks();

	CraftingManager.craftRegister();

	GameRegistry.registerWorldGenerator(mWorld, 1);

	proxyServer.registerRenderInfo();
}
public static void load(FMLInitializationEvent Event){
}

public static void postLoad(FMLPostInitializationEvent PostEvent){
	MinecraftForge.EVENT_BUS.register(new ModHarvestDropsEvent());
}
}

 

Note: I've tried registering it in both load and postLoad. If I put it into preLoad the default item it's self only droppes randomly.

Posted

Try doing event.getDrops().add instead of resetting the list.

 

I tried doing that and it didn't work so what I did was add " || true" to the if statements and it still didn't drop the item... And before you ask, I did move it to the load method.

Posted

When I said use event.getDrops.add(...) did you remove the clear and the new List you made from the method? Is it dropping any Items at all?

 

It only drops the default item and yes I did remove the things that you mentioned

 

Updated code:

 

package com.holydevils.event;

import java.util.List;

import com.holydevils.items.ModItems;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class ModHarvestDropsEvent {
@SubscribeEvent
public void onHarvestBlock(BlockEvent.HarvestDropsEvent event)
{
	final EntityPlayer PLAYER = event.getHarvester();
	if(null == PLAYER || null == PLAYER.getHeldItemMainhand()) return;

	if(event.getState().getBlock() == Blocks.DIAMOND_ORE && event.getWorld().rand.nextInt(100) < 75 || true)
	{
		event.getDrops().add(new ItemStack(ModItems.diamondDust, 1));
	}

	if(event.getState().getBlock() == Blocks.OBSIDIAN && event.getWorld().rand.nextInt(100) < 75 || true)
	{
		event.getDrops().add(new ItemStack(ModItems.obsidianDust, 1));
	}
}
}

 

Posted

*Edit you don't have @EventHandler above your load and postLoad methods.

Yeah I didn't notice this before, but making a github is recommended for later posts.

 

Ok, Thx!

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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