Player.java 45.1 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

KingRainbow44's avatar
KingRainbow44 committed
998
999

	public void interactWith(int gadgetEntityId, GadgetInteractReq opType) {
1000
		GameEntity entity = getScene().getEntityById(gadgetEntityId);
Melledy's avatar
Melledy committed
1001
1002
1003
		if (entity == null) {
			return;
		}
Asnxthaony's avatar
Asnxthaony committed
1004

Melledy's avatar
Melledy committed
1005
		// Handle
1006
		if (entity instanceof EntityItem drop) {
Melledy's avatar
Melledy committed
1007
			// Pick item
Kengxxiao's avatar
Kengxxiao committed
1008
1009
1010
			if (!drop.isShare()) // check drop owner to avoid someone picked up item in others' world
			{
				int dropOwner = (int)(drop.getGuid() >> 32);
1011
				if (dropOwner != getUid()) {
Kengxxiao's avatar
Kengxxiao committed
1012
					return;
1013
				}
Kengxxiao's avatar
Kengxxiao committed
1014
1015
			}
			entity.getScene().removeEntity(entity);
1016
			GameItem item = new GameItem(drop.getItemData(), drop.getCount());
Melledy's avatar
Melledy committed
1017
			// Add to inventory
1018
			boolean success = getInventory().addItem(item, ActionReason.SubfieldDrop);
Melledy's avatar
Melledy committed
1019
			if (success) {
1020
				if (!drop.isShare()) { // not shared drop
1021
					this.sendPacket(new PacketGadgetInteractRsp(drop, InteractType.INTERACT_TYPE_PICK_ITEM));
1022
				}else{
1023
					this.getScene().broadcastPacket(new PacketGadgetInteractRsp(drop, InteractType.INTERACT_TYPE_PICK_ITEM));
1024
				}
Melledy's avatar
Melledy committed
1025
			}
1026
		} else if (entity instanceof EntityGadget gadget) {
Melledy's avatar
Melledy committed
1027
1028
1029
			if (gadget.getContent() == null) {
				return;
			}
KingRainbow44's avatar
KingRainbow44 committed
1030
1031
1032

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

Melledy's avatar
Melledy committed
1033
			if (shouldDelete) {
1034
				entity.getScene().removeEntity(entity, VisionType.VISION_TYPE_REMOVE);
1035
			}
1036
1037
1038
1039
		} 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
1040
1041
1042
		} else {
			// Delete directly
			entity.getScene().removeEntity(entity);
Melledy's avatar
Melledy committed
1043
1044
		}
	}
Asnxthaony's avatar
Asnxthaony committed
1045

Melledy's avatar
Melledy committed
1046
	public void onPause() {
1047
		getStaminaManager().stopSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1048
	}
Asnxthaony's avatar
Asnxthaony committed
1049

Melledy's avatar
Melledy committed
1050
	public void onUnpause() {
1051
		getStaminaManager().startSustainedStaminaHandler();
Melledy's avatar
Melledy committed
1052
	}
Asnxthaony's avatar
Asnxthaony committed
1053

1054
	public void sendPacket(BasePacket packet) {
Melledy's avatar
Melledy committed
1055
		this.getSession().send(packet);
Melledy's avatar
Melledy committed
1056
	}
Asnxthaony's avatar
Asnxthaony committed
1057

Melledy's avatar
Melledy committed
1058
1059
	public OnlinePlayerInfo getOnlinePlayerInfo() {
		OnlinePlayerInfo.Builder onlineInfo = OnlinePlayerInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1060
1061
1062
1063
1064
1065
				.setUid(this.getUid())
				.setNickname(this.getNickname())
				.setPlayerLevel(this.getLevel())
				.setMpSettingType(this.getMpSetting())
				.setNameCardId(this.getNameCardId())
				.setSignature(this.getSignature())
Melledy's avatar
Melledy committed
1066
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()));
Asnxthaony's avatar
Asnxthaony committed
1067

Melledy's avatar
Melledy committed
1068
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1069
			onlineInfo.setCurPlayerNumInWorld(getWorld().getPlayerCount());
Melledy's avatar
Melledy committed
1070
1071
1072
		} else {
			onlineInfo.setCurPlayerNumInWorld(1);
		}
Asnxthaony's avatar
Asnxthaony committed
1073

Melledy's avatar
Melledy committed
1074
1075
1076
		return onlineInfo.build();
	}

Asnxthaony's avatar
Asnxthaony committed
1077
	public PlayerBirthday getBirthday() {
Miyucchi's avatar
Miyucchi committed
1078
1079
1080
1081
1082
1083
1084
1085
		return this.birthday;
	}

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

Asnxthaony's avatar
Asnxthaony committed
1086
1087
1088
1089
	public boolean hasBirthday() {
		return this.birthday.getDay() > 0;
	}

Melledy's avatar
Melledy committed
1090
1091
	public PlayerCodex getCodex() { 
	    return this.codex; 
1092
1093
1094
1095
1096
1097
	}

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

Melledy's avatar
Melledy committed
1098
	public SocialDetail.Builder getSocialDetail() {
Yazawazi's avatar
Yazawazi committed
1099
		List<SocialShowAvatarInfoOuterClass.SocialShowAvatarInfo> socialShowAvatarInfoList = new ArrayList<>();
Yazawazi's avatar
Yazawazi committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
		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 {
1114
1115
			List<Integer> showAvatarList = DatabaseHelper.getPlayerByUid(id).getShowAvatarList();
			AvatarStorage avatars = DatabaseHelper.getPlayerByUid(id).getAvatars();
Yazawazi's avatar
Yazawazi committed
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
			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
1129
1130
		}

Asnxthaony's avatar
Asnxthaony committed
1131
1132
		SocialDetail.Builder social = SocialDetail.newBuilder()
				.setUid(this.getUid())
Yazawazi's avatar
Yazawazi committed
1133
				.setProfilePicture(ProfilePicture.newBuilder().setAvatarId(this.getHeadImage()))
Asnxthaony's avatar
Asnxthaony committed
1134
1135
1136
1137
1138
1139
				.setNickname(this.getNickname())
				.setSignature(this.getSignature())
				.setLevel(this.getLevel())
				.setBirthday(this.getBirthday().getFilledProtoWhenNotEmpty())
				.setWorldLevel(this.getWorldLevel())
				.setNameCardId(this.getNameCardId())
Yazawazi's avatar
Yazawazi committed
1140
1141
				.setIsShowAvatar(this.isShowAvatars())
				.addAllShowAvatarInfoList(socialShowAvatarInfoList)
Asnxthaony's avatar
Asnxthaony committed
1142
				.setFinishAchievementNum(0);
Melledy's avatar
Melledy committed
1143
1144
		return social;
	}
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

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

		Player player;
		boolean shouldRecalc;
		if (this.isOnline()) {
			player = this;
			shouldRecalc = false;
		} else {
1155
			player = DatabaseHelper.getPlayerByUid(id);
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
			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
1174

1175
1176
	public PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo getWorldPlayerLocationInfo() {
		return PlayerWorldLocationInfoOuterClass.PlayerWorldLocationInfo.newBuilder()
Asnxthaony's avatar
Asnxthaony committed
1177
1178
1179
				.setSceneId(this.getSceneId())
				.setPlayerLoc(this.getPlayerLocationInfo())
				.build();
Melledy's avatar
Melledy committed
1180
	}
Asnxthaony's avatar
Asnxthaony committed
1181

Melledy's avatar
Melledy committed
1182
	public PlayerLocationInfo getPlayerLocationInfo() {
Asnxthaony's avatar
Asnxthaony committed
1183
1184
		return PlayerLocationInfo.newBuilder()
				.setUid(this.getUid())
1185
				.setPos(this.getPosition().toProto())
Asnxthaony's avatar
Asnxthaony committed
1186
1187
				.setRot(this.getRotation().toProto())
				.build();
Melledy's avatar
Melledy committed
1188
	}
Asnxthaony's avatar
Asnxthaony committed
1189

1190
1191
1192
	public void loadBattlePassManager() {
		if (this.battlePassManager != null) return;
		this.battlePassManager = DatabaseHelper.loadBattlePass(this);
1193
		this.battlePassManager.getMissions().values().removeIf(mission -> mission.getData() == null);
1194
	}
1195

1196
1197
1198
1199
1200
1201
1202
	public CollectionRecordStore getCollectionRecordStore() {
		if(this.collectionRecordStore==null){
			this.collectionRecordStore = new CollectionRecordStore();
		}
		return collectionRecordStore;
	}

Melledy's avatar
Melledy committed
1203
1204
1205
1206
1207
1208
	public Map<String, MapMark> getMapMarks() {
	    if (this.mapMarks == null) {
	        this.mapMarks = new HashMap<String, MapMark>();
	    }
	    return mapMarks; 
	}
gentlespoon's avatar
gentlespoon committed
1209

Melledy's avatar
Melledy committed
1210
1211
1212
1213
1214
1215
	public PlayerOpenStateManager getOpenStateManager() {
	    if (this.openStateManager == null) {
            this.openStateManager = new PlayerOpenStateManager(this);
        }
        return openStateManager;
    }
gentlespoon's avatar
gentlespoon committed
1216

Melledy's avatar
Melledy committed
1217
    public synchronized void onTick() {
Melledy's avatar
Melledy committed
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
		// 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()) {
1228
1229
1230
1231
				req.getRequester().sendPacket(new PacketPlayerApplyEnterMpResultNotify(
						this,
						false,
						PlayerApplyEnterMpResultNotifyOuterClass.PlayerApplyEnterMpResultNotify.Reason.REASON_SYSTEM_JUDGE));
Melledy's avatar
Melledy committed
1232
1233
1234
1235
1236
				it.remove();
			}
		}
		// Ping
		if (this.getWorld() != null) {
Melledy's avatar
Melledy committed
1237
1238
			// RTT notify - very important to send this often
			this.sendPacket(new PacketWorldPlayerRTTNotify(this.getWorld()));
Asnxthaony's avatar
Asnxthaony committed
1239

Melledy's avatar
Melledy committed
1240
1241
1242
1243
1244
1245
1246
			// 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
1247
		}
1248
1249
1250
1251

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

Kinesis's avatar
Kinesis committed
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
		// 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));
		}
1268
1269
1270

		// Send updated forge queue data, if necessary.
		this.getForgingManager().sendPlayerForgingUpdate();
ImmuState's avatar
ImmuState committed
1271
1272
1273

		// Recharge resin.
		this.getResinManager().rechargeResin();
1274
1275
	}

1276
	private synchronized void doDailyReset() {
1277
1278
1279
1280
1281
		// 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());
1282

1283
1284
1285
1286
1287
1288
1289
1290
		if (!currentDate.isAfter(lastResetDate)) {
			return;
		}

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

1291
1292
1293
		// Reset daily BP missions.
		this.getBattlePassManager().resetDailyMissions();

1294
1295
1296
1297
		// 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);

1298
1299
1300
1301
1302
		// Reset weekly BP missions.
		if (currentDate.getDayOfWeek() == DayOfWeek.MONDAY) {
			this.getBattlePassManager().resetWeeklyMissions();
		}

1303
1304
1305
		// Done. Update last reset time.
		this.setLastDailyReset(currentTime);
	}
1306

Melledy's avatar
Melledy committed
1307
1308
1309
	public void resetSendPlayerLocTime() {
		this.nextSendPlayerLocTime = System.currentTimeMillis() + 5000;
	}
Melledy's avatar
Melledy committed
1310
1311
1312

	@PostLoad
	private void onLoad() {
1313
		this.getCodex().setPlayer(this);
akatatsu27's avatar
akatatsu27 committed
1314
        this.getOpenStateManager().setPlayer(this);
Melledy's avatar
Melledy committed
1315
1316
		this.getTeamManager().setPlayer(this);
	}
Asnxthaony's avatar
Asnxthaony committed
1317

Melledy's avatar
Melledy committed
1318
1319
1320
	public void save() {
		DatabaseHelper.savePlayer(this);
	}
KingRainbow44's avatar
KingRainbow44 committed
1321

1322
1323
	// Called from tokenrsp
	public void loadFromDatabase() {
Melledy's avatar
Melledy committed
1324
1325
1326
		// Make sure these exist
		if (this.getTeamManager() == null) {
			this.teamManager = new TeamManager(this);
Asnxthaony's avatar
Asnxthaony committed
1327
		}
1328
1329
1330
		if (this.getCodex() == null) {
			this.codex = new PlayerCodex(this);
		}
Asnxthaony's avatar
Asnxthaony committed
1331
		if (this.getProfile().getUid() == 0) {
1332
			this.getProfile().syncWithCharacter(this);
Melledy's avatar
Melledy committed
1333
		}
KingRainbow44's avatar
KingRainbow44 committed
1334

Melledy's avatar
Melledy committed
1335
1336
1337
		// Load from db
		this.getAvatars().loadFromDatabase();
		this.getInventory().loadFromDatabase();
1338
		this.getAvatars().postLoad(); // Needs to be called after inventory is handled
Asnxthaony's avatar
Asnxthaony committed
1339

Melledy's avatar
Melledy committed
1340
		this.getFriendsList().loadFromDatabase();
1341
		this.getMailHandler().loadFromDatabase();
Melledy's avatar
Melledy committed
1342
		this.getQuestManager().loadFromDatabase();
KingRainbow44's avatar
KingRainbow44 committed
1343

1344
		this.loadBattlePassManager();
1345
1346
1347
	}

	public void onLogin() {
Melledy's avatar
Melledy committed
1348
1349
1350
1351
1352
1353
1354
1355
		// 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
1356

Melledy's avatar
Melledy committed
1357
1358
1359
1360
			this.setSceneId(3);
			this.getPos().set(GameConstants.START_POSITION);
		}
		*/
KingRainbow44's avatar
KingRainbow44 committed
1361

Melledy's avatar
Melledy committed
1362
1363
1364
		// Create world
		World world = new World(this);
		world.addPlayer(this);
Asnxthaony's avatar
Asnxthaony committed
1365
1366

		// Multiplayer setting
AnimeGitB's avatar
AnimeGitB committed
1367
1368
		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
1369

1370
1371
1372
		// Execute daily reset logic if this is a new day.
		this.doDailyReset();

Melledy's avatar
Melledy committed
1373
1374
1375
1376
1377
		// 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
1378
		session.send(new PacketFinishedParentQuestNotify(this));
1379
		session.send(new PacketBattlePassAllDataNotify(this));
Melledy's avatar
Melledy committed
1380
		session.send(new PacketQuestListNotify(this));
1381
		session.send(new PacketCodexDataFullNotify(this));
1382
1383
		session.send(new PacketAllWidgetDataNotify(this));
		session.send(new PacketWidgetGadgetAllDataNotify());
ImmuState's avatar
ImmuState committed
1384
		session.send(new PacketCombineDataNotify(this.unlockedCombines));
1385
		this.forgingManager.sendForgeDataNotify();
ImmuState's avatar
ImmuState committed
1386
		this.resinManager.onPlayerLogin();
1387
		this.cookingManager.sendCookDataNofity();
1388
1389

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

Yazawazi's avatar
Yazawazi committed
1393
1394
		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.

1395
1396
		// Battle Pass trigger
		this.getBattlePassManager().triggerMission(WatcherTriggerType.TRIGGER_LOGIN);
Akka's avatar
Akka committed
1397

Akka's avatar
Akka committed
1398
		this.furnitureManager.onLogin();
1399
1400
1401
		// Home
		home = GameHome.getByUid(getUid());
		home.onOwnerLogin(this);
Akka's avatar
Akka committed
1402
1403
        // Activity
        activityManager = new ActivityManager(this);
1404

Melledy's avatar
Melledy committed
1405
		session.send(new PacketPlayerEnterSceneNotify(this)); // Enter game world
1406
		session.send(new PacketPlayerLevelRewardUpdateNotify(rewardedLevels));
akatatsu27's avatar
akatatsu27 committed
1407

Melledy's avatar
Melledy committed
1408
1409
1410

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

1412
1413
1414
		// Send server welcome chat.
		this.getServer().getChatManager().sendServerWelcomeMessages(this);
		
1415
1416
		// Set session state
		session.setState(SessionState.ACTIVE);
1417
1418
1419

		// Call join event.
		PlayerJoinEvent event = new PlayerJoinEvent(this); event.call();
1420
		if(event.isCanceled()){ // If event is not cancelled, continue.
1421
			session.close();
1422
1423
			return;
		}
KingRainbow44's avatar
KingRainbow44 committed
1424

1425
1426
1427
		// register
		getServer().registerPlayer(this);
		getProfile().setPlayer(this); // Set online
Melledy's avatar
Melledy committed
1428
	}
Asnxthaony's avatar
Asnxthaony committed
1429

Melledy's avatar
Melledy committed
1430
	public void onLogout() {
1431
		try{
1432
1433
1434
			// Clear chat history.
			this.getServer().getChatManager().clearHistoryOnLogout(this);

1435
1436
			// stop stamina calculation
			getStaminaManager().stopSustainedStaminaHandler();
1437

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

1441
1442
1443
1444
1445
1446
1447
1448
			// 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
1449

1450
			this.getCoopRequests().clear();
Asnxthaony's avatar
Asnxthaony committed
1451

1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
			// 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();
		}
	}
1466

1467
1468
1469
1470
1471
	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
1472
	}
Asnxthaony's avatar
Asnxthaony committed
1473

Akka's avatar
Akka committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
    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
1485
1486
		NONE(0), LOADING(1), INIT(2), LOADED(3);

Melledy's avatar
Melledy committed
1487
		private final int value;
Asnxthaony's avatar
Asnxthaony committed
1488

Melledy's avatar
Melledy committed
1489
1490
1491
		private SceneLoadState(int value) {
			this.value = value;
		}
Asnxthaony's avatar
Asnxthaony committed
1492

Melledy's avatar
Melledy committed
1493
1494
1495
1496
		public int getValue() {
			return this.value;
		}
	}
1497
1498
1499
1500

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

AnimeGitB's avatar
AnimeGitB committed
1502
1503
1504
1505
1506
1507
1508
	public int getPropertyMin(PlayerProperty prop) {
		if (prop.getDynamicRange()) {
			return switch (prop) {
				default -> 0;
			};
		} else {
			return prop.getMin();
1509
		}
AnimeGitB's avatar
AnimeGitB committed
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
	}

	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 {
1535
1536
		return false;
	}
AnimeGitB's avatar
AnimeGitB committed
1537
	}
1538

Melledy's avatar
Melledy committed
1539
}