Passed
Push — master ( 552ecc...4814bf )
by Vincent
08:27 queued 12s
created

handle(CastScope,EffectScope)   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
c 1
b 0
f 0
cc 1
crap 1
rs 10
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2022 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.characteristic;
21
22
import fr.quatrevieux.araknemu.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
29
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
30
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
31
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
32
import fr.quatrevieux.araknemu.network.game.out.fight.action.ActionEffect;
33
34
/**
35
 * Alter vitality with buff effect
36
 * The effect will be added to current and max fighter life
37
 *
38
 * @see fr.quatrevieux.araknemu.game.fight.fighter.FighterLife#alterMax(fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter, int)
39
 */
40
public final class AlterVitalityHandler implements EffectHandler, BuffHook {
41
    private final Fight fight;
42
    private final int multiplier;
43
    private final boolean canBeDispelled;
44
45
    /**
46
     * @param fight Active fight
47
     * @param multiplier 1 for add vitality, -1 for remove
48
     * @param canBeDispelled Added buff effect can be dispelled. See: {@link Buff#canBeDispelled()}
49
     */
50 1
    public AlterVitalityHandler(Fight fight, int multiplier, boolean canBeDispelled) {
51 1
        this.fight = fight;
52 1
        this.multiplier = multiplier;
53 1
        this.canBeDispelled = canBeDispelled;
54 1
    }
55
56
    @Override
57
    public void handle(CastScope cast, CastScope.EffectScope effect) {
58 1
        throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff");
59
    }
60
61
    @Override
62
    public void buff(CastScope cast, CastScope.EffectScope effect) {
63 1
        final SpellEffect spellEffect = effect.effect();
64 1
        final ActiveFighter caster = cast.caster();
65
66 1
        EffectValue.forEachTargets(
67
            spellEffect,
68
            caster,
69 1
            effect.targets(),
70 1
            (target, effectValue) -> target.buffs().add(new Buff(
71 1
                BuffEffect.fixed(spellEffect, effectValue.value()),
72 1
                cast.action(),
73
                caster,
74
                target,
75
                this,
76
                canBeDispelled
77
            ))
78
        );
79 1
    }
80
81
    @Override
82
    public void onBuffStarted(Buff buff) {
83 1
        final int value = buff.effect().min() * multiplier;
84
85 1
        buff.target().characteristics().alter(Characteristic.VITALITY, value);
86 1
        fight.send(ActionEffect.buff(buff, buff.effect().min()));
87 1
        buff.target().life().alterMax(buff.caster(), value);
88 1
    }
89
90
    @Override
91
    public void onBuffTerminated(Buff buff) {
92 1
        final int value = -buff.effect().min() * multiplier;
93
94 1
        buff.target().characteristics().alter(Characteristic.VITALITY, value);
95 1
        buff.target().life().alterMax(buff.caster(), value);
96 1
    }
97
98
    /**
99
     * Create a dispellable add vitality effect
100
     */
101
    public static AlterVitalityHandler add(Fight fight) {
102 1
        return new AlterVitalityHandler(fight, 1, true);
103
    }
104
105
    /**
106
     * Create a not dispellable add vitality effect
107
     */
108
    public static AlterVitalityHandler addNotDispellable(Fight fight) {
109 1
        return new AlterVitalityHandler(fight, 1, true);
110
    }
111
112
    /**
113
     * Create a dispellable remove vitality effect
114
     */
115
    public static AlterVitalityHandler remove(Fight fight) {
116 1
        return new AlterVitalityHandler(fight, -1, true);
117
    }
118
}
119