Leaf Decay Dropping Items

Started by Bloodshot_pico on

Topic category: Help with modding (Java Edition)

Last seen on 20:30, 16. Dec 2017
Joined Dec 2015
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Leaf Decay Dropping Items

Help With Leaf Decay Dropping Mod Items

 

Hello so I was wondering how would one make it so that certain leaves within vanilla drop certain items and give them percentages? I'm not sure if this would be too advanced but I'm not sure honestly. I'm guessing it would be a little bit of code though. I just don't see an option in the events for anything I'm guessing it would be a global event but I just looked and there's nothing about leaf decay or anything about blocks. So I have no idea. :/

 

Anyhow I'd basically want another apple to be dropped from others leaves in the game, that is what I'm going after.

If anyone is able to help that would be appreciated, thanks for reading. :)

~Blood

Last seen on 17:17, 10. Jan 2024
Joined Aug 2013
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Hello…
Sat, 12/16/2017 - 20:45

Hello.
What you most likely want to do is hook to the BlockEvent.HarvestDropsEvent.
There is not quite, unfortunately, any direct support by MCreator for this, but nothing stops you from doing it manually.
First, you need some EventHandler class. From the name you can guess that it is basically a class, that listens for events and handles actions what should happen, when they are fired.
Fortunately, there is already one element type in MCreator, that provides you registred EventHandler - Overlay. So simply just create a new empty Overlay element.

Then open its code and remove everything in the eventHandler void, so you will get something like this:

...some code...

public static class GUIRenderEventClass
{
    @SubscribeEvent(priority = EventPriority.NORMAL)
	public void eventHandler(RenderGameOverlayEvent event)
        {

	}
}

..some code...

You have to listen for the correct event, so replace RenderGameOverlayEvent by BlockEvent.HarvestDropsEvent.
Now the code is ready for adding the actual logic of dropping the apples.
First, you need to check the leaves block. This is one of many possible ways to do it:
 

if(event.getState().getBlock() == Blocks.LEAVES)
{

}

(NOTE: This will check for any variants of Blocks.LEAVES - Oak, Birch, Spruce, Jungle. You may want to check for the variant.
Dark Oak and Acacia are stored in Blocks.LEAVES2)

The final step is adding the actual drop.
That is as easy as this code:
 

//20 = CHANCE OF DROPPING , LESSER NUMBER = BIGGER CHANCE
if(event.getWorld().rand.nextInt(20) == 0)
{
//DONT FORGET TO CHANGE TO YOUR ITEM
Block.spawnAsEntity(event.getWorld(), event.getPos(), new ItemStack(Items.APPLE, 1, 0));
}

At the end, you should end up with something like this:
 

public static class GUIRenderEventClass
{
    @SubscribeEvent(priority = EventPriority.NORMAL)
	public void eventHandler(BlockEvent.HarvestDropsEvent event){
		if(event.getState().getBlock() == Blocks.LEAVES){
			if(event.getWorld().rand.nextInt(20) == 0)
			{
				Block.spawnAsEntity(event.getWorld(), event.getPos(), new ItemStack(Items.APPLE, 1, 0));
			}
		}
	}
}

And that's it. This code should work, hopefully.

Last seen on 00:53, 14. Mar 2024
Joined Sep 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
So how would I do this with…
Tue, 05/21/2019 - 02:13

So how would I do this with entities?