Passed
Pull Request — master (#148)
by
unknown
10:40
created

MonsterInvocationModule(MonsterService,Fight)   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
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-2021 Vincent Quatrevieux Jean-Alexandre Valentin
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.module;
21
22
import fr.quatrevieux.araknemu.core.event.Listener;
23
import fr.quatrevieux.araknemu.game.fight.Fight;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectsHandler;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.invocations.MonsterInvocationHandler;
26
import fr.quatrevieux.araknemu.game.fight.event.FightStopped;
27
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
28
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterDie;
29
import fr.quatrevieux.araknemu.game.monster.MonsterService;
30
31
public class MonsterInvocationModule implements FightModule {
32
    final private MonsterService monsterService;
0 ignored issues
show
introduced by
'private' modifier out of order with the JLS suggestions.
Loading history...
33
    final private Fight fight;
0 ignored issues
show
introduced by
'private' modifier out of order with the JLS suggestions.
Loading history...
34
35 1
    public MonsterInvocationModule(MonsterService monsterService, Fight fight) {
36 1
        this.monsterService = monsterService;
37 1
        this.fight = fight;
38 1
    }
39
40
    @Override
41
    public void effects(EffectsHandler handler) {
42
        // moving creatures
43 1
        handler.register(181, new MonsterInvocationHandler(monsterService, fight));
44 1
    }
45
46
    @Override
47
    public Listener[] listeners() {
48 1
        return new Listener[] {
49 1
            new Listener<FighterDie>() {
50
                @Override
51
                public void on(FighterDie event) {
52 1
                    fight.fighters().forEach(fighter -> {
53 1
                        if (fighter.invoker().isPresent() && fighter.invoker().get().equals(event.fighter())) {
54
                            fighter.life().kill((ActiveFighter) event.fighter());
55
                            fight.turnList().remove(fighter);
56
                        }
57 1
                    });
58 1
                }
59
60
                @Override
61
                public Class<FighterDie> event() {
62 1
                    return FighterDie.class;
63
                }
64
            },
65
66 1
            new Listener<FightStopped>() {
67
                @Override
68
                public void on(FightStopped event) {
69 1
                    event.fight().fighters().forEach(fighter -> {
70 1
                        if (fighter.invoker().isPresent()) {
71
                            fighter.team().kick(fighter);
72
                        }
73 1
                    });
74 1
                }
75
76
                @Override
77
                public Class<FightStopped> event() {
78 1
                    return FightStopped.class;
79
                }
80
            },
81
        };
82
    }
83
}
84