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.player; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.core.event.DefaultListenerAggregate; |
23
|
|
|
import fr.quatrevieux.araknemu.core.event.ListenerAggregate; |
24
|
|
|
import fr.quatrevieux.araknemu.data.living.entity.player.Player; |
25
|
|
|
import fr.quatrevieux.araknemu.data.value.Position; |
26
|
|
|
import fr.quatrevieux.araknemu.game.account.GameAccount; |
27
|
|
|
import fr.quatrevieux.araknemu.game.chat.ChannelSet; |
28
|
|
|
import fr.quatrevieux.araknemu.game.chat.ChannelType; |
29
|
|
|
import fr.quatrevieux.araknemu.game.exploration.ExplorationPlayer; |
30
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.player.PlayerFighter; |
31
|
|
|
import fr.quatrevieux.araknemu.game.fight.spectator.Spectator; |
32
|
|
|
import fr.quatrevieux.araknemu.game.player.experience.GamePlayerExperience; |
33
|
|
|
import fr.quatrevieux.araknemu.game.player.inventory.LoadedInventory; |
34
|
|
|
import fr.quatrevieux.araknemu.game.player.inventory.PlayerInventory; |
35
|
|
|
import fr.quatrevieux.araknemu.game.player.race.GamePlayerRace; |
36
|
|
|
import fr.quatrevieux.araknemu.game.player.spell.SpellBook; |
37
|
|
|
import fr.quatrevieux.araknemu.game.player.sprite.GamePlayerSpriteInfo; |
38
|
|
|
import fr.quatrevieux.araknemu.game.player.sprite.SpriteInfo; |
39
|
|
|
import fr.quatrevieux.araknemu.network.game.GameSession; |
40
|
|
|
import org.checkerframework.dataflow.qual.Pure; |
41
|
|
|
|
42
|
|
|
import java.util.Set; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* GamePlayer object |
46
|
|
|
* A player is a logged character, with associated game session |
47
|
|
|
*/ |
48
|
|
|
public final class GamePlayer implements PlayerSessionScope { |
49
|
|
|
private final GameAccount account; |
50
|
|
|
private final Player entity; |
51
|
|
|
private final PlayerService service; |
52
|
|
|
private final GameSession session; |
53
|
|
|
private final GamePlayerRace race; |
54
|
|
|
private final Set<ChannelType> channels; |
55
|
|
|
private final PlayerInventory inventory; |
56
|
|
|
private final SpriteInfo spriteInfo; |
57
|
|
|
private final PlayerData data; |
58
|
|
|
private final Restrictions restrictions; |
59
|
|
|
|
60
|
1 |
|
private final ListenerAggregate dispatcher = new DefaultListenerAggregate(); |
61
|
|
|
|
62
|
|
|
private PlayerSessionScope scope; |
63
|
|
|
|
64
|
|
|
@SuppressWarnings({"assignment", "argument"}) |
65
|
1 |
|
public GamePlayer(GameAccount account, Player entity, GamePlayerRace race, GameSession session, PlayerService service, LoadedInventory inventory, SpellBook spells, GamePlayerExperience experience) { |
|
|
|
|
66
|
1 |
|
this.account = account; |
67
|
1 |
|
this.entity = entity; |
68
|
1 |
|
this.race = race; |
69
|
1 |
|
this.session = session; |
70
|
1 |
|
this.service = service; |
71
|
1 |
|
this.channels = new ChannelSet(entity.channels(), dispatcher); |
72
|
1 |
|
this.inventory = inventory.attach(this); |
73
|
1 |
|
this.data = new PlayerData(dispatcher, this, entity, spells, experience); |
74
|
1 |
|
this.spriteInfo = new GamePlayerSpriteInfo(entity, this.inventory); |
75
|
1 |
|
this.restrictions = new Restrictions(session); |
76
|
|
|
|
77
|
1 |
|
this.scope = this; |
78
|
|
|
|
79
|
1 |
|
this.data.build(); |
80
|
1 |
|
this.restrictions.init(this); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
@Override |
84
|
|
|
public void dispatch(Object event) { |
85
|
1 |
|
session.dispatch(event); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
@Override |
89
|
|
|
@Pure |
90
|
|
|
public ListenerAggregate dispatcher() { |
91
|
1 |
|
return dispatcher; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the base player data |
96
|
|
|
* The data should be used for change the player data |
97
|
|
|
* This value do not depends of the current player state (exploring / fighting) and should be modified carefully |
98
|
|
|
*/ |
99
|
|
|
@Override |
100
|
|
|
@Pure |
101
|
|
|
public PlayerData properties() { |
102
|
1 |
|
return data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
@Override |
106
|
|
|
public void send(Object packet) { |
107
|
1 |
|
session.send(packet); |
108
|
1 |
|
} |
109
|
|
|
|
110
|
|
|
@Override |
111
|
|
|
public void register(GameSession session) { |
112
|
1 |
|
session.setPlayer(this); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
@Override |
116
|
|
|
public void unregister(GameSession session) { |
117
|
1 |
|
session.setPlayer(null); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
@Pure |
121
|
|
|
public int id() { |
122
|
1 |
|
return entity.id(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
@Pure |
126
|
|
|
public GameAccount account() { |
127
|
1 |
|
return account; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the current player position |
132
|
|
|
*/ |
133
|
|
|
@Pure |
134
|
|
|
public Position position() { |
135
|
1 |
|
return entity.position(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set new position |
140
|
|
|
*/ |
141
|
|
|
public void setPosition(Position position) { |
142
|
1 |
|
entity.setPosition(position); |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the player saved position (teleport when die) |
147
|
|
|
*/ |
148
|
|
|
@Pure |
149
|
|
|
public Position savedPosition() { |
150
|
1 |
|
return entity.savedPosition(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set saved position (teleport when die) |
155
|
|
|
*/ |
156
|
|
|
public void setSavedPosition(Position position) { |
157
|
1 |
|
entity.setSavedPosition(position); |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the player race |
162
|
|
|
*/ |
163
|
|
|
@Pure |
164
|
|
|
public GamePlayerRace race() { |
165
|
1 |
|
return race; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
@Pure |
169
|
|
|
public String name() { |
170
|
1 |
|
return entity.name(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get channels subscriptions |
175
|
|
|
*/ |
176
|
|
|
@Pure |
177
|
|
|
public Set<ChannelType> subscriptions() { |
178
|
1 |
|
return channels; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the current session scope |
183
|
|
|
*/ |
184
|
|
|
@Pure |
185
|
|
|
public PlayerSessionScope scope() { |
186
|
1 |
|
return scope; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Start a new session scope |
191
|
|
|
*/ |
192
|
|
|
public void start(PlayerSessionScope scope) { |
193
|
1 |
|
if (this.scope == scope) { |
194
|
1 |
|
throw new IllegalStateException("Scope already started"); |
195
|
|
|
} |
196
|
|
|
|
197
|
1 |
|
scope.register(session); |
198
|
1 |
|
this.scope = scope; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Stop the scope |
203
|
|
|
*/ |
204
|
|
|
public void stop(PlayerSessionScope scope) { |
205
|
1 |
|
scope.unregister(session); |
206
|
|
|
|
207
|
|
|
// Reset the scope only if we stop the active scope |
208
|
1 |
|
if (this.scope == scope) { |
209
|
1 |
|
this.scope = this; |
210
|
|
|
} |
211
|
1 |
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Check if the player is exploring |
215
|
|
|
*/ |
216
|
|
|
@Pure |
217
|
|
|
public boolean isExploring() { |
218
|
1 |
|
return session.exploration() != null; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get the exploration player |
223
|
|
|
* |
224
|
|
|
* @throws IllegalStateException When the player is not on exploration state |
225
|
|
|
*/ |
226
|
|
|
@Pure |
227
|
|
|
public ExplorationPlayer exploration() { |
228
|
1 |
|
final ExplorationPlayer exploration = session.exploration(); |
229
|
|
|
|
230
|
1 |
|
if (exploration != null) { |
231
|
1 |
|
return exploration; |
232
|
|
|
} |
233
|
|
|
|
234
|
1 |
|
throw new IllegalStateException("The current player is not an exploration state"); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get the attached fighter |
239
|
|
|
*/ |
240
|
|
|
@Pure |
241
|
|
|
public PlayerFighter fighter() { |
242
|
1 |
|
final PlayerFighter fighter = session.fighter(); |
243
|
|
|
|
244
|
1 |
|
if (fighter != null) { |
245
|
1 |
|
return fighter; |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
throw new IllegalStateException("The current player is not in fight"); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Check if the current player is in fight |
253
|
|
|
*/ |
254
|
|
|
@Pure |
255
|
|
|
public boolean isFighting() { |
256
|
1 |
|
return session.fighter() != null; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get the attached spectator |
261
|
|
|
*/ |
262
|
|
|
@Pure |
263
|
|
|
public Spectator spectator() { |
264
|
1 |
|
final Spectator spectator = session.spectator(); |
265
|
|
|
|
266
|
1 |
|
if (spectator != null) { |
267
|
1 |
|
return spectator; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
throw new IllegalStateException("The current player is not a spectator"); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Check if the current player is a spectator |
275
|
|
|
*/ |
276
|
|
|
@Pure |
277
|
|
|
public boolean isSpectator() { |
278
|
1 |
|
return session.spectator() != null; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Save the player |
283
|
|
|
*/ |
284
|
|
|
public void save() { |
285
|
1 |
|
properties().life().setLifeWithCurrentRegeneration(); |
286
|
1 |
|
service.save(this); |
287
|
1 |
|
} |
288
|
|
|
|
289
|
|
|
@Pure |
290
|
|
|
public PlayerInventory inventory() { |
291
|
1 |
|
return inventory; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get the player sprite info |
296
|
|
|
*/ |
297
|
|
|
@Pure |
298
|
|
|
public SpriteInfo spriteInfo() { |
299
|
1 |
|
return spriteInfo; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get the current player restrictions |
304
|
|
|
*/ |
305
|
|
|
@Pure |
306
|
|
|
public Restrictions restrictions() { |
307
|
1 |
|
return restrictions; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Does the player is a newly created character ? (i.e. first connection) |
312
|
|
|
*/ |
313
|
|
|
public boolean isNew() { |
314
|
1 |
|
return session.log().map(log -> !log.hasAlreadyPlayed(entity)).orElse(false); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
@Pure |
318
|
|
|
Player entity() { |
319
|
1 |
|
return entity; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|