diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/ItemMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/ItemMixin.java index 57c6b7acb..5d0716cac 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/ItemMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/ItemMixin.java @@ -209,6 +209,13 @@ private void releaseUsing(ItemStack itemStack, Level level, LivingEntity livingE } } + @Inject(method = "hurtEnemy", at = @At("HEAD"), cancellable = true) + private void hurtEnemy(ItemStack itemStack, LivingEntity livingEntity, LivingEntity livingEntity2, CallbackInfoReturnable cir) { + if (kjs$itemBuilder != null && kjs$itemBuilder.hurtEnemy != null) { + cir.setReturnValue(kjs$itemBuilder.hurtEnemy.test(new ItemBuilder.HurtEnemyContext(itemStack, livingEntity, livingEntity2))); + } + } + @Override public Ingredient kjs$asIngredient() { if (kjs$asIngredient == null) { diff --git a/common/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/common/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index 640573caf..d8693280f 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -39,6 +39,7 @@ import java.util.UUID; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; import java.util.function.ToIntFunction; @SuppressWarnings({"unused", "UnusedReturnValue"}) @@ -112,6 +113,7 @@ public static Tier toToolTier(Object o) { public transient UseCallback use; public transient FinishUsingCallback finishUsing; public transient ReleaseUsingCallback releaseUsing; + public transient Predicate hurtEnemy; public String texture; public String parentModel; @@ -140,6 +142,7 @@ public ItemBuilder(ResourceLocation i) { finishUsing = null; releaseUsing = null; fireResistant = false; + hurtEnemy = null; } @Override @@ -445,6 +448,16 @@ public ItemBuilder releaseUsing(ReleaseUsingCallback releaseUsing) { return this; } + @Info(""" + Gets called when the item is used to hurt an entity. + + For example, when using a sword to hit a mob, this is called. + """) + public ItemBuilder hurtEnemy(Predicate context) { + this.hurtEnemy = context; + return this; + } + @FunctionalInterface public interface UseCallback { boolean use(Level level, Player player, InteractionHand interactionHand); @@ -464,4 +477,6 @@ public interface ReleaseUsingCallback { public interface NameCallback { Component apply(ItemStack itemStack); } + + public record HurtEnemyContext(ItemStack getItem, LivingEntity getTarget, LivingEntity getAttacker) {} } \ No newline at end of file