From e871bb2dc7c8a36f6c21eba2d109435db6dfa1c1 Mon Sep 17 00:00:00 2001 From: squoshi <95097230+squoshi@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:07:37 -0500 Subject: [PATCH 1/2] Add hurtEnemy callback to ItemBuilder --- .../mods/kubejs/core/mixin/common/ItemMixin.java | 7 +++++++ .../dev/latvian/mods/kubejs/item/ItemBuilder.java | 15 +++++++++++++++ 2 files changed, 22 insertions(+) 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..508d638c8 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")) + private void hurtEnemy(ItemStack itemStack, LivingEntity livingEntity, LivingEntity livingEntity2, CallbackInfoReturnable cir) { + if (kjs$itemBuilder != null && kjs$itemBuilder.hurtEnemy != null) { + 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 From ea289f1a11d2f5ebd684dcd6daf1be9f942fc4d9 Mon Sep 17 00:00:00 2001 From: squoshi <95097230+squoshi@users.noreply.github.com> Date: Mon, 25 Mar 2024 15:24:07 -0500 Subject: [PATCH 2/2] Fix return value on hurtEnemy --- .../dev/latvian/mods/kubejs/core/mixin/common/ItemMixin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 508d638c8..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,10 +209,10 @@ private void releaseUsing(ItemStack itemStack, Level level, LivingEntity livingE } } - @Inject(method = "hurtEnemy", at = @At("HEAD")) + @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) { - kjs$itemBuilder.hurtEnemy.test(new ItemBuilder.HurtEnemyContext(itemStack, livingEntity, livingEntity2)); + cir.setReturnValue(kjs$itemBuilder.hurtEnemy.test(new ItemBuilder.HurtEnemyContext(itemStack, livingEntity, livingEntity2))); } }