SiegeFlag.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j.gameserver.handler.skillhandlers;
  16. import net.sf.l2j.gameserver.datatables.NpcTable;
  17. import net.sf.l2j.gameserver.handler.ISkillHandler;
  18. import net.sf.l2j.gameserver.idfactory.IdFactory;
  19. import net.sf.l2j.gameserver.instancemanager.CastleManager;
  20. import net.sf.l2j.gameserver.instancemanager.FortManager;
  21. import net.sf.l2j.gameserver.instancemanager.FortSiegeManager;
  22. import net.sf.l2j.gameserver.instancemanager.SiegeManager;
  23. import net.sf.l2j.gameserver.model.L2Character;
  24. import net.sf.l2j.gameserver.model.L2Object;
  25. import net.sf.l2j.gameserver.model.L2Skill;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2SiegeFlagInstance;
  28. import net.sf.l2j.gameserver.model.entity.Castle;
  29. import net.sf.l2j.gameserver.model.entity.Fort;
  30. import net.sf.l2j.gameserver.templates.L2SkillType;
  31. /**
  32. * @author _drunk_
  33. *
  34. */
  35. public class SiegeFlag implements ISkillHandler
  36. {
  37. private static final L2SkillType[] SKILL_IDS =
  38. {
  39. L2SkillType.SIEGEFLAG
  40. };
  41. /**
  42. *
  43. * @see net.sf.l2j.gameserver.handler.ISkillHandler#useSkill(net.sf.l2j.gameserver.model.L2Character, net.sf.l2j.gameserver.model.L2Skill, net.sf.l2j.gameserver.model.L2Object[])
  44. */
  45. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  46. {
  47. if (!(activeChar instanceof L2PcInstance))
  48. return;
  49. L2PcInstance player = (L2PcInstance) activeChar;
  50. if (player.getClan() == null || player.getClan().getLeaderId() != player.getObjectId())
  51. return;
  52. Castle castle = CastleManager.getInstance().getCastle(player);
  53. Fort fort = FortManager.getInstance().getFort(player);
  54. if ((castle == null) && (fort == null))
  55. return;
  56. if (castle != null)
  57. {
  58. if (!checkIfOkToPlaceFlag(player, castle, true))
  59. return;
  60. }
  61. else
  62. {
  63. if (!checkIfOkToPlaceFlag(player, fort, true))
  64. return;
  65. }
  66. try
  67. {
  68. // Spawn a new flag
  69. L2SiegeFlagInstance flag = new L2SiegeFlagInstance(player, IdFactory.getInstance().getNextId(), NpcTable.getInstance().getTemplate(35062));
  70. flag.setTitle(player.getClan().getName());
  71. flag.setCurrentHpMp(flag.getMaxHp(), flag.getMaxMp());
  72. flag.setHeading(player.getHeading());
  73. flag.spawnMe(player.getX(), player.getY(), player.getZ() + 50);
  74. if (castle != null)
  75. castle.getSiege().getFlag(player.getClan()).add(flag);
  76. else
  77. fort.getSiege().getFlag(player.getClan()).add(flag);
  78. }
  79. catch (Exception e)
  80. {
  81. player.sendMessage("Error placing flag:" + e);
  82. }
  83. }
  84. /**
  85. *
  86. * @see net.sf.l2j.gameserver.handler.ISkillHandler#getSkillIds()
  87. */
  88. public L2SkillType[] getSkillIds()
  89. {
  90. return SKILL_IDS;
  91. }
  92. /**
  93. * Return true if character clan place a flag<BR><BR>
  94. *
  95. * @param activeChar The L2Character of the character placing the flag
  96. * @param isCheckOnly if false, it will send a notification to the player telling him
  97. * why it failed
  98. */
  99. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, boolean isCheckOnly)
  100. {
  101. Castle castle = CastleManager.getInstance().getCastle(activeChar);
  102. Fort fort = FortManager.getInstance().getFort(activeChar);
  103. if ((castle == null) && (fort == null))
  104. return false;
  105. if (castle != null)
  106. return checkIfOkToPlaceFlag(activeChar, castle, isCheckOnly);
  107. else
  108. return checkIfOkToPlaceFlag(activeChar, fort, isCheckOnly);
  109. }
  110. /**
  111. *
  112. * @param activeChar
  113. * @param castle
  114. * @param isCheckOnly
  115. * @return
  116. */
  117. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, Castle castle, boolean isCheckOnly)
  118. {
  119. if (!(activeChar instanceof L2PcInstance))
  120. return false;
  121. String text = "";
  122. L2PcInstance player = (L2PcInstance) activeChar;
  123. if (castle == null || castle.getCastleId() <= 0)
  124. text = "You must be on castle ground to place a flag";
  125. else if (!castle.getSiege().getIsInProgress())
  126. text = "You can only place a flag during a siege.";
  127. else if (castle.getSiege().getAttackerClan(player.getClan()) == null)
  128. text = "You must be an attacker to place a flag";
  129. else if (player.getClan() == null || !player.isClanLeader())
  130. text = "You must be a clan leader to place a flag";
  131. else if (castle.getSiege().getAttackerClan(player.getClan()).getNumFlags() >= SiegeManager.getInstance().getFlagMaxCount())
  132. text = "You have already placed the maximum number of flags possible";
  133. else
  134. return true;
  135. if (!isCheckOnly)
  136. player.sendMessage(text);
  137. return false;
  138. }
  139. /**
  140. *
  141. * @param activeChar
  142. * @param fort
  143. * @param isCheckOnly
  144. * @return
  145. */
  146. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, Fort fort, boolean isCheckOnly)
  147. {
  148. if (!(activeChar instanceof L2PcInstance))
  149. return false;
  150. String text = "";
  151. L2PcInstance player = (L2PcInstance) activeChar;
  152. if (fort == null || fort.getFortId() <= 0)
  153. text = "You must be on fort ground to place a flag";
  154. else if (!fort.getSiege().getIsInProgress())
  155. text = "You can only place a flag during a siege.";
  156. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  157. text = "You must be an attacker to place a flag";
  158. else if (player.getClan() == null || !player.isClanLeader())
  159. text = "You must be a clan leader to place a flag";
  160. else if (fort.getSiege().getAttackerClan(player.getClan()).getNumFlags() >= FortSiegeManager.getInstance().getFlagMaxCount())
  161. text = "You have already placed the maximum number of flags possible";
  162. else
  163. return true;
  164. if (!isCheckOnly)
  165. player.sendMessage(text);
  166. return false;
  167. }
  168. }