Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 14ecc17

Browse files
committed
Add BlockEvent.HarvestDropsEvent and its respective firing method, WorldEvents.fireBlockHarvesting
1 parent 494fd27 commit 14ecc17

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

patchwork-events-world/src/main/java/net/minecraftforge/event/world/BlockEvent.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
package net.minecraftforge.event.world;
2121

22+
import java.util.List;
23+
2224
import net.minecraftforge.eventbus.api.Event;
2325

26+
import net.minecraft.item.ItemStack;
27+
import net.minecraft.util.DefaultedList;
2428
import net.minecraft.block.BlockState;
2529
import net.minecraft.entity.player.PlayerEntity;
2630
import net.minecraft.util.math.BlockPos;
@@ -109,4 +113,54 @@ public boolean isCancelable() {
109113
return true;
110114
}
111115
}
116+
117+
/**
118+
* Fired when a block is about to drop it's harvested items. The {@link #drops} array can be amended, as can the {@link #dropChance}.
119+
* <strong>Note well:</strong> the {@link #harvester} player field is null in a variety of scenarios. Code expecting null.
120+
*
121+
* <p>The {@link #dropChance} is used to determine which items in this array will actually drop, compared to a random number. If you wish, you
122+
* can pre-filter yourself, and set {@link #dropChance} to 1.0f to always drop the contents of the {@link #drops} array.
123+
*
124+
* <p>{@link #isSilkTouching} is set if this is considered a silk touch harvesting operation, vs a normal harvesting operation. Act accordingly.
125+
*/
126+
public static class HarvestDropsEvent extends BlockEvent {
127+
private final int fortuneLevel;
128+
private final DefaultedList<ItemStack> drops;
129+
private final boolean isSilkTouching;
130+
private final PlayerEntity harvester; // May be null for non-player harvesting such as explosions or machines
131+
private float dropChance; // Change to e.g. 1.0f, if you manipulate the list and want to guarantee it always drops
132+
133+
public HarvestDropsEvent(World world, BlockPos pos, BlockState state, int fortuneLevel, float dropChance, DefaultedList<ItemStack> drops, PlayerEntity harvester, boolean isSilkTouching) {
134+
super(world, pos, state);
135+
this.fortuneLevel = fortuneLevel;
136+
this.setDropChance(dropChance);
137+
this.drops = drops;
138+
this.isSilkTouching = isSilkTouching;
139+
this.harvester = harvester;
140+
}
141+
142+
public int getFortuneLevel() {
143+
return fortuneLevel;
144+
}
145+
146+
public List<ItemStack> getDrops() {
147+
return drops;
148+
}
149+
150+
public boolean isSilkTouching() {
151+
return isSilkTouching;
152+
}
153+
154+
public float getDropChance() {
155+
return dropChance;
156+
}
157+
158+
public void setDropChance(float dropChance) {
159+
this.dropChance = dropChance;
160+
}
161+
162+
public PlayerEntity getHarvester() {
163+
return harvester;
164+
}
165+
}
112166
}

patchwork-events-world/src/main/java/net/patchworkmc/impl/event/world/WorldEvents.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,14 @@
2323
import java.util.List;
2424

2525
import net.minecraftforge.common.MinecraftForge;
26+
import net.minecraftforge.event.world.BlockEvent;
2627
import net.minecraftforge.event.world.WorldEvent;
2728

29+
import net.minecraft.block.BlockState;
30+
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.item.ItemStack;
32+
import net.minecraft.util.DefaultedList;
33+
import net.minecraft.world.World;
2834
import net.minecraft.entity.EntityCategory;
2935
import net.minecraft.util.math.BlockPos;
3036
import net.minecraft.world.IWorld;
@@ -57,4 +63,11 @@ public static void onWorldUnload(IWorld world) {
5763
public static void onWorldSave(IWorld world) {
5864
MinecraftForge.EVENT_BUS.post(new WorldEvent.Save(world));
5965
}
66+
67+
// TODO: Leaving this unimplemented is intentional. See: https://github.com/MinecraftForge/MinecraftForge/issues/5828
68+
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
69+
BlockEvent.HarvestDropsEvent event = new BlockEvent.HarvestDropsEvent(world, pos, state, fortune, dropChance, drops, player, silkTouch);
70+
MinecraftForge.EVENT_BUS.post(event);
71+
return event.getDropChance();
72+
}
6073
}

patchwork-god-classes/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ dependencies {
77
compile project(path: ':patchwork-events-entity', configuration: 'dev')
88
compile project(path: ':patchwork-events-lifecycle', configuration: 'dev')
99
compile project(path: ':patchwork-events-rendering', configuration: 'dev')
10+
compile project(path: ':patchwork-events-world', configuration: 'dev')
1011
compile project(path: ':patchwork-loot', configuration: 'dev')
1112
}

patchwork-god-classes/src/main/java/net/minecraftforge/event/ForgeEventFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@
3434
import net.minecraft.world.IWorld;
3535
import net.minecraft.world.MobSpawnerLogic;
3636
import net.minecraft.world.World;
37+
import net.minecraft.block.BlockState;
38+
import net.minecraft.item.ItemStack;
39+
import net.minecraft.util.DefaultedList;
40+
import net.minecraft.util.math.BlockPos;
3741

3842
import net.patchworkmc.impl.capability.CapabilityEvents;
3943
import net.patchworkmc.impl.event.entity.EntityEvents;
4044
import net.patchworkmc.impl.event.loot.LootEvents;
45+
import net.patchworkmc.impl.event.world.WorldEvents;
4146

4247
/*
4348
* Note: this class is intended for mod use only, to dispatch to the implementations kept in their own modules.
@@ -73,4 +78,8 @@ public static boolean doSpecialSpawn(MobEntity entity, World world, float x, flo
7378
public static LootTable loadLootTable(Identifier name, LootTable table, LootManager lootTableManager) {
7479
return LootEvents.loadLootTable(name, table, lootTableManager);
7580
}
81+
82+
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {
83+
return WorldEvents.fireBlockHarvesting(drops, world, pos, state, fortune, dropChance, silkTouch, player);
84+
}
7685
}

0 commit comments

Comments
 (0)