Advertisement
Evanth3

Untitled

Aug 28th, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.99 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2004-2015 L2J Server
  3.  *
  4.  * This file is part of L2J Server.
  5.  *
  6.  * L2J Server is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * L2J Server is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19. package l2r.gameserver.instancemanager;
  20.  
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.concurrent.ConcurrentHashMap;
  27.  
  28. import l2r.L2DatabaseFactory;
  29. import l2r.gameserver.ThreadPoolManager;
  30. import l2r.gameserver.data.xml.impl.ScenePlayerData;
  31. import l2r.gameserver.enums.CtrlIntention;
  32. import l2r.gameserver.model.L2World;
  33. import l2r.gameserver.model.actor.L2Attackable;
  34. import l2r.gameserver.model.actor.L2Npc;
  35. import l2r.gameserver.model.actor.instance.L2PcInstance;
  36. import l2r.gameserver.model.entity.Instance;
  37. import l2r.gameserver.model.instancezone.InstanceWorld;
  38. import l2r.gameserver.network.serverpackets.ExStartScenePlayer;
  39. import l2r.gameserver.network.serverpackets.L2GameServerPacket;
  40. import l2r.util.data.xml.IXmlReader.IXmlReader;
  41.  
  42. import org.w3c.dom.Document;
  43. import org.w3c.dom.NamedNodeMap;
  44. import org.w3c.dom.Node;
  45.  
  46. /**
  47.  * @author evill33t, GodKratos
  48.  */
  49. public class InstanceManager implements IXmlReader
  50. {
  51.     private static final Map<Integer, Instance> INSTANCES = new ConcurrentHashMap<>();
  52.     private final Map<Integer, InstanceWorld> _instanceWorlds = new ConcurrentHashMap<>();
  53.     private int _dynamic = 300000;
  54.     // InstanceId Names
  55.     private static final Map<Integer, String> _instanceIdNames = new HashMap<>();
  56.     private final Map<Integer, Map<Integer, Long>> _playerInstanceTimes = new ConcurrentHashMap<>();
  57.     // SQL Queries
  58.     private static final String ADD_INSTANCE_TIME = "INSERT INTO character_instance_time (charId,instanceId,time) values (?,?,?) ON DUPLICATE KEY UPDATE time=?";
  59.     private static final String RESTORE_INSTANCE_TIMES = "SELECT instanceId,time FROM character_instance_time WHERE charId=?";
  60.     private static final String DELETE_INSTANCE_TIME = "DELETE FROM character_instance_time WHERE charId=? AND instanceId=?";
  61.    
  62.     protected InstanceManager()
  63.     {
  64.         // Creates the multiverse.
  65.         INSTANCES.put(-1, new Instance(-1, "multiverse"));
  66.         LOGGER.info(getClass().getSimpleName() + ": Multiverse Instance created.");
  67.         // Creates the universe.
  68.         INSTANCES.put(0, new Instance(0, "universe"));
  69.         LOGGER.info(getClass().getSimpleName() + ": Universe Instance created.");
  70.         load();
  71.     }
  72.    
  73.     @Override
  74.     public void load()
  75.     {
  76.         _instanceIdNames.clear();
  77.         parseDatapackFile("data/xml/other/instancenames.xml");
  78.         LOGGER.info(getClass().getSimpleName() + ": Loaded " + _instanceIdNames.size() + " instance names.");
  79.     }
  80.    
  81.     /**
  82.      * @param playerObjId
  83.      * @param id
  84.      * @return
  85.      */
  86.     public long getInstanceTime(int playerObjId, int id)
  87.     {
  88.         if (!_playerInstanceTimes.containsKey(playerObjId))
  89.         {
  90.             restoreInstanceTimes(playerObjId);
  91.         }
  92.         if (_playerInstanceTimes.get(playerObjId).containsKey(id))
  93.         {
  94.             return _playerInstanceTimes.get(playerObjId).get(id);
  95.         }
  96.         return -1;
  97.     }
  98.    
  99.     /**
  100.      * @param playerObjId
  101.      * @return
  102.      */
  103.     public Map<Integer, Long> getAllInstanceTimes(int playerObjId)
  104.     {
  105.         if (!_playerInstanceTimes.containsKey(playerObjId))
  106.         {
  107.             restoreInstanceTimes(playerObjId);
  108.         }
  109.         return _playerInstanceTimes.get(playerObjId);
  110.     }
  111.    
  112.     /**
  113.      * @param playerObjId
  114.      * @param id
  115.      * @param time
  116.      */
  117.     public void setInstanceTime(int playerObjId, int id, long time)
  118.     {
  119.         if (!_playerInstanceTimes.containsKey(playerObjId))
  120.         {
  121.             restoreInstanceTimes(playerObjId);
  122.         }
  123.        
  124.         try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  125.             PreparedStatement ps = con.prepareStatement(ADD_INSTANCE_TIME))
  126.         {
  127.             ps.setInt(1, playerObjId);
  128.             ps.setInt(2, id);
  129.             ps.setLong(3, time);
  130.             ps.setLong(4, time);
  131.             ps.execute();
  132.             _playerInstanceTimes.get(playerObjId).put(id, time);
  133.         }
  134.         catch (Exception e)
  135.         {
  136.             LOGGER.warn(getClass().getSimpleName() + ": Could not insert character instance time data: " + e.getMessage());
  137.         }
  138.     }
  139.    
  140.     /**
  141.      * @param playerObjId
  142.      * @param id
  143.      */
  144.     public void deleteInstanceTime(int playerObjId, int id)
  145.     {
  146.         try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  147.             PreparedStatement ps = con.prepareStatement(DELETE_INSTANCE_TIME))
  148.         {
  149.             ps.setInt(1, playerObjId);
  150.             ps.setInt(2, id);
  151.             ps.execute();
  152.             _playerInstanceTimes.get(playerObjId).remove(id);
  153.         }
  154.         catch (Exception e)
  155.         {
  156.             LOGGER.warn(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  157.         }
  158.     }
  159.    
  160.     /**
  161.      * @param playerObjId
  162.      */
  163.     public void restoreInstanceTimes(int playerObjId)
  164.     {
  165.         if (_playerInstanceTimes.containsKey(playerObjId))
  166.         {
  167.             return; // already restored
  168.         }
  169.         _playerInstanceTimes.put(playerObjId, new ConcurrentHashMap<>());
  170.         try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  171.             PreparedStatement ps = con.prepareStatement(RESTORE_INSTANCE_TIMES))
  172.         {
  173.             ps.setInt(1, playerObjId);
  174.             try (ResultSet rs = ps.executeQuery())
  175.             {
  176.                 while (rs.next())
  177.                 {
  178.                     int id = rs.getInt("instanceId");
  179.                     long time = rs.getLong("time");
  180.                     if (time < System.currentTimeMillis())
  181.                     {
  182.                         deleteInstanceTime(playerObjId, id);
  183.                     }
  184.                     else
  185.                     {
  186.                         _playerInstanceTimes.get(playerObjId).put(id, time);
  187.                     }
  188.                 }
  189.             }
  190.         }
  191.         catch (Exception e)
  192.         {
  193.             LOGGER.warn(getClass().getSimpleName() + ": Could not delete character instance time data: " + e.getMessage());
  194.         }
  195.     }
  196.    
  197.     /**
  198.      * @param id
  199.      * @return
  200.      */
  201.     public String getInstanceIdName(int id)
  202.     {
  203.         if (_instanceIdNames.containsKey(id))
  204.         {
  205.             return _instanceIdNames.get(id);
  206.         }
  207.         return ("UnknownInstance");
  208.     }
  209.    
  210.     @Override
  211.     public void parseDocument(Document doc)
  212.     {
  213.         for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  214.         {
  215.             if ("list".equals(n.getNodeName()))
  216.             {
  217.                 NamedNodeMap attrs;
  218.                 for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  219.                 {
  220.                     if ("instance".equals(d.getNodeName()))
  221.                     {
  222.                         attrs = d.getAttributes();
  223.                         _instanceIdNames.put(parseInteger(attrs, "id"), attrs.getNamedItem("name").getNodeValue());
  224.                     }
  225.                 }
  226.             }
  227.         }
  228.     }
  229.    
  230.     /**
  231.      * @param world
  232.      */
  233.     public void addWorld(InstanceWorld world)
  234.     {
  235.         _instanceWorlds.put(world.getInstanceId(), world);
  236.     }
  237.    
  238.     /**
  239.      * @param instanceId
  240.      * @return
  241.      */
  242.     public InstanceWorld getWorld(int instanceId)
  243.     {
  244.         return _instanceWorlds.get(instanceId);
  245.     }
  246.    
  247.     /**
  248.      * Check if the player have a World Instance where it's allowed to enter.
  249.      * @param player the player to check
  250.      * @return the instance world
  251.      */
  252.     public InstanceWorld getPlayerWorld(L2PcInstance player)
  253.     {
  254.         for (InstanceWorld temp : _instanceWorlds.values())
  255.         {
  256.             if ((temp != null) && (temp.isAllowed(player.getObjectId())))
  257.             {
  258.                 return temp;
  259.             }
  260.         }
  261.         return null;
  262.     }
  263.    
  264.     /**
  265.      * @param instanceid
  266.      */
  267.     public void destroyInstance(int instanceid)
  268.     {
  269.         if (instanceid <= 0)
  270.         {
  271.             return;
  272.         }
  273.         final Instance temp = INSTANCES.get(instanceid);
  274.         if (temp != null)
  275.         {
  276.             temp.removeNpcs();
  277.             temp.removePlayers();
  278.             temp.removeDoors();
  279.             temp.removeItems();
  280.             temp.cancelTimer();
  281.             INSTANCES.remove(instanceid);
  282.             _instanceWorlds.remove(instanceid);
  283.         }
  284.     }
  285.    
  286.     /**
  287.      * @param instanceid
  288.      * @return
  289.      */
  290.     public Instance getInstance(int instanceid)
  291.     {
  292.         return INSTANCES.get(instanceid);
  293.     }
  294.    
  295.     /**
  296.      * @return
  297.      */
  298.     public Map<Integer, Instance> getInstances()
  299.     {
  300.         return INSTANCES;
  301.     }
  302.    
  303.     /**
  304.      * @param objectId
  305.      * @return
  306.      */
  307.     public int getPlayerInstance(int objectId)
  308.     {
  309.         for (Instance temp : INSTANCES.values())
  310.         {
  311.             if (temp == null)
  312.             {
  313.                 continue;
  314.             }
  315.             // check if the player is in any active instance
  316.             if (temp.containsPlayer(objectId))
  317.             {
  318.                 return temp.getId();
  319.             }
  320.         }
  321.         // 0 is default instance aka the world
  322.         return 0;
  323.     }
  324.    
  325.     /**
  326.      * @param id
  327.      * @return
  328.      */
  329.     public boolean createInstance(int id)
  330.     {
  331.         if (getInstance(id) != null)
  332.         {
  333.             return false;
  334.         }
  335.        
  336.         Instance instance = new Instance(id);
  337.         INSTANCES.put(id, instance);
  338.         return true;
  339.     }
  340.    
  341.     /**
  342.      * @param id
  343.      * @param template
  344.      * @return
  345.      */
  346.     public boolean createInstanceFromTemplate(int id, String template)
  347.     {
  348.         if (getInstance(id) != null)
  349.         {
  350.             return false;
  351.         }
  352.        
  353.         Instance instance = new Instance(id);
  354.         INSTANCES.put(id, instance);
  355.         instance.loadInstanceTemplate(template);
  356.         return true;
  357.     }
  358.    
  359.     /**
  360.      * Create a new instance with a dynamic instance id based on a template (or null)
  361.      * @param template xml file
  362.      * @return
  363.      */
  364.     public int createDynamicInstance(String template)
  365.     {
  366.         while (getInstance(_dynamic) != null)
  367.         {
  368.             _dynamic++;
  369.             if (_dynamic == Integer.MAX_VALUE)
  370.             {
  371.                 LOGGER.warn(getClass().getSimpleName() + ": More then " + (Integer.MAX_VALUE - 300000) + " instances created");
  372.                 _dynamic = 300000;
  373.             }
  374.         }
  375.         Instance instance = new Instance(_dynamic);
  376.         INSTANCES.put(_dynamic, instance);
  377.         if (template != null)
  378.         {
  379.             instance.loadInstanceTemplate(template);
  380.         }
  381.         return _dynamic;
  382.     }
  383.    
  384.     public void sendPacket(int instanceId, L2GameServerPacket packet)
  385.     {
  386.         for (L2PcInstance player : L2World.getInstance().getPlayers())
  387.         {
  388.             if ((player != null) && player.isOnline() && (player.getInstanceId() == instanceId))
  389.             {
  390.                 player.sendPacket(packet);
  391.             }
  392.         }
  393.     }
  394.    
  395.     public void showVidToInstance(int vidId, final int instId)
  396.     {
  397.         stopWholeInstance(instId);
  398.         broadcastMovie(vidId, instId);
  399.        
  400.         ThreadPoolManager.getInstance().scheduleGeneral(() -> startWholeInstance(instId), ScenePlayerData.getInstance().getVideoDuration(vidId) + 1000);
  401.     }
  402.    
  403.     public void broadcastMovie(int vidId, int instId)
  404.     {
  405.         for (L2PcInstance pl : L2World.getInstance().getPlayers())
  406.         {
  407.             if ((pl != null) && (pl.getInstanceId() == instId))
  408.             {
  409.                 pl.setMovieId(vidId);
  410.                 pl.sendPacket(new ExStartScenePlayer(vidId));
  411.             }
  412.         }
  413.     }
  414.    
  415.     public void stopWholeInstance(int instId)
  416.     {
  417.         for (L2Npc mobs : getInstance(instId).getNpcs())
  418.         {
  419.             if ((mobs == null) || !(mobs instanceof L2Attackable))
  420.             {
  421.                 continue;
  422.             }
  423.            
  424.             mobs.setTarget(null);
  425.             mobs.abortAttack();
  426.             mobs.abortCast();
  427.             mobs.disableAllSkills();
  428.             mobs.stopMove(null);
  429.             mobs.setIsInvul(true);
  430.             mobs.setIsImmobilized(true);
  431.         }
  432.        
  433.         for (L2PcInstance pl : L2World.getInstance().getPlayers())
  434.         {
  435.             if ((pl != null) && (pl.getInstanceId() == instId) && !pl.isGM())
  436.             {
  437.                 pl.setIsImmobilized(true);
  438.                 pl.setTarget(null);
  439.                 pl.disableAllSkills();
  440.                 pl.setIsInvul(true);
  441.                 pl.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
  442.             }
  443.         }
  444.     }
  445.    
  446.     public void startWholeInstance(int instId)
  447.     {
  448.         Instance inst = getInstance(instId);
  449.         if (inst == null)
  450.         {
  451.             return;
  452.         }
  453.         for (L2Npc mobs : inst.getNpcs())
  454.         {
  455.             if ((mobs == null) || !(mobs instanceof L2Attackable))
  456.             {
  457.                 continue;
  458.             }
  459.            
  460.             mobs.setIsInvul(false);
  461.             mobs.enableAllSkills();
  462.             mobs.setIsImmobilized(false);
  463.         }
  464.        
  465.         for (L2PcInstance pl : L2World.getInstance().getPlayers())
  466.         {
  467.             if ((pl != null) && (pl.getInstanceId() == instId) && !pl.isGM())
  468.             {
  469.                 pl.enableAllSkills();
  470.                 pl.setIsInvul(false);
  471.                 pl.setIsImmobilized(false);
  472.             }
  473.         }
  474.     }
  475.    
  476.     public static final InstanceManager getInstance()
  477.     {
  478.         return SingletonHolder._instance;
  479.     }
  480.    
  481.     private static class SingletonHolder
  482.     {
  483.         protected static final InstanceManager _instance = new InstanceManager();
  484.     }
  485. }
  486.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement