Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.46 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package com.l2jfrozen.gameserver.network.clientpackets;
  16.  
  17. import java.util.logging.Logger;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import java.util.regex.PatternSyntaxException;
  21.  
  22. import com.l2jfrozen.Config;
  23. import com.l2jfrozen.gameserver.GameServer;
  24. import com.l2jfrozen.gameserver.datatables.SkillTable;
  25. import com.l2jfrozen.gameserver.datatables.sql.CharNameTable;
  26. import com.l2jfrozen.gameserver.datatables.sql.CharTemplateTable;
  27. import com.l2jfrozen.gameserver.datatables.sql.ItemTable;
  28. import com.l2jfrozen.gameserver.datatables.sql.SkillTreeTable;
  29. import com.l2jfrozen.gameserver.datatables.xml.ExperienceData;
  30. import com.l2jfrozen.gameserver.idfactory.IdFactory;
  31. import com.l2jfrozen.gameserver.managers.QuestManager;
  32. import com.l2jfrozen.gameserver.model.L2ShortCut;
  33. import com.l2jfrozen.gameserver.model.L2SkillLearn;
  34. import com.l2jfrozen.gameserver.model.L2World;
  35. import com.l2jfrozen.gameserver.model.actor.instance.L2ItemInstance;
  36. import com.l2jfrozen.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jfrozen.gameserver.model.quest.Quest;
  38. import com.l2jfrozen.gameserver.model.quest.QuestState;
  39. import com.l2jfrozen.gameserver.network.L2GameClient;
  40. import com.l2jfrozen.gameserver.network.serverpackets.CharCreateFail;
  41. import com.l2jfrozen.gameserver.network.serverpackets.CharCreateOk;
  42. import com.l2jfrozen.gameserver.network.serverpackets.CharSelectInfo;
  43. import com.l2jfrozen.gameserver.templates.L2Item;
  44. import com.l2jfrozen.gameserver.templates.L2PcTemplate;
  45. import com.l2jfrozen.gameserver.util.Util;
  46.  
  47. @SuppressWarnings("unused")
  48. public final class CharacterCreate extends L2GameClientPacket
  49. {
  50.     private static Logger _log = Logger.getLogger(CharacterCreate.class.getName());
  51.     private static final Object CREATION_LOCK = new Object();
  52.  
  53.     private String _name;
  54.     private byte _sex, _hairStyle, _hairColor, _face;
  55.     private int _race, _classId, _int, _str, _con, _men, _dex, _wit;
  56.  
  57.     @Override
  58.     protected void readImpl()
  59.     {
  60.         _name = readS();
  61.         _race = readD();
  62.         _sex = (byte) readD();
  63.         _classId = readD();
  64.         _int = readD();
  65.         _str = readD();
  66.         _con = readD();
  67.         _men = readD();
  68.         _dex = readD();
  69.         _wit = readD();
  70.         _hairStyle = (byte) readD();
  71.         _hairColor = (byte) readD();
  72.         _face = (byte) readD();
  73.     }
  74.  
  75.     @Override
  76.     protected void runImpl()
  77.     {
  78.  
  79.         if (_name.length() < 3 || _name.length() > 16 || !Util.isAlphaNumeric(_name) || !isValidName(_name))
  80.         {
  81.             if (Config.DEBUG)
  82.                 _log.fine("DEBUG "+getType()+": charname: " + _name + " is invalid. creation failed.");
  83.  
  84.             sendPacket(new CharCreateFail(CharCreateFail.REASON_16_ENG_CHARS));
  85.             return;
  86.         }
  87.  
  88.         if (Config.DEBUG)
  89.             _log.fine("DEBUG "+getType()+": charname: " + _name + " classId: " + _classId);
  90.  
  91.         L2PcInstance newChar = null;
  92.         L2PcTemplate template = null;
  93.  
  94.         // Since checks for duplicate names are done using SQL, lock must be held until data is written to DB as well.
  95.         synchronized (CharNameTable.getInstance())
  96.         {
  97.             if (CharNameTable.getInstance().accountCharNumber(getClient().getAccountName()) >= Config.MAX_CHARACTERS_NUMBER_PER_ACCOUNT && Config.MAX_CHARACTERS_NUMBER_PER_ACCOUNT != 0)
  98.             {
  99.                 if (Config.DEBUG)
  100.                     _log.fine("DEBUG "+getType()+": Max number of characters reached. Creation failed.");
  101.                
  102.                 sendPacket(new CharCreateFail(CharCreateFail.REASON_TOO_MANY_CHARACTERS));
  103.                 return;
  104.             }
  105.             else if (CharNameTable.getInstance().doesCharNameExist(_name))
  106.             {
  107.                 if(Config.DEBUG)
  108.                     _log.fine("DEBUG "+getType()+": charname: " + _name + " already exists. creation failed.");
  109.  
  110.                 sendPacket(new CharCreateFail(CharCreateFail.REASON_NAME_ALREADY_EXISTS));
  111.                 return;
  112.             }else if (CharNameTable.getInstance().ipCharNumber(getClient().getConnection().getInetAddress().getHostName()) >= Config.MAX_CHARACTERS_NUMBER_PER_IP && Config.MAX_CHARACTERS_NUMBER_PER_IP != 0)
  113.             {
  114.                 if (Config.DEBUG)
  115.                     _log.fine("DEBUG "+getType()+": Max number of characters reached for IP. Creation failed.");
  116.                
  117.                 sendPacket(new CharCreateFail(CharCreateFail.REASON_TOO_MANY_CHARACTERS));
  118.                 return;
  119.             }
  120.  
  121.             template = CharTemplateTable.getInstance().getTemplate(_classId);
  122.  
  123.             if (Config.DEBUG)
  124.                 _log.fine("DEBUG "+getType()+": charname: " + _name + " classId: " + _classId + " template: " + template);
  125.  
  126.             if (template == null || template.classBaseLevel > 1)
  127.             {
  128.                 sendPacket(new CharCreateFail(CharCreateFail.REASON_CREATION_FAILED));
  129.                 return;
  130.             }
  131.  
  132.             int objectId = IdFactory.getInstance().getNextId();
  133.             newChar = L2PcInstance.create(objectId, template, getClient().getAccountName(), _name, _hairStyle, _hairColor, _face, _sex != 0);
  134.  
  135.             newChar.setCurrentHp(newChar.getMaxHp());//L2Off like
  136.             //newChar.setCurrentCp(template.baseCpMax);
  137.             newChar.setCurrentCp(0); //L2Off like
  138.             newChar.setCurrentMp(newChar.getMaxMp());//L2Off like
  139.             //newChar.setMaxLoad(template.baseLoad);
  140.  
  141.             // send acknowledgement
  142.             sendPacket(new CharCreateOk()); // Success
  143.             initNewChar(getClient(), newChar);
  144.         }
  145.     }
  146.  
  147.     private boolean isValidName(String text)
  148.     {
  149.         boolean result = true;
  150.         String test = text;
  151.         Pattern pattern;
  152.  
  153.         try
  154.         {
  155.             pattern = Pattern.compile(Config.CNAME_TEMPLATE);
  156.         }
  157.         catch (PatternSyntaxException e) // case of illegal pattern
  158.         {
  159.             if(Config.ENABLE_ALL_EXCEPTIONS)
  160.                 e.printStackTrace();
  161.  
  162.             _log.warning("ERROR "+getType()+": Character name pattern of config is wrong!");
  163.             pattern = Pattern.compile(".*");
  164.         }
  165.  
  166.         Matcher regexp = pattern.matcher(test);
  167.         if (!regexp.matches())
  168.             result = false;
  169.  
  170.         return result;
  171.     }
  172.  
  173.     private void initNewChar(L2GameClient client, L2PcInstance newChar)
  174.     {
  175.         if (Config.DEBUG)
  176.             _log.fine("DEBUG "+getType()+": Character init start");
  177.  
  178.         L2World.getInstance().storeObject(newChar);
  179.         L2PcTemplate template = newChar.getTemplate();
  180.  
  181.         // Starting Items
  182.         if (Config.STARTING_ADENA > 0)
  183.             newChar.addAdena("Init", Config.STARTING_ADENA, null, false);
  184.  
  185.         if (Config.STARTING_AA > 0)
  186.             newChar.addAncientAdena("Init", Config.STARTING_AA, null, false);
  187.  
  188.         if (Config.CUSTOM_STARTER_ITEMS_ENABLED)
  189.         {
  190.             if (newChar.isMageClass())
  191.             {
  192.                 for (int[] reward : Config.STARTING_CUSTOM_ITEMS_M)
  193.                 {
  194.                     if (ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  195.                         newChar.getInventory().addItem("Starter Items Mage", reward[0], reward[1], newChar, null);
  196.                     else
  197.                         for (int i = 0; i < reward[1]; ++i)
  198.                             newChar.getInventory().addItem("Starter Items Mage", reward[0], 1, newChar, null);
  199.                 }
  200.             }
  201.             else
  202.             {
  203.                 for (int[] reward : Config.STARTING_CUSTOM_ITEMS_F)
  204.                 {
  205.                     if(ItemTable.getInstance().createDummyItem(reward[0]).isStackable())
  206.                         newChar.getInventory().addItem("Starter Items Fighter", reward[0], reward[1], newChar, null);
  207.                     else
  208.                         for (int i = 0; i < reward[1]; ++i)
  209.                             newChar.getInventory().addItem("Starter Items Fighter", reward[0], 1, newChar, null);
  210.                 }
  211.             }
  212.         }
  213.  
  214.         if (Config.SPAWN_CHAR)
  215.             newChar.setXYZInvisible(Config.SPAWN_X, Config.SPAWN_Y, Config.SPAWN_Z);
  216.         else
  217.             newChar.setXYZInvisible(template.spawnX, template.spawnY, template.spawnZ);
  218.  
  219.         if (Config.ALLOW_CREATE_LVL)
  220.             newChar.getStat().addExp(ExperienceData.getInstance().getExpForLevel(Config.CHAR_CREATE_LVL));
  221.  
  222.         if (Config.CHAR_TITLE)
  223.             newChar.setTitle(Config.ADD_CHAR_TITLE);
  224.         else
  225.             newChar.setTitle("");
  226.  
  227.         if (Config.PVP_PK_TITLE)
  228.             newChar.setTitle(Config.PVP_TITLE_PREFIX + "0" + Config.PK_TITLE_PREFIX + "0 ");
  229.  
  230.         // Shortcuts
  231.         newChar.registerShortCut(new L2ShortCut(0, 0, 3, 2, -1, 1)); // Attack
  232.         newChar.registerShortCut(new L2ShortCut(3, 0, 3, 5, -1, 1)); // Take
  233.         newChar.registerShortCut(new L2ShortCut(10, 0, 3, 0, -1, 1)); // Sit
  234.  
  235.         ItemTable itemTable = ItemTable.getInstance();
  236.         L2Item[] items = template.getItems();
  237.  
  238.         for (L2Item item2 : items)
  239.         {
  240.             L2ItemInstance item = newChar.getInventory().addItem("Init", item2.getItemId(), 1, newChar, null);
  241.  
  242.             if (item.getItemId() == 5588)
  243.                 newChar.registerShortCut(new L2ShortCut(11, 0, 1, item.getObjectId(), -1, 1)); // Tutorial Book shortcut
  244.  
  245.             if (item.isEquipable())
  246.                 if(newChar.getActiveWeaponItem() == null || !(item.getItem().getType2() != L2Item.TYPE2_WEAPON))
  247.                     newChar.getInventory().equipItemAndRecord(item);
  248.         }
  249.  
  250.         L2SkillLearn[] startSkills = SkillTreeTable.getInstance().getAvailableSkills(newChar, newChar.getClassId());
  251.  
  252.         for(L2SkillLearn startSkill : startSkills)
  253.         {
  254.             newChar.addSkill(SkillTable.getInstance().getInfo(startSkill.getId(), startSkill.getLevel()), true);
  255.  
  256.             if(startSkill.getId() == 1001 || startSkill.getId() == 1177)
  257.                 newChar.registerShortCut(new L2ShortCut(1, 0, 2, startSkill.getId(), 1, 1));
  258.  
  259.             if(startSkill.getId() == 1216)
  260.                 newChar.registerShortCut(new L2ShortCut(10, 0, 2, startSkill.getId(), 1, 1));
  261.  
  262.             if(Config.DEBUG)
  263.                 _log.fine("DEBUG "+getType()+": Adding starter skill:" + startSkill.getId() + " / " + startSkill.getLevel());
  264.         }
  265.  
  266.         startTutorialQuest(newChar);
  267.         newChar.store();
  268.         newChar.deleteMe(); // Release the world of this character and it's inventory
  269.  
  270.         // Before the char selection, check shutdown status
  271.         if (GameServer.getSelectorThread().isShutdown())
  272.         {
  273.             client.closeNow();
  274.             return;
  275.         }
  276.        
  277.         // Send char list
  278.         CharSelectInfo cl = new CharSelectInfo(client.getAccountName(), client.getSessionId().playOkID1);
  279.         client.getConnection().sendPacket(cl);
  280.         client.setCharSelection(cl.getCharInfo());
  281.  
  282.         if(Config.DEBUG)
  283.             _log.fine("DEBUG "+getType()+": Character init end");
  284.     }
  285.  
  286.     public void startTutorialQuest(L2PcInstance player)
  287.     {
  288.         QuestState qs = player.getQuestState("255_Tutorial");
  289.         Quest q = null;
  290.  
  291.         if(qs == null && !Config.ALT_DEV_NO_QUESTS){
  292.             q = QuestManager.getInstance().getQuest("255_Tutorial");
  293.         }
  294.        
  295.         if(q != null)
  296.             q.newQuestState(player);
  297.     }
  298.  
  299.     @Override
  300.     public String getType()
  301.     {
  302.         return "[C] 0B CharacterCreate";
  303.     }
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement