-
Notifications
You must be signed in to change notification settings - Fork 24
Add events relating to falling blocks #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haykam821
wants to merge
1
commit into
NucleoidMC:1.17.1
Choose a base branch
from
haykam821:falling-block-events
base: 1.17.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/xyz/nucleoid/stimuli/event/block/BlockFallEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| * | ||
| * <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); | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
src/main/java/xyz/nucleoid/stimuli/mixin/block/FallingBlockMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 aBlockLandEventas well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good 👍