Skip to content

Commit

Permalink
0.2.7: add EntityDamageEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jan 21, 2022
1 parent cdedc76 commit e84984e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ loader_version=0.12.12
fabric_version=0.45.0+1.18

# Mod Properties
mod_version=0.2.6
mod_version=0.2.7
maven_group=xyz.nucleoid
archives_base_name=stimuli
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xyz.nucleoid.stimuli.event.entity;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.util.ActionResult;
import xyz.nucleoid.stimuli.event.StimulusEvent;

/**
* Called when a {@link LivingEntity} is damaged.
*
* <p>Upon return:
* <ul>
* <li>{@link ActionResult#SUCCESS} cancels further processing and damages the entity.
* <li>{@link ActionResult#FAIL} cancels further processing and does not damage the entity.
* <li>{@link ActionResult#PASS} moves on to the next listener.</ul>
* <p>
* If all listeners return {@link ActionResult#PASS}, the entity is damaged as per normal behavior.
*/
public interface EntityDamageEvent {
StimulusEvent<EntityDamageEvent> EVENT = StimulusEvent.create(EntityDamageEvent.class, ctx -> (entity, source, amount) -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onDamage(entity, source, amount);
if (result != ActionResult.PASS) {
return result;
}
}
} catch (Throwable t) {
ctx.handleException(t);
}
return ActionResult.PASS;
});

ActionResult onDamage(LivingEntity entity, DamageSource source, float amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xyz.nucleoid.stimuli.Stimuli;
import xyz.nucleoid.stimuli.event.entity.EntityDamageEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDeathEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDropItemsEvent;

Expand All @@ -26,6 +28,22 @@ private LivingEntityMixin(EntityType<?> type, World world) {
super(type, world);
}

@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
private void onDamage(DamageSource source, float amount, CallbackInfoReturnable<Boolean> ci) {
if (this.world.isClient) {
return;
}

var entity = (LivingEntity) (Object) this;

try (var invokers = Stimuli.select().forEntity(entity)) {
var result = invokers.get(EntityDamageEvent.EVENT).onDamage(entity, source, amount);
if (result == ActionResult.FAIL) {
ci.cancel();
}
}
}

@Inject(method = "onDeath", at = @At("HEAD"), cancellable = true)
private void callDeathListener(DamageSource source, CallbackInfo ci) {
if (this.world.isClient) {
Expand Down

0 comments on commit e84984e

Please sign in to comment.