Guest User

Offline Shop & Craft

a guest
Jun 5th, 2015
1,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.93 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P aCis_gameserver
  3. Index: config/offlineshop.properties
  4. ===================================================================
  5. --- config/offlineshop.properties   (revision 0)
  6. +++ config/offlineshop.properties   (working copy)
  7. @@ -0,0 +1,38 @@
  8. +# ---------------------------------------------------------------------------
  9. +#              Offline trade/craft
  10. +# ---------------------------------------------------------------------------
  11. +
  12. +# Option to enable or disable offline trade feature.
  13. +# Enable -> true, Disable -> false
  14. +OfflineTradeEnable = False
  15. +
  16. +# Option to enable or disable offline craft feature.
  17. +# Enable -> true, Disable -> false
  18. +OfflineCraftEnable = False
  19. +
  20. +# If set to True, off-line shops will be possible only peace zones.
  21. +# Default: False
  22. +OfflineModeInPeaceZone = False
  23. +
  24. +# If set to True, players in off-line shop mode wont take any damage, thus they cannot be killed.
  25. +# Default: False
  26. +OfflineModeNoDamage = False
  27. +
  28. +# If set to True, name color will be changed then entering offline mode
  29. +OfflineSetNameColor = False
  30. +
  31. +# Color of the name in offline mode (if OfflineSetNameColor = True)
  32. +OfflineNameColor = 808080
  33. +
  34. +#Restore offline traders/crafters after restart/shutdown. Default: false.
  35. +RestoreOffliners = False
  36. +
  37. +#Do not restore offline characters, after OfflineMaxDays days spent from first restore.
  38. +#Require server restart to disconnect expired shops.
  39. +#0 = disabled (always restore).
  40. +#Default: 10
  41. +OfflineMaxDays = 10
  42. +
  43. +#Disconnect shop after finished selling, buying.
  44. +#Default: True
  45. +OfflineDisconnectFinished = True
  46. \ No newline at end of file
  47. Index: java/net/sf/l2j/gameserver/GameServer.java
  48. ===================================================================
  49. --- java/net/sf/l2j/gameserver/GameServer.java  (revision 371)
  50. +++ java/net/sf/l2j/gameserver/GameServer.java  (working copy)
  51. @@ -56,6 +56,7 @@
  52.  import net.sf.l2j.gameserver.datatables.MultisellData;
  53.  import net.sf.l2j.gameserver.datatables.NpcTable;
  54.  import net.sf.l2j.gameserver.datatables.NpcWalkerRoutesTable;
  55. +import net.sf.l2j.gameserver.datatables.OfflineTradersTable;
  56.  import net.sf.l2j.gameserver.datatables.PetDataTable;
  57.  import net.sf.l2j.gameserver.datatables.RecipeTable;
  58.  import net.sf.l2j.gameserver.datatables.SkillTable;
  59. @@ -182,6 +183,9 @@
  60.         MovieMakerManager.getInstance();
  61.         PetitionManager.getInstance();
  62.        
  63. +       if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
  64. +           OfflineTradersTable.getInstance().restoreOfflineTraders();
  65. +      
  66.         StringUtil.printSection("Characters");
  67.         CharTemplateTable.getInstance();
  68.         CharNameTable.getInstance();
  69. Index: java/net/sf/l2j/Config.java
  70. ===================================================================
  71. --- java/net/sf/l2j/Config.java (revision 371)
  72. +++ java/net/sf/l2j/Config.java (working copy)
  73. @@ -47,6 +47,7 @@
  74.     public static final String GEOENGINE_FILE = "./config/geoengine.properties";
  75.     public static final String HEXID_FILE = "./config/hexid.txt";
  76.     public static final String LOGIN_CONFIGURATION_FILE = "./config/loginserver.properties";
  77. +   public static final String OFFLINE_FILE = "./config/offlineshop.properties";
  78.     public static final String NPCS_FILE = "./config/npcs.properties";
  79.     public static final String PLAYERS_FILE = "./config/players.properties";
  80.     public static final String SERVER_FILE = "./config/server.properties";
  81. @@ -279,6 +280,19 @@
  82.     public static int MAX_CONNECTION_PER_IP;
  83.    
  84.     // --------------------------------------------------
  85. +   // Offline Shop && Craft
  86. +   // --------------------------------------------------
  87. +   public static boolean OFFLINE_TRADE_ENABLE;
  88. +   public static boolean OFFLINE_CRAFT_ENABLE;
  89. +   public static boolean OFFLINE_MODE_IN_PEACE_ZONE;
  90. +   public static boolean OFFLINE_MODE_NO_DAMAGE;
  91. +   public static boolean RESTORE_OFFLINERS;
  92. +   public static int OFFLINE_MAX_DAYS;
  93. +   public static boolean OFFLINE_DISCONNECT_FINISHED;
  94. +   public static boolean OFFLINE_SET_NAME_COLOR;
  95. +   public static int OFFLINE_NAME_COLOR;
  96. +  
  97. +   // --------------------------------------------------
  98.     // NPCs / Monsters
  99.     // --------------------------------------------------
  100.    
  101. @@ -867,6 +881,18 @@
  102.             SERVER_ID = Integer.parseInt(hexid.getProperty("ServerID"));
  103.             HEX_ID = new BigInteger(hexid.getProperty("HexID"), 16).toByteArray();
  104.            
  105. +           // Offline Shop
  106. +           ExProperties offline = load(OFFLINE_FILE);
  107. +           OFFLINE_TRADE_ENABLE = offline.getProperty("OfflineTradeEnable", false);
  108. +           OFFLINE_CRAFT_ENABLE = offline.getProperty("OfflineCraftEnable", false);
  109. +           OFFLINE_MODE_IN_PEACE_ZONE = offline.getProperty("OfflineModeInPeaceZone", false);
  110. +           OFFLINE_MODE_NO_DAMAGE = offline.getProperty("OfflineModeNoDamage", false);
  111. +           OFFLINE_SET_NAME_COLOR = offline.getProperty("OfflineSetNameColor", false);
  112. +           OFFLINE_NAME_COLOR = Integer.decode("0x" + offline.getProperty("OfflineNameColor", 808080));
  113. +           RESTORE_OFFLINERS = offline.getProperty("RestoreOffliners", false);
  114. +           OFFLINE_MAX_DAYS = offline.getProperty("OfflineMaxDays", 10);
  115. +           OFFLINE_DISCONNECT_FINISHED = offline.getProperty("OfflineDisconnectFinished", true);
  116. +          
  117.             // NPCs / Monsters
  118.             ExProperties npcs = load(NPCS_FILE);
  119.             CHAMPION_FREQUENCY = npcs.getProperty("ChampionFrequency", 0);
  120. Index: java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java
  121. ===================================================================
  122. --- java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java  (revision 371)
  123. +++ java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java  (working copy)
  124. @@ -628,7 +628,13 @@
  125.         final String clientInfo = player.getClient().toString();
  126.         final String account = clientInfo.substring(clientInfo.indexOf("Account: ") + 9, clientInfo.indexOf(" - IP: "));
  127.         final String ip = clientInfo.substring(clientInfo.indexOf(" - IP: ") + 7, clientInfo.lastIndexOf("]"));
  128. +       final L2GameClient client = player.getClient();
  129.        
  130. +       if (client == null)
  131. +           activeChar.sendMessage("Client is null.");
  132. +       else if (client.isDetached())
  133. +           activeChar.sendMessage("Client is detached.");
  134. +      
  135.         final NpcHtmlMessage html = new NpcHtmlMessage(0);
  136.         html.setFile("data/html/admin/" + filename);
  137.         html.replace("%name%", player.getName());
  138. Index: java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java
  139. ===================================================================
  140. --- java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java   (revision 371)
  141. +++ java/net/sf/l2j/gameserver/model/actor/instance/L2PcInstance.java   (working copy)
  142. @@ -271,6 +271,16 @@
  143.         {
  144.             return _id;
  145.         }
  146. +      
  147. +       public static PrivateStoreType findById(int id)
  148. +       {
  149. +           for (PrivateStoreType privateStoreType : values())
  150. +           {
  151. +               if (privateStoreType.getId() == id)
  152. +                   return privateStoreType;
  153. +           }
  154. +           return null;
  155. +       }
  156.     }
  157.    
  158.     public enum PunishLevel
  159. @@ -415,6 +425,7 @@
  160.     public int _telemode = 0;
  161.     private boolean _inCrystallize;
  162.     private boolean _inCraftMode;
  163. +   private long _offlineShopStart = 0;
  164.    
  165.     private final Map<Integer, RecipeList> _dwarvenRecipeBook = new HashMap<>();
  166.     private final Map<Integer, RecipeList> _commonRecipeBook = new HashMap<>();
  167. @@ -704,9 +715,16 @@
  168.    
  169.     public String getAccountName()
  170.     {
  171. +       if (getClient() == null)
  172. +           return getAccountNamePlayer();
  173.         return getClient().getAccountName();
  174.     }
  175.    
  176. +   public String getAccountNamePlayer()
  177. +   {
  178. +       return _accountName;
  179. +   }
  180. +  
  181.     public Map<Integer, String> getAccountChars()
  182.     {
  183.         return _chars;
  184. @@ -4642,6 +4660,9 @@
  185.     public void setPrivateStoreType(PrivateStoreType type)
  186.     {
  187.         _privateStoreType = type;
  188. +      
  189. +       if (Config.OFFLINE_DISCONNECT_FINISHED && (type == PrivateStoreType.NONE) && ((getClient() == null) || getClient().isDetached()))
  190. +           deleteMe();
  191.     }
  192.    
  193.     /**
  194. @@ -10169,6 +10190,16 @@
  195.         return _isInSiege;
  196.     }
  197.    
  198. +   public long getOfflineStartTime()
  199. +   {
  200. +       return _offlineShopStart;
  201. +   }
  202. +  
  203. +   public void setOfflineStartTime(long time)
  204. +   {
  205. +       _offlineShopStart = time;
  206. +   }
  207. +  
  208.     /**
  209.      * Remove player from BossZones (used on char logout/exit)
  210.      */
  211. Index: java/net/sf/l2j/gameserver/model/actor/status/PcStatus.java
  212. ===================================================================
  213. --- java/net/sf/l2j/gameserver/model/actor/status/PcStatus.java (revision 371)
  214. +++ java/net/sf/l2j/gameserver/model/actor/status/PcStatus.java (working copy)
  215. @@ -22,6 +22,7 @@
  216.  import net.sf.l2j.gameserver.model.actor.L2Playable;
  217.  import net.sf.l2j.gameserver.model.actor.L2Summon;
  218.  import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  219. +import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance.PrivateStoreType;
  220.  import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  221.  import net.sf.l2j.gameserver.model.actor.stat.PcStat;
  222.  import net.sf.l2j.gameserver.model.entity.Duel.DuelState;
  223. @@ -68,6 +69,10 @@
  224.         if (getActiveChar().isDead())
  225.             return;
  226.        
  227. +       // If OFFLINE_MODE_NO_DAMAGE is enabled and player is offline and he is in store/craft mode, no damage is taken.
  228. +       if (Config.OFFLINE_MODE_NO_DAMAGE && (getActiveChar().getClient() != null) && getActiveChar().getClient().isDetached() && ((Config.OFFLINE_TRADE_ENABLE && ((getActiveChar().getPrivateStoreType() == PrivateStoreType.SELL) || (getActiveChar().getPrivateStoreType() == PrivateStoreType.BUY))) || (Config.OFFLINE_CRAFT_ENABLE && (getActiveChar().isInCraftMode() || (getActiveChar().getPrivateStoreType() == PrivateStoreType.MANUFACTURE)))))
  229. +           return;
  230. +      
  231.         // invul handling
  232.         if (getActiveChar().isInvul())
  233.         {
  234. Index: java/net/sf/l2j/gameserver/network/L2GameClient.java
  235. ===================================================================
  236. --- java/net/sf/l2j/gameserver/network/L2GameClient.java    (revision 371)
  237. +++ java/net/sf/l2j/gameserver/network/L2GameClient.java    (working copy)
  238. @@ -41,6 +41,8 @@
  239.  import net.sf.l2j.gameserver.model.L2Clan;
  240.  import net.sf.l2j.gameserver.model.L2World;
  241.  import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  242. +import net.sf.l2j.gameserver.model.olympiad.OlympiadManager;
  243. +import net.sf.l2j.gameserver.model.zone.ZoneId;
  244.  import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
  245.  import net.sf.l2j.gameserver.network.serverpackets.L2GameServerPacket;
  246.  import net.sf.l2j.gameserver.network.serverpackets.ServerClose;
  247. @@ -445,6 +447,9 @@
  248.    
  249.     public void close(L2GameServerPacket gsp)
  250.     {
  251. +       if (getConnection() == null)
  252. +           return;
  253. +      
  254.         getConnection().close(gsp);
  255.     }
  256.    
  257. @@ -528,9 +533,6 @@
  258.    
  259.     protected class DisconnectTask implements Runnable
  260.     {
  261. -       /**
  262. -        * @see java.lang.Runnable#run()
  263. -        */
  264.         @Override
  265.         public void run()
  266.         {
  267. @@ -541,6 +543,31 @@
  268.                 if (getActiveChar() != null && !isDetached())
  269.                 {
  270.                     setDetached(true);
  271. +                   if (offlineMode(getActiveChar()))
  272. +                   {
  273. +                       getActiveChar().leaveParty();
  274. +                       OlympiadManager.getInstance().unRegisterNoble(getActiveChar());
  275. +                      
  276. +                       // If the L2PcInstance has Pet, unsummon it
  277. +                       if (getActiveChar().hasPet())
  278. +                       {
  279. +                           getActiveChar().getPet().unSummon(getActiveChar());
  280. +                           // Dead pet wasn't unsummoned, broadcast npcinfo changes (pet will be without owner name - means owner offline)
  281. +                           if (getActiveChar().getPet() != null)
  282. +                               getActiveChar().getPet().broadcastNpcInfo(0);
  283. +                       }
  284. +                      
  285. +                       if (Config.OFFLINE_SET_NAME_COLOR)
  286. +                       {
  287. +                           getActiveChar().getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
  288. +                           getActiveChar().broadcastUserInfo();
  289. +                       }
  290. +                      
  291. +                       if (getActiveChar().getOfflineStartTime() == 0)
  292. +                           getActiveChar().setOfflineStartTime(System.currentTimeMillis());
  293. +                      
  294. +                       return;
  295. +                   }
  296.                     fast = !getActiveChar().isInCombat() && !getActiveChar().isLocked();
  297.                 }
  298.                 cleanMe(fast);
  299. @@ -552,6 +579,43 @@
  300.         }
  301.     }
  302.    
  303. +   /**
  304. +    * @param player the player to be check.
  305. +    * @return {@code true} if the player is allowed to remain as off-line shop.
  306. +    */
  307. +   protected static boolean offlineMode(L2PcInstance player)
  308. +   {
  309. +       if (player.isInOlympiadMode() || player.isFestivalParticipant() || player.isInJail() || player.getVehicle() != null)
  310. +           return false;
  311. +      
  312. +       boolean canSetShop = false;
  313. +       switch (player.getPrivateStoreType())
  314. +       {
  315. +           case SELL:
  316. +           case PACKAGE_SELL:
  317. +           case BUY:
  318. +           {
  319. +               canSetShop = Config.OFFLINE_TRADE_ENABLE;
  320. +               break;
  321. +           }
  322. +           case MANUFACTURE:
  323. +           {
  324. +               canSetShop = Config.OFFLINE_TRADE_ENABLE;
  325. +               break;
  326. +           }
  327. +           default:
  328. +           {
  329. +               canSetShop = Config.OFFLINE_CRAFT_ENABLE && player.isInCraftMode();
  330. +               break;
  331. +           }
  332. +       }
  333. +      
  334. +       if (Config.OFFLINE_MODE_IN_PEACE_ZONE && !player.isInsideZone(ZoneId.PEACE))
  335. +           canSetShop = false;
  336. +      
  337. +       return canSetShop;
  338. +   }
  339. +  
  340.     public void cleanMe(boolean fast)
  341.     {
  342.         try
  343. Index: java/net/sf/l2j/gameserver/datatables/OfflineTradersTable.java
  344. ===================================================================
  345. --- java/net/sf/l2j/gameserver/datatables/OfflineTradersTable.java  (revision 0)
  346. +++ java/net/sf/l2j/gameserver/datatables/OfflineTradersTable.java  (working copy)
  347. @@ -0,0 +1,270 @@
  348. +/*
  349. + * Copyright (C) 2004-2015 L2J Server
  350. + *
  351. + * This file is part of L2J Server.
  352. + *
  353. + * L2J Server is free software: you can redistribute it and/or modify
  354. + * it under the terms of the GNU General Public License as published by
  355. + * the Free Software Foundation, either version 3 of the License, or
  356. + * (at your option) any later version.
  357. + *
  358. + * L2J Server is distributed in the hope that it will be useful,
  359. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  360. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  361. + * General Public License for more details.
  362. + *
  363. + * You should have received a copy of the GNU General Public License
  364. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  365. + */
  366. +package net.sf.l2j.gameserver.datatables;
  367. +
  368. +import java.sql.Connection;
  369. +import java.sql.PreparedStatement;
  370. +import java.sql.ResultSet;
  371. +import java.sql.Statement;
  372. +import java.util.Calendar;
  373. +import java.util.logging.Level;
  374. +import java.util.logging.Logger;
  375. +
  376. +import net.sf.l2j.Config;
  377. +import net.sf.l2j.L2DatabaseFactory;
  378. +import net.sf.l2j.gameserver.LoginServerThread;
  379. +import net.sf.l2j.gameserver.model.L2ManufactureItem;
  380. +import net.sf.l2j.gameserver.model.L2ManufactureList;
  381. +import net.sf.l2j.gameserver.model.L2World;
  382. +import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  383. +import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance.PrivateStoreType;
  384. +import net.sf.l2j.gameserver.model.tradelist.TradeItem;
  385. +import net.sf.l2j.gameserver.network.L2GameClient;
  386. +import net.sf.l2j.gameserver.network.L2GameClient.GameClientState;
  387. +
  388. +public class OfflineTradersTable
  389. +{
  390. +   private static Logger LOGGER = Logger.getLogger(OfflineTradersTable.class.getName());
  391. +  
  392. +   // SQL DEFINITIONS
  393. +   private static final String SAVE_OFFLINE_STATUS = "INSERT INTO character_offline_trade (`charId`,`time`,`type`,`title`) VALUES (?,?,?,?)";
  394. +   private static final String SAVE_ITEMS = "INSERT INTO character_offline_trade_items (`charId`,`item`,`count`,`price`) VALUES (?,?,?,?)";
  395. +   private static final String CLEAR_OFFLINE_TABLE = "DELETE FROM character_offline_trade";
  396. +   private static final String CLEAR_OFFLINE_TABLE_ITEMS = "DELETE FROM character_offline_trade_items";
  397. +   private static final String LOAD_OFFLINE_STATUS = "SELECT * FROM character_offline_trade";
  398. +   private static final String LOAD_OFFLINE_ITEMS = "SELECT * FROM character_offline_trade_items WHERE charId = ?";
  399. +  
  400. +   public void storeOffliners()
  401. +   {
  402. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  403. +           PreparedStatement stm1 = con.prepareStatement(CLEAR_OFFLINE_TABLE);
  404. +           PreparedStatement stm2 = con.prepareStatement(CLEAR_OFFLINE_TABLE_ITEMS);
  405. +           PreparedStatement stm3 = con.prepareStatement(SAVE_OFFLINE_STATUS);
  406. +           PreparedStatement stm_items = con.prepareStatement(SAVE_ITEMS))
  407. +       {
  408. +           stm1.execute();
  409. +           stm2.execute();
  410. +           con.setAutoCommit(false); // avoid halfway done
  411. +          
  412. +           for (L2PcInstance pc : L2World.getInstance().getPlayers())
  413. +           {
  414. +               try
  415. +               {
  416. +                   if ((pc.getPrivateStoreType() != PrivateStoreType.NONE) && ((pc.getClient() == null) || pc.getClient().isDetached()))
  417. +                   {
  418. +                       stm3.setInt(1, pc.getObjectId()); // Char Id
  419. +                       stm3.setLong(2, pc.getOfflineStartTime());
  420. +                       stm3.setInt(3, pc.getPrivateStoreType().getId()); // store type
  421. +                       String title = null;
  422. +                      
  423. +                       switch (pc.getPrivateStoreType())
  424. +                       {
  425. +                           case BUY:
  426. +                               if (!Config.OFFLINE_TRADE_ENABLE)
  427. +                                   continue;
  428. +                              
  429. +                               title = pc.getBuyList().getTitle();
  430. +                               for (TradeItem i : pc.getBuyList().getItems())
  431. +                               {
  432. +                                   stm_items.setInt(1, pc.getObjectId());
  433. +                                   stm_items.setInt(2, i.getItem().getItemId());
  434. +                                   stm_items.setLong(3, i.getCount());
  435. +                                   stm_items.setLong(4, i.getPrice());
  436. +                                   stm_items.executeUpdate();
  437. +                                   stm_items.clearParameters();
  438. +                               }
  439. +                               break;
  440. +                           case SELL:
  441. +                           case PACKAGE_SELL:
  442. +                               if (!Config.OFFLINE_TRADE_ENABLE)
  443. +                                   continue;
  444. +                              
  445. +                               title = pc.getSellList().getTitle();
  446. +                               for (TradeItem i : pc.getSellList().getItems())
  447. +                               {
  448. +                                   stm_items.setInt(1, pc.getObjectId());
  449. +                                   stm_items.setInt(2, i.getObjectId());
  450. +                                   stm_items.setLong(3, i.getCount());
  451. +                                   stm_items.setLong(4, i.getPrice());
  452. +                                   stm_items.executeUpdate();
  453. +                                   stm_items.clearParameters();
  454. +                               }
  455. +                               break;
  456. +                           case MANUFACTURE:
  457. +                               if (!Config.OFFLINE_CRAFT_ENABLE)
  458. +                                   continue;
  459. +                              
  460. +                               title = pc.getCreateList().getStoreName();
  461. +                               for (L2ManufactureItem i : pc.getCreateList().getList())
  462. +                               {
  463. +                                   stm_items.setInt(1, pc.getObjectId());
  464. +                                   stm_items.setInt(2, i.getRecipeId());
  465. +                                   stm_items.setLong(3, 0);
  466. +                                   stm_items.setLong(4, i.getCost());
  467. +                                   stm_items.executeUpdate();
  468. +                                   stm_items.clearParameters();
  469. +                               }
  470. +                       }
  471. +                       stm3.setString(4, title);
  472. +                       stm3.executeUpdate();
  473. +                       stm3.clearParameters();
  474. +                       con.commit(); // flush
  475. +                   }
  476. +               }
  477. +               catch (Exception e)
  478. +               {
  479. +                   LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Error while saving offline trader: " + pc.getObjectId() + " " + e, e);
  480. +               }
  481. +           }
  482. +           LOGGER.info(getClass().getSimpleName() + ": Offline traders stored.");
  483. +       }
  484. +       catch (Exception e)
  485. +       {
  486. +           LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Error while saving offline traders: " + e, e);
  487. +       }
  488. +   }
  489. +  
  490. +   public void restoreOfflineTraders()
  491. +   {
  492. +       LOGGER.info(getClass().getSimpleName() + ": Loading offline traders...");
  493. +       int nTraders = 0;
  494. +       try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  495. +           Statement stm = con.createStatement();
  496. +           ResultSet rs = stm.executeQuery(LOAD_OFFLINE_STATUS))
  497. +       {
  498. +           while (rs.next())
  499. +           {
  500. +               long time = rs.getLong("time");
  501. +               if (Config.OFFLINE_MAX_DAYS > 0)
  502. +               {
  503. +                   Calendar cal = Calendar.getInstance();
  504. +                   cal.setTimeInMillis(time);
  505. +                   cal.add(Calendar.DAY_OF_YEAR, Config.OFFLINE_MAX_DAYS);
  506. +                   if (cal.getTimeInMillis() <= System.currentTimeMillis())
  507. +                       continue;
  508. +               }
  509. +              
  510. +               PrivateStoreType type = PrivateStoreType.findById(rs.getInt("type"));
  511. +               if (type == null)
  512. +               {
  513. +                   LOGGER.warning(getClass().getSimpleName() + ": PrivateStoreType with id " + rs.getInt("type") + " could not be found.");
  514. +                   continue;
  515. +               }
  516. +              
  517. +               if (type == PrivateStoreType.NONE)
  518. +                   continue;
  519. +              
  520. +               L2PcInstance player = null;
  521. +              
  522. +               try
  523. +               {
  524. +                   L2GameClient client = new L2GameClient(null);
  525. +                   client.setDetached(true);
  526. +                   player = L2PcInstance.restore(rs.getInt("charId"));
  527. +                   client.setActiveChar(player);
  528. +                   player.setOnlineStatus(true, false);
  529. +                   client.setAccountName(player.getAccountNamePlayer());
  530. +                   client.setState(GameClientState.IN_GAME);
  531. +                   player.setClient(client);
  532. +                   player.setOfflineStartTime(time);
  533. +                   player.spawnMe(player.getX(), player.getY(), player.getZ());
  534. +                   LoginServerThread.getInstance().addGameServerLogin(player.getAccountName(), client);
  535. +                   try (PreparedStatement stm_items = con.prepareStatement(LOAD_OFFLINE_ITEMS))
  536. +                   {
  537. +                       stm_items.setInt(1, player.getObjectId());
  538. +                       try (ResultSet items = stm_items.executeQuery())
  539. +                       {
  540. +                           switch (type)
  541. +                           {
  542. +                               case BUY:
  543. +                                   while (items.next())
  544. +                                   {
  545. +                                       if (player.getBuyList().addItemByItemId(items.getInt(2), items.getInt(3), items.getInt(4)) == null)
  546. +                                           throw new NullPointerException();
  547. +                                   }
  548. +                                   player.getBuyList().setTitle(rs.getString("title"));
  549. +                                   break;
  550. +                               case SELL:
  551. +                               case PACKAGE_SELL:
  552. +                                   while (items.next())
  553. +                                   {
  554. +                                       if (player.getSellList().addItem(items.getInt(2), items.getInt(3), items.getInt(4)) == null)
  555. +                                           throw new NullPointerException();
  556. +                                   }
  557. +                                   player.getSellList().setTitle(rs.getString("title"));
  558. +                                   player.getSellList().setPackaged(type == PrivateStoreType.PACKAGE_SELL);
  559. +                                   break;
  560. +                               case MANUFACTURE:
  561. +                                   L2ManufactureList createList = new L2ManufactureList();
  562. +                                   while (items.next())
  563. +                                   {
  564. +                                       createList.add(new L2ManufactureItem(items.getInt(2), items.getInt(4)));
  565. +                                   }
  566. +                                  
  567. +                                   createList.setStoreName(rs.getString("title"));
  568. +                                   player.setCreateList(createList);
  569. +                                   break;
  570. +                           }
  571. +                       }
  572. +                   }
  573. +                   player.sitDown();
  574. +                   if (Config.OFFLINE_SET_NAME_COLOR)
  575. +                       player.getAppearance().setNameColor(Config.OFFLINE_NAME_COLOR);
  576. +                   player.setPrivateStoreType(type);
  577. +                   player.setOnlineStatus(true, true);
  578. +                   player.restoreEffects();
  579. +                   player.broadcastUserInfo();
  580. +                   nTraders++;
  581. +               }
  582. +               catch (Exception e)
  583. +               {
  584. +                   LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Error loading trader: " + player, e);
  585. +                   if (player != null)
  586. +                       player.deleteMe();
  587. +               }
  588. +           }
  589. +          
  590. +           LOGGER.info(getClass().getSimpleName() + ": Loaded: " + nTraders + " offline trader(s)");
  591. +          
  592. +           try (Statement stm1 = con.createStatement())
  593. +           {
  594. +               stm1.execute(CLEAR_OFFLINE_TABLE);
  595. +               stm1.execute(CLEAR_OFFLINE_TABLE_ITEMS);
  596. +           }
  597. +       }
  598. +       catch (Exception e)
  599. +       {
  600. +           LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Error while loading offline traders: ", e);
  601. +       }
  602. +   }
  603. +  
  604. +   /**
  605. +    * Gets the single instance of OfflineTradersTable.
  606. +    * @return single instance of OfflineTradersTable
  607. +    */
  608. +   public static OfflineTradersTable getInstance()
  609. +   {
  610. +       return SingletonHolder._instance;
  611. +   }
  612. +  
  613. +   private static class SingletonHolder
  614. +   {
  615. +       protected static final OfflineTradersTable _instance = new OfflineTradersTable();
  616. +   }
  617. +}
  618. Index: java/net/sf/l2j/gameserver/Shutdown.java
  619. ===================================================================
  620. --- java/net/sf/l2j/gameserver/Shutdown.java    (revision 371)
  621. +++ java/net/sf/l2j/gameserver/Shutdown.java    (working copy)
  622. @@ -21,6 +21,7 @@
  623.  import net.sf.l2j.L2DatabaseFactory;
  624.  import net.sf.l2j.commons.lang.StringUtil;
  625.  import net.sf.l2j.gameserver.datatables.BufferTable;
  626. +import net.sf.l2j.gameserver.datatables.OfflineTradersTable;
  627.  import net.sf.l2j.gameserver.instancemanager.CastleManorManager;
  628.  import net.sf.l2j.gameserver.instancemanager.FishingChampionshipManager;
  629.  import net.sf.l2j.gameserver.instancemanager.FourSepulchersManager;
  630. @@ -118,6 +119,19 @@
  631.         {
  632.             StringUtil.printSection("Under " + MODE_TEXT[_shutdownMode] + " process");
  633.            
  634. +           try
  635. +           {
  636. +               if ((Config.OFFLINE_TRADE_ENABLE || Config.OFFLINE_CRAFT_ENABLE) && Config.RESTORE_OFFLINERS)
  637. +               {
  638. +                   OfflineTradersTable.getInstance().storeOffliners();
  639. +                   _log.info("Offline Traders Table: Offline shops stored.");
  640. +               }
  641. +           }
  642. +           catch (Throwable t)
  643. +           {
  644. +               _log.log(Level.WARNING, "Error saving offline shops.", t);
  645. +           }
  646. +          
  647.             // disconnect players
  648.             try
  649.             {
Advertisement
Add Comment
Please, Sign In to add comment