Skip to content

Commit

Permalink
Merge pull request #812 from squoshi/1902
Browse files Browse the repository at this point in the history
Add hurtEnemy callback to ItemBuilder
  • Loading branch information
LatvianModder authored Mar 26, 2024
2 parents 26d9636 + ea289f1 commit 178baae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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) {
Expand Down
15 changes: 15 additions & 0 deletions common/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down Expand Up @@ -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<HurtEnemyContext> hurtEnemy;

public String texture;
public String parentModel;
Expand Down Expand Up @@ -140,6 +142,7 @@ public ItemBuilder(ResourceLocation i) {
finishUsing = null;
releaseUsing = null;
fireResistant = false;
hurtEnemy = null;
}

@Override
Expand Down Expand Up @@ -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<HurtEnemyContext> context) {
this.hurtEnemy = context;
return this;
}

@FunctionalInterface
public interface UseCallback {
boolean use(Level level, Player player, InteractionHand interactionHand);
Expand All @@ -464,4 +477,6 @@ public interface ReleaseUsingCallback {
public interface NameCallback {
Component apply(ItemStack itemStack);
}

public record HurtEnemyContext(ItemStack getItem, LivingEntity getTarget, LivingEntity getAttacker) {}
}

0 comments on commit 178baae

Please sign in to comment.