-
Notifications
You must be signed in to change notification settings - Fork 754
/
DecayModifier.java
71 lines (62 loc) · 3.42 KB
/
DecayModifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package slimeknights.tconstruct.tools.modifiers.traits.melee;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.phys.EntityHitResult;
import slimeknights.tconstruct.library.modifiers.Modifier;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierHooks;
import slimeknights.tconstruct.library.modifiers.hook.combat.MeleeHitModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.ranged.ProjectileHitModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.ranged.ProjectileLaunchModifierHook;
import slimeknights.tconstruct.library.module.ModuleHookMap.Builder;
import slimeknights.tconstruct.library.tools.context.ToolAttackContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ModifierNBT;
import slimeknights.tconstruct.library.tools.nbt.NamespacedNBT;
import javax.annotation.Nullable;
public class DecayModifier extends Modifier implements ProjectileLaunchModifierHook, ProjectileHitModifierHook, MeleeHitModifierHook {
/* gets the effect for the given level, including a random time */
private static MobEffectInstance makeDecayEffect(int level) {
// potions are 0 indexed instead of 1 indexed
// wither skeletons apply 10 seconds of wither for comparison
return new MobEffectInstance(MobEffects.WITHER, 20 * (5 + (RANDOM.nextInt(level * 3))), level - 1);
}
@Override
protected void registerHooks(Builder hookBuilder) {
super.registerHooks(hookBuilder);
hookBuilder.addHook(this, ModifierHooks.PROJECTILE_LAUNCH, ModifierHooks.PROJECTILE_HIT, ModifierHooks.MELEE_HIT);
}
@Override
public void afterMeleeHit(IToolStackView tool, ModifierEntry modifier, ToolAttackContext context, float damageDealt) {
if (context.isFullyCharged()) {
// note the time of each effect is calculated independently
// 25% chance to poison yourself
if (RANDOM.nextInt(3) == 0) {
context.getAttacker().addEffect(makeDecayEffect(modifier.getLevel()));
}
// always poison the target, means it works twice as often as lacerating
LivingEntity target = context.getLivingTarget();
if (target != null && target.isAlive()) {
target.addEffect(makeDecayEffect(modifier.getLevel()));
}
}
}
@Override
public boolean onProjectileHitEntity(ModifierNBT modifiers, NamespacedNBT persistentData, ModifierEntry modifier, Projectile projectile, EntityHitResult hit, @Nullable LivingEntity attacker, @Nullable LivingEntity target) {
if (target != null && (!(projectile instanceof AbstractArrow arrow) || arrow.isCritArrow())) {
// always poison the target, means it works twice as often as lacerating
target.addEffect(makeDecayEffect(modifier.getLevel()));
}
return false;
}
@Override
public void onProjectileLaunch(IToolStackView tool, ModifierEntry modifier, LivingEntity shooter, Projectile projectile, @Nullable AbstractArrow arrow, NamespacedNBT persistentData, boolean primary) {
if (primary && (arrow == null || arrow.isCritArrow()) && RANDOM.nextInt(3) == 0) {
// 25% chance to poison yourself
shooter.addEffect(makeDecayEffect(modifier.getLevel()));
}
}
}