Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
Trigger cooldown for ActionOnProjectileHitPower upon hitting a block …
Browse files Browse the repository at this point in the history
…if an action has triggered.
  • Loading branch information
MerchantPug committed Dec 11, 2023
1 parent c5916ba commit 8107789
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ public abstract class ProjectileEntityMixin implements ProjectileEntityAccess {
});
}

@Inject(method = "onHit", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/Projectile;onHitBlock(Lnet/minecraft/world/phys/BlockHitResult;)V"))
private void apugli$handleProjectileCooldown(HitResult hitResult, CallbackInfo ci) {
if (this.getOwner() instanceof LivingEntity living) {
var aophPowers = Services.POWER.getPowers(living, ApugliPowers.ACTION_ON_PROJECTILE_HIT.get(), true);
aophPowers.forEach(power -> {
if (ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().canUse(power, living) && this.apugli$powersThatHaveLanded().contains(Services.POWER.getPowerId(power))) {
ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().use(power, living);
}
});
this.apugli$entitiesHit.clear();
}
}

@Override
public Set<ResourceLocation> apugli$powersThatHaveLanded() {
return apugli$entitiesHit.keySet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,25 @@ public static void onMobTargetChange(LivingChangeTargetEvent event) {

@SubscribeEvent
public static void onProjectileImpact(ProjectileImpactEvent event) {
if (event.getRayTraceResult().getType() != HitResult.Type.ENTITY) return;

if (((EntityHitResult)event.getRayTraceResult()).getEntity() instanceof LivingEntity living)
Services.POWER.getPowers(living, ApugliPowers.ACTION_WHEN_PROJECTILE_HIT.get()).forEach(power -> ApugliPowers.ACTION_WHEN_PROJECTILE_HIT.get().execute(power, living, event.getProjectile()));
if ((event.getProjectile().getOwner() instanceof LivingEntity living))
Services.POWER.getPowers(living, ApugliPowers.ACTION_ON_PROJECTILE_HIT.get()).forEach(power -> {
event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).ifPresent(cap -> cap.addHitEntity(Services.POWER.getPowerId(power)));
ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().execute(power, living, ((EntityHitResult)event.getRayTraceResult()).getEntity(), event.getProjectile(), event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).map(cap -> cap.getPowerValue(Services.POWER.getPowerId(power))).orElse(0));
});
if (event.getRayTraceResult().getType() == HitResult.Type.ENTITY) {
if (((EntityHitResult)event.getRayTraceResult()).getEntity() instanceof LivingEntity living)
Services.POWER.getPowers(living, ApugliPowers.ACTION_WHEN_PROJECTILE_HIT.get()).forEach(power -> ApugliPowers.ACTION_WHEN_PROJECTILE_HIT.get().execute(power, living, event.getProjectile()));
if ((event.getProjectile().getOwner() instanceof LivingEntity living))
Services.POWER.getPowers(living, ApugliPowers.ACTION_ON_PROJECTILE_HIT.get()).forEach(power -> {
event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).ifPresent(cap -> cap.addHitEntity(Services.POWER.getPowerId(power)));
ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().execute(power, living, ((EntityHitResult)event.getRayTraceResult()).getEntity(), event.getProjectile(), event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).map(cap -> cap.getPowerValue(Services.POWER.getPowerId(power))).orElse(0));
});
} else if (event.getRayTraceResult().getType() == HitResult.Type.BLOCK) {
if (event.getProjectile().getOwner() instanceof LivingEntity living) {
var aophPowers = Services.POWER.getPowers(living, ApugliPowers.ACTION_ON_PROJECTILE_HIT.get(), true);
aophPowers.forEach(power -> {
if (ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().canUse(power, living) && event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).map(inst -> inst.apugli$powersThatHaveLanded().contains(Services.POWER.getPowerId(power))).orElse(false)) {
ApugliPowers.ACTION_ON_PROJECTILE_HIT.get().use(power, living);
}
});
event.getProjectile().getCapability(EntitiesHitCapability.INSTANCE).ifPresent(EntitiesHitCapability::clearHitEntities);
}
}
}

// Lowest so it can execute after any damage modifications.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public int getPowerValue(ResourceLocation value) {
public void addHitEntity(ResourceLocation value) {
this.apugli$entitiesHit.compute(value, (resourceLocation, integer) -> integer != null ? integer + 1 : 1);
}

@Override
public void clearHitEntities() {
this.apugli$entitiesHit.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface IEntitiesHitCapability extends ProjectileEntityAccess {

void addHitEntity(ResourceLocation value);

void clearHitEntities();

}

0 comments on commit 8107789

Please sign in to comment.