Grasscutter/src/main/java/emu/grasscutter/game/avatar/AvatarStorage.java

178 lines
4.4 KiB
Java
Raw Normal View History

2022-04-17 12:43:07 +00:00
package emu.grasscutter.game.avatar;
import java.util.Iterator;
import java.util.List;
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;
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;
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
public AvatarStorage(Player player) {
2022-04-17 12:43:07 +00:00
this.player = player;
this.avatars = new Int2ObjectOpenHashMap<>();
this.avatarsGuid = new Long2ObjectOpenHashMap<>();
}
public Player getPlayer() {
2022-04-17 12:43:07 +00:00
return player;
}
public Int2ObjectMap<Avatar> getAvatars() {
2022-04-17 12:43:07 +00:00
return avatars;
}
public int getAvatarCount() {
return this.avatars.size();
}
public Avatar getAvatarById(int id) {
2022-04-17 12:43:07 +00:00
return getAvatars().get(id);
}
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);
}
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;
}
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
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) {
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) {
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 {
getPlayer().getScene().broadcastPacket(new PacketAvatarChangeCostumeNotify(entity));
2022-04-17 12:43:07 +00:00
}
// Done
return true;
}
public void loadFromDatabase() {
List<Avatar> avatars = DatabaseHelper.getAvatars(getPlayer());
2022-04-17 12:43:07 +00:00
for (Avatar avatar : avatars) {
2022-04-17 12:43:07 +00:00
// Should never happen
if (avatar.getObjectId() == null) {
continue;
}
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
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() {
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
public Iterator<Avatar> iterator() {
2022-04-17 12:43:07 +00:00
return getAvatars().values().iterator();
}
}