Guest User

L2jhellas Conquerable Location Engine

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