Skip to content

Commit a547115

Browse files
committed
Add support for farmblock trample listeners.
1 parent 06ff6f9 commit a547115

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

Common/src/main/java/net/darkhax/bookshelf/api/event/IEventHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.darkhax.bookshelf.api.event;
22

3+
import net.darkhax.bookshelf.api.event.block.IFarmlandTrampleListener;
34
import net.darkhax.bookshelf.api.event.client.IRecipeSyncEvent;
45
import net.darkhax.bookshelf.api.event.entity.player.IPlayerWakeUpEvent;
56
import net.darkhax.bookshelf.api.event.item.IItemTooltipEvent;
@@ -30,4 +31,11 @@ public interface IEventHelper {
3031
* @param listener The event listener.
3132
*/
3233
void addRecipeSyncListener(IRecipeSyncEvent listener);
34+
35+
/**
36+
* Registers a listener that will be invoked when a farmland block is trampled by an entity.
37+
*
38+
* @param listener The event listener.
39+
*/
40+
void addFarmlandTrampleListener(IFarmlandTrampleListener listener);
3341
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.darkhax.bookshelf.api.event.block;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.world.entity.Entity;
5+
import net.minecraft.world.level.block.state.BlockState;
6+
7+
public interface IFarmlandTrampleListener {
8+
9+
/**
10+
* A neutral event listener that will be invoked when an entity tramples on farmland.
11+
*
12+
* @param trampler The entity that trampled the farmland.
13+
* @param pos The position of the farmland.
14+
* @param state The farmland's blockstate.
15+
* @return The cancel state of the listener. If true the farmland will not be trampled.
16+
*/
17+
boolean apply(Entity trampler, BlockPos pos, BlockState state);
18+
}

Fabric/src/main/java/net/darkhax/bookshelf/impl/event/EventHelperFabric.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.darkhax.bookshelf.api.Services;
44
import net.darkhax.bookshelf.api.event.IEventHelper;
5+
import net.darkhax.bookshelf.api.event.block.IFarmlandTrampleListener;
56
import net.darkhax.bookshelf.api.event.client.IRecipeSyncEvent;
67
import net.darkhax.bookshelf.api.event.entity.player.IPlayerWakeUpEvent;
78
import net.darkhax.bookshelf.api.event.item.IItemTooltipEvent;
@@ -40,4 +41,10 @@ public void addRecipeSyncListener(IRecipeSyncEvent listener) {
4041
FabricBookshelfEvents.RECIPE_SYNC.register(listener);
4142
}
4243
}
44+
45+
@Override
46+
public void addFarmlandTrampleListener(IFarmlandTrampleListener listener) {
47+
48+
FabricBookshelfEvents.FARMLAND_TRAMPLE_EVENT.register(listener);
49+
}
4350
}

Fabric/src/main/java/net/darkhax/bookshelf/impl/event/FabricBookshelfEvents.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.darkhax.bookshelf.impl.event;
22

3+
import net.darkhax.bookshelf.api.event.block.IFarmlandTrampleListener;
34
import net.darkhax.bookshelf.api.event.client.IRecipeSyncEvent;
45
import net.fabricmc.fabric.api.event.Event;
56
import net.fabricmc.fabric.api.event.EventFactory;
@@ -16,4 +17,20 @@ public class FabricBookshelfEvents {
1617
callback.apply(recipeManager);
1718
}
1819
});
20+
21+
/**
22+
* This event is posted when an entity tramples on farmland.
23+
*/
24+
public static final Event<IFarmlandTrampleListener> FARMLAND_TRAMPLE_EVENT = EventFactory.createArrayBacked(IFarmlandTrampleListener.class, callbacks -> (player, pos, state) -> {
25+
26+
for (IFarmlandTrampleListener callback : callbacks) {
27+
28+
if (callback.apply(player, pos, state)) {
29+
30+
return true;
31+
}
32+
}
33+
34+
return false;
35+
});
1936
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.darkhax.bookshelf.mixin.block;
2+
3+
import net.darkhax.bookshelf.impl.event.FabricBookshelfEvents;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.world.entity.Entity;
6+
import net.minecraft.world.level.Level;
7+
import net.minecraft.world.level.block.FarmBlock;
8+
import net.minecraft.world.level.block.state.BlockState;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
@Mixin(FarmBlock.class)
15+
public class MixinFarmBlock {
16+
17+
@Inject(method = "fallOn", at = @At("HEAD"), cancellable = true)
18+
private void fallOn(Level level, BlockState blockState, BlockPos blockPos, Entity entity, float f, CallbackInfo ci) {
19+
20+
if (FabricBookshelfEvents.FARMLAND_TRAMPLE_EVENT.invoker().apply(entity, blockPos, blockState)) {
21+
22+
ci.cancel();
23+
}
24+
}
25+
}

Fabric/src/main/resources/bookshelf.fabric.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"package": "net.darkhax.bookshelf.mixin",
55
"compatibilityLevel": "JAVA_17",
66
"mixins": [
7+
"block.MixinFarmBlock"
78
],
89
"client": [
910
"client.AccessorItemBlockRenderTypes",

Forge/src/main/java/net/darkhax/bookshelf/impl/event/EventHelperForge.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import net.darkhax.bookshelf.api.Services;
44
import net.darkhax.bookshelf.api.event.IEventHelper;
5+
import net.darkhax.bookshelf.api.event.block.IFarmlandTrampleListener;
56
import net.darkhax.bookshelf.api.event.client.IRecipeSyncEvent;
67
import net.darkhax.bookshelf.api.event.entity.player.IPlayerWakeUpEvent;
78
import net.darkhax.bookshelf.api.event.item.IItemTooltipEvent;
89
import net.minecraftforge.client.event.RecipesUpdatedEvent;
910
import net.minecraftforge.common.MinecraftForge;
1011
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
1112
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
13+
import net.minecraftforge.event.world.BlockEvent;
14+
import net.minecraftforge.eventbus.api.Event;
1215
import net.minecraftforge.eventbus.api.EventPriority;
1316

1417
public class EventHelperForge implements IEventHelper {
@@ -36,4 +39,16 @@ public void addRecipeSyncListener(IRecipeSyncEvent listener) {
3639
MinecraftForge.EVENT_BUS.addListener(EventPriority.NORMAL, false, RecipesUpdatedEvent.class, e -> listener.apply(e.getRecipeManager()));
3740
}
3841
}
42+
43+
@Override
44+
public void addFarmlandTrampleListener(IFarmlandTrampleListener listener) {
45+
46+
MinecraftForge.EVENT_BUS.addListener(EventPriority.NORMAL, false, BlockEvent.FarmlandTrampleEvent.class, e -> {
47+
48+
if (listener.apply(e.getEntity(), e.getPos(), e.getState())) {
49+
50+
e.setCanceled(true);
51+
}
52+
});
53+
}
3954
}

0 commit comments

Comments
 (0)