buff(FightCastScope,EffectScope)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.7
cc 2
crap 2
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.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.castable.FightCastScope;
25
import fr.quatrevieux.araknemu.game.fight.castable.Castable;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect;
29
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
30
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
31
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
32
import org.checkerframework.checker.index.qual.NonNegative;
33
34
import java.util.function.BiConsumer;
35
36
/**
37
 * Steal a characteristic from a fighter to give to the caster
38
 * A configured effect id will be used for both add and remove characteristic buff effects
39
 *
40
 * This effect is equivalent of chaining {@link RemoveCharacteristicHandler} and {@link AddCharacteristicHandler} effects
41
 *
42
 * @see AlterCharacteristicHook
43
 */
44
public class StealCharacteristicHandler implements EffectHandler {
45
    private final AlterCharacteristicHook addHook;
46
    private final AlterCharacteristicHook removeHook;
47
    private final int addEffectId;
48
    private final int removeEffectId;
49
50
    /**
51
     * @param fight Fight where the effect takes effect
52
     * @param characteristic Characteristic to steal
53
     * @param addEffectId Spell effect id used as "add characteristic" effect
54
     * @param removeEffectId Spell effect id used as "remove characteristic" effect
55
     */
56
    public StealCharacteristicHandler(Fight fight, Characteristic characteristic, int addEffectId, int removeEffectId) {
57 1
        this(
58 1
            AlterCharacteristicHook.add(fight, characteristic), addEffectId,
59 1
            AlterCharacteristicHook.remove(fight, characteristic), removeEffectId
60
        );
61 1
    }
62
63
    /**
64
     * @param addHook Buff used for add characteristic to caster
65
     * @param addEffectId Spell effect id used as "add characteristic" effect
66
     * @param removeHook Buff used for remove characteristic from targets
67
     * @param removeEffectId Spell effect id used as "remove characteristic" effect
68
     */
69 1
    protected StealCharacteristicHandler(AlterCharacteristicHook addHook, int addEffectId, AlterCharacteristicHook removeHook, int removeEffectId) {
70 1
        this.addHook = addHook;
71 1
        this.removeHook = removeHook;
72 1
        this.addEffectId = addEffectId;
73 1
        this.removeEffectId = removeEffectId;
74 1
    }
75
76
    @Override
77
    public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
78 1
        throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff");
79
    }
80
81
    @Override
82
    public final void buff(FightCastScope cast, FightCastScope.EffectScope effect) {
83 1
        final SpellEffect spellEffect = effect.effect();
84 1
        final Fighter caster = cast.caster();
85 1
        final Castable action = cast.action();
86
87 1
        final Applier applier = new Applier(caster, spellEffect, action);
88
89 1
        EffectValue.forEachTargets(spellEffect, caster, effect.targets(), applier);
90
91 1
        if (applier.total() > 0) {
92 1
            caster.buffs().add(new Buff(
93 1
                BuffEffect.withCustomEffect(spellEffect, addEffectId, applier.total()),
94
                action,
95
                caster,
96
                caster,
97
                addHook
98
            ));
99
        }
100 1
    }
101
102
    /**
103
     * Apply removal effect to target
104
     */
105
    private class Applier implements BiConsumer<Fighter, EffectValue> {
106
        private final Fighter caster;
107
        private final SpellEffect spellEffect;
108
        private final Castable action;
109 1
        private @NonNegative int total = 0;
110
111 1
        public Applier(Fighter caster, SpellEffect spellEffect, Castable action) {
112 1
            this.caster = caster;
113 1
            this.spellEffect = spellEffect;
114 1
            this.action = action;
115 1
        }
116
117
        @Override
118
        public void accept(Fighter target, EffectValue effectValue) {
119 1
            final int value = effectValue.value();
120 1
            total += value;
121
122 1
            target.buffs().add(new Buff(
123 1
                BuffEffect.withCustomEffect(spellEffect, removeEffectId, value),
124
                action,
125
                caster,
126
                target,
127 1
                removeHook
128
            ));
129 1
        }
130
131
        /**
132
         * Total stolen characteristic value
133
         */
134
        public @NonNegative int total() {
135 1
            return total;
136
        }
137
    }
138
}
139