Skip to content

Commit

Permalink
Make sure projectile hit considers multipart entities
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Dec 27, 2022
1 parent c9f325a commit 725e1ee
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ public static boolean attackEntity(IToolStackView tool, LivingEntity attackerLiv
return attackEntity(tool, attackerLiving, hand, targetEntity, cooldownFunction, isExtraAttack, Util.getSlotType(hand));
}

/**
* Gets a living entity from the given entity, getting the parent if needed
* @param entity Entity instance
* @return Living entity, or null if its not living
*/
@Nullable
public static LivingEntity getLivingEntity(Entity entity) {
if (entity instanceof PartEntity<?> part) {
entity = part.getParent();
}
return entity instanceof LivingEntity living ? living : null;
}

/**
* Base attack logic, used by normal attacks, projectiles, and extra attacks.
* Based on {@link Player#attack(Entity)}
Expand All @@ -159,15 +172,7 @@ public static boolean attackEntity(IToolStackView tool, LivingEntity attackerLiv
}

// fetch relevant entities
LivingEntity targetLiving = null;
if (targetEntity instanceof LivingEntity) {
targetLiving = (LivingEntity) targetEntity;
} else if (targetEntity instanceof PartEntity) {
Entity parent = ((PartEntity<?>) targetEntity).getParent();
if (parent instanceof LivingEntity) {
targetLiving = (LivingEntity)parent;
}
}
LivingEntity targetLiving = getLivingEntity(targetEntity);
Player attackerPlayer = null;
if (attackerLiving instanceof Player player) {
attackerPlayer = player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import slimeknights.tconstruct.library.tools.definition.ModifiableArmorMaterial;
import slimeknights.tconstruct.library.tools.helper.ArmorUtil;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolAttackUtil;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;
Expand Down Expand Up @@ -410,7 +411,7 @@ static void projectileHit(ProjectileImpactEvent event) {
// yes, hardcoded to enderference, if you need your own enderference for whatever reason, talk to us
if (entityHit.getEntity().getType() != EntityType.ENDERMAN || modifiers.getLevel(TinkerModifiers.enderference.getId()) > 0) {
// extract a living target as that is the most common need
LivingEntity target = entityHit.getEntity() instanceof LivingEntity l ? l : null;
LivingEntity target = ToolAttackUtil.getLivingEntity(entityHit.getEntity());
for (ModifierEntry entry : modifiers.getModifiers()) {
if (entry.getHook(TinkerHooks.PROJECTILE_HIT).onProjectileHitEntity(modifiers, nbt, entry, projectile, entityHit, attacker, target)) {
event.setCanceled(true);
Expand Down

0 comments on commit 725e1ee

Please sign in to comment.