Skip to content

Commit

Permalink
Make the modify damage hook not require shields to be blocking
Browse files Browse the repository at this point in the history
Due to timing, this hook will not run often as blocking means the damage does not happen
Plus, all usages right now are armor exclusive
  • Loading branch information
KnightMiner committed May 5, 2024
1 parent 587465c commit 37a2d89
Showing 1 changed file with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.module.ModuleHook;
import slimeknights.tconstruct.library.tools.context.EquipmentContext;
import slimeknights.tconstruct.library.tools.definition.ModifiableArmorMaterial;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.utils.Util;

import java.util.Collection;

Expand Down Expand Up @@ -50,20 +47,6 @@ public float modifyDamageTaken(IToolStackView tool, ModifierEntry modifier, Equi
}
}

/** Internal logic for {@link #modifyDamageTaken(ModuleHook, EquipmentContext, DamageSource, float, boolean)} */
private static float modifyDamageTaken(ModuleHook<ModifyDamageModifierHook> hook, EquipmentContext context, DamageSource source, float amount, boolean isDirectDamage, EquipmentSlot slotType) {
IToolStackView toolStack = context.getToolInSlot(slotType);
if (toolStack != null && !toolStack.isBroken()) {
for (ModifierEntry entry : toolStack.getModifierList()) {
amount = entry.getHook(hook).modifyDamageTaken(toolStack, entry, context, slotType, source, amount, isDirectDamage);
if (amount < 0) {
return 0;
}
}
}
return amount;
}

/**
* Allows modifiers to respond to the entity being attacked
* @param hook Hook to use
Expand All @@ -73,18 +56,17 @@ private static float modifyDamageTaken(ModuleHook<ModifyDamageModifierHook> hook
* @param isDirectDamage If true, the damage source is applying directly
*/
static float modifyDamageTaken(ModuleHook<ModifyDamageModifierHook> hook, EquipmentContext context, DamageSource source, float amount, boolean isDirectDamage) {
// first we need to determine if any of the four slots want to cancel the event, then we need to determine if any want to respond assuming its not canceled
for (EquipmentSlot slotType : ModifiableArmorMaterial.ARMOR_SLOTS) {
amount = modifyDamageTaken(hook, context, source, amount, isDirectDamage, slotType);
if (amount <= 0) {
return 0;
for (EquipmentSlot slotType : EquipmentSlot.values()) {
IToolStackView toolStack = context.getToolInSlot(slotType);
if (toolStack != null && !toolStack.isBroken()) {
for (ModifierEntry entry : toolStack.getModifierList()) {
amount = entry.getHook(hook).modifyDamageTaken(toolStack, entry, context, slotType, source, amount, isDirectDamage);
if (amount < 0) {
return 0;
}
}
}
}
// shields only run this hook when blocking
LivingEntity entity = context.getEntity();
if (entity.isBlocking()) {
amount = modifyDamageTaken(hook, context, source, amount, isDirectDamage, Util.getSlotType(entity.getUsedItemHand()));
}
return amount;
}
}

0 comments on commit 37a2d89

Please sign in to comment.