Skip to content

Commit

Permalink
Added EntityTotemActivateEvent (#48)
Browse files Browse the repository at this point in the history
* Added EntityTotemActivateEvent

* Made the mixin not dumb and cleaned up.

* Check isClient before invoking event

* Return early for the isClient check

* Fix the isClient check
  • Loading branch information
VendoAU authored May 18, 2024
1 parent 7a51e5b commit 83e7461
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package xyz.nucleoid.stimuli.event.entity;

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

/**
* Called when a {@link LivingEntity} activates a totem of undying.
*
* <p>Upon return:
* <ul>
* <li>{@link ActionResult#SUCCESS} cancels further processing and activates the totem.
* <li>{@link ActionResult#FAIL} cancels further processing and does not activate the totem.
* <li>{@link ActionResult#PASS} moves on to the next listener.
* </ul>
* </p>
*/
public interface EntityActivateTotemEvent {

StimulusEvent<EntityActivateTotemEvent> EVENT = StimulusEvent.create(EntityActivateTotemEvent.class, ctx -> (entity, source, itemStack) -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onTotemActivate(entity, source, itemStack);
if (result != ActionResult.PASS) {
return result;
}
}
} catch (Throwable t) {
ctx.handleException(t);
}
return ActionResult.PASS;
});

ActionResult onTotemActivate(LivingEntity entity, DamageSource source, ItemStack itemStack);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
Expand All @@ -17,6 +18,7 @@
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.EntityActivateTotemEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDamageEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDeathEvent;
import xyz.nucleoid.stimuli.event.entity.EntityDropItemsEvent;
Expand Down Expand Up @@ -81,4 +83,19 @@ private void modifyDroppedLoot(LootTable instance, LootContextParameterSet param
}
}
}

@Inject(method = "tryUseTotem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;decrement(I)V"), cancellable = true)
private void tryUseTotem(DamageSource source, CallbackInfoReturnable<Boolean> cir, @Local(ordinal = 1) ItemStack itemStack) {
if (this.getWorld().isClient) {
return;
}

var entity = (LivingEntity) (Object) this;
try (var invokers = Stimuli.select().forEntity(entity)) {
var result = invokers.get(EntityActivateTotemEvent.EVENT).onTotemActivate(entity, source, itemStack);
if (result == ActionResult.FAIL) {
cir.setReturnValue(false);
}
}
}
}

0 comments on commit 83e7461

Please sign in to comment.