handle(FightCastScope,EffectScope)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 1
crap 1
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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.characteristic;
21
22
import fr.quatrevieux.araknemu.game.fight.castable.FightCastScope;
23
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
28
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
29
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
30
31
/**
32
 * Alter a characteristic with buff effect
33
 */
34
public abstract class AbstractAlterCharacteristicHandler implements EffectHandler, BuffHook {
35
    private final AlterCharacteristicHook hook;
36
    private final boolean canBeDispelled;
37
38
    /**
39
     * @param hook Buff hook which handle characteristic change
40
     * @param canBeDispelled Added buff effect can be dispelled. See: {@link Buff#canBeDispelled()}
41
     */
42 1
    public AbstractAlterCharacteristicHandler(AlterCharacteristicHook hook, boolean canBeDispelled) {
43 1
        this.hook = hook;
44 1
        this.canBeDispelled = canBeDispelled;
45 1
    }
46
47
    @Override
48
    public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
49 1
        throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff");
50
    }
51
52
    @Override
53
    public void buff(FightCastScope cast, FightCastScope.EffectScope effect) {
54 1
        final SpellEffect spellEffect = effect.effect();
55 1
        final Fighter caster = cast.caster();
56
57 1
        EffectValue.forEachTargets(
58
            spellEffect,
59
            caster,
60 1
            effect.targets(),
61 1
            (target, effectValue) -> target.buffs().add(new Buff(
62 1
                BuffEffect.fixed(spellEffect, effectValue.value()),
63 1
                cast.action(),
64
                caster,
65
                target,
66
                this,
67
                canBeDispelled
68
            ))
69
        );
70 1
    }
71
72
    @Override
73
    public void onBuffStarted(Buff buff) {
74 1
        hook.onBuffStarted(buff);
75 1
    }
76
77
    @Override
78
    public void onBuffTerminated(Buff buff) {
79 1
        hook.onBuffTerminated(buff);
80 1
    }
81
}
82