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

Commit 20ee0db

Browse files
cittyinthecloudJFronnyTheGlitch76
authored
Implement SaplingGrowTreeEvent and BlockEvent.FarmlandTrampleEvent (#146)
* Implement SaplingGrowTreeEvent and BlockEvent.FarmlandTrampleEvent * Requested changes * Has result * Fix imports * Fix checkstyle Co-authored-by: JFronny <33260128+JFronny@users.noreply.github.com> Co-authored-by: Glitch <glitchieproductionsofficial@gmail.com>
1 parent cf02fa1 commit 20ee0db

File tree

10 files changed

+191
-3
lines changed

10 files changed

+191
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* If a method utilizes this {@link net.minecraftforge.eventbus.api.Event} as
3232
* its parameter, the method will receive every child event of this class.<br>
3333
* <br>
34-
* {@link #Chunk} contains the Chunk this event is affecting.<br>
34+
* {@link #chunk} contains the Chunk this event is affecting.<br>
3535
* <br>
3636
* All children of this event are fired on the
3737
* {@link MinecraftForge#EVENT_BUS}.<br>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.event.world;
21+
22+
import java.util.Random;
23+
24+
import net.minecraft.block.BlockState;
25+
import net.minecraft.util.math.BlockPos;
26+
import net.minecraft.world.IWorld;
27+
28+
/**
29+
* SaplingGrowTreeEvent is fired when a sapling grows into a tree.
30+
*
31+
* <p>This event is fired during sapling growth in
32+
* {@link net.minecraft.block.SaplingBlock#generate(IWorld, BlockPos, BlockState, Random)}.
33+
*
34+
* <p>{@link #pos} contains the coordinates of the growing sapling.
35+
* {@link #rand} contains an instance of Random for use.
36+
*
37+
* <p>This event is not cancellable.
38+
*
39+
* <p>This event has a result.
40+
* This result determines if the sapling is allowed to grow.
41+
*/
42+
43+
public class SaplingGrowTreeEvent extends WorldEvent {
44+
private final BlockPos pos;
45+
private final Random rand;
46+
47+
public SaplingGrowTreeEvent(IWorld world, Random rand, BlockPos pos) {
48+
super(world);
49+
this.rand = rand;
50+
this.pos = pos;
51+
}
52+
53+
public BlockPos getPos() {
54+
return pos;
55+
}
56+
57+
public Random getRand() {
58+
return rand;
59+
}
60+
61+
@Override
62+
public boolean hasResult() {
63+
return true;
64+
}
65+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121

2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Random;
2425

2526
import javax.annotation.Nullable;
2627

2728
import net.minecraftforge.common.MinecraftForge;
2829
import net.minecraftforge.event.world.ChunkEvent;
2930
import net.minecraftforge.event.world.ChunkWatchEvent;
31+
import net.minecraftforge.event.world.SaplingGrowTreeEvent;
3032
import net.minecraftforge.event.world.WorldEvent;
33+
import net.minecraftforge.eventbus.api.Event;
3134

3235
import net.minecraft.entity.EntityCategory;
3336
import net.minecraft.server.network.ServerPlayerEntity;
@@ -105,4 +108,10 @@ public void onInitialize() {
105108
// Fire ChunkEvent.Unload on server side
106109
ServerChunkEvents.CHUNK_UNLOAD.register((server, chunk) -> MinecraftForge.EVENT_BUS.post(new ChunkEvent.Unload(chunk)));
107110
}
111+
112+
public static boolean onSaplingGrowTree(IWorld world, Random rand, BlockPos pos) {
113+
SaplingGrowTreeEvent event = new SaplingGrowTreeEvent(world, rand, pos);
114+
MinecraftForge.EVENT_BUS.post(event);
115+
return event.getResult() != Event.Result.DENY;
116+
}
108117
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.event.world;
21+
22+
import java.util.Random;
23+
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
29+
import net.minecraft.block.BlockState;
30+
import net.minecraft.block.SaplingBlock;
31+
import net.minecraft.util.math.BlockPos;
32+
import net.minecraft.world.IWorld;
33+
34+
import net.patchworkmc.impl.event.world.WorldEvents;
35+
36+
@Mixin(SaplingBlock.class)
37+
public class MixinSaplingBlock {
38+
@Inject(method = "generate", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/sapling/SaplingGenerator;generate(Lnet/minecraft/world/IWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Ljava/util/Random;)Z"), cancellable = true)
39+
private void onGenerateTree(IWorld world, BlockPos pos, BlockState state, Random random, CallbackInfo ci) {
40+
if (!WorldEvents.onSaplingGrowTree(world, random, pos)) {
41+
ci.cancel();
42+
}
43+
}
44+
}

patchwork-events-world/src/main/resources/patchwork-events-world.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"mixins": [
66
"MixinChunkGenerator",
77
"MixinMinecraftServer",
8+
"MixinSaplingBlock",
89
"MixinServerWorld",
910
"MixinSpawnHelper",
1011
"MixinThreadedAnvilChunkStorage"

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.minecraftforge.common.extensions.IForgeBlockState;
2525
import net.minecraftforge.eventbus.api.Event;
2626

27+
import net.minecraft.entity.Entity;
2728
import net.minecraft.item.ItemStack;
2829
import net.minecraft.util.DefaultedList;
2930
import net.minecraft.block.BlockState;
@@ -166,4 +167,32 @@ public PlayerEntity getHarvester() {
166167
return harvester;
167168
}
168169
}
170+
171+
/**
172+
* Fired when when farmland gets trampled
173+
* This event is cancellable.
174+
*/
175+
public static class FarmlandTrampleEvent extends BlockEvent {
176+
private final Entity entity;
177+
private final float fallDistance;
178+
179+
public FarmlandTrampleEvent(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
180+
super(world, pos, state);
181+
this.entity = entity;
182+
this.fallDistance = fallDistance;
183+
}
184+
185+
public Entity getEntity() {
186+
return entity;
187+
}
188+
189+
public float getFallDistance() {
190+
return fallDistance;
191+
}
192+
193+
@Override
194+
public boolean isCancelable() {
195+
return true;
196+
}
197+
}
169198
}

patchwork-extensions-block/src/main/java/net/patchworkmc/impl/extensions/block/BlockHarvestManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import net.minecraft.block.RedstoneOreBlock;
3636
import net.minecraft.block.SpawnerBlock;
3737
import net.minecraft.block.entity.BlockEntity;
38+
import net.minecraft.entity.Entity;
3839
import net.minecraft.entity.player.PlayerEntity;
3940
import net.minecraft.item.ItemStack;
4041
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
@@ -170,4 +171,13 @@ public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World wo
170171
MinecraftForge.EVENT_BUS.post(event);
171172
return event.getDropChance();
172173
}
174+
175+
public static boolean onFarmlandTrample(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
176+
// TODO: In forge, the possibility of trampling is handled by IForgeEntity.canTrample
177+
// Maybe there's a good way to reconcile that to not break any Fabric mods trying to
178+
// manipulate crop trampling, but for now I just let the vanilla check do it's thing.
179+
BlockEvent.FarmlandTrampleEvent event = new BlockEvent.FarmlandTrampleEvent(world, pos, state, fallDistance, entity);
180+
MinecraftForge.EVENT_BUS.post(event);
181+
return !event.isCanceled();
182+
}
173183
}

patchwork-extensions-block/src/main/java/net/patchworkmc/mixin/extensions/block/MixinFarmlandBlock.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,26 @@
2424
import org.spongepowered.asm.mixin.Mixin;
2525
import org.spongepowered.asm.mixin.injection.At;
2626
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2728
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2829

30+
import net.minecraft.block.Block;
2931
import net.minecraft.block.BlockState;
3032
import net.minecraft.block.FarmlandBlock;
33+
import net.minecraft.entity.Entity;
3134
import net.minecraft.util.math.BlockPos;
3235
import net.minecraft.util.math.Direction;
3336
import net.minecraft.world.BlockView;
37+
import net.minecraft.world.World;
38+
39+
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
3440

3541
@Mixin(FarmlandBlock.class)
36-
public class MixinFarmlandBlock {
42+
public class MixinFarmlandBlock extends Block {
43+
public MixinFarmlandBlock() {
44+
super(null);
45+
}
46+
3747
@Inject(method = "hasCrop", cancellable = true, at = @At("HEAD"))
3848
private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
3949
final BlockState ourState = world.getBlockState(pos);
@@ -44,4 +54,12 @@ private static void onHasCrop(BlockView world, BlockPos pos, CallbackInfoReturna
4454
cir.cancel();
4555
}
4656
}
57+
58+
@Inject(method = "onLandedUpon", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/FarmlandBlock;setToDirt(Lnet/minecraft/block/BlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V"), cancellable = true)
59+
private void onSetToDirt(World world, BlockPos pos, Entity entity, float distance, CallbackInfo ci) {
60+
if (!BlockHarvestManager.onFarmlandTrample(world, pos, world.getBlockState(pos), distance, entity)) {
61+
super.onLandedUpon(world, pos, entity, distance);
62+
ci.cancel();
63+
}
64+
}
4765
}

patchwork-god-classes/src/main/java/net/minecraftforge/common/ForgeHooks.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraftforge.event.ForgeEventFactory;
2929
import net.minecraftforge.eventbus.api.Event;
3030

31+
import net.minecraft.block.BlockState;
3132
import net.minecraft.world.dimension.DimensionType;
3233
import net.minecraft.entity.Entity;
3334
import net.minecraft.entity.ItemEntity;
@@ -142,4 +143,8 @@ public static LootTable loadLootTable(Gson gson, Identifier name, JsonObject dat
142143
public static String readPoolName(JsonObject json) {
143144
return LootHooks.readPoolName(json);
144145
}
146+
147+
public static boolean onFarmlandTrample(World world, BlockPos pos, BlockState state, float fallDistance, Entity entity) {
148+
return BlockHarvestManager.onFarmlandTrample(world, pos, state, fallDistance, entity);
149+
}
145150
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package net.minecraftforge.event;
2121

22+
import java.util.Random;
23+
2224
import javax.annotation.Nullable;
2325

2426
import net.minecraftforge.common.capabilities.CapabilityDispatcher;
@@ -38,18 +40,19 @@
3840
import net.minecraft.loot.LootTable;
3941
import net.minecraft.util.Identifier;
4042
import net.minecraft.util.hit.HitResult;
43+
import net.minecraft.util.math.BlockPos;
4144
import net.minecraft.world.IWorld;
4245
import net.minecraft.world.MobSpawnerLogic;
4346
import net.minecraft.world.World;
4447
import net.minecraft.block.BlockState;
4548
import net.minecraft.item.ItemStack;
4649
import net.minecraft.util.DefaultedList;
47-
import net.minecraft.util.math.BlockPos;
4850

4951
import net.patchworkmc.impl.capability.CapabilityEvents;
5052
import net.patchworkmc.impl.event.entity.EntityEvents;
5153
import net.patchworkmc.impl.event.entity.PlayerEvents;
5254
import net.patchworkmc.impl.event.loot.LootEvents;
55+
import net.patchworkmc.impl.event.world.WorldEvents;
5356
import net.patchworkmc.impl.extensions.block.BlockHarvestManager;
5457

5558
/*
@@ -87,6 +90,10 @@ public static LootTable loadLootTable(Identifier name, LootTable table, LootMana
8790
return LootEvents.loadLootTable(name, table, lootTableManager);
8891
}
8992

93+
public static boolean saplingGrowTree(IWorld world, Random rand, BlockPos pos) {
94+
return WorldEvents.onSaplingGrowTree(world, rand, pos);
95+
}
96+
9097
// Forge might remove BlockEvent.HarvestDropsEvent, which is replaced by the new loot modifier.
9198
@Deprecated
9299
public static float fireBlockHarvesting(DefaultedList<ItemStack> drops, World world, BlockPos pos, BlockState state, int fortune, float dropChance, boolean silkTouch, PlayerEntity player) {

0 commit comments

Comments
 (0)