Player.java 46 KB
Newer Older
Melledy's avatar
Melledy committed
1
package emu.grasscutter.game.player;
Melledy's avatar
Melledy committed
2
3

import dev.morphia.annotations.*;
4
import emu.grasscutter.GameConstants;
5
import emu.grasscutter.Grasscutter;
6
import emu.grasscutter.data.GameData;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.data.excels.PlayerLevelData;
AnimeGitB's avatar
AnimeGitB committed
8
import emu.grasscutter.data.excels.WeatherData;
Melledy's avatar
Melledy committed
9
import emu.grasscutter.database.DatabaseHelper;
10
import emu.grasscutter.database.DatabaseManager;
Melledy's avatar
Melledy committed
11
12
import emu.grasscutter.game.Account;
import emu.grasscutter.game.CoopRequest;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.game.ability.AbilityManager;
Akka's avatar
Akka committed
14
import emu.grasscutter.game.activity.ActivityManager;
15
import emu.grasscutter.game.avatar.Avatar;
Kengxxiao's avatar
Kengxxiao committed
16
17
import emu.grasscutter.game.avatar.AvatarProfileData;
import emu.grasscutter.game.avatar.AvatarStorage;
18
import emu.grasscutter.game.battlepass.BattlePassManager;
19
20
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.entity.EntityVehicle;
21
import emu.grasscutter.game.home.GameHome;
22
import emu.grasscutter.game.entity.EntityGadget;
Melledy's avatar
Melledy committed
23
import emu.grasscutter.game.entity.EntityItem;
24
import emu.grasscutter.game.entity.GameEntity;
Kinesis's avatar
Kinesis committed
25
import emu.grasscutter.game.expedition.ExpeditionInfo;
Melledy's avatar
Melledy committed
26
27
28
import emu.grasscutter.game.friends.FriendsList;
import emu.grasscutter.game.friends.PlayerProfile;
import emu.grasscutter.game.gacha.PlayerGachaInfo;
29
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
30
import emu.grasscutter.game.inventory.Inventory;
Melledy's avatar
Melledy committed
31
import emu.grasscutter.game.mail.Mail;
32
import emu.grasscutter.game.mail.MailHandler;
Akka's avatar
Akka committed
33
import emu.grasscutter.game.managers.FurnitureManager;
34
import emu.grasscutter.game.managers.InsectCaptureManager;
ImmuState's avatar
ImmuState committed
35
import emu.grasscutter.game.managers.ResinManager;
36
37
import emu.grasscutter.game.managers.collection.CollectionManager;
import emu.grasscutter.game.managers.collection.CollectionRecordStore;
Melledy's avatar
Melledy committed
38
39
40
41
42
43
import emu.grasscutter.game.managers.deforestation.DeforestationManager;
import emu.grasscutter.game.managers.energy.EnergyManager;
import emu.grasscutter.game.managers.forging.ActiveForgeData;
import emu.grasscutter.game.managers.forging.ForgingManager;
import emu.grasscutter.game.managers.mapmark.*;
import emu.grasscutter.game.managers.stamina.StaminaManager;
44
import emu.grasscutter.game.managers.SotSManager;
Melledy's avatar
Melledy committed
45
import emu.grasscutter.game.props.ActionReason;
AnimeGitB's avatar
AnimeGitB committed
46
import emu.grasscutter.game.props.ClimateType;
Melledy's avatar
Melledy committed
47
import emu.grasscutter.game.props.PlayerProperty;
48
import emu.grasscutter.game.props.WatcherTriggerType;
Melledy's avatar
Melledy committed
49
import emu.grasscutter.game.quest.QuestManager;
Kengxxiao's avatar
Kengxxiao committed
50
import emu.grasscutter.game.shop.ShopLimit;
51
import emu.grasscutter.game.tower.TowerData;
Akka's avatar
Akka committed
52
import emu.grasscutter.game.tower.TowerManager;
Melledy's avatar
Melledy committed
53
54
import emu.grasscutter.game.world.Scene;
import emu.grasscutter.game.world.World;
55
import emu.grasscutter.net.packet.BasePacket;
56
import emu.grasscutter.net.proto.*;
Melledy's avatar
Melledy committed
57
import emu.grasscutter.net.proto.AbilityInvokeEntryOuterClass.AbilityInvokeEntry;
58
import emu.grasscutter.net.proto.AttackResultOuterClass.AttackResult;
Melledy's avatar
Melledy committed
59
import emu.grasscutter.net.proto.CombatInvokeEntryOuterClass.CombatInvokeEntry;
ImmuState's avatar
ImmuState committed
60
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
Melledy's avatar
Melledy committed
61
62
63
64
import emu.grasscutter.net.proto.InteractTypeOuterClass.InteractType;
import emu.grasscutter.net.proto.MpSettingTypeOuterClass.MpSettingType;
import emu.grasscutter.net.proto.OnlinePlayerInfoOuterClass.OnlinePlayerInfo;
import emu.grasscutter.net.proto.PlayerLocationInfoOuterClass.PlayerLocationInfo;
Melledy's avatar
Melledy committed
65
import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture;
Melledy's avatar
Melledy committed
66
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
67
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
68
69
import emu.grasscutter.server.event.player.PlayerJoinEvent;
import emu.grasscutter.server.event.player.PlayerQuitEvent;
Melledy's avatar
Melledy committed
70
71
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.game.GameSession;
72
import emu.grasscutter.server.game.GameSession.SessionState;
73
import emu.grasscutter.server.packet.send.*;
Yazawazi's avatar
utils    
Yazawazi committed
74
import emu.grasscutter.utils.DateHelper;
Kengxxiao's avatar
Kengxxiao committed
75
import emu.grasscutter.utils.Position;
76
import emu.grasscutter.utils.MessageHandler;
Kinesis's avatar
Kinesis committed
77
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
78
79
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
AnimeGitB's avatar
AnimeGitB committed
80
import lombok.Getter;
Melledy's avatar
Melledy committed
81

82
import java.time.DayOfWeek;
83
84
85
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
Asnxthaony's avatar
Asnxthaony committed
86
import java.util.*;
87
import java.util.concurrent.LinkedBlockingQueue;
Asnxthaony's avatar
Asnxthaony committed
88

89
90
import static emu.grasscutter.Configuration.*;

KingRainbow44's avatar
KingRainbow44 committed
91
@Entity(value = "players", useDiscriminator = false)
92
public class Player {
Angda Song's avatar
Angda Song committed
93

Melledy's avatar
Melledy committed
94
95
	@Id private int id;
	@Indexed(options = @IndexOptions(unique = true)) private String accountId;
Asnxthaony's avatar
Asnxthaony committed
96

Melledy's avatar
Melledy committed
97
98
99
100
101
102
103
	@Transient private Account account;
	private String nickname;
	private String signature;
	private int headImage;
	private int nameCardId = 210001;
	private Position pos;
	private Position rotation;
Miyucchi's avatar
Miyucchi committed
104
	private PlayerBirthday birthday;
105
	private PlayerCodex codex;
Asnxthaony's avatar
Asnxthaony committed
106

Melledy's avatar
Melledy committed
107
108
109
110
	private Map<Integer, Integer> properties;
	private Set<Integer> nameCardList;
	private Set<Integer> flyCloakList;
	private Set<Integer> costumeList;
111
	private Set<Integer> unlockedForgingBlueprints;
ImmuState's avatar
ImmuState committed
112
	private Set<Integer> unlockedCombines;
Akka's avatar
Akka committed
113
114
	private Set<Integer> unlockedFurniture;
	private Set<Integer> unlockedFurnitureSuite;
115
	private List<ActiveForgeData> activeForges;
Asnxthaony's avatar
Asnxthaony committed
116

117
118
	private Integer widgetId;

119
120
121
	private Set<Integer> realmList;
	private Integer currentRealmId;

Melledy's avatar
Melledy committed
122
123
124
	@Transient private long nextGuid = 0;
	@Transient private int peerId;
	@Transient private World world;
125
	@Transient private Scene scene;
AnimeGitB's avatar
AnimeGitB committed
126
127
	@Transient @Getter private int weatherId = 0;
	@Transient @Getter private ClimateType climate = ClimateType.CLIMATE_SUNNY;
Melledy's avatar
Melledy committed
128
129
130
131
	@Transient private GameSession session;
	@Transient private AvatarStorage avatars;
	@Transient private Inventory inventory;
	@Transient private FriendsList friendsList;
132
	@Transient private MailHandler mailHandler;
133
	@Transient private MessageHandler messageHandler;
Melledy's avatar
Melledy committed
134
	@Transient private AbilityManager abilityManager;
Melledy's avatar
Melledy committed
135
	@Transient private QuestManager questManager;
KingRainbow44's avatar
KingRainbow44 committed
136

137
	@Transient private SotSManager sotsManager;
138
	@Transient private InsectCaptureManager insectCaptureManager;
139

Melledy's avatar
Melledy committed
140
	private TeamManager teamManager;
Akka's avatar
Akka committed
141

142
143
	@Transient private TowerManager towerManager;
	private TowerData towerData;
Melledy's avatar
Melledy committed
144
145
146
147
	private PlayerGachaInfo gachaInfo;
	private PlayerProfile playerProfile;
	private boolean showAvatar;
	private ArrayList<AvatarProfileData> shownAvatars;
148
	private Set<Integer> rewardedLevels;
Kengxxiao's avatar
Kengxxiao committed
149
	private ArrayList<ShopLimit> shopLimit;
Kinesis's avatar
Kinesis committed
150
	private Map<Long, ExpeditionInfo> expeditionInfo;
Asnxthaony's avatar
Asnxthaony committed
151

Melledy's avatar
Melledy committed
152
153
154
155
	private int sceneId;
	private int regionId;
	private int mainCharacterId;
	private boolean godmode;
156
	private boolean stamina;
AnimeGitB's avatar
AnimeGitB committed
157

Yazawazi's avatar
Yazawazi committed
158
159
160
161
162
	private boolean moonCard;
	private Date moonCardStartTime;
	private int moonCardDuration;
	private Set<Date> moonCardGetTimes;

Yazawazi's avatar
Yazawazi committed
163
164
165
	private List<Integer> showAvatarList;
	private boolean showAvatars;

Melledy's avatar
Melledy committed
166
167
168
169
	@Transient private boolean paused;
	@Transient private int enterSceneToken;
	@Transient private SceneLoadState sceneState;
	@Transient private boolean hasSentAvatarDataNotify;
Melledy's avatar
Melledy committed
170
	@Transient private long nextSendPlayerLocTime = 0;
Asnxthaony's avatar
Asnxthaony committed
171

Melledy's avatar
Melledy committed
172
	@Transient private final Int2ObjectMap<CoopRequest> coopRequests;
173
	@Transient private final Queue<AttackResult> attackResults;
Melledy's avatar
Melledy committed
174
175
	@Transient private final InvokeHandler<CombatInvokeEntry> combatInvokeHandler;
	@Transient private final InvokeHandler<AbilityInvokeEntry> abilityInvokeHandler;
176
	@Transient private final InvokeHandler<AbilityInvokeEntry> clientAbilityInitFinishHandler;
Asnxthaony's avatar
Asnxthaony committed
177

gentlespoon's avatar
gentlespoon committed
178
	@Transient private MapMarksManager mapMarksManager;
179
	@Transient private StaminaManager staminaManager;
180
	@Transient private EnergyManager energyManager;
ImmuState's avatar
ImmuState committed
181
	@Transient private ResinManager resinManager;
182
	@Transient private ForgingManager forgingManager;
183
	@Transient private DeforestationManager deforestationManager;
184
	@Transient private GameHome home;
Akka's avatar
Akka committed
185
	@Transient private FurnitureManager furnitureManager;
186
	@Transient private BattlePassManager battlePassManager;
Akka's avatar
Akka committed
187
	@Getter @Transient private ActivityManager activityManager;
188

189
190
191
	@Transient private CollectionManager collectionManager;
	private CollectionRecordStore collectionRecordStore;

192
	private long springLastUsed;
gentlespoon's avatar
gentlespoon committed
193
	private HashMap<String, MapMark> mapMarks;
ImmuState's avatar
ImmuState committed
194
	private int nextResinRefresh;
195
	private int lastDailyReset;
196

Asnxthaony's avatar
Asnxthaony committed
197
198
	@Deprecated
	@SuppressWarnings({"rawtypes", "unchecked"}) // Morphia only!
199
	public Player() {
Melledy's avatar
Melledy committed
200
201
202
		this.inventory = new Inventory(this);
		this.avatars = new AvatarStorage(this);
		this.friendsList = new FriendsList(this);
203
		this.mailHandler = new MailHandler(this);
204
		this.towerManager = new TowerManager(this);
Melledy's avatar
Melledy committed
205
		this.abilityManager = new AbilityManager(this);
206
		this.deforestationManager = new DeforestationManager(this);
207
		this.insectCaptureManager = new InsectCaptureManager(this);
208

Melledy's avatar
Melledy committed
209
		this.setQuestManager(new QuestManager(this));
Melledy's avatar
Melledy committed
210
211
212
213
214
215
216
217
218
		this.pos = new Position();
		this.rotation = new Position();
		this.properties = new HashMap<>();
		for (PlayerProperty prop : PlayerProperty.values()) {
			if (prop.getId() < 10000) {
				continue;
			}
			this.properties.put(prop.getId(), 0);
		}
Asnxthaony's avatar
Asnxthaony committed
219

220
221
222
223
		this.gachaInfo = new PlayerGachaInfo();
		this.nameCardList = new HashSet<>();
		this.flyCloakList = new HashSet<>();
		this.costumeList = new HashSet<>();
224
		this.towerData = new TowerData();
225
		this.collectionRecordStore = new CollectionRecordStore();
226
		this.unlockedForgingBlueprints = new HashSet<>();
ImmuState's avatar
ImmuState committed
227
		this.unlockedCombines = new HashSet<>();
Akka's avatar
Akka committed
228
229
		this.unlockedFurniture = new HashSet<>();
		this.unlockedFurnitureSuite = new HashSet<>();
230
		this.activeForges = new ArrayList<>();
Asnxthaony's avatar
Asnxthaony committed
231

Melledy's avatar
Melledy committed
232
233
234
		this.setSceneId(3);
		this.setRegionId(1);
		this.sceneState = SceneLoadState.NONE;
Asnxthaony's avatar
Asnxthaony committed
235

236
		this.attackResults = new LinkedBlockingQueue<>();
Melledy's avatar
Melledy committed
237
238
239
		this.coopRequests = new Int2ObjectOpenHashMap<>();
		this.combatInvokeHandler = new InvokeHandler(PacketCombatInvocationsNotify.class);
		this.abilityInvokeHandler = new InvokeHandler(PacketAbilityInvocationsNotify.class);
240
		this.clientAbilityInitFinishHandler = new InvokeHandler(PacketClientAbilityInitFinishNotify.class);
Miyucchi's avatar
Miyucchi committed
241
242

		this.birthday = new PlayerBirthday();
243
		this.rewardedLevels = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
244
		this.moonCardGetTimes = new HashSet<>();
245
		this.codex = new PlayerCodex(this);
Kengxxiao's avatar
Kengxxiao committed
246
247

		this.shopLimit = new ArrayList<>();
Kinesis's avatar
Kinesis committed
248
		this.expeditionInfo = new HashMap<>();
249
		this.messageHandler = null;
gentlespoon's avatar
gentlespoon committed
250
		this.mapMarksManager = new MapMarksManager(this);
251
		this.staminaManager = new StaminaManager(this);
252
		this.sotsManager = new SotSManager(this);
253
		this.energyManager = new EnergyManager(this);
ImmuState's avatar
ImmuState committed
254
		this.resinManager = new ResinManager(this);
255
		this.forgingManager = new ForgingManager(this);
Akka's avatar
Akka committed
256
		this.furnitureManager = new FurnitureManager(this);
Melledy's avatar
Melledy committed
257
	}
Asnxthaony's avatar
Asnxthaony committed
258

Melledy's avatar
Melledy committed
259
	// On player creation
260
	public Player(GameSession session) {
Melledy's avatar
Melledy committed
261
262
263
264
265
266
267
		this();
		this.account = session.getAccount();
		this.accountId = this.getAccount().getId();
		this.session = session;
		this.nickname = "Traveler";
		this.signature = "";
		this.teamManager = new TeamManager(this);
Miyucchi's avatar
Miyucchi committed
268
		this.birthday = new PlayerBirthday();
269
		this.codex = new PlayerCodex(this);
AnimeGitB's avatar
AnimeGitB committed
270
271
272
273
274
275
276
277
		this.setProperty(PlayerProperty.PROP_PLAYER_LEVEL, 1, false);
		this.setProperty(PlayerProperty.PROP_IS_SPRING_AUTO_USE, 1, false);
		this.setProperty(PlayerProperty.PROP_SPRING_AUTO_USE_PERCENT, 50, false);
		this.setProperty(PlayerProperty.PROP_IS_FLYABLE, 1, false);
		this.setProperty(PlayerProperty.PROP_IS_TRANSFERABLE, 1, false);
		this.setProperty(PlayerProperty.PROP_MAX_STAMINA, 24000, false);
		this.setProperty(PlayerProperty.PROP_CUR_PERSIST_STAMINA, 24000, false);
		this.setProperty(PlayerProperty.PROP_PLAYER_RESIN, 160, false);
Melledy's avatar
Melledy committed
278
279
		this.getFlyCloakList().add(140001);
		this.getNameCardList().add(210001);
280
		this.getPos().set(GameConstants.START_POSITION);
Melledy's avatar
Melledy committed
281
		this.getRotation().set(0, 307, 0);
282
		this.messageHandler = null;
gentlespoon's avatar
gentlespoon committed
283
		this.mapMarksManager = new MapMarksManager(this);
284
		this.staminaManager = new StaminaManager(this);
285
		this.sotsManager = new SotSManager(this);
286
		this.energyManager = new EnergyManager(this);
ImmuState's avatar
ImmuState committed
287
		this.resinManager = new ResinManager(this);
288
		this.deforestationManager = new DeforestationManager(this);
289
		this.forgingManager = new ForgingManager(this);
Akka's avatar
Akka committed
290
		this.furnitureManager = new FurnitureManager(this);
Melledy's avatar
Melledy committed
291
292
	}

293
	public int getUid() {
Melledy's avatar
Melledy committed
294
295
296
		return id;
	}

297
	public void setUid(int id) {
Melledy's avatar
Melledy committed
298
299
		this.id = id;
	}
Asnxthaony's avatar
Asnxthaony committed
300

301
	public long getNextGameGuid() {
Melledy's avatar
Melledy committed
302
		long nextId = ++this.nextGuid;
303
		return ((long) this.getUid() << 32) + nextId;
Melledy's avatar
Melledy committed
304
305
306
	}

	public Account getAccount() {
307
308
309
		if (this.account == null)
			this.account = DatabaseHelper.getAccountById(Integer.toString(this.id));
		return this.account;
Melledy's avatar
Melledy committed
310
311
312
313
314
315
316
317
318
319
320
321
322
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public GameSession getSession() {
		return session;
	}

	public void setSession(GameSession session) {
		this.session = session;
	}
Asnxthaony's avatar
Asnxthaony committed
323

Melledy's avatar
Melledy committed
324
325
326
	public boolean isOnline() {
		return this.getSession() != null && this.getSession().isActive();
	}
Asnxthaony's avatar
Asnxthaony committed
327

Melledy's avatar
Melledy committed
328
329
330
	public GameServer getServer() {
		return this.getSession().getServer();
	}
Asnxthaony's avatar
Asnxthaony committed
331

Melledy's avatar
Melledy committed
332
333
334
	public synchronized World getWorld() {
		return this.world;
	}
Asnxthaony's avatar
Asnxthaony committed
335

Melledy's avatar
Melledy committed
336
337
338
	public synchronized void setWorld(World world) {
		this.world = world;
	}
Asnxthaony's avatar
Asnxthaony committed
339

340
	public synchronized Scene getScene() {
341
342
343
		return scene;
	}

344
	public synchronized void setScene(Scene scene) {
345
346
347
		this.scene = scene;
	}

AnimeGitB's avatar
AnimeGitB committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
	synchronized public void setClimate(ClimateType climate) {
		this.climate = climate;
		this.session.send(new PacketSceneAreaWeatherNotify(this));
	}

	synchronized public void setWeather(int weather) {
		this.setWeather(weather, ClimateType.CLIMATE_NONE);
	}

	synchronized public void setWeather(int weatherId, ClimateType climate) {
		// Lookup default climate for this weather
		if (climate == ClimateType.CLIMATE_NONE) {
			WeatherData w = GameData.getWeatherDataMap().get(weatherId);
			if (w != null) {
				climate = w.getDefaultClimate();
			}
		}
		this.weatherId = weatherId;
		this.climate = climate;
		this.session.send(new PacketSceneAreaWeatherNotify(this));
	}

Melledy's avatar
Melledy committed
370
371
372
373
374
375
376
377
378
379
380
381
	public int getGmLevel() {
		return 1;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickName) {
		this.nickname = nickName;
		this.updateProfile();
	}
Asnxthaony's avatar
Asnxthaony committed
382

Melledy's avatar
Melledy committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
	public int getHeadImage() {
		return headImage;
	}

	public void setHeadImage(int picture) {
		this.headImage = picture;
		this.updateProfile();
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
		this.updateProfile();
	}

401
402
403
404
405
406
407
408
	public Integer getWidgetId() {
		return widgetId;
	}

	public void setWidgetId(Integer widgetId) {
		this.widgetId = widgetId;
	}

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
	public Set<Integer> getRealmList() {
		return realmList;
	}

	public void setRealmList(Set<Integer> realmList) {
		this.realmList = realmList;
	}

	public void addRealmList(int realmId) {
		if (this.realmList == null) {
			this.realmList = new HashSet<>();
		} else if (this.realmList.contains(realmId)) {
			return;
		}
		this.realmList.add(realmId);
	}

	public Integer getCurrentRealmId() {
		return currentRealmId;
	}

	public void setCurrentRealmId(Integer currentRealmId) {
		this.currentRealmId = currentRealmId;
	}
433
434
435
	public GameHome getHome(){
		return home;
	}
Melledy's avatar
Melledy committed
436
437
438
	public Position getPos() {
		return pos;
	}
Asnxthaony's avatar
Asnxthaony committed
439

Melledy's avatar
Melledy committed
440
441
442
	public Position getRotation() {
		return rotation;
	}
Asnxthaony's avatar
Asnxthaony committed
443

Melledy's avatar
Melledy committed
444
445
446
	public int getLevel() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_LEVEL);
	}
Asnxthaony's avatar
Asnxthaony committed
447

448
449
450
451
452
453
454
	public boolean setLevel(int level) {
		if (this.setProperty(PlayerProperty.PROP_PLAYER_LEVEL, level)) {
			this.updateWorldLevel();
			this.updateProfile();
			return true;
		}
		return false;
455
456
	}

Melledy's avatar
Melledy committed
457
458
459
	public int getExp() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_EXP);
	}
Asnxthaony's avatar
Asnxthaony committed
460

Melledy's avatar
Melledy committed
461
462
463
	public int getWorldLevel() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL);
	}
464
465
466
467
468
469
470
471
472
	
	public boolean setWorldLevel(int level) {
		if (this.setProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL, level)) {
			if (this.world.getHost() == this)  // Don't update World's WL if we are in someone else's world
            	this.world.setWorldLevel(level);
			this.updateProfile();
			return true;
		}
		return false;
473
	}
Asnxthaony's avatar
Asnxthaony committed
474

475
476
477
478
	public int getForgePoints() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_FORGE_POINT);
	}

479
	public boolean setForgePoints(int value) {
480
		if (value == this.getForgePoints()) {
481
			return true;
482
483
		}

484
		return this.setProperty(PlayerProperty.PROP_PLAYER_FORGE_POINT, value);
485
486
	}

Melledy's avatar
Melledy committed
487
488
489
	public int getPrimogems() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_HCOIN);
	}
Asnxthaony's avatar
Asnxthaony committed
490

491
492
	public boolean setPrimogems(int primogem) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_HCOIN, primogem);
Melledy's avatar
Melledy committed
493
	}
Asnxthaony's avatar
Asnxthaony committed
494

Melledy's avatar
Melledy committed
495
496
497
	public int getMora() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_SCOIN);
	}
Asnxthaony's avatar
Asnxthaony committed
498

499
500
	public boolean setMora(int mora) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_SCOIN, mora);
Melledy's avatar
Melledy committed
501
	}
KingRainbow44's avatar
KingRainbow44 committed
502

503
504
505
506
	public int getCrystals() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_MCOIN);
	}

507
508
	public boolean setCrystals(int crystals) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_MCOIN, crystals);
509
	}
Asnxthaony's avatar
Asnxthaony committed
510

511
512
513
514
	public int getHomeCoin() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_HOME_COIN);
	}

515
516
	public boolean setHomeCoin(int coin) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_HOME_COIN, coin);
517
	}
Melledy's avatar
Melledy committed
518
	private int getExpRequired(int level) {
519
		PlayerLevelData levelData = GameData.getPlayerLevelDataMap().get(level);
Asnxthaony's avatar
Asnxthaony committed
520
		return levelData != null ? levelData.getExp() : 0;
Melledy's avatar
Melledy committed
521
	}
Asnxthaony's avatar
Asnxthaony committed
522

Melledy's avatar
Melledy committed
523
	private float getExpModifier() {
524
		return GAME_OPTIONS.rates.adventureExp;
Melledy's avatar
Melledy committed
525
	}
Asnxthaony's avatar
Asnxthaony committed
526

Melledy's avatar
Melledy committed
527
528
529
530
	// Affected by exp rate
	public void earnExp(int exp) {
		addExpDirectly((int) (exp * getExpModifier()));
	}
Asnxthaony's avatar
Asnxthaony committed
531

Melledy's avatar
Melledy committed
532
533
534
535
536
537
	// Directly give player exp
	public void addExpDirectly(int gain) {
		boolean hasLeveledUp = false;
		int level = getLevel();
		int exp = getExp();
		int reqExp = getExpRequired(level);
Asnxthaony's avatar
Asnxthaony committed
538

Melledy's avatar
Melledy committed
539
		exp += gain;
Asnxthaony's avatar
Asnxthaony committed
540

Melledy's avatar
Melledy committed
541
542
543
544
545
546
		while (exp >= reqExp && reqExp > 0) {
			exp -= reqExp;
			level += 1;
			reqExp = getExpRequired(level);
			hasLeveledUp = true;
		}
Asnxthaony's avatar
Asnxthaony committed
547

Melledy's avatar
Melledy committed
548
		if (hasLeveledUp) {
549
			this.setLevel(level);
Melledy's avatar
Melledy committed
550
		}
Asnxthaony's avatar
Asnxthaony committed
551

Melledy's avatar
Melledy committed
552
553
554
		// Set exp
		this.setProperty(PlayerProperty.PROP_PLAYER_EXP, exp);
	}
Asnxthaony's avatar
Asnxthaony committed
555

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
	private void updateWorldLevel() {
		int currentWorldLevel = this.getWorldLevel();
		int currentLevel = this.getLevel();

		int newWorldLevel =
			(currentLevel >= 55) ? 8 :
			(currentLevel >= 50) ? 7 :
			(currentLevel >= 45) ? 6 :
			(currentLevel >= 40) ? 5 :
			(currentLevel >= 35) ? 4 :
			(currentLevel >= 30) ? 3 :
			(currentLevel >= 25) ? 2 :
			(currentLevel >= 20) ? 1 :
			0;

		if (newWorldLevel != currentWorldLevel) {
			this.setWorldLevel(newWorldLevel);
		}
	}

Melledy's avatar
Melledy committed
576
577
578
	private void updateProfile() {
		getProfile().syncWithCharacter(this);
	}
Asnxthaony's avatar
Asnxthaony committed
579

Melledy's avatar
Melledy committed
580
581
582
583
584
585
586
	public boolean isFirstLoginEnterScene() {
		return !this.hasSentAvatarDataNotify;
	}

	public TeamManager getTeamManager() {
		return this.teamManager;
	}
KingRainbow44's avatar
KingRainbow44 committed
587

Akka's avatar
Akka committed
588
589
590
	public TowerManager getTowerManager() {
		return towerManager;
	}
KingRainbow44's avatar
KingRainbow44 committed
591

592
	public TowerData getTowerData() {
593
		if (towerData == null) {
594
595
596
			// because of mistake, null may be saved as storage at some machine, this if can be removed in future
			towerData = new TowerData();
		}
597
598
		return towerData;
	}
KingRainbow44's avatar
KingRainbow44 committed
599

Melledy's avatar
Melledy committed
600
601
602
603
604
605
606
607
	public QuestManager getQuestManager() {
		return questManager;
	}

	public void setQuestManager(QuestManager questManager) {
		this.questManager = questManager;
	}

Melledy's avatar
Melledy committed
608
609
610
611
612
613
614
615
616
617
618
	public PlayerGachaInfo getGachaInfo() {
		return gachaInfo;
	}

	public PlayerProfile getProfile() {
		if (this.playerProfile == null) {
			this.playerProfile = new PlayerProfile(this);
		}
		return playerProfile;
	}

619
620
	// TODO: Based on the proto, property value could be int or float.
	//  Although there's no float value at this moment, our code should be prepared for float values.
Melledy's avatar
Melledy committed
621
622
623
	public Map<Integer, Integer> getProperties() {
		return properties;
	}
Asnxthaony's avatar
Asnxthaony committed
624

625
	public boolean setProperty(PlayerProperty prop, int value) {
AnimeGitB's avatar
AnimeGitB committed
626
627
628
629
630
		return setPropertyWithSanityCheck(prop, value, true);
	}

	public boolean setProperty(PlayerProperty prop, int value, boolean sendPacket) {
		return setPropertyWithSanityCheck(prop, value, sendPacket);
Melledy's avatar
Melledy committed
631
	}
Asnxthaony's avatar
Asnxthaony committed
632

Melledy's avatar
Melledy committed
633
634
635
636
637
638
639
	public int getProperty(PlayerProperty prop) {
		return getProperties().get(prop.getId());
	}

	public Set<Integer> getFlyCloakList() {
		return flyCloakList;
	}
Asnxthaony's avatar
Asnxthaony committed
640

Melledy's avatar
Melledy committed
641
642
643
	public Set<Integer> getCostumeList() {
		return costumeList;
	}
Asnxthaony's avatar
Asnxthaony committed
644

Melledy's avatar
Melledy committed
645
646
647
648
	public Set<Integer> getNameCardList() {
		return this.nameCardList;
	}

649
	public Set<Integer> getUnlockedForgingBlueprints() {
650
651
652
		return this.unlockedForgingBlueprints;
	}

ImmuState's avatar
ImmuState committed
653
654
655
656
	public Set<Integer> getUnlockedCombines() {
		return this.unlockedCombines;
	}

Akka's avatar
Akka committed
657
658
659
660
661
662
663
664
	public Set<Integer> getUnlockedFurniture() {
		return unlockedFurniture;
	}

	public Set<Integer> getUnlockedFurnitureSuite() {
		return unlockedFurnitureSuite;
	}

665
	public List<ActiveForgeData> getActiveForges() {
666
		return this.activeForges;
667
668
	}

Melledy's avatar
Melledy committed
669
	public MpSettingType getMpSetting() {
670
		return MpSettingType.MP_SETTING_TYPE_ENTER_AFTER_APPLY; // TEMP
Melledy's avatar
Melledy committed
671
	}
KingRainbow44's avatar
KingRainbow44 committed
672

673
674
675
	public Queue<AttackResult> getAttackResults() {
		return this.attackResults;
	}
Asnxthaony's avatar
Asnxthaony committed
676

Melledy's avatar
Melledy committed
677
678
679
680
681
682
683
	public synchronized Int2ObjectMap<CoopRequest> getCoopRequests() {
		return coopRequests;
	}

	public InvokeHandler<CombatInvokeEntry> getCombatInvokeHandler() {
		return this.combatInvokeHandler;
	}
Asnxthaony's avatar
Asnxthaony committed
684

Melledy's avatar
Melledy committed
685
686
687
688
	public InvokeHandler<AbilityInvokeEntry> getAbilityInvokeHandler() {
		return this.abilityInvokeHandler;
	}

689
690
691
692
	public InvokeHandler<AbilityInvokeEntry> getClientAbilityInitFinishHandler() {
		return clientAbilityInitFinishHandler;
	}

Melledy's avatar
Melledy committed
693
694
695
	public AvatarStorage getAvatars() {
		return avatars;
	}
Asnxthaony's avatar
Asnxthaony committed
696

Melledy's avatar
Melledy committed
697
698
699
	public Inventory getInventory() {
		return inventory;
	}
Asnxthaony's avatar
Asnxthaony committed
700

Melledy's avatar
Melledy committed
701
702
703
704
	public FriendsList getFriendsList() {
		return this.friendsList;
	}

705
706
707
708
	public MailHandler getMailHandler() {
		return mailHandler;
	}

Melledy's avatar
Melledy committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
	public int getEnterSceneToken() {
		return enterSceneToken;
	}

	public void setEnterSceneToken(int enterSceneToken) {
		this.enterSceneToken = enterSceneToken;
	}

	public int getNameCardId() {
		return nameCardId;
	}

	public void setNameCardId(int nameCardId) {
		this.nameCardId = nameCardId;
		this.updateProfile();
	}

	public int getMainCharacterId() {
		return mainCharacterId;
	}

	public void setMainCharacterId(int mainCharacterId) {
		this.mainCharacterId = mainCharacterId;
	}

	public int getPeerId() {
		return peerId;
	}

	public void setPeerId(int peerId) {
		this.peerId = peerId;
	}

	public int getClientTime() {
		return session.getClientTime();
	}

	public long getLastPingTime() {
		return session.getLastPingTime();
	}

	public boolean isPaused() {
		return paused;
	}

	public void setPaused(boolean newPauseState) {
		boolean oldPauseState = this.paused;
		this.paused = newPauseState;
Asnxthaony's avatar
Asnxthaony committed
757

Melledy's avatar
Melledy committed
758
759
760
761
762
763
764
		if (newPauseState && !oldPauseState) {
			this.onPause();
		} else if (oldPauseState && !newPauseState) {
			this.onUnpause();
		}
	}

765
766
767
768
769
770
771
772
	public long getSpringLastUsed() {
		return springLastUsed;
	}

	public void setSpringLastUsed(long val) {
		springLastUsed = val;
	}

ImmuState's avatar
ImmuState committed
773
774
775
776
777
778
779
780
	public int getNextResinRefresh() {
		return nextResinRefresh;
	}

	public void setNextResinRefresh(int value) {
		this.nextResinRefresh = value;
	}

Melledy's avatar
Melledy committed
781
782
783
784
785
786
787
	public SceneLoadState getSceneLoadState() {
		return sceneState;
	}

	public void setSceneLoadState(SceneLoadState sceneState) {
		this.sceneState = sceneState;
	}
Asnxthaony's avatar
Asnxthaony committed
788

Melledy's avatar
Melledy committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
	public boolean isInMultiplayer() {
		return this.getWorld() != null && this.getWorld().isMultiplayer();
	}

	public int getSceneId() {
		return sceneId;
	}

	public void setSceneId(int sceneId) {
		this.sceneId = sceneId;
	}

	public int getRegionId() {
		return regionId;
	}

	public void setRegionId(int regionId) {
		this.regionId = regionId;
	}
Asnxthaony's avatar
Asnxthaony committed
808

Yazawazi's avatar
Yazawazi committed
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
	public void setShowAvatars(boolean showAvatars) {
		this.showAvatars = showAvatars;
	}

	public boolean isShowAvatars() {
		return showAvatars;
	}

	public void setShowAvatarList(List<Integer> showAvatarList) {
		this.showAvatarList = showAvatarList;
	}

	public List<Integer> getShowAvatarList() {
		return showAvatarList;
	}

825
826
827
828
829
830
831
832
	public int getLastDailyReset() {
		return this.lastDailyReset;
	}

	public void setLastDailyReset(int value) {
		this.lastDailyReset = value;
	}

Yazawazi's avatar
Yazawazi committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
	public boolean inMoonCard() {
		return moonCard;
	}

	public void setMoonCard(boolean moonCard) {
		this.moonCard = moonCard;
	}

	public void addMoonCardDays(int days) {
		this.moonCardDuration += days;
	}

	public int getMoonCardDuration() {
		return moonCardDuration;
	}

	public void setMoonCardDuration(int moonCardDuration) {
		this.moonCardDuration = moonCardDuration;
	}

	public Date getMoonCardStartTime() {
		return moonCardStartTime;
	}

	public void setMoonCardStartTime(Date moonCardStartTime) {
		this.moonCardStartTime = moonCardStartTime;
	}

	public Set<Date> getMoonCardGetTimes() {
		return moonCardGetTimes;
	}

	public void setMoonCardGetTimes(Set<Date> moonCardGetTimes) {
		this.moonCardGetTimes = moonCardGetTimes;
	}

	public int getMoonCardRemainDays() {
		Calendar remainCalendar = Calendar.getInstance();
		remainCalendar.setTime(moonCardStartTime);
		remainCalendar.add(Calendar.DATE, moonCardDuration);
		Date theLastDay = remainCalendar.getTime();
Yazawazi's avatar
Yazawazi committed
874
		Date now = DateHelper.onlyYearMonthDay(new Date());
KingRainbow44's avatar
KingRainbow44 committed
875
		return (int) ((theLastDay.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)); // By copilot
Yazawazi's avatar
Yazawazi committed
876
877
878
	}

	public void rechargeMoonCard() {
879
		inventory.addItem(new GameItem(203, 300));
Yazawazi's avatar
Yazawazi committed
880
881
882
		if (!moonCard) {
			moonCard = true;
			Date now = new Date();
Yazawazi's avatar
Yazawazi committed
883
			moonCardStartTime = DateHelper.onlyYearMonthDay(now);
Yazawazi's avatar
Yazawazi committed
884
885
886
887
888
889
890
891
892
893
894
895
896
			moonCardDuration = 30;
		} else {
			moonCardDuration += 30;
		}
		if (!moonCardGetTimes.contains(moonCardStartTime)) {
			moonCardGetTimes.add(moonCardStartTime);
		}
	}

	public void getTodayMoonCard() {
		if (!moonCard) {
			return;
		}
Yazawazi's avatar
Yazawazi committed
897
		Date now = DateHelper.onlyYearMonthDay(new Date());
Yazawazi's avatar
Yazawazi committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
		if (moonCardGetTimes.contains(now)) {
			return;
		}
		Date stopTime = new Date();
		Calendar stopCalendar = Calendar.getInstance();
		stopCalendar.setTime(stopTime);
		stopCalendar.add(Calendar.DATE, moonCardDuration);
		stopTime = stopCalendar.getTime();
		if (now.after(stopTime)) {
			moonCard = false;
			return;
		}
		moonCardGetTimes.add(now);
		addMoonCardDays(1);
912
913
		GameItem item = new GameItem(201, 90);
		getInventory().addItem(item, ActionReason.BlessingRedeemReward);
Yazawazi's avatar
Yazawazi committed
914
915
916
		session.send(new PacketCardProductRewardNotify(getMoonCardRemainDays()));
	}

Kinesis's avatar
Kinesis committed
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
	public Map<Long, ExpeditionInfo> getExpeditionInfo() {
		return expeditionInfo;
	}

	public void addExpeditionInfo(long avaterGuid, int expId, int hourTime, int startTime){
		ExpeditionInfo exp = new ExpeditionInfo();
		exp.setExpId(expId);
		exp.setHourTime(hourTime);
		exp.setState(1);
		exp.setStartTime(startTime);
		expeditionInfo.put(avaterGuid, exp);
	}

	public void removeExpeditionInfo(long avaterGuid){
		expeditionInfo.remove(avaterGuid);
	}

	public ExpeditionInfo getExpeditionInfo(long avaterGuid){
		return expeditionInfo.get(avaterGuid);
	}

Kengxxiao's avatar
Kengxxiao committed
938
939
940
941
	public List<ShopLimit> getShopLimit() {
		return shopLimit;
	}

Kengxxiao's avatar
Kengxxiao committed
942
943
944
945
946
	public ShopLimit getGoodsLimit(int goodsId) {
		Optional<ShopLimit> shopLimit = this.shopLimit.stream().filter(x -> x.getShopGoodId() == goodsId).findFirst();
		if (shopLimit.isEmpty())
			return null;
		return shopLimit.get();
Kengxxiao's avatar
Kengxxiao committed
947
948
	}

Kengxxiao's avatar
Kengxxiao committed
949
950
951
952
953
954
955
	public void addShopLimit(int goodsId, int boughtCount, int nextRefreshTime) {
		ShopLimit target = getGoodsLimit(goodsId);
		if (target != null) {
			target.setHasBought(target.getHasBought() + boughtCount);
			target.setHasBoughtInPeriod(target.getHasBoughtInPeriod() + boughtCount);
			target.setNextRefreshTime(nextRefreshTime);
		} else {
Kengxxiao's avatar
Kengxxiao committed
956
957
958
			ShopLimit sl = new ShopLimit();
			sl.setShopGoodId(goodsId);
			sl.setHasBought(boughtCount);
Kengxxiao's avatar
Kengxxiao committed
959
960
961
			sl.setHasBoughtInPeriod(boughtCount);
			sl.setNextRefreshTime(nextRefreshTime);
			getShopLimit().add(sl);
Kengxxiao's avatar
Kengxxiao committed
962
963
964
		}
		this.save();
	}
965
	public boolean getUnlimitedStamina() {
966
967
		return stamina;
	}
968
	public void setUnlimitedStamina(boolean stamina) {
969
970
		this.stamina = stamina;
	}
KingRainbow44's avatar
KingRainbow44 committed
971
	public boolean inGodmode() {
Melledy's avatar
Melledy committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
		return godmode;
	}

	public void setGodmode(boolean godmode) {
		this.godmode = godmode;
	}

	public boolean hasSentAvatarDataNotify() {
		return hasSentAvatarDataNotify;
	}

	public void setHasSentAvatarDataNotify(boolean hasSentAvatarDataNotify) {
		this.hasSentAvatarDataNotify = hasSentAvatarDataNotify;
	}

ProxyismGH's avatar
ProxyismGH committed
987
	public void addAvatar(Avatar avatar, boolean addToCurrentTeam) {
Melledy's avatar
Melledy committed
988
		boolean result = getAvatars().addAvatar(avatar);
Asnxthaony's avatar
Asnxthaony committed
989

Melledy's avatar
Melledy committed
990
991
992
		if (result) {
			// Add starting weapon
			getAvatars().addStartingWeapon(avatar);
Asnxthaony's avatar
Asnxthaony committed
993

Melledy's avatar
Melledy committed
994
995
996
997
			// Done
			if (hasSentAvatarDataNotify()) {
				// Recalc stats
				avatar.recalcStats();
ProxyismGH's avatar
ProxyismGH committed
998
999
1000
1001
1002
1003
				// Packet, show notice on left if the avatar will be added to the team
				sendPacket(new PacketAvatarAddNotify(avatar, addToCurrentTeam && this.getTeamManager().canAddAvatarToCurrentTeam()));
				if (addToCurrentTeam) {
					// If space in team, add
					this.getTeamManager().addAvatarToCurrentTeam(avatar);
				}
Melledy's avatar
Melledy committed
1004
1005
1006
1007
1008
			}
		} else {
			// Failed adding avatar
		}
	}
Asnxthaony's avatar
Asnxthaony committed
1009

ProxyismGH's avatar
ProxyismGH committed
1010
1011
1012
1013
	public void addAvatar(Avatar avatar) {
		addAvatar(avatar, true);
	}

Melledy's avatar
Melledy committed
1014
1015
1016
1017
	public void addFlycloak(int flycloakId) {
		this.getFlyCloakList().add(flycloakId);
		this.sendPacket(new PacketAvatarGainFlycloakNotify(flycloakId));
	}
Asnxthaony's avatar
Asnxthaony committed
1018

Melledy's avatar
Melledy committed
1019
1020
1021
1022
	public void addCostume(int costumeId) {
		this.getCostumeList().add(costumeId);
		this.sendPacket(new PacketAvatarGainCostumeNotify(costumeId));
	}
Asnxthaony's avatar
Asnxthaony committed
1023

Melledy's avatar
Melledy committed
1024
1025
1026
1027
	public void addNameCard(int nameCardId) {
		this.getNameCardList().add(nameCardId);
		this.sendPacket(new PacketUnlockNameCardNotify(nameCardId));
	}
Asnxthaony's avatar
Asnxthaony committed
1028

Melledy's avatar
Melledy committed
1029
1030
1031
1032
	public void setNameCard(int nameCardId) {
		if (!this.getNameCardList().contains(nameCardId)) {
			return;
		}
Asnxthaony's avatar
Asnxthaony committed
1033

Melledy's avatar
Melledy committed
1034
		this.setNameCardId(nameCardId);
Asnxthaony's avatar
Asnxthaony committed
1035

Melledy's avatar
Melledy committed
1036
1037
		this.sendPacket(new PacketSetNameCardRsp(nameCardId));
	}
Asnxthaony's avatar
Asnxthaony committed
1038

Melledy's avatar
Melledy committed
1039
	public void dropMessage(Object message) {
1040
1041
1042
1043
		if (this.messageHandler != null) {
			this.messageHandler.append(message.toString());
			return;
		}
1044
		this.sendPacket(new PacketPrivateChatNotify(GameConstants.SERVER_CONSOLE_UID, getUid(), message.toString()));
Melledy's avatar
Melledy committed
1045
	}
Melledy's avatar
Melledy committed
1046
1047
1048

	/**
	 * Sends a message to another player.
Asnxthaony's avatar
Asnxthaony committed
1049
1050
	 *
	 * @param sender  The sender of the message.
Melledy's avatar
Melledy committed
1051
1052
	 * @param message The message to send.
	 */
1053
	public void sendMessage(Player sender, Object message) {
1054
		this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString()));
Melledy's avatar
Melledy committed
1055
	}
Asnxthaony's avatar
Asnxthaony committed
1056

Benjamin Elsdon's avatar
Benjamin Elsdon committed
1057
	// ---------------------MAIL------------------------
1058

1059
	public List<Mail> getAllMail() { return this.getMailHandler().getMail(); }
1060

Benjamin Elsdon's avatar
Benjamin Elsdon committed
1061
	public void sendMail(Mail message) {
1062
		this.getMailHandler().sendMail(message);
1063
	}
Benjamin Elsdon's avatar
Benjamin Elsdon committed
1064
1065

	public boolean deleteMail(int mailId) {
1066
		return this.getMailHandler().deleteMail(mailId);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
1067
1068
	}

1069
	public Mail getMail(int index) { return this.getMailHandler().getMailById(index); }
KingRainbow44's avatar
KingRainbow44 committed
1070

1071
	public int getMailId(Mail message) {
1072
		return this.getMailHandler().getMailIndex(message);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
1073
1074
	}

1075
	public boolean replaceMailByIndex(int index, Mail message) {
1076
		return this.getMailHandler().replaceMailByIndex(index, message);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
1077
	}
1078

KingRainbow44's avatar
KingRainbow44 committed
1079
1080

	public void interactWith(int gadgetEntityId, GadgetInteractReq opType) {
1081
		GameEntity entity = getScene().getEntityById(gadgetEntityId);
Melledy's avatar
Melledy committed
1082
1083
1084
		if (entity == null) {
			return;
		}
Asnxthaony's avatar
Asnxthaony committed
1085

Melledy's avatar
Melledy committed
1086
		// Handle
1087
		if (entity instanceof EntityItem drop) {
Melledy's avatar
Melledy committed
1088
			// Pick item
Kengxxiao's avatar
Kengxxiao committed
1089
1090
1091
			if (!drop.isShare()) // check drop owner to avoid someone picked up item in others' world
			{
				int dropOwner = (int)(drop.getGuid() >> 32);
1092
				if (dropOwner != getUid()) {
Kengxxiao's avatar
Kengxxiao committed
1093
					return;
1094
				}
Kengxxiao's avatar
Kengxxiao committed
1095
1096
			}
			entity.getScene().removeEntity(entity);
1097
			GameItem item = new GameItem(drop.getItemData(), drop.getCount());
Melledy's avatar
Melledy committed
1098
			// Add to inventory
1099
			boolean success = getInventory().addItem(item, ActionReason.SubfieldDrop);
Melledy's avatar
Melledy committed
1100
			if (success) {
1101
				if (!drop.isShare()) { // not shared drop
1102
					this.sendPacket(new PacketGadgetInteractRsp(drop, InteractType.INTERACT_TYPE_PICK_ITEM));
1103
				}else{
1104
					this.getScene().broadcastPacket(new PacketGadgetInteractRsp(drop, InteractType.INTERACT_TYPE_PICK_ITEM));
1105
				}
Melledy's avatar
Melledy committed
1106
			}
1107
		} else if (entity instanceof EntityGadget gadget) {
Melledy's avatar
Melledy committed
1108
1109
1110
			if (gadget.getContent() == null) {
				return;
			}
KingRainbow44's avatar
KingRainbow44 committed
1111
1112
1113

			boolean shouldDelete = gadget.getContent().onInteract(this, opType);

Melledy's avatar
Melledy committed
1114
			if (shouldDelete) {
1115
				entity.getScene().removeEntity(entity, VisionType.VISION_TYPE_REMOVE);
1116
			}
1117
1118
1119
1120
		} else if (entity instanceof EntityMonster monster) {
			insectCaptureManager.arrestSmallCreature(monster);
		} else if (entity instanceof EntityVehicle vehicle) {// try to arrest it, example: glowworm
			insectCaptureManager.arrestSmallCreature(vehicle);
Kengxxiao's avatar
Kengxxiao committed
1121
1122
1123
		} else {
			// Delete directly
			entity.getScene().removeEntity(entity);
Melledy's avatar
Melledy committed
1124
1125
		}
	}
Asnxthaony's avatar
Asnxthaony committed
1126

Melledy's avatar
Melledy committed
1127
	public void onPause() {
1128
		getStaminaManager().stopSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1129
	}
Asnxthaony's avatar
Asnxthaony committed
1130

Melledy's avatar
Melledy committed
1131
	public void onUnpause() {
1132
		getStaminaManager().startSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1133
	}
Asnxthaony's avatar
Asnxthaony committed
1134

1135
	public void sendPacket(BasePacket packet) {
Melledy's avatar
Melledy committed
1136
		this.getSession().send(packet);
Melledy's avatar
Melledy committed
1137
	}
Asnxthaony's avatar
Asnxthaony committed
1138

Melledy's avatar
Melledy committed
1139
1140
	public OnlinePlayerInfo getOnlinePlayerInfo() {
		OnlinePlayerInfo.Builder onlineInfo = OnlinePlayerInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1141
1142
1143
1144
1145
1146
				.setUid(this.getUid())
				.setNickname(this.getNickname())
				.setPlayerLevel(this.getLevel())
				.setMpSettingType(this.getMpSetting())
				.setNameCardId(this.getNameCardId())
				.setSignature(this.getSignature())
Melledy's avatar
Melledy committed
1147
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()));
Asnxthaony's avatar
Asnxthaony committed
1148

Melledy's avatar
Melledy committed
1149
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1150
			onlineInfo.setCurPlayerNumInWorld(getWorld().getPlayerCount());
Melledy's avatar
Melledy committed
1151
1152
1153
		} else {
			onlineInfo.setCurPlayerNumInWorld(1);
		}
Asnxthaony's avatar
Asnxthaony committed
1154

Melledy's avatar
Melledy committed
1155
1156
1157
		return onlineInfo.build();
	}

Asnxthaony's avatar
Asnxthaony committed
1158
	public PlayerBirthday getBirthday() {
Miyucchi's avatar
Miyucchi committed
1159
1160
1161
1162
1163
1164
1165
1166
		return this.birthday;
	}

	public void setBirthday(int d, int m) {
		this.birthday = new PlayerBirthday(d, m);
		this.updateProfile();
	}

Asnxthaony's avatar
Asnxthaony committed
1167
1168
1169
1170
	public boolean hasBirthday() {
		return this.birthday.getDay() > 0;
	}

1171
1172
	public PlayerCodex getCodex(){ return this.codex; }

1173
1174
1175
1176
1177
1178
1179
1180
	public Set<Integer> getRewardedLevels() {
		return rewardedLevels;
	}

	public void setRewardedLevels(Set<Integer> rewardedLevels) {
		this.rewardedLevels = rewardedLevels;
	}

Melledy's avatar
Melledy committed
1181
	public SocialDetail.Builder getSocialDetail() {
Yazawazi's avatar
Yazawazi committed
1182
		List<SocialShowAvatarInfoOuterClass.SocialShowAvatarInfo> socialShowAvatarInfoList = new ArrayList<>();
Yazawazi's avatar
Yazawazi committed
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
		if (this.isOnline()) {
			if (this.getShowAvatarList() != null) {
				for (int avatarId : this.getShowAvatarList()) {
					socialShowAvatarInfoList.add(
							socialShowAvatarInfoList.size(),
							SocialShowAvatarInfoOuterClass.SocialShowAvatarInfo.newBuilder()
									.setAvatarId(avatarId)
									.setLevel(getAvatars().getAvatarById(avatarId).getLevel())
									.setCostumeId(getAvatars().getAvatarById(avatarId).getCostume())
									.build()
					);
				}
			}
		} else {
1197
1198
			List<Integer> showAvatarList = DatabaseHelper.getPlayerByUid(id).getShowAvatarList();
			AvatarStorage avatars = DatabaseHelper.getPlayerByUid(id).getAvatars();
Yazawazi's avatar
Yazawazi committed
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
			avatars.loadFromDatabase();
			if (showAvatarList != null) {
				for (int avatarId : showAvatarList) {
					socialShowAvatarInfoList.add(
							socialShowAvatarInfoList.size(),
							SocialShowAvatarInfoOuterClass.SocialShowAvatarInfo.newBuilder()
									.setAvatarId(avatarId)
									.setLevel(avatars.getAvatarById(avatarId).getLevel())
									.setCostumeId(avatars.getAvatarById(avatarId).getCostume())
									.build()
					);
				}
			}
Yazawazi's avatar
Yazawazi committed
1212
1213
		}

Asnxthaony's avatar
Asnxthaony committed
1214
1215
		SocialDetail.Builder social = SocialDetail.newBuilder()
				.setUid(this.getUid())
Yazawazi's avatar
Yazawazi committed
1216
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()))
Asnxthaony's avatar
Asnxthaony committed
1217
1218
1219
1220
1221
1222
				.setNickname(this.getNickname())
				.setSignature(this.getSignature())
				.setLevel(this.getLevel())
				.setBirthday(this.getBirthday().getFilledProtoWhenNotEmpty())
				.setWorldLevel(this.getWorldLevel())
				.setNameCardId(this.getNameCardId())
Yazawazi's avatar
Yazawazi committed
1223
1224
				.setIsShowAvatar(this.isShowAvatars())
				.addAllShowAvatarInfoList(socialShowAvatarInfoList)
Asnxthaony's avatar
Asnxthaony committed
1225
				.setFinishAchievementNum(0);
Melledy's avatar
Melledy committed
1226
1227
		return social;
	}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237

	public List<ShowAvatarInfoOuterClass.ShowAvatarInfo> getShowAvatarInfoList() {
		List<ShowAvatarInfoOuterClass.ShowAvatarInfo> showAvatarInfoList = new ArrayList<>();

		Player player;
		boolean shouldRecalc;
		if (this.isOnline()) {
			player = this;
			shouldRecalc = false;
		} else {
1238
			player = DatabaseHelper.getPlayerByUid(id);
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
			player.getAvatars().loadFromDatabase();
			player.getInventory().loadFromDatabase();
			shouldRecalc = true;
		}

		List<Integer> showAvatarList = player.getShowAvatarList();
		AvatarStorage avatars = player.getAvatars();
		if (showAvatarList != null) {
			for (int avatarId : showAvatarList) {
				Avatar avatar = avatars.getAvatarById(avatarId);
				if (shouldRecalc) {
					avatar.recalcStats();
				}
				showAvatarInfoList.add(avatar.toShowAvatarInfoProto());
			}
		}
		return showAvatarInfoList;
	}
KingRainbow44's avatar
KingRainbow44 committed
1257

1258
1259
	public PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo getWorldPlayerLocationInfo() {
		return PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1260
1261
1262
				.setSceneId(this.getSceneId())
				.setPlayerLoc(this.getPlayerLocationInfo())
				.build();
Melledy's avatar
Melledy committed
1263
	}
Asnxthaony's avatar
Asnxthaony committed
1264

Melledy's avatar
Melledy committed
1265
	public PlayerLocationInfo getPlayerLocationInfo() {
Asnxthaony's avatar
Asnxthaony committed
1266
1267
1268
1269
1270
		return PlayerLocationInfo.newBuilder()
				.setUid(this.getUid())
				.setPos(this.getPos().toProto())
				.setRot(this.getRotation().toProto())
				.build();
Melledy's avatar
Melledy committed
1271
	}
Asnxthaony's avatar
Asnxthaony committed
1272

Angda Song's avatar
Angda Song committed
1273
1274
1275
1276
	public MapMarksManager getMapMarksManager() {
		return mapMarksManager;
	}

1277
	public StaminaManager getStaminaManager() { return staminaManager; }
1278

1279
1280
	public SotSManager getSotSManager() { return sotsManager; }

1281
1282
1283
1284
	public EnergyManager getEnergyManager() {
		return this.energyManager;
	}

ImmuState's avatar
ImmuState committed
1285
1286
1287
1288
	public ResinManager getResinManager() {
		return this.resinManager;
	}

1289
1290
1291
1292
	public ForgingManager getForgingManager() {
		return this.forgingManager;
	}

Akka's avatar
Akka committed
1293
1294
1295
1296
	public FurnitureManager getFurnitureManager() {
		return furnitureManager;
	}

1297
1298
1299
	public BattlePassManager getBattlePassManager(){
		return battlePassManager;
	}
KingRainbow44's avatar
KingRainbow44 committed
1300

1301
1302
1303
	public void loadBattlePassManager() {
		if (this.battlePassManager != null) return;
		this.battlePassManager = DatabaseHelper.loadBattlePass(this);
1304
		this.battlePassManager.getMissions().values().removeIf(mission -> mission.getData() == null);
1305
	}
1306

Melledy's avatar
Melledy committed
1307
1308
1309
1310
	public AbilityManager getAbilityManager() {
		return abilityManager;
	}

1311
1312
1313
1314
	public DeforestationManager getDeforestationManager() {
		return deforestationManager;
	}

1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
	public CollectionManager getCollectionManager() {
		if(this.collectionManager==null){
			this.collectionManager = new CollectionManager();
		}
		return this.collectionManager;
	}

	public CollectionRecordStore getCollectionRecordStore() {
		if(this.collectionRecordStore==null){
			this.collectionRecordStore = new CollectionRecordStore();
		}
		return collectionRecordStore;
	}

gentlespoon's avatar
gentlespoon committed
1329
1330
1331
1332
	public HashMap<String, MapMark> getMapMarks() { return mapMarks; }

	public void setMapMarks(HashMap<String, MapMark> newMarks) { mapMarks = newMarks; }

Melledy's avatar
Melledy committed
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
	public synchronized void onTick() {
		// Check ping
		if (this.getLastPingTime() > System.currentTimeMillis() + 60000) {
			this.getSession().close();
			return;
		}
		// Check co-op requests
		Iterator<CoopRequest> it = this.getCoopRequests().values().iterator();
		while (it.hasNext()) {
			CoopRequest req = it.next();
			if (req.isExpired()) {
1344
1345
1346
1347
				req.getRequester().sendPacket(new PacketPlayerApplyEnterMpResultNotify(
						this,
						false,
						PlayerApplyEnterMpResultNotifyOuterClass.PlayerApplyEnterMpResultNotify.Reason.REASON_SYSTEM_JUDGE));
Melledy's avatar
Melledy committed
1348
1349
1350
1351
1352
				it.remove();
			}
		}
		// Ping
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1353
1354
			// RTT notify - very important to send this often
			this.sendPacket(new PacketWorldPlayerRTTNotify(this.getWorld()));
Asnxthaony's avatar
Asnxthaony committed
1355

Melledy's avatar
Melledy committed
1356
1357
1358
1359
1360
1361
1362
			// Update player locations if in multiplayer every 5 seconds
			long time = System.currentTimeMillis();
			if (this.getWorld().isMultiplayer() && this.getScene() != null && time > nextSendPlayerLocTime) {
				this.sendPacket(new PacketWorldPlayerLocationNotify(this.getWorld()));
				this.sendPacket(new PacketScenePlayerLocationNotify(this.getScene()));
				this.resetSendPlayerLocTime();
			}
Melledy's avatar
Melledy committed
1363
		}
1364
1365
1366
1367

		// Handle daily reset.
		this.doDailyReset();

Kinesis's avatar
Kinesis committed
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
		// Expedition
		var timeNow = Utils.getCurrentSeconds();
		var needNotify = false;
		for (Long key : expeditionInfo.keySet()) {
			ExpeditionInfo e = expeditionInfo.get(key);
			if(e.getState() == 1){
				if(timeNow - e.getStartTime() >= e.getHourTime() * 60 * 60){
					e.setState(2);
					needNotify = true;
				}
			}
		}
		if(needNotify){
			this.save();
			this.sendPacket(new PacketAvatarExpeditionDataNotify(this));
		}
1384
1385
1386

		// Send updated forge queue data, if necessary.
		this.getForgingManager().sendPlayerForgingUpdate();
ImmuState's avatar
ImmuState committed
1387
1388
1389

		// Recharge resin.
		this.getResinManager().rechargeResin();
1390
1391
	}

1392
	private synchronized void doDailyReset() {
1393
1394
1395
1396
1397
		// Check if we should execute a daily reset on this tick.
		int currentTime = Utils.getCurrentSeconds();

		var currentDate = LocalDate.ofInstant(Instant.ofEpochSecond(currentTime), ZoneId.systemDefault());
		var lastResetDate = LocalDate.ofInstant(Instant.ofEpochSecond(this.getLastDailyReset()), ZoneId.systemDefault());
1398

1399
1400
1401
1402
1403
1404
1405
1406
		if (!currentDate.isAfter(lastResetDate)) {
			return;
		}

		// We should - now execute all the resetting logic we need.
		// Reset forge points.
		this.setForgePoints(300_000);

1407
1408
1409
		// Reset daily BP missions.
		this.getBattlePassManager().resetDailyMissions();

1410
1411
1412
1413
		// Trigger login BP mission, so players who are online during the reset
		// don't have to relog to clear the mission.
		this.getBattlePassManager().triggerMission(WatcherTriggerType.TRIGGER_LOGIN);

1414
1415
1416
1417
1418
		// Reset weekly BP missions.
		if (currentDate.getDayOfWeek() == DayOfWeek.MONDAY) {
			this.getBattlePassManager().resetWeeklyMissions();
		}

1419
1420
1421
		// Done. Update last reset time.
		this.setLastDailyReset(currentTime);
	}
1422

Melledy's avatar
Melledy committed
1423
1424
1425
	public void resetSendPlayerLocTime() {
		this.nextSendPlayerLocTime = System.currentTimeMillis() + 5000;
	}
Melledy's avatar
Melledy committed
1426
1427
1428

	@PostLoad
	private void onLoad() {
1429
		this.getCodex().setPlayer(this);
Melledy's avatar
Melledy committed
1430
		this.getTeamManager().setPlayer(this);
1431
		this.getTowerManager().setPlayer(this);
Melledy's avatar
Melledy committed
1432
	}
Asnxthaony's avatar
Asnxthaony committed
1433

Melledy's avatar
Melledy committed
1434
1435
1436
	public void save() {
		DatabaseHelper.savePlayer(this);
	}
KingRainbow44's avatar
KingRainbow44 committed
1437

1438
1439
	// Called from tokenrsp
	public void loadFromDatabase() {
Melledy's avatar
Melledy committed
1440
		// Make sure these exist
1441
1442
1443
		if (this.getTowerManager() == null) {
			this.towerManager = new TowerManager(this);
		}
Melledy's avatar
Melledy committed
1444
1445
		if (this.getTeamManager() == null) {
			this.teamManager = new TeamManager(this);
Asnxthaony's avatar
Asnxthaony committed
1446
		}
1447
1448
1449
		if (this.getCodex() == null) {
			this.codex = new PlayerCodex(this);
		}
Asnxthaony's avatar
Asnxthaony committed
1450
		if (this.getProfile().getUid() == 0) {
1451
			this.getProfile().syncWithCharacter(this);
Melledy's avatar
Melledy committed
1452
		}
1453
1454
		//Make sure towerManager's player is online player
		this.getTowerManager().setPlayer(this);
1455
		this.getCollectionManager().setPlayer(this);
KingRainbow44's avatar
KingRainbow44 committed
1456

Melledy's avatar
Melledy committed
1457
1458
1459
		// Load from db
		this.getAvatars().loadFromDatabase();
		this.getInventory().loadFromDatabase();
1460
		this.getAvatars().postLoad(); // Needs to be called after inventory is handled
Asnxthaony's avatar
Asnxthaony committed
1461

Melledy's avatar
Melledy committed
1462
		this.getFriendsList().loadFromDatabase();
1463
		this.getMailHandler().loadFromDatabase();
Melledy's avatar
Melledy committed
1464
		this.getQuestManager().loadFromDatabase();
KingRainbow44's avatar
KingRainbow44 committed
1465

1466
		this.loadBattlePassManager();
1467
1468
1469
	}

	public void onLogin() {
Melledy's avatar
Melledy committed
1470
1471
1472
1473
1474
1475
1476
1477
		// Quest - Commented out because a problem is caused if you log out while this quest is active
		/*
		if (getQuestManager().getMainQuestById(351) == null) {
			GameQuest quest = getQuestManager().addQuest(35104);
			if (quest != null) {
				quest.finish();
			}
			getQuestManager().addQuest(35101);
KingRainbow44's avatar
KingRainbow44 committed
1478

Melledy's avatar
Melledy committed
1479
1480
1481
1482
			this.setSceneId(3);
			this.getPos().set(GameConstants.START_POSITION);
		}
		*/
KingRainbow44's avatar
KingRainbow44 committed
1483

Melledy's avatar
Melledy committed
1484
1485
1486
		// Create world
		World world = new World(this);
		world.addPlayer(this);
Asnxthaony's avatar
Asnxthaony committed
1487
1488

		// Multiplayer setting
AnimeGitB's avatar
AnimeGitB committed
1489
1490
		this.setProperty(PlayerProperty.PROP_PLAYER_MP_SETTING_TYPE, this.getMpSetting().getNumber(), false);
		this.setProperty(PlayerProperty.PROP_IS_MP_MODE_AVAILABLE, 1, false);
Asnxthaony's avatar
Asnxthaony committed
1491

1492
1493
1494
		// Execute daily reset logic if this is a new day.
		this.doDailyReset();

Melledy's avatar
Melledy committed
1495
1496
1497
1498
1499
		// Packets
		session.send(new PacketPlayerDataNotify(this)); // Player data
		session.send(new PacketStoreWeightLimitNotify());
		session.send(new PacketPlayerStoreNotify(this));
		session.send(new PacketAvatarDataNotify(this));
Melledy's avatar
Melledy committed
1500
		session.send(new PacketFinishedParentQuestNotify(this));
1501
		session.send(new PacketBattlePassAllDataNotify(this));
Melledy's avatar
Melledy committed
1502
		session.send(new PacketQuestListNotify(this));
1503
		session.send(new PacketCodexDataFullNotify(this));
1504
1505
		session.send(new PacketAllWidgetDataNotify(this));
		session.send(new PacketWidgetGadgetAllDataNotify());
ImmuState's avatar
ImmuState committed
1506
		session.send(new PacketCombineDataNotify(this.unlockedCombines));
1507
		this.forgingManager.sendForgeDataNotify();
ImmuState's avatar
ImmuState committed
1508
		this.resinManager.onPlayerLogin();
Yazawazi's avatar
Yazawazi committed
1509
1510
		getTodayMoonCard(); // The timer works at 0:0, some users log in after that, use this method to check if they have received a reward today or not. If not, send the reward.

1511
1512
		// Battle Pass trigger
		this.getBattlePassManager().triggerMission(WatcherTriggerType.TRIGGER_LOGIN);
Akka's avatar
Akka committed
1513

Akka's avatar
Akka committed
1514
		this.furnitureManager.onLogin();
1515
1516
1517
		// Home
		home = GameHome.getByUid(getUid());
		home.onOwnerLogin(this);
Akka's avatar
Akka committed
1518
1519
        // Activity
        activityManager = new ActivityManager(this);
1520

Melledy's avatar
Melledy committed
1521
		session.send(new PacketPlayerEnterSceneNotify(this)); // Enter game world
1522
		session.send(new PacketPlayerLevelRewardUpdateNotify(rewardedLevels));
Melledy's avatar
Melledy committed
1523
1524
1525
1526
		session.send(new PacketOpenStateUpdateNotify());

		// First notify packets sent
		this.setHasSentAvatarDataNotify(true);
KingRainbow44's avatar
KingRainbow44 committed
1527

1528
1529
		// Set session state
		session.setState(SessionState.ACTIVE);
1530
1531
1532

		// Call join event.
		PlayerJoinEvent event = new PlayerJoinEvent(this); event.call();
1533
		if(event.isCanceled()){ // If event is not cancelled, continue.
1534
			session.close();
1535
1536
			return;
		}
KingRainbow44's avatar
KingRainbow44 committed
1537

1538
1539
1540
		// register
		getServer().registerPlayer(this);
		getProfile().setPlayer(this); // Set online
Melledy's avatar
Melledy committed
1541
	}
Asnxthaony's avatar
Asnxthaony committed
1542

Melledy's avatar
Melledy committed
1543
	public void onLogout() {
1544
1545
1546
		try{
			// stop stamina calculation
			getStaminaManager().stopSustainedStaminaHandler();
1547

1548
			// force to leave the dungeon (inside has a "if")
1549
			this.getServer().getDungeonManager().exitDungeon(this);
Asnxthaony's avatar
Asnxthaony committed
1550

1551
1552
1553
1554
1555
1556
1557
1558
			// Leave world
			if (this.getWorld() != null) {
				this.getWorld().removePlayer(this);
			}

			// Status stuff
			this.getProfile().syncWithCharacter(this);
			this.getProfile().setPlayer(null); // Set offline
Asnxthaony's avatar
Asnxthaony committed
1559

1560
			this.getCoopRequests().clear();
Asnxthaony's avatar
Asnxthaony committed
1561

1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
			// Save to db
			this.save();
			this.getTeamManager().saveAvatars();
			this.getFriendsList().save();

			// Call quit event.
			PlayerQuitEvent event = new PlayerQuitEvent(this); event.call();
		}catch (Throwable e){
			e.printStackTrace();
			Grasscutter.getLogger().warn("Player (UID {}) save failure", getUid());
		}finally {
			removeFromServer();
		}
	}
1576

1577
1578
1579
1580
1581
	public void removeFromServer() {
		// Remove from server.
		//Note: DON'T DELETE BY UID,BECAUSE THERE ARE MULTIPLE SAME UID PLAYERS WHEN DUPLICATED LOGIN!
		//so I decide to delete by object rather than uid
		getServer().getPlayers().values().removeIf(player1 -> player1 == this);
Melledy's avatar
Melledy committed
1582
	}
Asnxthaony's avatar
Asnxthaony committed
1583

Melledy's avatar
Melledy committed
1584
	public enum SceneLoadState {
Asnxthaony's avatar
Asnxthaony committed
1585
1586
		NONE(0), LOADING(1), INIT(2), LOADED(3);

Melledy's avatar
Melledy committed
1587
		private final int value;
Asnxthaony's avatar
Asnxthaony committed
1588

Melledy's avatar
Melledy committed
1589
1590
1591
		private SceneLoadState(int value) {
			this.value = value;
		}
Asnxthaony's avatar
Asnxthaony committed
1592

Melledy's avatar
Melledy committed
1593
1594
1595
1596
		public int getValue() {
			return this.value;
		}
	}
1597
1598
1599
1600

	public void setMessageHandler(MessageHandler messageHandler) {
		this.messageHandler = messageHandler;
	}
1601

AnimeGitB's avatar
AnimeGitB committed
1602
1603
1604
1605
1606
1607
1608
	public int getPropertyMin(PlayerProperty prop) {
		if (prop.getDynamicRange()) {
			return switch (prop) {
				default -> 0;
			};
		} else {
			return prop.getMin();
1609
		}
AnimeGitB's avatar
AnimeGitB committed
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
	}

	public int getPropertyMax(PlayerProperty prop) {
		if (prop.getDynamicRange()) {
			return switch (prop) {
				case PROP_CUR_SPRING_VOLUME -> getProperty(PlayerProperty.PROP_MAX_SPRING_VOLUME);
				case PROP_CUR_PERSIST_STAMINA -> getProperty(PlayerProperty.PROP_MAX_STAMINA);
				default -> 0;
			};
		} else {
			return prop.getMax();
		}
	}

	private boolean setPropertyWithSanityCheck(PlayerProperty prop, int value, boolean sendPacket) {
		int min = this.getPropertyMin(prop);
		int max = this.getPropertyMax(prop);
		if (min <= value && value <= max) {
			this.properties.put(prop.getId(), value);
			if (sendPacket) {
				// Update player with packet
				this.sendPacket(new PacketPlayerPropNotify(this, prop));
			}
			return true;
		} else {
1635
1636
		return false;
	}
AnimeGitB's avatar
AnimeGitB committed
1637
	}
1638

Melledy's avatar
Melledy committed
1639
}