last analyzed

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 4
ccs 2
cts 2
cp 1
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.handler.dialog;
21
22
import fr.quatrevieux.araknemu.core.network.exception.ErrorPacket;
23
import fr.quatrevieux.araknemu.core.network.parser.PacketHandler;
24
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer;
25
import fr.quatrevieux.araknemu.game.exploration.creature.Operation;
26
import fr.quatrevieux.araknemu.game.exploration.interaction.dialog.NpcDialog;
27
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap;
28
import fr.quatrevieux.araknemu.game.exploration.npc.GameNpc;
29
import fr.quatrevieux.araknemu.network.game.GameSession;
30
import fr.quatrevieux.araknemu.network.game.in.dialog.CreateDialogRequest;
31
import fr.quatrevieux.araknemu.network.game.out.basic.Noop;
32
import org.checkerframework.checker.nullness.util.NullnessUtil;
33
34
/**
35
 * Start a new dialog with NPC
36
 */
37 1
public final class StartDialog implements PacketHandler<GameSession, CreateDialogRequest> {
38
    @Override
39
    public void handle(GameSession session, CreateDialogRequest packet) {
40 1
        final ExplorationPlayer exploration = NullnessUtil.castNonNull(session.exploration());
41 1
        final ExplorationMap map = exploration.map();
42
43 1
        if (map == null) {
44 1
            throw new ErrorPacket(new Noop());
45
        }
46
47 1
        map.creature(packet.npcId()).apply(new Operation<Void>() {
48
            @Override
49
            public Void onExplorationPlayer(ExplorationPlayer player) {
50 1
                throw new IllegalArgumentException("Cannot start a dialog with a player");
51
            }
52
53
            @Override
54
            public Void onNpc(GameNpc npc) {
55 1
                exploration.interactions().start(new NpcDialog(exploration, npc));
56 1
                return null;
57
            }
58
        });
59 1
    }
60
61
    @Override
62
    public Class<CreateDialogRequest> packet() {
63 1
        return CreateDialogRequest.class;
64
    }
65
}
66