1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-10 09:22:49 +00:00

Character creation code moved to CharacterService

This commit is contained in:
2011-08-07 22:57:17 -03:00
parent 4fe7eeb95b
commit 5ca0dae601
6 changed files with 231 additions and 289 deletions

View File

@@ -17,20 +17,12 @@
package com.l2jserver.game.net.packet.client;
import org.jboss.netty.buffer.ChannelBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
import com.l2jserver.game.net.Lineage2Client;
import com.l2jserver.game.net.packet.AbstractClientPacket;
import com.l2jserver.game.net.packet.server.SM_CHAR_CREATE_FAIL;
import com.l2jserver.game.net.packet.server.SM_CHAR_CREATE_OK;
import com.l2jserver.model.dao.CharacterDAO;
import com.l2jserver.model.id.object.CharacterID;
import com.l2jserver.model.id.object.provider.CharacterIDProvider;
import com.l2jserver.model.id.template.CharacterTemplateID;
import com.l2jserver.model.id.template.provider.CharacterTemplateIDProvider;
import com.l2jserver.model.template.CharacterTemplate;
import com.l2jserver.model.template.actor.ActorSex;
import com.l2jserver.model.template.character.CharacterClass;
import com.l2jserver.model.template.character.CharacterRace;
@@ -38,6 +30,10 @@ import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.character.CharacterAppearance.CharacterFace;
import com.l2jserver.model.world.character.CharacterAppearance.CharacterHairColor;
import com.l2jserver.model.world.character.CharacterAppearance.CharacterHairStyle;
import com.l2jserver.service.game.character.CharacterInvalidAppearanceException;
import com.l2jserver.service.game.character.CharacterInvalidNameException;
import com.l2jserver.service.game.character.CharacterNameAlreadyExistsException;
import com.l2jserver.service.game.character.CharacterService;
import com.l2jserver.util.BufferUtils;
/**
@@ -52,25 +48,11 @@ public class CM_CHAR_CREATE extends AbstractClientPacket {
*/
public static final int OPCODE = 0x0c;
/**
* The logger
*/
private static final Logger log = LoggerFactory
.getLogger(CM_CHAR_CREATE.class);
// services and daos
/**
* The {@link CharacterDAO} implementation
* The {@link CharacterService} implementation
*/
private final CharacterDAO characterDao;
/**
* The {@link CharacterID} factory
*/
private final CharacterIDProvider characterIdFactory;
/**
* The {@link CharacterTemplateID} factory
*/
private final CharacterTemplateIDProvider characterTemplateIdProvider;
private final CharacterService characterService;
// packet
/**
@@ -78,11 +60,14 @@ public class CM_CHAR_CREATE extends AbstractClientPacket {
*/
private String name;
/**
* The race of the new character
* The race of the new character. Note that this is ignored and the template
* value is used.
*/
@SuppressWarnings("unused")
private CharacterRace race;
/**
* The sex of the new character
* The sex of the new character. Note that this is ignored and the template
* value is used.
*/
private ActorSex sex;
/**
@@ -94,31 +79,37 @@ public class CM_CHAR_CREATE extends AbstractClientPacket {
* The new character intelligence. Note that this is ignored and the
* template value is used.
*/
@SuppressWarnings("unused")
private int intelligence;
/**
* The new character intelligence. Note that this is ignored and the
* template value is used.
*/
@SuppressWarnings("unused")
private int strength;
/**
* The new character strength. Note that this is ignored and the template
* value is used.
*/
@SuppressWarnings("unused")
private int concentration;
/**
* The new character concentration. Note that this is ignored and the
* template value is used.
*/
@SuppressWarnings("unused")
private int mentality;
/**
* The new character mentality. Note that this is ignored and the template
* value is used.
*/
@SuppressWarnings("unused")
private int dexterity;
/**
* The new character dexterity. Note that this is ignored and the template
* value is used.
*/
@SuppressWarnings("unused")
private int witness;
/**
@@ -135,12 +126,8 @@ public class CM_CHAR_CREATE extends AbstractClientPacket {
private CharacterFace face;
@Inject
public CM_CHAR_CREATE(CharacterDAO characterDao,
CharacterIDProvider characterIdFactory,
CharacterTemplateIDProvider characterTemplateIdFactory) {
this.characterDao = characterDao;
this.characterIdFactory = characterIdFactory;
this.characterTemplateIdProvider = characterTemplateIdFactory;
public CM_CHAR_CREATE(CharacterService characterService) {
this.characterService = characterService;
}
@Override
@@ -164,268 +151,24 @@ public class CM_CHAR_CREATE extends AbstractClientPacket {
@Override
public void process(final Lineage2Client conn) {
log.debug("Creating a new character, race={}, sex={}, class={}",
new Object[] { race, sex, characterClass });
if ((name.length() < 1) || (name.length() > 16)) {
log.debug("Character name length invalid: {}. Aborting.", name);
try {
final L2Character character = characterService.create(name, sex,
characterClass, hairStyle, hairColor, face);
if (character != null)
conn.write(SM_CHAR_CREATE_OK.INSTANCE);
else
conn.write(new SM_CHAR_CREATE_FAIL(
SM_CHAR_CREATE_FAIL.Reason.REASON_CREATION_FAILED));
} catch (CharacterInvalidNameException e) {
conn.write(new SM_CHAR_CREATE_FAIL(
SM_CHAR_CREATE_FAIL.Reason.REASON_16_ENG_CHARS));
return;
}
// TODO check alphanumeric name
if (sex == null || hairStyle == null || hairColor == null
|| face == null) {
log.debug("Character appearance invalid. Aborting.");
// if some of those attributes is null, something wrong happened.
// Maybe we don't support the value sent!
} catch (CharacterInvalidAppearanceException e) {
conn.write(new SM_CHAR_CREATE_FAIL(
SM_CHAR_CREATE_FAIL.Reason.REASON_CREATION_FAILED));
return;
}
// existence check
final L2Character existenceCheck = characterDao.selectByName(name);
if (existenceCheck != null) {
log.debug("Character name already exists: {}. Aborting.", name);
} catch (CharacterNameAlreadyExistsException e) {
conn.write(new SM_CHAR_CREATE_FAIL(
SM_CHAR_CREATE_FAIL.Reason.REASON_NAME_ALREADY_EXISTS));
return;
}
// create template id and lookup for the template instance
final CharacterTemplateID templateId = characterTemplateIdProvider
.resolveID(characterClass.id);
final CharacterTemplate template = templateId.getTemplate();
log.debug("Creating character with template {}", template);
// ensure parameters passed by the client are true
if ((race != template.getRace())) {
log.debug("race, expected {}, received {}", template.getRace(),
race);
log.debug(
"Values sent by client and from template does not match: {}",
template);
// some of the values didn't match, canceling creation
conn.write(new SM_CHAR_CREATE_FAIL(
SM_CHAR_CREATE_FAIL.Reason.REASON_CREATION_FAILED));
return;
}
// everything is fine, allocate a new ID
final CharacterID id = characterIdFactory.createID();
// create the instance from the template
final L2Character character = template.create();
log.debug("Character object created, ID: {}, Object: {}", id, character);
// set parameters
character.setID(id);
character.setName(name);
character.setSex(sex);
character.getAppearance().setHairStyle(hairStyle);
character.getAppearance().setHairColor(hairColor);
character.getAppearance().setFace(face);
if (characterDao.save(character)) {
log.debug("Character saved to database");
conn.write(SM_CHAR_CREATE_OK.INSTANCE);
}
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the race
*/
public CharacterRace getRace() {
return race;
}
/**
* @param race
* the race to set
*/
public void setRace(CharacterRace race) {
this.race = race;
}
/**
* @return the sex
*/
public ActorSex getSex() {
return sex;
}
/**
* @param sex
* the sex to set
*/
public void setSex(ActorSex sex) {
this.sex = sex;
}
/**
* @return the character class
*/
public CharacterClass getCharacterClass() {
return characterClass;
}
/**
* @param characterClass
* the character class
*/
public void setClassId(CharacterClass characterClass) {
this.characterClass = characterClass;
}
/**
* @return the intelligence
*/
public int getIntelligence() {
return intelligence;
}
/**
* @param intelligence
* the intelligence to set
*/
public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
/**
* @return the strength
*/
public int getStrength() {
return strength;
}
/**
* @param strength
* the strength to set
*/
public void setStrength(int strength) {
this.strength = strength;
}
/**
* @return the concentration
*/
public int getConcentration() {
return concentration;
}
/**
* @param concentration
* the concentration to set
*/
public void setConcentration(int concentration) {
this.concentration = concentration;
}
/**
* @return the mentality
*/
public int getMentality() {
return mentality;
}
/**
* @param mentality
* the mentality to set
*/
public void setMentality(int mentality) {
this.mentality = mentality;
}
/**
* @return the dexterity
*/
public int getDexterity() {
return dexterity;
}
/**
* @param dexterity
* the dexterity to set
*/
public void setDexterity(int dexterity) {
this.dexterity = dexterity;
}
/**
* @return the witness
*/
public int getWitness() {
return witness;
}
/**
* @param witness
* the witness to set
*/
public void setWitness(int witness) {
this.witness = witness;
}
/**
* @return the hairStyle
*/
public CharacterHairStyle getHairStyle() {
return hairStyle;
}
/**
* @param hairStyle
* the hairStyle to set
*/
public void setHairStyle(CharacterHairStyle hairStyle) {
this.hairStyle = hairStyle;
}
/**
* @return the hairColor
*/
public CharacterHairColor getHairColor() {
return hairColor;
}
/**
* @param hairColor
* the hairColor to set
*/
public void setHairColor(CharacterHairColor hairColor) {
this.hairColor = hairColor;
}
/**
* @return the face
*/
public CharacterFace getFace() {
return face;
}
/**
* @param face
* the face to set
*/
public void setFace(CharacterFace face) {
this.face = face;
}
}