Skip to content

Commit

Permalink
Add piecing support to arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Dec 18, 2022
1 parent b89b0c1 commit 2059b98
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"type": "tconstruct:modifier_salvage",
"tools": {
"tag": "tconstruct:modifiable/melee"
},
"tools": [
{
"tag": "tconstruct:modifiable/melee"
},
{
"tag": "tconstruct:modifiable/ranged/bows"
}
],
"slots": {
"upgrades": 1
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
},
"amount_per_item": 1,
"needed_per_level": 25,
"tools": {
"tag": "tconstruct:modifiable/melee"
},
"tools": [
{
"tag": "tconstruct:modifiable/melee"
},
{
"tag": "tconstruct:modifiable/ranged/bows"
}
],
"slots": {
"upgrades": 1
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,10 @@ public static boolean attackEntitySecondary(DamageSource source, float damage, E
}

// set hurt resistance time to 0 because we always want to deal damage in traits
int lastInvulnerableTime = target.invulnerableTime;
target.invulnerableTime = 0;
boolean hit = target.hurt(source, damage);
target.invulnerableTime = lastInvulnerableTime; // reset to the old time so bows work right
// set total received damage, important for AI and stuff
if (living != null) {
living.lastHurt += oldLastDamage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private void addModifierRecipes(Consumer<FinishedRecipe> consumer) {
* damage boost
*/
IncrementalModifierRecipeBuilder.modifier(TinkerModifiers.piercing)
.setTools(TinkerTags.Items.MELEE)
.setTools(ingredientFromTags(TinkerTags.Items.MELEE, TinkerTags.Items.BOWS))
.setInput(Blocks.CACTUS, 1, 25)
.setMaxLevel(5) // +2.5 pierce damage
.setSlots(SlotType.UPGRADE, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.phys.EntityHitResult;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.TinkerHooks;
import slimeknights.tconstruct.library.modifiers.hook.ArrowLaunchModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.ProjectileHitModifierHook;
import slimeknights.tconstruct.library.modifiers.impl.IncrementalModifier;
import slimeknights.tconstruct.library.modifiers.util.ModifierHookMap.Builder;
import slimeknights.tconstruct.library.tools.context.ToolAttackContext;
import slimeknights.tconstruct.library.tools.context.ToolRebuildContext;
import slimeknights.tconstruct.library.tools.helper.ToolAttackUtil;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModDataNBT;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;
import slimeknights.tconstruct.library.tools.nbt.NamespacedNBT;
import slimeknights.tconstruct.library.tools.stat.ModifierStatsBuilder;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.library.utils.TooltipKey;

import javax.annotation.Nullable;
import java.util.List;

public class PiercingModifier extends IncrementalModifier {
public class PiercingModifier extends IncrementalModifier implements ProjectileHitModifierHook, ArrowLaunchModifierHook {
private static final ResourceLocation PIERCING_DEBUFF = TConstruct.getResource("piercing_debuff");

@Override
protected void registerHooks(Builder hookBuilder) {
hookBuilder.addHook(this, TinkerHooks.PROJECTILE_HIT, TinkerHooks.ARROW_LAUNCH);
}

@Override
public void addVolatileData(ToolRebuildContext context, int level, ModDataNBT volatileData) {
float toRemove = 0.5f * getScaledLevel(context, level);
Expand Down Expand Up @@ -56,6 +72,27 @@ public int afterEntityHit(IToolStackView tool, int level, ToolAttackContext cont
return 0;
}

@Override
public void onArrowLaunch(IToolStackView tool, ModifierEntry modifier, LivingEntity shooter, AbstractArrow arrow, NamespacedNBT persistentData) {
// store the float level as we don't have access to the incremental level in the projectile hit hook
persistentData.putFloat(getId(), modifier.getEffectiveLevel(tool));
}

@Override
public boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, EntityHitResult hit, @Nullable LivingEntity attacker, @Nullable LivingEntity target) {
// deals 1 pierce damage per level
DamageSource source;
if (attacker instanceof Player player) {
source = DamageSource.playerAttack(player).bypassArmor();
} else if (attacker != null) {
source = DamageSource.mobAttack(attacker).bypassArmor();
} else {
source = DamageSource.GENERIC;
}
ToolAttackUtil.attackEntitySecondary(source, persistentData.getFloat(getId()), hit.getEntity(), target, true);
return false;
}

@Override
public void addInformation(IToolStackView tool, int level, @Nullable Player player, List<Component> tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) {
addDamageTooltip(tool, getScaledLevel(tool, level) - tool.getVolatileData().getFloat(PIERCING_DEBUFF), tooltip);
Expand Down

0 comments on commit 2059b98

Please sign in to comment.