Passed
Pull Request — master (#222)
by Vincent
12:26
created

supportsElement(Buff,Element)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
cp 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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.armor;
21
22
import fr.quatrevieux.araknemu.data.constant.Characteristic;
23
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.Element;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
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.castable.effect.handler.damage.Damage;
29
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
30
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
31
import fr.quatrevieux.araknemu.game.world.creature.characteristics.Characteristics;
32
33
/**
34
 * Handle reducing damages
35
 */
36 1
public final class ReduceDamageHandler implements EffectHandler, BuffHook {
37
    @Override
38
    public void handle(CastScope cast, CastScope.EffectScope effect) {
39 1
        throw new UnsupportedOperationException("ReduceDamageHandler can only be used as buff");
40
    }
41
42
    @Override
43
    public void buff(CastScope cast, CastScope.EffectScope effect) {
44 1
        for (PassiveFighter target : effect.targets()) {
45 1
            target.buffs().add(new Buff(effect.effect(), cast.action(), cast.caster(), target, this));
46 1
        }
47 1
    }
48
49
    @Override
50
    public void onDirectDamage(Buff buff, ActiveFighter caster, Damage value) {
51 1
        if (!supportsElement(buff, value.element())) {
52 1
            return;
53
        }
54
55 1
        final Characteristics characteristics = buff.target().characteristics();
56
57 1
        final int boost = 200 + characteristics.get(Characteristic.INTELLIGENCE) + characteristics.get(value.element().boost());
58 1
        final int reduce = buff.effect().min() * boost / 200;
59
60 1
        if (reduce < 0) {
61 1
            return;
62
        }
63
64 1
        value.reduce(reduce);
65 1
    }
66
67
    /**
68
     * Check if the armor supports the damage element
69
     *
70
     * @param buff The buff
71
     * @param element The damage element
72
     *
73
     * @return true if the armor supports the element
74
     */
75
    private boolean supportsElement(Buff buff, Element element) {
76 1
        return buff.effect().special() == 0 || Element.fromBitSet(buff.effect().special()).contains(element);
77
    }
78
}
79