2022-04-17 12:43:07 +00:00
|
|
|
package emu.grasscutter.game.avatar;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
import emu.grasscutter.data.GameData;
|
2022-05-28 11:21:47 +00:00
|
|
|
import emu.grasscutter.data.excels.AvatarData;
|
|
|
|
import emu.grasscutter.data.excels.AvatarSkillDepotData;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.database.DatabaseHelper;
|
|
|
|
import emu.grasscutter.game.entity.EntityAvatar;
|
2022-04-27 04:21:57 +00:00
|
|
|
import emu.grasscutter.game.inventory.GameItem;
|
2022-04-27 04:24:25 +00:00
|
|
|
import emu.grasscutter.game.player.Player;
|
2022-04-17 12:43:07 +00:00
|
|
|
import emu.grasscutter.server.packet.send.PacketAvatarChangeCostumeNotify;
|
|
|
|
import emu.grasscutter.server.packet.send.PacketAvatarFlycloakChangeNotify;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public class AvatarStorage implements Iterable<Avatar> {
|
|
|
|
private final Player player;
|
|
|
|
private final Int2ObjectMap<Avatar> avatars;
|
|
|
|
private final Long2ObjectMap<Avatar> avatarsGuid;
|
2022-04-17 12:43:07 +00:00
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public AvatarStorage(Player player) {
|
2022-04-17 12:43:07 +00:00
|
|
|
this.player = player;
|
|
|
|
this.avatars = new Int2ObjectOpenHashMap<>();
|
|
|
|
this.avatarsGuid = new Long2ObjectOpenHashMap<>();
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public Player getPlayer() {
|
2022-04-17 12:43:07 +00:00
|
|
|
return player;
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public Int2ObjectMap<Avatar> getAvatars() {
|
2022-04-17 12:43:07 +00:00
|
|
|
return avatars;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getAvatarCount() {
|
|
|
|
return this.avatars.size();
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public Avatar getAvatarById(int id) {
|
2022-04-17 12:43:07 +00:00
|
|
|
return getAvatars().get(id);
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public Avatar getAvatarByGuid(long id) {
|
2022-04-17 12:43:07 +00:00
|
|
|
return avatarsGuid.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasAvatar(int id) {
|
|
|
|
return getAvatars().containsKey(id);
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public boolean addAvatar(Avatar avatar) {
|
2022-04-17 12:43:07 +00:00
|
|
|
if (avatar.getAvatarData() == null || this.hasAvatar(avatar.getAvatarId())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set owner first
|
|
|
|
avatar.setOwner(getPlayer());
|
|
|
|
|
|
|
|
// Put into maps
|
|
|
|
this.avatars.put(avatar.getAvatarId(), avatar);
|
|
|
|
this.avatarsGuid.put(avatar.getGuid(), avatar);
|
|
|
|
|
|
|
|
avatar.save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
public void addStartingWeapon(Avatar avatar) {
|
2022-04-17 12:43:07 +00:00
|
|
|
// Make sure avatar owner is this player
|
|
|
|
if (avatar.getPlayer() != this.getPlayer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create weapon
|
2022-04-27 04:21:57 +00:00
|
|
|
GameItem weapon = new GameItem(avatar.getAvatarData().getInitialWeapon());
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
if (weapon.getItemData() != null) {
|
|
|
|
this.getPlayer().getInventory().addItem(weapon);
|
|
|
|
|
|
|
|
avatar.equipItem(weapon, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean wearFlycloak(long avatarGuid, int flycloakId) {
|
2022-04-27 04:21:57 +00:00
|
|
|
Avatar avatar = this.getAvatarByGuid(avatarGuid);
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
if (avatar == null || !getPlayer().getFlyCloakList().contains(flycloakId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
avatar.setFlyCloak(flycloakId);
|
|
|
|
avatar.save();
|
|
|
|
|
|
|
|
// Update
|
|
|
|
getPlayer().sendPacket(new PacketAvatarFlycloakChangeNotify(avatar));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean changeCostume(long avatarGuid, int costumeId) {
|
2022-04-27 04:21:57 +00:00
|
|
|
Avatar avatar = this.getAvatarByGuid(avatarGuid);
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
if (avatar == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (costumeId != 0 && !getPlayer().getCostumeList().contains(costumeId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO make sure avatar can wear costume
|
|
|
|
|
|
|
|
avatar.setCostume(costumeId);
|
|
|
|
avatar.save();
|
|
|
|
|
|
|
|
// Update entity
|
|
|
|
EntityAvatar entity = avatar.getAsEntity();
|
|
|
|
if (entity == null) {
|
|
|
|
entity = new EntityAvatar(avatar);
|
|
|
|
getPlayer().sendPacket(new PacketAvatarChangeCostumeNotify(entity));
|
|
|
|
} else {
|
2022-04-18 16:39:29 +00:00
|
|
|
getPlayer().getScene().broadcastPacket(new PacketAvatarChangeCostumeNotify(entity));
|
2022-04-17 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadFromDatabase() {
|
2022-04-27 04:21:57 +00:00
|
|
|
List<Avatar> avatars = DatabaseHelper.getAvatars(getPlayer());
|
2022-04-17 12:43:07 +00:00
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
for (Avatar avatar : avatars) {
|
2022-04-17 12:43:07 +00:00
|
|
|
// Should never happen
|
|
|
|
if (avatar.getObjectId() == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-04-27 04:21:57 +00:00
|
|
|
AvatarData avatarData = GameData.getAvatarDataMap().get(avatar.getAvatarId());
|
2022-05-08 12:31:53 +00:00
|
|
|
AvatarSkillDepotData skillDepot = GameData.getAvatarSkillDepotDataMap().get(avatar.getSkillDepotId());
|
|
|
|
if (avatarData == null || skillDepot == null) {
|
2022-04-17 12:43:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set ownerships
|
|
|
|
avatar.setAvatarData(avatarData);
|
2022-05-08 12:31:53 +00:00
|
|
|
avatar.setSkillDepot(skillDepot);
|
2022-04-17 12:43:07 +00:00
|
|
|
avatar.setOwner(getPlayer());
|
|
|
|
|
|
|
|
// Force recalc of const boosted skills
|
2022-05-01 02:34:50 +00:00
|
|
|
avatar.recalcConstellations();
|
2022-04-17 12:43:07 +00:00
|
|
|
|
|
|
|
// Add to avatar storage
|
|
|
|
this.avatars.put(avatar.getAvatarId(), avatar);
|
|
|
|
this.avatarsGuid.put(avatar.getGuid(), avatar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void postLoad() {
|
2022-04-27 04:21:57 +00:00
|
|
|
for (Avatar avatar : this) {
|
2022-04-17 12:43:07 +00:00
|
|
|
// Weapon check
|
|
|
|
if (avatar.getWeapon() == null) {
|
|
|
|
this.addStartingWeapon(avatar);
|
|
|
|
}
|
|
|
|
// Recalc stats
|
|
|
|
avatar.recalcStats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-27 04:21:57 +00:00
|
|
|
public Iterator<Avatar> iterator() {
|
2022-04-17 12:43:07 +00:00
|
|
|
return getAvatars().values().iterator();
|
|
|
|
}
|
|
|
|
}
|