Skip to content

Forge API

Crystal Spider edited this page Nov 24, 2023 · 14 revisions

Multi-harvest and Mono-harvest

Since version 8.0.0.0 it is possible for a player to right-click harvest multiple crops at the same time in a flat squared area centered on the right-clicked crop.
When multi-harvesting, each event described below is fired once for each involved crop.
Each event has a property, first, that tells whether the current crop is the actual right-clicked crop (the one also at the center of the harvest area).
Finally, note that for HarvestWithEaseServerEvent events the hitResult property is always null unless first is true.

HarvestWithEaseEvent abstract class

HarvestWithEaseEvent is the abstract class from which all other events of this mod are derived.
It extends PlayerEvent, which means that a method subscribing to PlayerEvent will receive also this event and all of its children.
Like PlayerEvent, all children of this event are fired on the MinecraftForge#EVENT_BUS.
A method subscribing to HarvestWithEaseEvent will receive all of its children.

There's also a second abstract class called HarvestWithEaseServerEvent that extends this one and is extended by all events of this mod that only get fired server-side.
The considerations about subscribing to the abstract events are the same as above.


RightClickHarvestCheck event

This event is fired when checking whether the player can harvest the right-clicked crop.
When the event fires, the right-clicked block will have been already verified as a crop block, meaning it is either a subclass of CropBlock, NetherWartBlock, CocoaBlock or its resource location is one of the items inside the crops property of this mod configuration file.

By setting the RightClickHarvestCheck#success to false you will prevent entirely the harvest, unless some other mods sets it back to true.
The event is cancelable, so that if needed it's possible to cancel further processing (for example setting RightClickHarvestCheck#success to false and canceling the event will ensure that no other mod can set it back to true).

Note that, when this event fires, the age of the crop won't have been determined yet, so even if true is returned at the end of this event, the right-click harvest may not happen.
Note also that this is the only HarvestWithEaseEvent that fires on both server and client, unlike all other events that fire only server-side, so make sure to return and/or cancel the event the same on both times to avoid desynchronization.

This event is called RightClickHarvestCheck to avoid confusing it with Forge HarvestCheck event that instead gets fired right before this one and also when breaking blocks.


BeforeHarvest event

This event is fired only server-side right before all the harvesting related actions are taken.
This event is not cancelable and only serves as a hook for other mods to execute some code before right-click harvesting.
If you wish to prevent the harvest use the RightClickHarvestCheck event.


HarvestDrops event

This event is fired only server-side when collecting the harvest drops to drop.
The default drops are calculated before dispatching the event and already have the one seed, the one that would be needed to replant, removed.

HarvestDrops#drops is where the current list of drops is stored, be aware that further computations may change the elements in it. Since HarvestDrops#drops is public and final, it is possible to operate with it as with any other List while granting it's never going to be null.
If you wish to prevent further changes to the list of drops by other mods that may be subscribed to the event, the event is cancelable and canceling it will avoid further computations.

Calling HarvestDrops#haveDropsChanged() will return whether the list of drops has changed from its default value.
Be aware that if you change the drops from their default value, then the other harvested blocks won't drop anything. This is generally not important as Vanilla and most mods only have crops made of a single block, so generally there are no other blocks to harvest. However this becomes important if the crop being harvested is a multi-block crop (e.g. Thermal Cultivation crops) because it means that instead of the whole crop (each block) only the right-clicked part will drop something.
If you need to change the drops you should also take care of this aspect. You can check whether a crop is a multi-block crop by calling HarvestWithEaseAPI#isTallCrop(Level, BlockState, BlockPos). This will tell you whether the given crop is made of at least 2 vertically connected blocks. The precise number of blocks that make up a crop is not known and, if you need it, you need to calculate it yourself.

Note that this event is fired before resetting the crop age.


AfterHarvest event

This event is fired only server-side right after all the harvesting related actions are taken (the right-click harvest succeeded).
This event is not cancelable and only serves as a hook for other mods to execute some code after right-click harvesting.