Advertisement
Guest User

L2CrystalInstance

a guest
May 1st, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 30.38 KB | None | 0 0
  1. package com.l2jserver.gameserver.model.actor.instance;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.StringTokenizer;
  9.  
  10. import javolution.text.TextBuilder;
  11. import javolution.util.FastSet;
  12.  
  13. import com.l2jserver.L2DatabaseFactory;
  14. import com.l2jserver.gameserver.ThreadPoolManager;
  15. import com.l2jserver.gameserver.ai.CtrlIntention;
  16. import com.l2jserver.gameserver.cache.HtmCache;
  17. import com.l2jserver.gameserver.datatables.ClanTable;
  18. import com.l2jserver.gameserver.datatables.NpcTable;
  19. import com.l2jserver.gameserver.model.L2Clan;
  20. import com.l2jserver.gameserver.model.L2ClanMember;
  21. import com.l2jserver.gameserver.model.L2Skill;
  22. import com.l2jserver.gameserver.model.L2Spawn;
  23. import com.l2jserver.gameserver.model.actor.L2Attackable;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.L2Npc;
  26. import com.l2jserver.gameserver.network.clientpackets.Say2;
  27. import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
  28. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  29. import com.l2jserver.gameserver.network.serverpackets.ExShowScreenMessage;
  30. import com.l2jserver.gameserver.network.serverpackets.MyTargetSelected;
  31. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  32. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  33. import com.l2jserver.gameserver.network.serverpackets.ValidateLocation;
  34. import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
  35. import com.l2jserver.util.Rnd;
  36.  
  37. /**
  38.  * @author Matim
  39.  * @version 2.1
  40.  * <br><br>
  41.  * L2Occupation Crystal Instance<br><br>
  42.  * <li>will not move anywhere.
  43.  * <li>will not attack attackers.
  44.  * <li>it has hp bar status.
  45.  * <br><br>
  46.  * Owner of the crystal may manage crystal functions, such as:
  47.  * <li>defence abilitiy
  48.  * <li>warning ability
  49.  * <li>stats upgrades
  50.  * <li>special items/buffs/teleports (TODO)
  51.  */
  52. public class L2CrystalInstance extends L2Npc
  53. {
  54.     private final int DEFENDERS_COUNT = 5; 
  55.     private int _ownerClanId = 1;
  56.     private String _locationName = "";
  57.     private boolean _defenceAbilityEnabled = false;
  58.     private boolean _warningAbilityEnabled = false;
  59.     private boolean _statsUpgradeEnabled = false;
  60.     private boolean _canWarn = true;
  61.     private boolean _canSpawnDefenders = true;
  62.    
  63.     private int _warningCount = 0;
  64.     private int _defenceSpawnCount = 0;
  65.    
  66.     public static FastSet<L2Npc> _defenders = new FastSet<L2Npc>();
  67.    
  68.     public L2CrystalInstance(int objectId, L2NpcTemplate template)
  69.     {
  70.         super(objectId, template);
  71.         setIsInvul(false);
  72.     }
  73.    
  74.     @Override
  75.     public void onSpawn()
  76.     {
  77.         super.onSpawn();
  78.         loadData(this.getNpcId());
  79.     }
  80.    
  81.     /**
  82.      * Load Data about crystal from database.
  83.      * Clan owner Id, location name, enabled avilities etc.
  84.      * @param crystalId
  85.      */
  86.     private void loadData(int crystalId)
  87.     {
  88.         Connection con = null;
  89.         try
  90.         {
  91.             PreparedStatement statement;
  92.             ResultSet rs;
  93.            
  94.             con = L2DatabaseFactory.getInstance().getConnection();
  95.            
  96.             statement = con.prepareStatement("Select * from crystal_list where mapId =" + this.getNpcId());
  97.             rs = statement.executeQuery();
  98.            
  99.             while (rs.next())
  100.             {
  101.                 _ownerClanId = rs.getInt("clanId");
  102.                 _locationName = rs.getString("locationName");
  103.                 _defenceAbilityEnabled = rs.getBoolean("defenceEnabled");
  104.                 _warningAbilityEnabled = rs.getBoolean("warningEnabled");
  105.                 _statsUpgradeEnabled = rs.getBoolean("statsEnabled");
  106.                 _warningCount = rs.getInt("warningCount");
  107.                 _defenceSpawnCount = rs.getInt("defenceCount");
  108.             }
  109.             rs.close();
  110.             statement.close();
  111.  
  112.         }
  113.         catch (Exception e)
  114.         {
  115.             _log.info("Crystal Manager [Load Data]: " + e);
  116.         }
  117.         finally
  118.         {
  119.             L2DatabaseFactory.close(con);
  120.         }
  121.     }
  122.    
  123.     /**
  124.      * Save Data about crystal into database.
  125.      * @param crystalId
  126.      */
  127.     public void saveData(int crystalId)
  128.     {
  129.         Connection con = null;
  130.         try
  131.         {
  132.             con = L2DatabaseFactory.getInstance().getConnection();
  133.             Statement statement = con.createStatement();
  134.             statement.executeUpdate("UPDATE crystal_list SET clanId ='" + _ownerClanId + "', defenceEnabled ='" + _defenceAbilityEnabled + "', warningEnabled ='" + _warningAbilityEnabled + "', statsEnabled ='" + _statsUpgradeEnabled  + "', warningCount ='" + _warningCount + "', defenceCount ='" + _defenceSpawnCount + "' WHERE mapId ='" + crystalId + "'");
  135.             statement.close();
  136.         }
  137.         catch (SQLException e)
  138.         {
  139.             _log.info("Crystal Manager [Save Data]: " + e);
  140.         }
  141.         finally
  142.         {
  143.             L2DatabaseFactory.close(con);
  144.         }
  145.     }
  146.    
  147.     @Override
  148.     public void onBypassFeedback(L2PcInstance player, String command)
  149.     {
  150.         if (player == null || player.getLastFolkNPC() == null || player.getLastFolkNPC().getObjectId() != this.getObjectId())
  151.         {
  152.             return;
  153.         }
  154.         if (command.startsWith("manageCrystal"))
  155.         {          
  156.             if (player.isClanLeader())
  157.             {
  158.                 try
  159.                 {
  160.                     showManageCrystalWindow(player);
  161.                 }
  162.                 catch (Exception e)
  163.                 {
  164.                     System.out.println("MANAGE CRYSTAL ERROR: " + e);
  165.                 }
  166.             }
  167.         }
  168.         else if (command.startsWith("disableDefence"))
  169.         {
  170.             setDefenceAbilityEnabled(false);
  171.             saveDataAndShowManageWindow(player);
  172.         }
  173.         else if (command.startsWith("enableDefence"))
  174.         {
  175.             setDefenceAbilityEnabled(true);
  176.             saveDataAndShowManageWindow(player);
  177.         }
  178.         else if (command.startsWith("disableWarning"))
  179.         {
  180.             setWarningAbilityEnabled(false);
  181.             saveDataAndShowManageWindow(player);
  182.         }
  183.         else if (command.startsWith("enableWarning"))
  184.         {
  185.             setWarningAbilityEnabled(true);
  186.             saveDataAndShowManageWindow(player);
  187.         }
  188.         else if (command.startsWith("disableStats"))
  189.         {
  190.             setStatsEnabled(false);
  191.             saveDataAndShowManageWindow(player);
  192.         }
  193.         else if (command.startsWith("enableStats"))
  194.         {
  195.             setStatsEnabled(true);
  196.             saveDataAndShowManageWindow(player);
  197.         }
  198.         else if (command.startsWith("defenceInfo"))
  199.         {
  200.             showCrystalWindow(player, "defence.htm");
  201.         }
  202.         else if (command.startsWith("warningInfo"))
  203.         {
  204.             showCrystalWindow(player, "warning.htm");
  205.         }
  206.         else if (command.startsWith("statsInfo"))
  207.         {
  208.             showCrystalWindow(player, "stats.htm");
  209.         }
  210.         else if (command.startsWith("mainWindow"))
  211.         {
  212.             showChatWindow(player, 0);
  213.         }
  214.         else if (command.startsWith("ownerInfo"))
  215.         {
  216.             showOwnerInfoWindow(player);
  217.         }
  218.         else if (command.startsWith("crystalGuide"))
  219.         {
  220.             showCrystalWindow(player, "guide.htm");
  221.         }
  222.         else if (command.startsWith("abilitiesWindow"))
  223.         {
  224.             showAbilitiesShopWindow(player);
  225.         }
  226.         else if (command.startsWith("buyWarning"))
  227.         {
  228.             try
  229.             {
  230.                 String val = command.substring(10);
  231.                 StringTokenizer st = new StringTokenizer(val);
  232.  
  233.                 String id = st.nextToken();
  234.                 int idval = Integer.parseInt(id);
  235.                 buyAbility(player, idval, 1);
  236.                 showAbilitiesShopWindow(player);
  237.                 saveData(this.getNpcId());
  238.             }
  239.             catch (StringIndexOutOfBoundsException e)
  240.             {
  241.                 player.sendMessage("Error!");
  242.             }
  243.             catch (NumberFormatException nfe)
  244.             {
  245.                 player.sendMessage("Specify a valid number.");
  246.             }
  247.         }
  248.         else if (command.startsWith("buyDefence"))
  249.         {
  250.             try
  251.             {
  252.                 String val = command.substring(10);
  253.                 StringTokenizer st = new StringTokenizer(val);
  254.  
  255.                 String id = st.nextToken();
  256.                 int idval = Integer.parseInt(id);
  257.                 buyAbility(player, idval, 2);
  258.                 showAbilitiesShopWindow(player);
  259.                 saveData(this.getNpcId());
  260.             }
  261.             catch (StringIndexOutOfBoundsException e)
  262.             {
  263.                 player.sendMessage("Error!");
  264.             }
  265.             catch (NumberFormatException nfe)
  266.             {
  267.                 player.sendMessage("Specify a valid number.");
  268.             }
  269.         }
  270.         else if (command.startsWith("locStatus"))
  271.         {
  272.             showLocationsStatusWindow(player);
  273.         }
  274.         else if (command.startsWith("crystalFunctions"))
  275.         {
  276.             showCrystalFunctionsWindow(player);
  277.         }
  278.     }
  279.    
  280.     @Override
  281.     public void showChatWindow(L2PcInstance player, int val)
  282.     {
  283.         TextBuilder tb = new TextBuilder();
  284.         tb.append("<html><title>Crystal</title><body><center><br>");
  285.         tb.append("Conqerable <font color=LEVEL>" + getLocationName() + "</font> Location:<br>");
  286.         tb.append("Welcome <font color=LEVEL>" + player.getName() + "</font>, Im Occupation Crystal!<br>");
  287.        
  288.         if (player.getClanId() == getOwnerClanId())
  289.         {
  290.             if (player.isClanLeader())
  291.             {
  292.                 tb.append("How may I help you my owner?<br>");
  293.                 tb.append("<button value=\"Manage Functions\" action=\"bypass -h npc_%objectId%_manageCrystal\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  294.             }
  295.             else
  296.             {
  297.                 tb.append("Glad to see you!<br>");
  298.                 tb.append("Only clan leader may manage crystal!<br>");
  299.             }
  300.            
  301.             tb.append("<button value=\"Crystal Functions\" action=\"bypass -h npc_%objectId%_crystalFunctions\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  302.         }
  303.         else
  304.         {
  305.             tb.append("Go away, I will not help you!<br>");
  306.             tb.append("You are not my owner!<br>");
  307.         }  
  308.        
  309.         tb.append("<button value=\"Owner Info\" action=\"bypass -h npc_%objectId%_ownerInfo\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  310.         tb.append("<button value=\"Locations Status\" action=\"bypass -h npc_%objectId%_locStatus\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  311.         tb.append("<button value=\"Crystal Guide\" action=\"bypass -h npc_%objectId%_crystalGuide\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  312.         tb.append("<br><br><br>By Matim");
  313.        
  314.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  315.         msg.setHtml(tb.toString());
  316.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  317.    
  318.         player.sendPacket(msg);
  319.     }
  320.    
  321.     /**
  322.      * Show info about crystal functions.
  323.      * Are they enabled or disabled.
  324.      * Also possibility to use functions is there.
  325.      * Such as Teleports and additional buffs.
  326.      * @param player
  327.      */
  328.     public void showCrystalFunctionsWindow(L2PcInstance player)
  329.     {  
  330.         TextBuilder tb = new TextBuilder();
  331.         tb.append("<html><title>Crystal</title><body><center><br>");
  332.         tb.append("Crystal Functions:<br>");
  333.         tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_1\" width=300 height=32 align=left><br><br>");
  334.        
  335.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  336.         tb.append("<td width=270 align=\"center\">Defence Ability: <font color=\"LEVEL\">" + isEnabled(defenceAbilityEnabled()) + "</font></td></tr></table><br>");
  337.        
  338.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  339.         tb.append("<td width=270 align=\"center\">Warning Ability: <font color=\"LEVEL\">" + isEnabled(warningAbilityEnabled()) + "</font></td></tr></table><br>");
  340.        
  341.         tb.append("<br><br><img src=\"L2UI_CH3.onscrmsg_pattern01_2\" width=300 height=32 align=left><br>");
  342.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  343.        
  344.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  345.         msg.setHtml(tb.toString());
  346.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  347.    
  348.         player.sendPacket(msg);
  349.     }
  350.    
  351.     /**
  352.      * Show basic info about clan which is owner of the location.
  353.      * Info like leader name, clan level, member count etc.
  354.      * <br><br>
  355.      * @param player
  356.      */
  357.     public void showOwnerInfoWindow(L2PcInstance player)
  358.     {
  359.         TextBuilder tb = new TextBuilder();
  360.         tb.append("<html><title>Crystal</title><body><center><br>");
  361.         tb.append("Info About Owner:<br>");
  362.         tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_1\" width=300 height=32 align=left><br><br>");
  363.        
  364.         if (_ownerClanId == 0)
  365.         {
  366.             tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  367.             tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">Nobody</font>!</td></tr></table>");
  368.         }
  369.         else
  370.         {
  371.             if (ClanTable.getInstance().getClan(_ownerClanId) != null)
  372.             {
  373.                 L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  374.                 String ownerClanName = clan.getName();
  375.                
  376.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  377.                 tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">" + ownerClanName + "</font> clan!</td></tr></table><br>");
  378.                
  379.                 tb.append("<table width=270 bgcolor=\"144499\"><tr>");
  380.                 tb.append("<td width=270 align=\"center\">Clan Leader: <font color=\"LEVEL\">" + clan.getLeaderName() + "</font></td></tr></table><br>");
  381.                
  382.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  383.                 tb.append("<td width=270 align=\"center\">Members Count: <font color=\"LEVEL\">" + clan.getMembersCount() + "</font></td></tr></table><br>");
  384.                
  385.                 tb.append("<table width=270 bgcolor=\"144499\"><tr>");
  386.                 tb.append("<td width=270 align=\"center\">Clan Level: <font color=\"LEVEL\">" + clan.getLevel() + "</font></td></tr></table><br>");
  387.                
  388.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  389.                 tb.append("<td width=270 align=\"center\">Online Members: <font color=\"LEVEL\">" + clan.getOnlineMembersCount() + "</font></td></tr></table><br>");
  390.             }
  391.             else
  392.             {
  393.                 tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr>");
  394.                 tb.append("<td width=270 align=\"center\">Location Owned by: <font color=\"LEVEL\">Nobody</font>!</td></tr></table>");
  395.             }
  396.         }
  397.        
  398.         tb.append("<br><br><img src=\"L2UI_CH3.onscrmsg_pattern01_2\" width=300 height=32 align=left><br>");
  399.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  400.        
  401.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  402.         msg.setHtml(tb.toString());
  403.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  404.    
  405.         player.sendPacket(msg);
  406.     }
  407.    
  408.     /**
  409.      * Main window, where clan leader which is owner of the crystal,
  410.      * may manage crystal functions, till now there are 3 available:
  411.      * <li>Stats Upgrade - additional HP/Defence etc.
  412.      * <li>Defence Ability - spawn defenders while crystal is beeing attacked.
  413.      * <li>Warning Ability - warn each clan member while crystal is attacket.
  414.      * <br><br>
  415.      * @param player
  416.      */
  417.     public void showManageCrystalWindow(L2PcInstance player)
  418.     {
  419.         TextBuilder tb = new TextBuilder();
  420.         tb.append("<html><title>Crystal Manage</title><body><center><br>");
  421.         tb.append("<font color=LEVEL>" + player.getName() + "</font>, you are my Leader!<br>");
  422.        
  423.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Defence Ability</td>");
  424.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_defenceInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  425.        
  426.         if (defenceAbilityEnabled())
  427.         {
  428.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableDefence\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  429.         }
  430.         else
  431.         {
  432.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableDefence\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  433.         }
  434.         tb.append("</tr></table>");
  435.         tb.append("<table width=270 border=0><tr><td>Warning Ability</td>");
  436.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_warningInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  437.        
  438.         if (warningAbilityEnabled())
  439.         {
  440.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableWarning\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  441.         }
  442.         else
  443.         {
  444.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableWarning\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  445.         }
  446.         tb.append("</tr></table>");
  447.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Stats Upgrades</td>");
  448.         tb.append("<td><button value=\"Info\" action=\"bypass -h npc_%objectId%_statsInfo\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  449.        
  450.         if (statsUpgradeEnabled())
  451.         {
  452.             tb.append("<td><button value=\"Disable\" action=\"bypass -h npc_%objectId%_disableStats\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  453.         }
  454.         else
  455.         {
  456.             tb.append("<td><button value=\"Enable\" action=\"bypass -h npc_%objectId%_enableStats\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
  457.         }
  458.         tb.append("</tr></table>");
  459.         tb.append("<br><br><button value=\"Buy Abilities\" action=\"bypass -h npc_%objectId%_abilitiesWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\">");
  460.         tb.append("<button value=\"Buy Functions\" action=\"bypass -h npc_%objectId%_functionsWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  461.         tb.append("<button value=\"Main Window\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  462.            
  463.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  464.         msg.setHtml(tb.toString());
  465.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  466.    
  467.         player.sendPacket(msg);
  468.     }
  469.    
  470.     /**
  471.      * Basic window where owner of the crystal (Clan Leader) may buy additional
  472.      * ammount of the abilities, each time when crystal will use his ability, for example
  473.      * will warn clan members or spawn his defenders (defence ability) defence ability or
  474.      * warning ability count will be decrested, each ability cos 100 Clan Reputation Points.
  475.      * To prevent spamming with warning etc, there is delay - one minute.                  
  476.      * @param player
  477.      */
  478.     public void showAbilitiesShopWindow(L2PcInstance player)
  479.     {
  480.         L2Clan clan = player.getClan();
  481.        
  482.         TextBuilder tb = new TextBuilder();
  483.         tb.append("<html><title>Crystal</title><body><center><br>");
  484.         tb.append("<font color=\"LEVEL\">Abilities Shop:</font><br>");
  485.         tb.append("Each time your clan will be warned or crystal will use Defence Ability (will spawn defenders) - your bought abilities count will be decrested. Here you can buy additional ammount of the abilities!<br>");
  486.         tb.append("You need <font color=\"LEVEL\">100</font> Clan Repotation Points to buy one.<br>");
  487.         tb.append("Your clan CRP ammount: <font color=\"LEVEL\">" + clan.getReputationScore() + "</font><br>");
  488.        
  489.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Warning Count:</td><td><edit var=\"count\" width=50></td>");
  490.         tb.append("<td><button value=\"Buy\" action=\"bypass -h npc_%objectId%_buyWarning $count\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br>");
  491.        
  492.         tb.append("<table width=270 border=0 bgcolor=\"444444\"><tr><td>Defence Count:</td><td><edit var=\"count2\" width=50></td>");
  493.         tb.append("<td><button value=\"Buy\" action=\"bypass -h npc_%objectId%_buyDefence $count2\" width=65 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table><br>");
  494.        
  495.         tb.append("<br><br>Held Warnings Left: <font color=\"LEVEL\">" + _warningCount + "</font><br>");
  496.         tb.append("Held Defenders Spawn Left: <font color=\"LEVEL\">" + _defenceSpawnCount + "</font><br>");
  497.         tb.append("<button value=\"Back\" action=\"bypass -h npc_%objectId%_manageCrystal\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  498.        
  499.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  500.         msg.setHtml(tb.toString());
  501.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  502.    
  503.         player.sendPacket(msg);
  504.     }
  505.    
  506.     /**
  507.      * Easier way to show Crystal Htm Window.
  508.      * @param player
  509.      * @param htm
  510.      */
  511.     public void showCrystalWindow(L2PcInstance player, String htm)
  512.     {
  513.         String html = null;
  514.         html = HtmCache.getInstance().getHtm(null, "data/html/ephion/crystal/" + htm);
  515.    
  516.         NpcHtmlMessage msg = new NpcHtmlMessage(getObjectId());
  517.         msg.setHtml(html);
  518.         msg.replace("%objectId%", String.valueOf(getObjectId()));
  519.         player.sendPacket(msg);
  520.     }
  521.    
  522.     /**
  523.      * Show list with all conquerable locations status.
  524.      * With info about owner (only clan names)
  525.      * TODO
  526.      * @param player
  527.      */
  528.     public void showLocationsStatusWindow(L2PcInstance player)
  529.     {      
  530.         TextBuilder tb = new TextBuilder();
  531.         tb.append("<html><title>Crystal</title><body><center><br>");
  532.         tb.append("<font color=\"LEVEL\">Conquerable Locations Status: </font><br>");
  533.         tb.append("<font color=\"LEVEL\">Not implemented yet, sorry! </font><br>");
  534.         tb.append("<button value=\"Back\" action=\"bypass -h npc_%objectId%_mainWindow\" width=160 height=32 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></body></html>");
  535.        
  536.         NpcHtmlMessage msg = new NpcHtmlMessage(this.getObjectId());
  537.         msg.setHtml(tb.toString());
  538.         msg.replace("%objectId%", String.valueOf(this.getObjectId()));
  539.    
  540.         player.sendPacket(msg);
  541.     }
  542.    
  543.     @Override
  544.     public boolean doDie(L2Character killer)
  545.     {
  546.         if (!super.doDie(killer))
  547.             return false;
  548.        
  549.         L2PcInstance player = null;
  550.         int _clanId = 0;
  551.        
  552.         if (killer instanceof L2PcInstance)
  553.             player = (L2PcInstance) killer;
  554.         else if (killer instanceof L2SummonInstance)
  555.             player = ((L2SummonInstance) killer).getOwner();
  556.        
  557.         if (player.getClan() != null)
  558.         {
  559.             if (_ownerClanId != 0)
  560.             {
  561.                 L2Clan oldOwner = ClanTable.getInstance().getClan(_ownerClanId);
  562.                 sendMessageToClan(oldOwner, "Your clan lost your " + getLocationName() + " Conquerable Location");
  563.             }
  564.            
  565.             _clanId = player.getClanId();
  566.             _ownerClanId = _clanId;
  567.            
  568.             L2Clan newOwner = ClanTable.getInstance().getClan(_clanId);
  569.            
  570.             cleanCrystalFunctions();
  571.             saveData(this.getNpcId());
  572.             sendMessageToClan(newOwner, "Your clan Conquered " + getLocationName() + " Location!");
  573.         }
  574.         else
  575.         {
  576.             player.sendPacket(new ExShowScreenMessage("You need clan to conquer a location!", 5000));
  577.         }
  578.        
  579.         return true;
  580.     }
  581.    
  582.     @Override
  583.     public void onAction(L2PcInstance player, boolean interact)
  584.     {
  585.         if (!this.canTarget(player))
  586.             return;
  587.    
  588.         player.setLastFolkNPC(this);
  589.    
  590.         if(player.getTarget() != this)
  591.         {
  592.             player.setTarget(this);
  593.             getAI();
  594.             MyTargetSelected my = new MyTargetSelected(getObjectId(), player.getLevel() - getLevel());
  595.             player.sendPacket(my);
  596.  
  597.             StatusUpdate su = new StatusUpdate(this);
  598.             su.addAttribute(StatusUpdate.CUR_HP, (int) getCurrentHp());
  599.             su.addAttribute(StatusUpdate.MAX_HP, getMaxHp());
  600.             player.sendPacket(su);
  601.             player.sendPacket(new ValidateLocation(this));
  602.         }
  603.         else if(interact)
  604.         {
  605.             if(!isAutoAttackable(player))
  606.                 showChatWindow(player, 0);
  607.             else if(!player.isAlikeDead())
  608.                 player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
  609.            
  610.             player.sendPacket(new ValidateLocation(this));
  611.             player.sendPacket(ActionFailed.STATIC_PACKET);
  612.         }
  613.     }
  614.    
  615.     @Override
  616.     public void reduceCurrentHp(double damage, L2Character attacker, L2Skill skill)
  617.     {
  618.         L2PcInstance player = null;
  619.         boolean canAttack = false;
  620.         if (attacker instanceof L2PcInstance)
  621.             player = (L2PcInstance) attacker;
  622.         else if (attacker instanceof L2SummonInstance)
  623.             player = ((L2SummonInstance) attacker).getOwner();
  624.    
  625.         if (player.getClanId() != _ownerClanId)
  626.             canAttack = true;          
  627.         if (canAttack)
  628.         {
  629.             super.reduceCurrentHp(damage, attacker, skill);
  630.             warnClanOwnerMembers();
  631.             spawnDefenders(attacker);
  632.         }
  633.         else if (player.getClan() == null)
  634.         {
  635.             attacker.sendPacket(new ExShowScreenMessage("You need clan to conquer a location!", 5000));
  636.         }
  637.         else
  638.         {
  639.             attacker.sendPacket(new ExShowScreenMessage("You cant kill your own Crystal!", 5000));
  640.         }
  641.     }
  642.    
  643.     /**
  644.      * Send Message to each online clan member.
  645.      * <br>
  646.      * @param clan
  647.      * @param msg
  648.      */
  649.     private static void sendMessageToClan(L2Clan clan, String msg)
  650.     {
  651.         if (clan != null)
  652.         {
  653.             for (L2ClanMember member: clan.getMembers())
  654.             {              
  655.                 if (member.isOnline())
  656.                 {
  657.                     L2PcInstance player = member.getPlayerInstance();
  658.                     player.sendPacket(new ExShowScreenMessage(msg, 5000));
  659.                 }
  660.             }
  661.         }
  662.     }
  663.    
  664.     /**
  665.      * Save data into Database and show Managment window again.
  666.      * Just to reduce same code all the time.
  667.      * @param player
  668.      */
  669.     private void saveDataAndShowManageWindow(L2PcInstance player)
  670.     {
  671.         saveData(this.getNpcId());
  672.         showManageCrystalWindow(player);
  673.     }
  674.        
  675.     /**
  676.      * A way to sell owner Crystal Abilities such as Warning and Defence ones.
  677.      * If abilitiId is 1, it means that it is Warning ability, if 2 its Defence.
  678.      * Owner need Clan Reputation Points to purchase abilities.
  679.      * Each cost 100 Clan Reputation Points.
  680.      * @param player
  681.      * @param count
  682.      * @param abilitiId
  683.      */
  684.     private void buyAbility(L2PcInstance player, int count, int abilitiId)
  685.     {
  686.         int crp = player.getClan().getReputationScore();
  687.         int price = count * 100;
  688.        
  689.         if (crp >= price)
  690.         {
  691.             if (abilitiId == 1)
  692.             {
  693.                 int current = _warningCount;
  694.                 _warningCount = current + count;
  695.                 player.getClan().setReputationScore(player.getClan().getReputationScore() - price, true);  
  696.                 player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "Successfully purchased " + count + " warnings!"));
  697.             }
  698.             else if (abilitiId == 2)
  699.             {
  700.                 int current = _defenceSpawnCount;
  701.                 _defenceSpawnCount = current + count;
  702.                 player.getClan().setReputationScore(player.getClan().getReputationScore() - price, true);  
  703.                 player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "Successfully purchased " + count + " Defence Abilities!"));
  704.             }
  705.         }
  706.         else
  707.         {
  708.             player.sendPacket(new CreatureSay(0, Say2.PARTY, "Crystal", "You don't have enough Clan Reputation Points!"));
  709.         }
  710.     }
  711.    
  712.     /**
  713.      * Warn each online clan member, if their crystal is under attack.
  714.      * To prevent spam after each hit from the attacker, there is delay.
  715.      * Default delay between next warning: 60 second.
  716.      * Also Crystal Warning Ability should be enabled.
  717.      */
  718.     private void warnClanOwnerMembers()
  719.     {
  720.         if (warningAbilityEnabled())
  721.         {
  722.             if (canWarn())
  723.             {
  724.                 if (_warningCount > 0)
  725.                 {
  726.                     L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  727.                     if (clan != null)
  728.                     {
  729.                         sendMessageToClan(clan, "Your " + getLocationName() + " location is under attack!");
  730.                         _warningCount --;
  731.                         setCanWarn(false);
  732.                         ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleWarningTask(), 60000);
  733.                     }
  734.                 }
  735.             }
  736.         }
  737.     }
  738.    
  739.     /**
  740.      * Spawn 5 new Defenders (NPC's)
  741.      * Provoke them to attack character which attacked crystal.
  742.      * After 60 seconds, defenders should disappear.
  743.      * @param attacker
  744.      */
  745.     private void spawnDefenders(L2Character attacker)
  746.     {      
  747.         if (defenceAbilityEnabled())
  748.         {
  749.             if (canSpawnDefenders())
  750.             {
  751.                 if (_defenceSpawnCount > 0)
  752.                 {
  753.                     L2Clan clan = ClanTable.getInstance().getClan(getOwnerClanId());
  754.                     if (clan != null)
  755.                     {
  756.                         L2Npc defender1 = null;
  757.                         L2Npc defender2 = null;
  758.                        
  759.                         for (int i=1; i<= DEFENDERS_COUNT; i++)
  760.                         {
  761.                             defender1 = addSpawn(50050, this.getX() - Rnd.get(100), this.getY() - Rnd.get(100), this.getZ());
  762.                             defender1.getKnownList().addKnownObject(attacker);
  763.                             _defenders.add(defender1);
  764.                             defender2 = addSpawn(50051, this.getX() - Rnd.get(100), this.getY() - Rnd.get(100), this.getZ());
  765.                             defender2.getKnownList().addKnownObject(attacker);
  766.                             _defenders.add(defender2);
  767.                         }
  768.                        
  769.                         for (L2Npc npc: _defenders)
  770.                         {
  771.                             ((L2Attackable) npc).addDamageHate(attacker, 9000, 9000);
  772.                             npc.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, attacker, null);
  773.                             npc.setRunning();
  774.                         }
  775.                        
  776.                         setCanSpawnDefenders(false);
  777.                         _defenceSpawnCount --;
  778.                         ThreadPoolManager.getInstance().scheduleGeneral(new ScheduleDefenceTast(), 60000);
  779.                     }
  780.                 }
  781.             }
  782.         }
  783.     }
  784.    
  785.     private static L2Npc addSpawn(int npcId, int x, int y, int z)
  786.     {
  787.         L2Npc result = null;
  788.         try
  789.         {
  790.             L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
  791.             if (template != null)
  792.             {
  793.                 L2Spawn spawn = new L2Spawn(template);
  794.                 spawn.setInstanceId(0);
  795.                 spawn.setHeading(1);
  796.                 spawn.setLocx(x);
  797.                 spawn.setLocy(y);
  798.                 spawn.setLocz(z);
  799.                 spawn.stopRespawn();
  800.                 result = spawn.spawnOne(true);
  801.    
  802.                 return result;
  803.             }
  804.         }
  805.         catch (Exception e1)
  806.         {
  807.            
  808.         }  
  809.         return null;
  810.     }
  811.    
  812.     /**
  813.      * Prepare crystal functions for new owner.
  814.      * At begining each functions should be disabled.
  815.      * <li>Clean Crystal Functions
  816.      * <li>Save Crystal Data
  817.      */
  818.     private void cleanCrystalFunctions()
  819.     {
  820.         setCanWarn(true);
  821.         setCanSpawnDefenders(true);
  822.         setDefenceAbilityEnabled(false);
  823.         setWarningAbilityEnabled(false);
  824.         setStatsEnabled(false);
  825.         _warningCount = 0;
  826.         _defenceSpawnCount = 0;
  827.     }
  828.    
  829.     /**
  830.      * Delay for warning ability, to prevent spam.
  831.      */
  832.     private class ScheduleWarningTask implements Runnable
  833.     {
  834.         public ScheduleWarningTask()
  835.         {
  836.             // Nothing
  837.         }
  838.        
  839.         public void run()
  840.         {
  841.             setCanWarn(true);
  842.         }
  843.     }
  844.    
  845.     /**
  846.      * Delay for defence spawn, to prevent spam.
  847.      */
  848.     private class ScheduleDefenceTast implements Runnable
  849.     {
  850.         public ScheduleDefenceTast()
  851.         {
  852.             // Nothing
  853.         }
  854.        
  855.         public void run()
  856.         {
  857.             setCanSpawnDefenders(true);
  858.            
  859.             for (L2Npc npc: _defenders)
  860.             {
  861.                 npc.deleteMe();
  862.             }
  863.            
  864.             _defenders.clear();
  865.         }
  866.     }
  867.    
  868.     /**
  869.      * @param b
  870.      * @return Enabled/Disabled
  871.      */
  872.     private String isEnabled(boolean b)
  873.     {
  874.         if (b)
  875.             return "Enabled";
  876.         else
  877.             return "Disabled";
  878.     }
  879.    
  880.     public void setOwnerClanId(int clanId)
  881.     {
  882.         _ownerClanId = clanId;
  883.     }
  884.  
  885.     public int getOwnerClanId()
  886.     {
  887.         return _ownerClanId;
  888.     }
  889.    
  890.     public void setLocationName(String name)
  891.     {
  892.         _locationName = name;
  893.     }
  894.  
  895.     public String getLocationName()
  896.     {
  897.         return _locationName;
  898.     }
  899.    
  900.     public boolean defenceAbilityEnabled()
  901.     {
  902.         return _defenceAbilityEnabled;
  903.     }
  904.    
  905.     public void setDefenceAbilityEnabled(boolean b)
  906.     {
  907.         _defenceAbilityEnabled = b;
  908.     }
  909.    
  910.     public boolean warningAbilityEnabled()
  911.     {
  912.         return _warningAbilityEnabled;
  913.     }
  914.    
  915.     public void setWarningAbilityEnabled(boolean b)
  916.     {
  917.         _warningAbilityEnabled = b;
  918.     }
  919.    
  920.     public boolean statsUpgradeEnabled()
  921.     {
  922.         return _statsUpgradeEnabled;
  923.     }
  924.    
  925.     public void setStatsEnabled(boolean b)
  926.     {
  927.         _statsUpgradeEnabled = b;
  928.     }
  929.    
  930.     public boolean canWarn()
  931.     {
  932.         return _canWarn;
  933.     }
  934.    
  935.     public void setCanWarn(boolean b)
  936.     {
  937.         _canWarn = b;
  938.     }
  939.    
  940.     public boolean canSpawnDefenders()
  941.     {
  942.         return _canSpawnDefenders;
  943.     }
  944.    
  945.     public void setCanSpawnDefenders(boolean b)
  946.     {
  947.         _canSpawnDefenders = b;
  948.     }
  949. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement