Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/xyz/nucleoid/stimuli/event/block/BlockFallEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package xyz.nucleoid.stimuli.event.block;

import net.minecraft.block.BlockState;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import xyz.nucleoid.stimuli.event.StimulusEvent;

/**
* Called when a gravity-affected block attempts to fall.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to clarify here that this is for blocks starting to fall (block -> entity) rather than entity -> block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to rename the event entirely to BlockStartFallEvent? This event will be complemented by a BlockLandEvent as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

*
* <p>Upon return:
* <ul>
* <li>{@link ActionResult#SUCCESS} cancels further processing and allows the block to fall.
* <li>{@link ActionResult#FAIL} cancels further processing and prevents the block from falling.
* <li>{@link ActionResult#PASS} moves on to the next listener.</ul>
* <p>
* If all listeners return {@link ActionResult#PASS}, the block successfully falls.
*/
public interface BlockFallEvent {
StimulusEvent<BlockFallEvent> EVENT = StimulusEvent.create(BlockFallEvent.class, ctx -> (world, pos, state) -> {
try {
for (BlockFallEvent listener : ctx.getListeners()) {
ActionResult result = listener.onBlockFall(world, pos, state);
if (result != ActionResult.PASS) {
return result;
}
}
} catch (Throwable t) {
ctx.handleException(t);
}
return ActionResult.PASS;
});

ActionResult onBlockFall(ServerWorld world, BlockPos pos, BlockState state);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xyz.nucleoid.stimuli.mixin.block;

import java.util.Random;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.block.BlockState;
import net.minecraft.block.FallingBlock;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPos;
import xyz.nucleoid.stimuli.EventInvokers;
import xyz.nucleoid.stimuli.Stimuli;
import xyz.nucleoid.stimuli.event.block.BlockFallEvent;

@Mixin(FallingBlock.class)
public class FallingBlockMixin {
@Inject(method = "scheduledTick", at = @At("HEAD"), cancellable = true)
private void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random, CallbackInfo ci) {
try (EventInvokers invokers = Stimuli.select().at(world, pos)) {
ActionResult result = invokers.get(BlockFallEvent.EVENT).onBlockFall(world, pos, state);
if (result == ActionResult.FAIL) {
ci.cancel();
}
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/stimuli.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"mixins": [
"block.BlockItemMixin",
"block.BlockMixin",
"block.FallingBlockMixin",
"block.FarmlandBlockMixin",
"entity.LivingEntityMixin",
"player.CraftingResultInventoryMixin",
Expand Down