-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/xyz/nucleoid/stimuli/event/block/BlockTrampleEvent.java
This file contains 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,37 @@ | ||
package xyz.nucleoid.stimuli.event.block; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.entity.LivingEntity; | ||
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 any {@link LivingEntity} attempts to trample a block, such as farmland or turtle eggs. | ||
* | ||
* <p>Upon return: | ||
* <ul> | ||
* <li>{@link ActionResult#SUCCESS} cancels further processing and allows the trample. | ||
* <li>{@link ActionResult#FAIL} cancels further processing and cancels the trample. | ||
* <li>{@link ActionResult#PASS} moves on to the next listener.</ul> | ||
* <p> | ||
* If all listeners return {@link ActionResult#PASS}, the trample succeeds. | ||
*/ | ||
public interface BlockTrampleEvent { | ||
StimulusEvent<BlockTrampleEvent> EVENT = StimulusEvent.create(BlockTrampleEvent.class, ctx -> (entity, world, pos, from, to) -> { | ||
try { | ||
for (var listener : ctx.getListeners()) { | ||
var result = listener.onTrample(entity, world, pos, from, to); | ||
if (result != ActionResult.PASS) { | ||
return result; | ||
} | ||
} | ||
} catch (Throwable t) { | ||
ctx.handleException(t); | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
|
||
ActionResult onTrample(LivingEntity entity, ServerWorld world, BlockPos pos, BlockState from, BlockState to); | ||
} |
This file contains 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
40 changes: 40 additions & 0 deletions
40
src/main/java/xyz/nucleoid/stimuli/mixin/block/TurtleEggBlockMixin.java
This file contains 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,40 @@ | ||
package xyz.nucleoid.stimuli.mixin.block; | ||
|
||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.block.TurtleEggBlock; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.entity.LivingEntity; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.world.World; | ||
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 xyz.nucleoid.stimuli.Stimuli; | ||
import xyz.nucleoid.stimuli.event.block.BlockTrampleEvent; | ||
|
||
@Mixin(TurtleEggBlock.class) | ||
public class TurtleEggBlockMixin { | ||
@Inject(method = "tryBreakEgg", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/TurtleEggBlock;breakEgg(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;)V", shift = At.Shift.BEFORE), cancellable = true) | ||
private void trampleTurtleEgg(World world, BlockState from, BlockPos pos, Entity entity, int inverseChance, CallbackInfo ci) { | ||
if (world instanceof ServerWorld serverWorld && entity instanceof LivingEntity livingEntity) { | ||
BlockState to = Blocks.AIR.getDefaultState(); | ||
if (from.contains(TurtleEggBlock.EGGS)) { | ||
int eggs = from.get(TurtleEggBlock.EGGS); | ||
if (eggs > 1) { | ||
to = from.with(TurtleEggBlock.EGGS, eggs - 1); | ||
} | ||
} | ||
|
||
try (var invokers = Stimuli.select().forEntityAt(entity, pos)) { | ||
var trampleResult = invokers.get(BlockTrampleEvent.EVENT).onTrample(livingEntity, serverWorld, pos, from, to); | ||
if (trampleResult == ActionResult.FAIL) { | ||
ci.cancel(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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