|
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.ExplorationCreature; |
|
26
|
|
|
import fr.quatrevieux.araknemu.game.exploration.creature.Operation; |
|
27
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.ExplorationMap; |
|
28
|
|
|
import fr.quatrevieux.araknemu.game.exploration.map.cell.ExplorationMapCell; |
|
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 |
|
return map.has(packet.target()) |
|
95
|
1 |
|
? NullnessUtil.castNonNull(map.creature(packet.target()).apply(operation)) // The implementation ensure that the return value is never null |
|
96
|
1 |
|
: operation.onNull() |
|
97
|
|
|
; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private static class ApplyItemOperation implements Operation<Boolean> { |
|
101
|
|
|
private final UsableItem item; |
|
102
|
|
|
private final ExplorationPlayer caster; |
|
103
|
|
|
private final @Nullable ExplorationMapCell targetCell; |
|
104
|
|
|
|
|
105
|
1 |
|
public ApplyItemOperation(UsableItem item, ExplorationPlayer caster, @Nullable ExplorationMapCell targetCell) { |
|
106
|
1 |
|
this.item = item; |
|
107
|
1 |
|
this.caster = caster; |
|
108
|
1 |
|
this.targetCell = targetCell; |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
@Override |
|
112
|
|
|
public Boolean onExplorationPlayer(@Nullable ExplorationPlayer target) { |
|
113
|
1 |
|
if (!item.checkTarget(caster, target, targetCell)) { |
|
114
|
1 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
item.applyToTarget(caster, target, targetCell); |
|
118
|
1 |
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
@Override |
|
122
|
|
|
public Boolean onCreature(ExplorationCreature creature) { |
|
123
|
1 |
|
return onNull(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public Boolean onNull() { |
|
127
|
1 |
|
return onExplorationPlayer(null); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|