Test Failed
Branch master (8e433c)
by Vincent
18:50
created

onLifeAltered(int)   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
c 0
b 0
f 0
cc 2
ccs 2
cts 2
cp 1
crap 2
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.buff;
21
22
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
23
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
24
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
25
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
26
import fr.quatrevieux.araknemu.network.game.out.fight.AddBuff;
27
28
import java.util.Collection;
29
import java.util.Iterator;
30
import java.util.LinkedList;
31
import java.util.function.Predicate;
32
import java.util.stream.Stream;
33
import java.util.stream.StreamSupport;
34
35
/**
36
 * Handle buff list for a fighter
37
 */
38
public final class BuffList implements Iterable<Buff>, Buffs {
39
    private final Fighter fighter;
40 1
    private final Collection<Buff> buffs = new LinkedList<>();
41
42 1
    public BuffList(Fighter fighter) {
43 1
        this.fighter = fighter;
44 1
    }
45
46
    @Override
47
    public Iterator<Buff> iterator() {
48 1
        return buffs.iterator();
49
    }
50
51
    /**
52
     * Get the Buff stream
53
     */
54
    public Stream<Buff> stream() {
55 1
        return StreamSupport.stream(spliterator(), false);
56
    }
57
58
    @Override
59
    public void add(Buff buff) {
60 1
        buffs.add(buff);
61 1
        buff.hook().onBuffStarted(buff);
62
63 1
        if (buff.remainingTurns() == 0) {
64 1
            buff.incrementRemainingTurns();
65
        }
66
67 1
        fighter.fight().send(new AddBuff(buff));
68
69
        // Add one turn on self-buff
70 1
        if (fighter.equals(buff.caster())) {
71 1
            buff.incrementRemainingTurns();
72
        }
73 1
    }
74
75
    @Override
76
    public boolean onStartTurn() {
77 1
        boolean result = true;
78
79 1
        for (Buff buff : buffs) {
80 1
            result &= buff.hook().onStartTurn(buff);
81 1
        }
82
83 1
        return result;
84
    }
85
86
    @Override
87
    public void onEndTurn() {
88 1
        for (Buff buff : buffs) {
89 1
            buff.hook().onEndTurn(buff);
90 1
        }
91 1
    }
92
93
    @Override
94
    public void onCastTarget(CastScope cast) {
95 1
        for (Buff buff : buffs) {
96 1
            buff.hook().onCastTarget(buff, cast);
97 1
        }
98 1
    }
99
100
    @Override
101
    public void onDamage(Damage value) {
102 1
        for (Buff buff : buffs) {
103 1
            buff.hook().onDamage(buff, value);
104 1
        }
105 1
    }
106
107
    @Override
108
    public void onLifeAltered(int value) {
109 1
        for (Buff buff : buffs) {
110 1
            buff.hook().onLifeAltered(buff, value);
111 1
        }
112 1
    }
113
114
    @Override
115
    public void refresh() {
116 1
        removeIf(buff -> {
117 1
            buff.decrementRemainingTurns();
118
119 1
            return !buff.valid();
120
        });
121 1
    }
122
123
    @Override
124
    public boolean removeAll() {
125 1
        return removeIf(Buff::canBeDispelled);
126
    }
127
128
    @Override
129
    public boolean removeByCaster(PassiveFighter caster) {
130 1
        return removeIf(buff -> buff.caster().equals(caster));
131
    }
132
133
    /**
134
     * Remove buff by a predicate
135
     *
136
     * @param predicate Takes the buff as parameter, and return true to delete (and terminate) the buff
137
     *
138
     * @return true if there is a change (i.e. a buff is terminated)
139
     */
140
    private boolean removeIf(Predicate<Buff> predicate) {
141 1
        final Iterator<Buff> iterator = buffs.iterator();
142
143 1
        boolean hasChanged = false;
144
145 1
        while (iterator.hasNext()) {
146 1
            final Buff buff = iterator.next();
147
148 1
            if (predicate.test(buff)) {
149 1
                iterator.remove();
150 1
                buff.hook().onBuffTerminated(buff);
151 1
                hasChanged = true;
152
            }
153 1
        }
154
155 1
        return hasChanged;
156
    }
157
}
158