Player.java 43.8 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;
Melledy's avatar
Melledy committed
10
11
import emu.grasscutter.game.Account;
import emu.grasscutter.game.CoopRequest;
Melledy's avatar
Melledy committed
12
import emu.grasscutter.game.ability.AbilityManager;
Akka's avatar
Akka committed
13
import emu.grasscutter.game.activity.ActivityManager;
14
import emu.grasscutter.game.avatar.Avatar;
Kengxxiao's avatar
Kengxxiao committed
15
import emu.grasscutter.game.avatar.AvatarStorage;
16
import emu.grasscutter.game.battlepass.BattlePassManager;
17
18
import emu.grasscutter.game.entity.EntityMonster;
import emu.grasscutter.game.entity.EntityVehicle;
19
import emu.grasscutter.game.home.GameHome;
20
import emu.grasscutter.game.entity.EntityGadget;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.game.entity.EntityItem;
22
import emu.grasscutter.game.entity.GameEntity;
Kinesis's avatar
Kinesis committed
23
import emu.grasscutter.game.expedition.ExpeditionInfo;
Melledy's avatar
Melledy committed
24
25
26
import emu.grasscutter.game.friends.FriendsList;
import emu.grasscutter.game.friends.PlayerProfile;
import emu.grasscutter.game.gacha.PlayerGachaInfo;
27
import emu.grasscutter.game.inventory.GameItem;
Melledy's avatar
Melledy committed
28
import emu.grasscutter.game.inventory.Inventory;
Melledy's avatar
Melledy committed
29
import emu.grasscutter.game.mail.Mail;
30
import emu.grasscutter.game.mail.MailHandler;
31
import emu.grasscutter.game.managers.CookingManager;
Akka's avatar
Akka committed
32
import emu.grasscutter.game.managers.FurnitureManager;
33
import emu.grasscutter.game.managers.InsectCaptureManager;
ImmuState's avatar
ImmuState committed
34
import emu.grasscutter.game.managers.ResinManager;
35
import emu.grasscutter.game.managers.collection.CollectionRecordStore;
Melledy's avatar
Melledy committed
36
37
38
39
40
41
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;
42
import emu.grasscutter.game.managers.SotSManager;
Melledy's avatar
Melledy committed
43
import emu.grasscutter.game.props.ActionReason;
AnimeGitB's avatar
AnimeGitB committed
44
import emu.grasscutter.game.props.ClimateType;
Melledy's avatar
Melledy committed
45
import emu.grasscutter.game.props.PlayerProperty;
46
import emu.grasscutter.game.props.WatcherTriggerType;
Melledy's avatar
Melledy committed
47
import emu.grasscutter.game.quest.QuestManager;
Kengxxiao's avatar
Kengxxiao committed
48
import emu.grasscutter.game.shop.ShopLimit;
49
import emu.grasscutter.game.tower.TowerData;
Akka's avatar
Akka committed
50
import emu.grasscutter.game.tower.TowerManager;
Melledy's avatar
Melledy committed
51
52
import emu.grasscutter.game.world.Scene;
import emu.grasscutter.game.world.World;
53
import emu.grasscutter.net.packet.BasePacket;
54
import emu.grasscutter.net.proto.*;
Melledy's avatar
Melledy committed
55
import emu.grasscutter.net.proto.AbilityInvokeEntryOuterClass.AbilityInvokeEntry;
56
import emu.grasscutter.net.proto.AttackResultOuterClass.AttackResult;
Melledy's avatar
Melledy committed
57
import emu.grasscutter.net.proto.CombatInvokeEntryOuterClass.CombatInvokeEntry;
ImmuState's avatar
ImmuState committed
58
import emu.grasscutter.net.proto.GadgetInteractReqOuterClass.GadgetInteractReq;
Melledy's avatar
Melledy committed
59
60
61
62
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
63
import emu.grasscutter.net.proto.ProfilePictureOuterClass.ProfilePicture;
Melledy's avatar
Melledy committed
64
import emu.grasscutter.net.proto.SocialDetailOuterClass.SocialDetail;
65
import emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType;
66
67
import emu.grasscutter.server.event.player.PlayerJoinEvent;
import emu.grasscutter.server.event.player.PlayerQuitEvent;
Melledy's avatar
Melledy committed
68
69
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.game.GameSession;
70
import emu.grasscutter.server.game.GameSession.SessionState;
71
import emu.grasscutter.server.packet.send.*;
Yazawazi's avatar
utils    
Yazawazi committed
72
import emu.grasscutter.utils.DateHelper;
Kengxxiao's avatar
Kengxxiao committed
73
import emu.grasscutter.utils.Position;
74
import emu.grasscutter.utils.MessageHandler;
Kinesis's avatar
Kinesis committed
75
import emu.grasscutter.utils.Utils;
Melledy's avatar
Melledy committed
76
77
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
AnimeGitB's avatar
AnimeGitB committed
78
import lombok.Getter;
Melledy's avatar
Melledy committed
79

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

87
88
import static emu.grasscutter.Configuration.*;

KingRainbow44's avatar
KingRainbow44 committed
89
@Entity(value = "players", useDiscriminator = false)
90
public class Player {
Melledy's avatar
Melledy committed
91
92
	@Id private int id;
	@Indexed(options = @IndexOptions(unique = true)) private String accountId;
Melledy's avatar
Melledy committed
93
94
95
	private transient Account account;
	private transient GameSession session;
	
Melledy's avatar
Melledy committed
96
97
98
99
	private String nickname;
	private String signature;
	private int headImage;
	private int nameCardId = 210001;
100
	private Position position;
Melledy's avatar
Melledy committed
101
	private Position rotation;
Miyucchi's avatar
Miyucchi committed
102
	private PlayerBirthday birthday;
103
	private PlayerCodex codex;
Melledy's avatar
Melledy committed
104
105
	private boolean showAvatars;
	private List<Integer> showAvatarList;
Melledy's avatar
Melledy committed
106
	private Map<Integer, Integer> properties;
Melledy's avatar
Melledy committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	private int currentRealmId;
	private int widgetId;
	private int sceneId;
    private int regionId;
    private int mainCharacterId;
    private boolean godmode;
    private boolean stamina;
    
    @Getter private Set<Integer> nameCardList;
    @Getter private Set<Integer> flyCloakList;
    @Getter private Set<Integer> costumeList;
    @Getter private Set<Integer> rewardedLevels;
    @Getter private Set<Integer> realmList;
    @Getter private Set<Integer> unlockedForgingBlueprints;
    @Getter private Set<Integer> unlockedCombines;
    @Getter private Set<Integer> unlockedFurniture;
    @Getter private Set<Integer> unlockedFurnitureSuite;
    @Getter private Map<Long, ExpeditionInfo> expeditionInfo;
    @Getter private Map<Integer, Integer> unlockedRecipies;
    @Getter private List<ActiveForgeData> activeForges;
127

Melledy's avatar
Melledy committed
128
129
130
	@Transient private long nextGuid = 0;
	@Transient private int peerId;
	@Transient private World world;
131
	@Transient private Scene scene;
AnimeGitB's avatar
AnimeGitB committed
132
133
	@Transient @Getter private int weatherId = 0;
	@Transient @Getter private ClimateType climate = ClimateType.CLIMATE_SUNNY;
Melledy's avatar
Melledy committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
	
	// Player managers go here
	@Getter private transient AvatarStorage avatars;
	@Getter private transient Inventory inventory;
	@Getter private transient FriendsList friendsList;
	@Getter private transient MailHandler mailHandler;
	@Getter private transient MessageHandler messageHandler;
	@Getter private transient AbilityManager abilityManager;
	@Getter private transient QuestManager questManager;
	@Getter private transient TowerManager towerManager;
	@Getter private transient SotSManager sotsManager;
	@Getter private transient InsectCaptureManager insectCaptureManager;
    @Getter private transient MapMarksManager mapMarksManager;
    @Getter private transient StaminaManager staminaManager;
    @Getter private transient EnergyManager energyManager;
    @Getter private transient ResinManager resinManager;
    @Getter private transient ForgingManager forgingManager;
    @Getter private transient DeforestationManager deforestationManager;
    @Getter private transient FurnitureManager furnitureManager;
    @Getter private transient BattlePassManager battlePassManager;
    @Getter private transient CookingManager cookingManager;
    @Getter private transient ActivityManager activityManager;

    // Manager data (Save-able to the database)
    private PlayerProfile playerProfile;
    private TeamManager teamManager;
160
	private TowerData towerData;
Melledy's avatar
Melledy committed
161
	private PlayerGachaInfo gachaInfo;
Melledy's avatar
Melledy committed
162
163
	private PlayerOpenStateManager openStateManager;
	private CollectionRecordStore collectionRecordStore;
Kengxxiao's avatar
Kengxxiao committed
164
	private ArrayList<ShopLimit> shopLimit;
Melledy's avatar
Melledy committed
165
166
	
	@Getter private transient GameHome home;
AnimeGitB's avatar
AnimeGitB committed
167

Yazawazi's avatar
Yazawazi committed
168
169
170
171
172
	private boolean moonCard;
	private Date moonCardStartTime;
	private int moonCardDuration;
	private Set<Date> moonCardGetTimes;

Melledy's avatar
Melledy committed
173
174
175
176
	@Transient private boolean paused;
	@Transient private int enterSceneToken;
	@Transient private SceneLoadState sceneState;
	@Transient private boolean hasSentAvatarDataNotify;
Melledy's avatar
Melledy committed
177
	@Transient private long nextSendPlayerLocTime = 0;
Asnxthaony's avatar
Asnxthaony committed
178

Melledy's avatar
Melledy committed
179
180
181
182
183
	private transient final Int2ObjectMap<CoopRequest> coopRequests;
	private transient final Queue<AttackResult> attackResults;
	@Getter private transient final InvokeHandler<CombatInvokeEntry> combatInvokeHandler;
	@Getter private transient final InvokeHandler<AbilityInvokeEntry> abilityInvokeHandler;
	@Getter private transient final InvokeHandler<AbilityInvokeEntry> clientAbilityInitFinishHandler;
184

185
	private long springLastUsed;
gentlespoon's avatar
gentlespoon committed
186
	private HashMap<String, MapMark> mapMarks;
ImmuState's avatar
ImmuState committed
187
	private int nextResinRefresh;
188
	private int lastDailyReset;
189

Asnxthaony's avatar
Asnxthaony committed
190
191
	@Deprecated
	@SuppressWarnings({"rawtypes", "unchecked"}) // Morphia only!
192
	public Player() {
Melledy's avatar
Melledy committed
193
194
195
		this.inventory = new Inventory(this);
		this.avatars = new AvatarStorage(this);
		this.friendsList = new FriendsList(this);
196
		this.mailHandler = new MailHandler(this);
197
		this.towerManager = new TowerManager(this);
Melledy's avatar
Melledy committed
198
		this.abilityManager = new AbilityManager(this);
199
		this.deforestationManager = new DeforestationManager(this);
200
		this.insectCaptureManager = new InsectCaptureManager(this);
Melledy's avatar
Melledy committed
201
202
		this.questManager = new QuestManager(this);
		
203
		this.position = new Position();
Melledy's avatar
Melledy committed
204
205
206
207
208
209
210
211
		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
212

213
214
215
216
		this.gachaInfo = new PlayerGachaInfo();
		this.nameCardList = new HashSet<>();
		this.flyCloakList = new HashSet<>();
		this.costumeList = new HashSet<>();
217
		this.towerData = new TowerData();
218
		this.collectionRecordStore = new CollectionRecordStore();
219
		this.unlockedForgingBlueprints = new HashSet<>();
ImmuState's avatar
ImmuState committed
220
		this.unlockedCombines = new HashSet<>();
Akka's avatar
Akka committed
221
222
		this.unlockedFurniture = new HashSet<>();
		this.unlockedFurnitureSuite = new HashSet<>();
223
		this.activeForges = new ArrayList<>();
224
		this.unlockedRecipies = new HashMap<>();
Asnxthaony's avatar
Asnxthaony committed
225

Melledy's avatar
Melledy committed
226
227
228
		this.setSceneId(3);
		this.setRegionId(1);
		this.sceneState = SceneLoadState.NONE;
Asnxthaony's avatar
Asnxthaony committed
229

230
		this.attackResults = new LinkedBlockingQueue<>();
Melledy's avatar
Melledy committed
231
232
233
		this.coopRequests = new Int2ObjectOpenHashMap<>();
		this.combatInvokeHandler = new InvokeHandler(PacketCombatInvocationsNotify.class);
		this.abilityInvokeHandler = new InvokeHandler(PacketAbilityInvocationsNotify.class);
234
		this.clientAbilityInitFinishHandler = new InvokeHandler(PacketClientAbilityInitFinishNotify.class);
Miyucchi's avatar
Miyucchi committed
235
236

		this.birthday = new PlayerBirthday();
237
		this.rewardedLevels = new HashSet<>();
Yazawazi's avatar
Yazawazi committed
238
		this.moonCardGetTimes = new HashSet<>();
239
		this.codex = new PlayerCodex(this);
akatatsu27's avatar
akatatsu27 committed
240
        this.openStateManager = new PlayerOpenStateManager(this);
Kengxxiao's avatar
Kengxxiao committed
241
		this.shopLimit = new ArrayList<>();
Kinesis's avatar
Kinesis committed
242
		this.expeditionInfo = new HashMap<>();
243
		this.messageHandler = null;
gentlespoon's avatar
gentlespoon committed
244
		this.mapMarksManager = new MapMarksManager(this);
245
		this.staminaManager = new StaminaManager(this);
246
		this.sotsManager = new SotSManager(this);
247
		this.energyManager = new EnergyManager(this);
ImmuState's avatar
ImmuState committed
248
		this.resinManager = new ResinManager(this);
249
		this.forgingManager = new ForgingManager(this);
Akka's avatar
Akka committed
250
		this.furnitureManager = new FurnitureManager(this);
251
		this.cookingManager = new CookingManager(this);
Melledy's avatar
Melledy committed
252
	}
Asnxthaony's avatar
Asnxthaony committed
253

Melledy's avatar
Melledy committed
254
	// On player creation
255
	public Player(GameSession session) {
Melledy's avatar
Melledy committed
256
257
258
259
260
261
262
		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
263
		this.birthday = new PlayerBirthday();
264
		this.codex = new PlayerCodex(this);
AnimeGitB's avatar
AnimeGitB committed
265
266
267
268
269
270
271
272
		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
273
274
		this.getFlyCloakList().add(140001);
		this.getNameCardList().add(210001);
275
		this.getPosition().set(GameConstants.START_POSITION);
Melledy's avatar
Melledy committed
276
		this.getRotation().set(0, 307, 0);
277
		this.messageHandler = null;
gentlespoon's avatar
gentlespoon committed
278
		this.mapMarksManager = new MapMarksManager(this);
279
		this.staminaManager = new StaminaManager(this);
280
		this.sotsManager = new SotSManager(this);
281
		this.energyManager = new EnergyManager(this);
ImmuState's avatar
ImmuState committed
282
		this.resinManager = new ResinManager(this);
283
		this.deforestationManager = new DeforestationManager(this);
284
		this.forgingManager = new ForgingManager(this);
Akka's avatar
Akka committed
285
		this.furnitureManager = new FurnitureManager(this);
286
		this.cookingManager = new CookingManager(this);
Melledy's avatar
Melledy committed
287
288
	}

289
	public int getUid() {
Melledy's avatar
Melledy committed
290
291
292
		return id;
	}

293
	public void setUid(int id) {
Melledy's avatar
Melledy committed
294
295
		this.id = id;
	}
Asnxthaony's avatar
Asnxthaony committed
296

297
	public long getNextGameGuid() {
Melledy's avatar
Melledy committed
298
		long nextId = ++this.nextGuid;
299
		return ((long) this.getUid() << 32) + nextId;
Melledy's avatar
Melledy committed
300
301
302
	}

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

	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
319

Melledy's avatar
Melledy committed
320
321
322
	public boolean isOnline() {
		return this.getSession() != null && this.getSession().isActive();
	}
Asnxthaony's avatar
Asnxthaony committed
323

Melledy's avatar
Melledy committed
324
325
326
	public GameServer getServer() {
		return this.getSession().getServer();
	}
Asnxthaony's avatar
Asnxthaony committed
327

Melledy's avatar
Melledy committed
328
329
330
	public synchronized World getWorld() {
		return this.world;
	}
Asnxthaony's avatar
Asnxthaony committed
331

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

336
	public synchronized Scene getScene() {
337
338
339
		return scene;
	}

340
	public synchronized void setScene(Scene scene) {
341
342
343
		this.scene = scene;
	}

AnimeGitB's avatar
AnimeGitB committed
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	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
366
367
368
369
370
371
372
373
	public String getNickname() {
		return nickname;
	}

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

Melledy's avatar
Melledy committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
	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();
	}

393
394
395
396
397
398
399
400
	public Integer getWidgetId() {
		return widgetId;
	}

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

401
402
403
404
405
406
407
408
409
410
411
412
413
	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);
	}

Melledy's avatar
Melledy committed
414
	public int getCurrentRealmId() {
415
416
417
		return currentRealmId;
	}

Melledy's avatar
Melledy committed
418
	public void setCurrentRealmId(int currentRealmId) {
419
420
		this.currentRealmId = currentRealmId;
	}
Melledy's avatar
Melledy committed
421
	
422
423
	public Position getPosition() {
		return position;
Melledy's avatar
Melledy committed
424
	}
Asnxthaony's avatar
Asnxthaony committed
425

Melledy's avatar
Melledy committed
426
427
428
	public Position getRotation() {
		return rotation;
	}
Asnxthaony's avatar
Asnxthaony committed
429

Melledy's avatar
Melledy committed
430
431
432
	public int getLevel() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_LEVEL);
	}
Asnxthaony's avatar
Asnxthaony committed
433

434
	public boolean setLevel(int level) {
435
436
437
438
		if (this.getLevel() == level) {
			return true;
		}

439
		if (this.setProperty(PlayerProperty.PROP_PLAYER_LEVEL, level)) {
440
			// Update world level and profile.
441
442
			this.updateWorldLevel();
			this.updateProfile();
443
444

			// Handle OpenState unlocks from level-up.
445
			this.getOpenStateManager().unlockLevelDependentStates();
446

447
448
449
			return true;
		}
		return false;
450
451
	}

Melledy's avatar
Melledy committed
452
453
454
	public int getExp() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_EXP);
	}
Asnxthaony's avatar
Asnxthaony committed
455

Melledy's avatar
Melledy committed
456
457
458
	public int getWorldLevel() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_WORLD_LEVEL);
	}
akatatsu27's avatar
akatatsu27 committed
459

460
461
462
463
464
465
466
467
	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;
468
	}
Asnxthaony's avatar
Asnxthaony committed
469

470
471
472
473
	public int getForgePoints() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_FORGE_POINT);
	}

474
	public boolean setForgePoints(int value) {
475
		if (value == this.getForgePoints()) {
476
			return true;
477
478
		}

479
		return this.setProperty(PlayerProperty.PROP_PLAYER_FORGE_POINT, value);
480
481
	}

Melledy's avatar
Melledy committed
482
483
484
	public int getPrimogems() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_HCOIN);
	}
Asnxthaony's avatar
Asnxthaony committed
485

486
487
	public boolean setPrimogems(int primogem) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_HCOIN, primogem);
Melledy's avatar
Melledy committed
488
	}
Asnxthaony's avatar
Asnxthaony committed
489

Melledy's avatar
Melledy committed
490
491
492
	public int getMora() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_SCOIN);
	}
Asnxthaony's avatar
Asnxthaony committed
493

494
495
	public boolean setMora(int mora) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_SCOIN, mora);
Melledy's avatar
Melledy committed
496
	}
KingRainbow44's avatar
KingRainbow44 committed
497

498
499
500
501
	public int getCrystals() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_MCOIN);
	}

502
503
	public boolean setCrystals(int crystals) {
		return this.setProperty(PlayerProperty.PROP_PLAYER_MCOIN, crystals);
504
	}
Asnxthaony's avatar
Asnxthaony committed
505

506
507
508
509
	public int getHomeCoin() {
		return this.getProperty(PlayerProperty.PROP_PLAYER_HOME_COIN);
	}

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

Melledy's avatar
Melledy committed
518
	private float getExpModifier() {
519
		return GAME_OPTIONS.rates.adventureExp;
Melledy's avatar
Melledy committed
520
	}
Asnxthaony's avatar
Asnxthaony committed
521

Melledy's avatar
Melledy committed
522
523
524
525
	// Affected by exp rate
	public void earnExp(int exp) {
		addExpDirectly((int) (exp * getExpModifier()));
	}
Asnxthaony's avatar
Asnxthaony committed
526

Melledy's avatar
Melledy committed
527
528
529
530
531
	// Directly give player exp
	public void addExpDirectly(int gain) {
		int level = getLevel();
		int exp = getExp();
		int reqExp = getExpRequired(level);
Asnxthaony's avatar
Asnxthaony committed
532

Melledy's avatar
Melledy committed
533
		exp += gain;
Asnxthaony's avatar
Asnxthaony committed
534

Melledy's avatar
Melledy committed
535
536
537
538
		while (exp >= reqExp && reqExp > 0) {
			exp -= reqExp;
			level += 1;
			reqExp = getExpRequired(level);
Asnxthaony's avatar
Asnxthaony committed
539

540
			// Set level each time to allow level-up specific logic to run.
541
			this.setLevel(level);
Melledy's avatar
Melledy committed
542
		}
Asnxthaony's avatar
Asnxthaony committed
543

Melledy's avatar
Melledy committed
544
545
546
		// Set exp
		this.setProperty(PlayerProperty.PROP_PLAYER_EXP, exp);
	}
Asnxthaony's avatar
Asnxthaony committed
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
	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
568
569
570
	private void updateProfile() {
		getProfile().syncWithCharacter(this);
	}
Asnxthaony's avatar
Asnxthaony committed
571

Melledy's avatar
Melledy committed
572
573
574
575
576
577
578
	public boolean isFirstLoginEnterScene() {
		return !this.hasSentAvatarDataNotify;
	}

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

580
	public TowerData getTowerData() {
581
		if (towerData == null) {
582
583
584
			// because of mistake, null may be saved as storage at some machine, this if can be removed in future
			towerData = new TowerData();
		}
585
586
		return towerData;
	}
KingRainbow44's avatar
KingRainbow44 committed
587

Melledy's avatar
Melledy committed
588
589
590
591
592
593
594
595
596
597
598
	public PlayerGachaInfo getGachaInfo() {
		return gachaInfo;
	}

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

599
600
	// 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
601
602
603
	public Map<Integer, Integer> getProperties() {
		return properties;
	}
Asnxthaony's avatar
Asnxthaony committed
604

605
	public boolean setProperty(PlayerProperty prop, int value) {
AnimeGitB's avatar
AnimeGitB committed
606
607
608
609
610
		return setPropertyWithSanityCheck(prop, value, true);
	}

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

Melledy's avatar
Melledy committed
613
614
615
616
617
	public int getProperty(PlayerProperty prop) {
		return getProperties().get(prop.getId());
	}

	public MpSettingType getMpSetting() {
618
		return MpSettingType.MP_SETTING_TYPE_ENTER_AFTER_APPLY; // TEMP
Melledy's avatar
Melledy committed
619
	}
KingRainbow44's avatar
KingRainbow44 committed
620

621
622
623
	public Queue<AttackResult> getAttackResults() {
		return this.attackResults;
	}
Asnxthaony's avatar
Asnxthaony committed
624

Melledy's avatar
Melledy committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
	public synchronized Int2ObjectMap<CoopRequest> getCoopRequests() {
		return coopRequests;
	}

	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
677

Melledy's avatar
Melledy committed
678
679
680
681
682
683
684
		if (newPauseState && !oldPauseState) {
			this.onPause();
		} else if (oldPauseState && !newPauseState) {
			this.onUnpause();
		}
	}

685
686
687
688
689
690
691
692
	public long getSpringLastUsed() {
		return springLastUsed;
	}

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

ImmuState's avatar
ImmuState committed
693
694
695
696
697
698
699
700
	public int getNextResinRefresh() {
		return nextResinRefresh;
	}

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

Melledy's avatar
Melledy committed
701
702
703
704
705
706
707
	public SceneLoadState getSceneLoadState() {
		return sceneState;
	}

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

Melledy's avatar
Melledy committed
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
	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
728

Yazawazi's avatar
Yazawazi committed
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
	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;
	}

745
746
747
748
749
750
751
752
	public int getLastDailyReset() {
		return this.lastDailyReset;
	}

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

Yazawazi's avatar
Yazawazi committed
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
	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
794
		Date now = DateHelper.onlyYearMonthDay(new Date());
KingRainbow44's avatar
KingRainbow44 committed
795
		return (int) ((theLastDay.getTime() - now.getTime()) / (24 * 60 * 60 * 1000)); // By copilot
Yazawazi's avatar
Yazawazi committed
796
797
798
	}

	public void rechargeMoonCard() {
799
		inventory.addItem(new GameItem(203, 300));
Yazawazi's avatar
Yazawazi committed
800
801
802
		if (!moonCard) {
			moonCard = true;
			Date now = new Date();
Yazawazi's avatar
Yazawazi committed
803
			moonCardStartTime = DateHelper.onlyYearMonthDay(now);
Yazawazi's avatar
Yazawazi committed
804
805
806
807
808
809
810
811
812
813
814
815
816
			moonCardDuration = 30;
		} else {
			moonCardDuration += 30;
		}
		if (!moonCardGetTimes.contains(moonCardStartTime)) {
			moonCardGetTimes.add(moonCardStartTime);
		}
	}

	public void getTodayMoonCard() {
		if (!moonCard) {
			return;
		}
Yazawazi's avatar
Yazawazi committed
817
		Date now = DateHelper.onlyYearMonthDay(new Date());
Yazawazi's avatar
Yazawazi committed
818
819
820
821
822
823
824
825
826
827
828
829
830
831
		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);
832
833
		GameItem item = new GameItem(201, 90);
		getInventory().addItem(item, ActionReason.BlessingRedeemReward);
Yazawazi's avatar
Yazawazi committed
834
835
836
		session.send(new PacketCardProductRewardNotify(getMoonCardRemainDays()));
	}

Kinesis's avatar
Kinesis committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
	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
854
855
856
857
	public List<ShopLimit> getShopLimit() {
		return shopLimit;
	}

Kengxxiao's avatar
Kengxxiao committed
858
859
860
861
862
	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
863
864
	}

Kengxxiao's avatar
Kengxxiao committed
865
866
867
868
869
870
871
	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
872
873
874
			ShopLimit sl = new ShopLimit();
			sl.setShopGoodId(goodsId);
			sl.setHasBought(boughtCount);
Kengxxiao's avatar
Kengxxiao committed
875
876
877
			sl.setHasBoughtInPeriod(boughtCount);
			sl.setNextRefreshTime(nextRefreshTime);
			getShopLimit().add(sl);
Kengxxiao's avatar
Kengxxiao committed
878
879
880
		}
		this.save();
	}
881
	public boolean getUnlimitedStamina() {
882
883
		return stamina;
	}
884
	public void setUnlimitedStamina(boolean stamina) {
885
886
		this.stamina = stamina;
	}
KingRainbow44's avatar
KingRainbow44 committed
887
	public boolean inGodmode() {
Melledy's avatar
Melledy committed
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
		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
903
	public void addAvatar(Avatar avatar, boolean addToCurrentTeam) {
Melledy's avatar
Melledy committed
904
		boolean result = getAvatars().addAvatar(avatar);
Asnxthaony's avatar
Asnxthaony committed
905

Melledy's avatar
Melledy committed
906
907
908
		if (result) {
			// Add starting weapon
			getAvatars().addStartingWeapon(avatar);
Asnxthaony's avatar
Asnxthaony committed
909

Melledy's avatar
Melledy committed
910
911
912
913
			// Done
			if (hasSentAvatarDataNotify()) {
				// Recalc stats
				avatar.recalcStats();
ProxyismGH's avatar
ProxyismGH committed
914
915
916
917
918
919
				// 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
920
921
922
923
924
			}
		} else {
			// Failed adding avatar
		}
	}
Asnxthaony's avatar
Asnxthaony committed
925

ProxyismGH's avatar
ProxyismGH committed
926
927
928
929
	public void addAvatar(Avatar avatar) {
		addAvatar(avatar, true);
	}

Melledy's avatar
Melledy committed
930
931
932
933
	public void addFlycloak(int flycloakId) {
		this.getFlyCloakList().add(flycloakId);
		this.sendPacket(new PacketAvatarGainFlycloakNotify(flycloakId));
	}
Asnxthaony's avatar
Asnxthaony committed
934

Melledy's avatar
Melledy committed
935
936
937
938
	public void addCostume(int costumeId) {
		this.getCostumeList().add(costumeId);
		this.sendPacket(new PacketAvatarGainCostumeNotify(costumeId));
	}
Asnxthaony's avatar
Asnxthaony committed
939

Melledy's avatar
Melledy committed
940
941
942
943
	public void addNameCard(int nameCardId) {
		this.getNameCardList().add(nameCardId);
		this.sendPacket(new PacketUnlockNameCardNotify(nameCardId));
	}
Asnxthaony's avatar
Asnxthaony committed
944

Melledy's avatar
Melledy committed
945
946
947
948
	public void setNameCard(int nameCardId) {
		if (!this.getNameCardList().contains(nameCardId)) {
			return;
		}
Asnxthaony's avatar
Asnxthaony committed
949

Melledy's avatar
Melledy committed
950
		this.setNameCardId(nameCardId);
Asnxthaony's avatar
Asnxthaony committed
951

Melledy's avatar
Melledy committed
952
953
		this.sendPacket(new PacketSetNameCardRsp(nameCardId));
	}
Asnxthaony's avatar
Asnxthaony committed
954

Melledy's avatar
Melledy committed
955
	public void dropMessage(Object message) {
956
957
958
959
		if (this.messageHandler != null) {
			this.messageHandler.append(message.toString());
			return;
		}
960
961
962

		this.getServer().getChatManager().sendPrivateMessageFromServer(getUid(), message.toString());
		// this.sendPacket(new PacketPrivateChatNotify(GameConstants.SERVER_CONSOLE_UID, getUid(), message.toString()));
Melledy's avatar
Melledy committed
963
	}
Melledy's avatar
Melledy committed
964
965
966

	/**
	 * Sends a message to another player.
Asnxthaony's avatar
Asnxthaony committed
967
968
	 *
	 * @param sender  The sender of the message.
Melledy's avatar
Melledy committed
969
970
	 * @param message The message to send.
	 */
971
	public void sendMessage(Player sender, Object message) {
972
973
		// this.sendPacket(new PacketPrivateChatNotify(sender.getUid(), this.getUid(), message.toString()));
		this.getServer().getChatManager().sendPrivateMessage(sender, this.getUid(), message.toString());
Melledy's avatar
Melledy committed
974
	}
Asnxthaony's avatar
Asnxthaony committed
975

Benjamin Elsdon's avatar
Benjamin Elsdon committed
976
	// ---------------------MAIL------------------------
977

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

Benjamin Elsdon's avatar
Benjamin Elsdon committed
980
	public void sendMail(Mail message) {
981
		this.getMailHandler().sendMail(message);
982
	}
Benjamin Elsdon's avatar
Benjamin Elsdon committed
983
984

	public boolean deleteMail(int mailId) {
985
		return this.getMailHandler().deleteMail(mailId);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
986
987
	}

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

990
	public int getMailId(Mail message) {
991
		return this.getMailHandler().getMailIndex(message);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
992
993
	}

994
	public boolean replaceMailByIndex(int index, Mail message) {
995
		return this.getMailHandler().replaceMailByIndex(index, message);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
996
	}
997

Melledy's avatar
Melledy committed
998
999
1000
1001
	public void interactWith(int gadgetEntityId, GadgetInteractReq interactReq) {
		GameEntity target = getScene().getEntityById(gadgetEntityId);
		
		if (target == null) {
Melledy's avatar
Melledy committed
1002
1003
			return;
		}
Melledy's avatar
Melledy committed
1004
1005
		
		target.onInteract(this, interactReq);
Melledy's avatar
Melledy committed
1006
	}
Asnxthaony's avatar
Asnxthaony committed
1007

Melledy's avatar
Melledy committed
1008
	public void onPause() {
1009
		getStaminaManager().stopSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1010
	}
Asnxthaony's avatar
Asnxthaony committed
1011

Melledy's avatar
Melledy committed
1012
	public void onUnpause() {
1013
		getStaminaManager().startSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1014
	}
Asnxthaony's avatar
Asnxthaony committed
1015

1016
	public void sendPacket(BasePacket packet) {
Melledy's avatar
Melledy committed
1017
		this.getSession().send(packet);
Melledy's avatar
Melledy committed
1018
	}
Asnxthaony's avatar
Asnxthaony committed
1019

Melledy's avatar
Melledy committed
1020
1021
	public OnlinePlayerInfo getOnlinePlayerInfo() {
		OnlinePlayerInfo.Builder onlineInfo = OnlinePlayerInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1022
1023
1024
1025
1026
1027
				.setUid(this.getUid())
				.setNickname(this.getNickname())
				.setPlayerLevel(this.getLevel())
				.setMpSettingType(this.getMpSetting())
				.setNameCardId(this.getNameCardId())
				.setSignature(this.getSignature())
Melledy's avatar
Melledy committed
1028
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()));
Asnxthaony's avatar
Asnxthaony committed
1029

Melledy's avatar
Melledy committed
1030
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1031
			onlineInfo.setCurPlayerNumInWorld(getWorld().getPlayerCount());
Melledy's avatar
Melledy committed
1032
1033
1034
		} else {
			onlineInfo.setCurPlayerNumInWorld(1);
		}
Asnxthaony's avatar
Asnxthaony committed
1035

Melledy's avatar
Melledy committed
1036
1037
1038
		return onlineInfo.build();
	}

Asnxthaony's avatar
Asnxthaony committed
1039
	public PlayerBirthday getBirthday() {
Miyucchi's avatar
Miyucchi committed
1040
1041
1042
1043
1044
1045
1046
1047
		return this.birthday;
	}

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

Asnxthaony's avatar
Asnxthaony committed
1048
1049
1050
1051
	public boolean hasBirthday() {
		return this.birthday.getDay() > 0;
	}

Melledy's avatar
Melledy committed
1052
1053
	public PlayerCodex getCodex() { 
	    return this.codex; 
1054
1055
1056
1057
1058
1059
	}

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

Melledy's avatar
Melledy committed
1060
	public SocialDetail.Builder getSocialDetail() {
Yazawazi's avatar
Yazawazi committed
1061
		List<SocialShowAvatarInfoOuterClass.SocialShowAvatarInfo> socialShowAvatarInfoList = new ArrayList<>();
Yazawazi's avatar
Yazawazi committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
		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 {
1076
1077
			List<Integer> showAvatarList = DatabaseHelper.getPlayerByUid(id).getShowAvatarList();
			AvatarStorage avatars = DatabaseHelper.getPlayerByUid(id).getAvatars();
Yazawazi's avatar
Yazawazi committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
			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
1091
1092
		}

Asnxthaony's avatar
Asnxthaony committed
1093
1094
		SocialDetail.Builder social = SocialDetail.newBuilder()
				.setUid(this.getUid())
Yazawazi's avatar
Yazawazi committed
1095
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()))
Asnxthaony's avatar
Asnxthaony committed
1096
1097
1098
1099
1100
1101
				.setNickname(this.getNickname())
				.setSignature(this.getSignature())
				.setLevel(this.getLevel())
				.setBirthday(this.getBirthday().getFilledProtoWhenNotEmpty())
				.setWorldLevel(this.getWorldLevel())
				.setNameCardId(this.getNameCardId())
Yazawazi's avatar
Yazawazi committed
1102
1103
				.setIsShowAvatar(this.isShowAvatars())
				.addAllShowAvatarInfoList(socialShowAvatarInfoList)
Asnxthaony's avatar
Asnxthaony committed
1104
				.setFinishAchievementNum(0);
Melledy's avatar
Melledy committed
1105
1106
		return social;
	}
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116

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

		Player player;
		boolean shouldRecalc;
		if (this.isOnline()) {
			player = this;
			shouldRecalc = false;
		} else {
1117
			player = DatabaseHelper.getPlayerByUid(id);
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
			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
1136

1137
1138
	public PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo getWorldPlayerLocationInfo() {
		return PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1139
1140
1141
				.setSceneId(this.getSceneId())
				.setPlayerLoc(this.getPlayerLocationInfo())
				.build();
Melledy's avatar
Melledy committed
1142
	}
Asnxthaony's avatar
Asnxthaony committed
1143

Melledy's avatar
Melledy committed
1144
	public PlayerLocationInfo getPlayerLocationInfo() {
Asnxthaony's avatar
Asnxthaony committed
1145
1146
		return PlayerLocationInfo.newBuilder()
				.setUid(this.getUid())
1147
				.setPos(this.getPosition().toProto())
Asnxthaony's avatar
Asnxthaony committed
1148
1149
				.setRot(this.getRotation().toProto())
				.build();
Melledy's avatar
Melledy committed
1150
	}
Asnxthaony's avatar
Asnxthaony committed
1151

1152
1153
1154
	public void loadBattlePassManager() {
		if (this.battlePassManager != null) return;
		this.battlePassManager = DatabaseHelper.loadBattlePass(this);
1155
		this.battlePassManager.getMissions().values().removeIf(mission -> mission.getData() == null);
1156
	}
1157

1158
1159
1160
1161
1162
1163
1164
	public CollectionRecordStore getCollectionRecordStore() {
		if(this.collectionRecordStore==null){
			this.collectionRecordStore = new CollectionRecordStore();
		}
		return collectionRecordStore;
	}

Melledy's avatar
Melledy committed
1165
1166
1167
1168
1169
1170
	public Map<String, MapMark> getMapMarks() {
	    if (this.mapMarks == null) {
	        this.mapMarks = new HashMap<String, MapMark>();
	    }
	    return mapMarks; 
	}
gentlespoon's avatar
gentlespoon committed
1171

Melledy's avatar
Melledy committed
1172
1173
1174
1175
1176
1177
	public PlayerOpenStateManager getOpenStateManager() {
	    if (this.openStateManager == null) {
            this.openStateManager = new PlayerOpenStateManager(this);
        }
        return openStateManager;
    }
gentlespoon's avatar
gentlespoon committed
1178

Melledy's avatar
Melledy committed
1179
    public synchronized void onTick() {
Melledy's avatar
Melledy committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
		// 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()) {
1190
1191
1192
1193
				req.getRequester().sendPacket(new PacketPlayerApplyEnterMpResultNotify(
						this,
						false,
						PlayerApplyEnterMpResultNotifyOuterClass.PlayerApplyEnterMpResultNotify.Reason.REASON_SYSTEM_JUDGE));
Melledy's avatar
Melledy committed
1194
1195
1196
1197
1198
				it.remove();
			}
		}
		// Ping
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1199
1200
			// RTT notify - very important to send this often
			this.sendPacket(new PacketWorldPlayerRTTNotify(this.getWorld()));
Asnxthaony's avatar
Asnxthaony committed
1201

Melledy's avatar
Melledy committed
1202
1203
1204
1205
1206
1207
1208
			// 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
1209
		}
1210
1211
1212
1213

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

Kinesis's avatar
Kinesis committed
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
		// 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));
		}
1230
1231
1232

		// Send updated forge queue data, if necessary.
		this.getForgingManager().sendPlayerForgingUpdate();
ImmuState's avatar
ImmuState committed
1233
1234
1235

		// Recharge resin.
		this.getResinManager().rechargeResin();
1236
1237
	}

1238
	private synchronized void doDailyReset() {
1239
1240
1241
1242
1243
		// 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());
1244

1245
1246
1247
1248
1249
1250
1251
1252
		if (!currentDate.isAfter(lastResetDate)) {
			return;
		}

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

1253
1254
1255
		// Reset daily BP missions.
		this.getBattlePassManager().resetDailyMissions();

1256
1257
1258
1259
		// 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);

1260
1261
1262
1263
1264
		// Reset weekly BP missions.
		if (currentDate.getDayOfWeek() == DayOfWeek.MONDAY) {
			this.getBattlePassManager().resetWeeklyMissions();
		}

1265
1266
1267
		// Done. Update last reset time.
		this.setLastDailyReset(currentTime);
	}
1268

Melledy's avatar
Melledy committed
1269
1270
1271
	public void resetSendPlayerLocTime() {
		this.nextSendPlayerLocTime = System.currentTimeMillis() + 5000;
	}
Melledy's avatar
Melledy committed
1272
1273
1274

	@PostLoad
	private void onLoad() {
1275
		this.getCodex().setPlayer(this);
akatatsu27's avatar
akatatsu27 committed
1276
        this.getOpenStateManager().setPlayer(this);
Melledy's avatar
Melledy committed
1277
1278
		this.getTeamManager().setPlayer(this);
	}
Asnxthaony's avatar
Asnxthaony committed
1279

Melledy's avatar
Melledy committed
1280
1281
1282
	public void save() {
		DatabaseHelper.savePlayer(this);
	}
KingRainbow44's avatar
KingRainbow44 committed
1283

1284
1285
	// Called from tokenrsp
	public void loadFromDatabase() {
Melledy's avatar
Melledy committed
1286
1287
1288
		// Make sure these exist
		if (this.getTeamManager() == null) {
			this.teamManager = new TeamManager(this);
Asnxthaony's avatar
Asnxthaony committed
1289
		}
1290
1291
1292
		if (this.getCodex() == null) {
			this.codex = new PlayerCodex(this);
		}
Asnxthaony's avatar
Asnxthaony committed
1293
		if (this.getProfile().getUid() == 0) {
1294
			this.getProfile().syncWithCharacter(this);
Melledy's avatar
Melledy committed
1295
		}
KingRainbow44's avatar
KingRainbow44 committed
1296

Melledy's avatar
Melledy committed
1297
1298
1299
		// Load from db
		this.getAvatars().loadFromDatabase();
		this.getInventory().loadFromDatabase();
1300
		this.getAvatars().postLoad(); // Needs to be called after inventory is handled
Asnxthaony's avatar
Asnxthaony committed
1301

Melledy's avatar
Melledy committed
1302
		this.getFriendsList().loadFromDatabase();
1303
		this.getMailHandler().loadFromDatabase();
Melledy's avatar
Melledy committed
1304
		this.getQuestManager().loadFromDatabase();
KingRainbow44's avatar
KingRainbow44 committed
1305

1306
		this.loadBattlePassManager();
1307
1308
1309
	}

	public void onLogin() {
Melledy's avatar
Melledy committed
1310
1311
1312
1313
1314
1315
1316
1317
		// 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
1318

Melledy's avatar
Melledy committed
1319
1320
1321
1322
			this.setSceneId(3);
			this.getPos().set(GameConstants.START_POSITION);
		}
		*/
KingRainbow44's avatar
KingRainbow44 committed
1323

Melledy's avatar
Melledy committed
1324
1325
1326
		// Create world
		World world = new World(this);
		world.addPlayer(this);
Asnxthaony's avatar
Asnxthaony committed
1327
1328

		// Multiplayer setting
AnimeGitB's avatar
AnimeGitB committed
1329
1330
		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
1331

1332
1333
1334
		// Execute daily reset logic if this is a new day.
		this.doDailyReset();

Melledy's avatar
Melledy committed
1335
1336
1337
1338
1339
		// 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
1340
		session.send(new PacketFinishedParentQuestNotify(this));
1341
		session.send(new PacketBattlePassAllDataNotify(this));
Melledy's avatar
Melledy committed
1342
		session.send(new PacketQuestListNotify(this));
1343
		session.send(new PacketCodexDataFullNotify(this));
1344
1345
		session.send(new PacketAllWidgetDataNotify(this));
		session.send(new PacketWidgetGadgetAllDataNotify());
ImmuState's avatar
ImmuState committed
1346
		session.send(new PacketCombineDataNotify(this.unlockedCombines));
1347
		this.forgingManager.sendForgeDataNotify();
ImmuState's avatar
ImmuState committed
1348
		this.resinManager.onPlayerLogin();
1349
		this.cookingManager.sendCookDataNofity();
1350
1351

		// Unlock in case this is an existing user that reached a level before we implemented unlocking.
Melledy's avatar
Melledy committed
1352
1353
		this.getOpenStateManager().unlockLevelDependentStates();
        this.getOpenStateManager().onPlayerLogin();
1354

Yazawazi's avatar
Yazawazi committed
1355
1356
		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.

1357
1358
		// Battle Pass trigger
		this.getBattlePassManager().triggerMission(WatcherTriggerType.TRIGGER_LOGIN);
Akka's avatar
Akka committed
1359

Akka's avatar
Akka committed
1360
		this.furnitureManager.onLogin();
1361
1362
1363
		// Home
		home = GameHome.getByUid(getUid());
		home.onOwnerLogin(this);
Akka's avatar
Akka committed
1364
1365
        // Activity
        activityManager = new ActivityManager(this);
1366

Melledy's avatar
Melledy committed
1367
		session.send(new PacketPlayerEnterSceneNotify(this)); // Enter game world
1368
		session.send(new PacketPlayerLevelRewardUpdateNotify(rewardedLevels));
akatatsu27's avatar
akatatsu27 committed
1369

Melledy's avatar
Melledy committed
1370
1371
1372

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

1374
1375
1376
		// Send server welcome chat.
		this.getServer().getChatManager().sendServerWelcomeMessages(this);
		
1377
1378
		// Set session state
		session.setState(SessionState.ACTIVE);
1379
1380
1381

		// Call join event.
		PlayerJoinEvent event = new PlayerJoinEvent(this); event.call();
1382
		if(event.isCanceled()){ // If event is not cancelled, continue.
1383
			session.close();
1384
1385
			return;
		}
KingRainbow44's avatar
KingRainbow44 committed
1386

1387
1388
1389
		// register
		getServer().registerPlayer(this);
		getProfile().setPlayer(this); // Set online
Melledy's avatar
Melledy committed
1390
	}
Asnxthaony's avatar
Asnxthaony committed
1391

Melledy's avatar
Melledy committed
1392
	public void onLogout() {
1393
		try{
1394
1395
1396
			// Clear chat history.
			this.getServer().getChatManager().clearHistoryOnLogout(this);

1397
1398
			// stop stamina calculation
			getStaminaManager().stopSustainedStaminaHandler();
1399

1400
			// force to leave the dungeon (inside has a "if")
1401
			this.getServer().getDungeonSystem().exitDungeon(this);
Asnxthaony's avatar
Asnxthaony committed
1402

1403
1404
1405
1406
1407
1408
1409
1410
			// 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
1411

1412
			this.getCoopRequests().clear();
Asnxthaony's avatar
Asnxthaony committed
1413

1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
			// 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();
		}
	}
1428

1429
1430
1431
1432
1433
	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
1434
	}
Asnxthaony's avatar
Asnxthaony committed
1435

Akka's avatar
Akka committed
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
    public int getLegendaryKey() {
        return this.getProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY);
    }
    public synchronized void addLegendaryKey(int count) {
        this.setProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY, getLegendaryKey() + count);
    }
    public synchronized void useLegendaryKey(int count) {
        this.setProperty(PlayerProperty.PROP_PLAYER_LEGENDARY_KEY, getLegendaryKey() - count);
    }

    public enum SceneLoadState {
Asnxthaony's avatar
Asnxthaony committed
1447
1448
		NONE(0), LOADING(1), INIT(2), LOADED(3);

Melledy's avatar
Melledy committed
1449
		private final int value;
Asnxthaony's avatar
Asnxthaony committed
1450

Melledy's avatar
Melledy committed
1451
1452
1453
		private SceneLoadState(int value) {
			this.value = value;
		}
Asnxthaony's avatar
Asnxthaony committed
1454

Melledy's avatar
Melledy committed
1455
1456
1457
1458
		public int getValue() {
			return this.value;
		}
	}
1459
1460
1461
1462

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

AnimeGitB's avatar
AnimeGitB committed
1464
1465
1466
1467
1468
1469
1470
	public int getPropertyMin(PlayerProperty prop) {
		if (prop.getDynamicRange()) {
			return switch (prop) {
				default -> 0;
			};
		} else {
			return prop.getMin();
1471
		}
AnimeGitB's avatar
AnimeGitB committed
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
	}

	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 {
1497
1498
		return false;
	}
AnimeGitB's avatar
AnimeGitB committed
1499
	}
1500

Melledy's avatar
Melledy committed
1501
}