Passed
Push — master ( a1b3f0...93435f )
by Vincent
06:31 queued 23s
created

ApplyItemOperation(UsableItem,ExplorationPlayer,ExplorationMapCell)   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
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.object;
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.map.ExplorationMap;
27
import fr.quatrevieux.araknemu.game.exploration.map.cell.ExplorationMapCell;
28
import fr.quatrevieux.araknemu.game.exploration.npc.GameNpc;
29
import fr.quatrevieux.araknemu.game.item.type.UsableItem;
30
import fr.quatrevieux.araknemu.game.player.GamePlayer;
31
import fr.quatrevieux.araknemu.game.player.inventory.InventoryEntry;
32
import fr.quatrevieux.araknemu.network.game.GameSession;
33
import fr.quatrevieux.araknemu.network.game.in.object.ObjectUseRequest;
34
import fr.quatrevieux.araknemu.network.game.out.basic.Noop;
35
import fr.quatrevieux.araknemu.network.game.out.info.Error;
36
import org.checkerframework.checker.nullness.qual.Nullable;
37
import org.checkerframework.checker.nullness.util.NullnessUtil;
38
39
/**
40
 * Use an object
41
 */
42 1
public final class UseObject implements PacketHandler<GameSession, ObjectUseRequest> {
43
    @Override
44
    public void handle(GameSession session, ObjectUseRequest packet) throws Exception {
45 1
        final GamePlayer player = NullnessUtil.castNonNull(session.player());
46
47 1
        if (!player.restrictions().canUseObject()) {
48 1
            throw new ErrorPacket(Error.cantDoOnCurrentState());
49
        }
50
51 1
        final InventoryEntry entry = player.inventory().get(packet.objectId());
52 1
        final UsableItem item = UsableItem.class.cast(entry.item());
53
54 1
        boolean result = true;
55
56
        try {
57 1
            result = packet.isTarget()
58 1
                ? handleForTarget(NullnessUtil.castNonNull(session.exploration()), item, packet)
59 1
                : handleForSelf(NullnessUtil.castNonNull(session.exploration()), item)
60
            ;
61
        } finally {
62 1
            if (result) {
63 1
                entry.remove(1);
64
            } else {
65 1
                session.send(new Noop());
66
            }
67
        }
68 1
    }
69
70
    @Override
71
    public Class<ObjectUseRequest> packet() {
72 1
        return ObjectUseRequest.class;
73
    }
74
75
    private boolean handleForSelf(ExplorationPlayer exploration, UsableItem item) {
76 1
        if (!item.check(exploration)) {
77 1
            return false;
78
        }
79
80 1
        item.apply(exploration);
81 1
        return true;
82
    }
83
84
    private boolean handleForTarget(ExplorationPlayer exploration, UsableItem item, ObjectUseRequest packet) {
85 1
        final ExplorationMap map = exploration.map();
86
87 1
        if (map == null || packet.cell() >= map.size()) {
88 1
            return false;
89
        }
90
91 1
        final ExplorationMapCell cell = packet.cell() != -1 ? map.get(packet.cell()) : null;
92 1
        final ApplyItemOperation operation = new ApplyItemOperation(item, exploration, cell);
93
94 1
        if (map.has(packet.target())) {
95 1
            map.creature(packet.target()).apply(operation);
96
        } else {
97 1
            operation.onNull();
98
        }
99
100 1
        return operation.success;
101
    }
102
103
    private static class ApplyItemOperation implements Operation {
104
        private final UsableItem item;
105
        private final ExplorationPlayer caster;
106
        private final @Nullable ExplorationMapCell targetCell;
107
108 1
        private boolean success = false;
109
110 1
        public ApplyItemOperation(UsableItem item, ExplorationPlayer caster, @Nullable ExplorationMapCell targetCell) {
111 1
            this.item = item;
112 1
            this.caster = caster;
113 1
            this.targetCell = targetCell;
114 1
        }
115
116
        @Override
117
        public void onExplorationPlayer(@Nullable ExplorationPlayer target) {
118 1
            if (!item.checkTarget(caster, target, targetCell)) {
119 1
                success = false;
120 1
                return;
121
            }
122
123 1
            item.applyToTarget(caster, target, targetCell);
124 1
            success = true;
125 1
        }
126
127
        @Override
128
        public void onNpc(GameNpc npc) {
129 1
            onExplorationPlayer(null);
130 1
        }
131
132
        public void onNull() {
133 1
            onExplorationPlayer(null);
134 1
        }
135
    }
136
}
137