apply(ExplorationPlayer)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
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.exploration.npc.dialog.action.teleport;
21
22
import fr.quatrevieux.araknemu.data.value.Position;
23
import fr.quatrevieux.araknemu.data.world.entity.environment.npc.ResponseAction;
24
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
25
import fr.quatrevieux.araknemu.game.exploration.interaction.action.move.ChangeMap;
26
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
27
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMapService;
28
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.Action;
29
import fr.quatrevieux.araknemu.game.exploration.npc.dialog.action.ActionFactory;
30
import fr.quatrevieux.araknemu.util.Splitter;
31
32
/**
33
 * Teleport to the given position
34
 *
35
 * Arguments : [mapid],[cellid],[cinematic]
36
 *
37
 * Cinematic is not required (if not set, or set to 0, no cinematic will be displayed)
38
 */
39
public final class Teleport implements Action {
40
    private final ExplorationMapService service;
41
    private final Position position;
42
    private final int cinematic;
43
44 1
    public Teleport(ExplorationMapService service, Position position, int cinematic) {
45 1
        this.service = service;
46 1
        this.position = position;
47 1
        this.cinematic = cinematic;
48 1
    }
49
50
    @Override
51
    public boolean check(ExplorationPlayer player) {
52
        // @todo check if map exists ?
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
53 1
        return true;
54
    }
55
56
    @Override
57
    public void apply(ExplorationPlayer player) {
58 1
        final ExplorationMap map = service.load(position.map());
59
60 1
        if (position.cell() >= map.size()) {
61 1
            throw new IllegalArgumentException("Invalid cell for teleport target " + position);
62
        }
63
64 1
        player.interactions().push(new ChangeMap(player, map, position.cell(), cinematic));
65 1
    }
66
67
    public static final class Factory implements ActionFactory {
68
        private final ExplorationMapService service;
69
70 1
        public Factory(ExplorationMapService service) {
71 1
            this.service = service;
72 1
        }
73
74
        @Override
75
        public String type() {
76 1
            return "TELEPORT";
77
        }
78
79
        @Override
80
        public Action create(ResponseAction entity) {
81 1
            final Splitter splitter = new Splitter(entity.arguments(), ',');
82
83 1
            return new Teleport(
84
                service,
85
                new Position(
86 1
                    splitter.nextNonNegativeInt(),
87 1
                    splitter.nextNonNegativeInt()
88
                ),
89 1
                splitter.nextIntOrDefault(0)
90
            );
91
        }
92
    }
93
}
94