Call Player#onPlayerBorn when the player is created

This commit is contained in:
KingRainbow44 2023-04-13 17:34:04 -04:00
parent 41be058509
commit d6efaaea86
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE

View File

@ -1,89 +1,90 @@
package emu.grasscutter.server.packet.recv;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.commands.SendMailCommand.MailBuilder;
import emu.grasscutter.data.GameData;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.mail.Mail;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SetPlayerBornDataReqOuterClass.SetPlayerBornDataReq;
import emu.grasscutter.server.game.GameSession;
import java.util.Arrays;
@Opcodes(PacketOpcodes.SetPlayerBornDataReq)
public class HandlerSetPlayerBornDataReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
SetPlayerBornDataReq req = SetPlayerBornDataReq.parseFrom(payload);
// Sanity checks
int avatarId = req.getAvatarId();
int startingSkillDepot;
if (avatarId == GameConstants.MAIN_CHARACTER_MALE) {
startingSkillDepot = 504;
} else if (avatarId == GameConstants.MAIN_CHARACTER_FEMALE) {
startingSkillDepot = 704;
} else {
return;
}
// Make sure resources folder is set
if (!GameData.getAvatarDataMap().containsKey(avatarId)) {
Grasscutter.getLogger()
.error("No avatar data found! Please check your ExcelBinOutput folder.");
session.close();
return;
}
// Get player object
Player player = session.getPlayer();
player.setNickname(req.getNickName());
// Create avatar
if (player.getAvatars().getAvatarCount() == 0) {
Avatar mainCharacter = new Avatar(avatarId);
mainCharacter.setSkillDepotData(
GameData.getAvatarSkillDepotDataMap().get(startingSkillDepot));
// Manually handle adding to team
player.addAvatar(mainCharacter, false);
player.setMainCharacterId(avatarId);
player.setHeadImage(avatarId);
player
.getTeamManager()
.getCurrentSinglePlayerTeamInfo()
.getAvatars()
.add(mainCharacter.getAvatarId());
player.save(); // TODO save player team in different object
} else {
return;
}
// Login done
session.getPlayer().onLogin();
// Born resp packet
session.send(new BasePacket(PacketOpcodes.SetPlayerBornDataRsp));
// Default mail
var welcomeMail = GAME_INFO.joinOptions.welcomeMail;
MailBuilder mailBuilder = new MailBuilder(player.getUid(), new Mail());
mailBuilder.mail.mailContent.title = welcomeMail.title;
mailBuilder.mail.mailContent.sender = welcomeMail.sender;
// Please credit Grasscutter if changing something here. We don't condone commercial use of the
// project.
mailBuilder.mail.mailContent.content =
welcomeMail.content
+ "\n<type=\"browser\" text=\"GitHub\" href=\"https://github.com/Grasscutters/Grasscutter\"/>";
mailBuilder.mail.itemList.addAll(Arrays.asList(welcomeMail.items));
mailBuilder.mail.importance = 1;
player.sendMail(mailBuilder.mail);
}
}
package emu.grasscutter.server.packet.recv;
import static emu.grasscutter.config.Configuration.GAME_INFO;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.commands.SendMailCommand.MailBuilder;
import emu.grasscutter.data.GameData;
import emu.grasscutter.game.avatar.Avatar;
import emu.grasscutter.game.mail.Mail;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SetPlayerBornDataReqOuterClass.SetPlayerBornDataReq;
import emu.grasscutter.server.game.GameSession;
import java.util.Arrays;
@Opcodes(PacketOpcodes.SetPlayerBornDataReq)
public class HandlerSetPlayerBornDataReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
SetPlayerBornDataReq req = SetPlayerBornDataReq.parseFrom(payload);
// Sanity checks
int avatarId = req.getAvatarId();
int startingSkillDepot;
if (avatarId == GameConstants.MAIN_CHARACTER_MALE) {
startingSkillDepot = 504;
} else if (avatarId == GameConstants.MAIN_CHARACTER_FEMALE) {
startingSkillDepot = 704;
} else {
return;
}
// Make sure resources folder is set
if (!GameData.getAvatarDataMap().containsKey(avatarId)) {
Grasscutter.getLogger()
.error("No avatar data found! Please check your ExcelBinOutput folder.");
session.close();
return;
}
// Get player object
Player player = session.getPlayer();
player.setNickname(req.getNickName());
// Create avatar
if (player.getAvatars().getAvatarCount() == 0) {
Avatar mainCharacter = new Avatar(avatarId);
mainCharacter.setSkillDepotData(
GameData.getAvatarSkillDepotDataMap().get(startingSkillDepot));
// Manually handle adding to team
player.addAvatar(mainCharacter, false);
player.setMainCharacterId(avatarId);
player.setHeadImage(avatarId);
player
.getTeamManager()
.getCurrentSinglePlayerTeamInfo()
.getAvatars()
.add(mainCharacter.getAvatarId());
player.save(); // TODO save player team in different object
} else {
return;
}
// Login done
session.getPlayer().onLogin();
session.getPlayer().onPlayerBorn();
// Born resp packet
session.send(new BasePacket(PacketOpcodes.SetPlayerBornDataRsp));
// Default mail
var welcomeMail = GAME_INFO.joinOptions.welcomeMail;
MailBuilder mailBuilder = new MailBuilder(player.getUid(), new Mail());
mailBuilder.mail.mailContent.title = welcomeMail.title;
mailBuilder.mail.mailContent.sender = welcomeMail.sender;
// Please credit Grasscutter if changing something here. We don't condone commercial use of the
// project.
mailBuilder.mail.mailContent.content =
welcomeMail.content
+ "\n<type=\"browser\" text=\"GitHub\" href=\"https://github.com/Grasscutters/Grasscutter\"/>";
mailBuilder.mail.itemList.addAll(Arrays.asList(welcomeMail.items));
mailBuilder.mail.importance = 1;
player.sendMail(mailBuilder.mail);
}
}