SiegeFlag.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.L2Skill.SkillType;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2SiegeFlagInstance;
  29. import net.sf.l2j.gameserver.model.entity.Castle;
  30. import net.sf.l2j.gameserver.model.entity.Fort;
  31. /**
  32. * @author _drunk_
  33. *
  34. * TODO To change the template for this generated type comment go to
  35. * Window - Preferences - Java - Code Style - Code Templates
  36. */
  37. public class SiegeFlag implements ISkillHandler
  38. {
  39. //private static Logger _log = Logger.getLogger(SiegeFlag.class.getName());
  40. private static final SkillType[] SKILL_IDS = {SkillType.SIEGEFLAG};
  41. public void useSkill(L2Character activeChar, L2Skill skill, L2Object[] targets)
  42. {
  43. if (!(activeChar instanceof L2PcInstance)) return;
  44. L2PcInstance player = (L2PcInstance)activeChar;
  45. if (player.getClan() == null || player.getClan().getLeaderId() != player.getObjectId()) return;
  46. Castle castle = CastleManager.getInstance().getCastle(player);
  47. Fort fort = FortManager.getInstance().getFort(player);
  48. if ((castle == null) && (fort == null))
  49. return;
  50. if ( castle != null )
  51. {
  52. if (!checkIfOkToPlaceFlag(player, castle, true)) return;
  53. }
  54. else
  55. {
  56. if (!checkIfOkToPlaceFlag(player, fort, true)) return;
  57. }
  58. try
  59. {
  60. // Spawn a new flag
  61. L2SiegeFlagInstance flag = new L2SiegeFlagInstance(player, IdFactory.getInstance().getNextId(), NpcTable.getInstance().getTemplate(35062));
  62. flag.setTitle(player.getClan().getName());
  63. flag.setCurrentHpMp(flag.getMaxHp(), flag.getMaxMp());
  64. flag.setHeading(player.getHeading());
  65. flag.spawnMe(player.getX(), player.getY(), player.getZ() + 50);
  66. if ( castle != null )
  67. castle.getSiege().getFlag(player.getClan()).add(flag);
  68. else
  69. fort.getSiege().getFlag(player.getClan()).add(flag);
  70. }
  71. catch (Exception e)
  72. {
  73. player.sendMessage("Error placing flag:" + e);
  74. }
  75. }
  76. public SkillType[] getSkillIds()
  77. {
  78. return SKILL_IDS;
  79. }
  80. /**
  81. * Return true if character clan place a flag<BR><BR>
  82. *
  83. * @param activeChar The L2Character of the character placing the flag
  84. * @param isCheckOnly if false, it will send a notification to the player telling him
  85. * why it failed
  86. */
  87. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, boolean isCheckOnly)
  88. {
  89. Castle castle = CastleManager.getInstance().getCastle(activeChar);
  90. Fort fort = FortManager.getInstance().getFort(activeChar);
  91. if ((castle == null) && (fort == null))
  92. return false;
  93. if (castle != null)
  94. return checkIfOkToPlaceFlag(activeChar, castle, isCheckOnly);
  95. else
  96. return checkIfOkToPlaceFlag(activeChar, fort, isCheckOnly);
  97. }
  98. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, Castle castle, boolean isCheckOnly)
  99. {
  100. if (!(activeChar instanceof L2PcInstance))
  101. return false;
  102. String text = "";
  103. L2PcInstance player = (L2PcInstance)activeChar;
  104. if (castle == null || castle.getCastleId() <= 0)
  105. text = "You must be on castle ground to place a flag";
  106. else if (!castle.getSiege().getIsInProgress())
  107. text = "You can only place a flag during a siege.";
  108. else if (castle.getSiege().getAttackerClan(player.getClan()) == null)
  109. text = "You must be an attacker to place a flag";
  110. else if (player.getClan() == null || !player.isClanLeader())
  111. text = "You must be a clan leader to place a flag";
  112. else if (castle.getSiege().getAttackerClan(player.getClan()).getNumFlags() >= SiegeManager.getInstance().getFlagMaxCount())
  113. text = "You have already placed the maximum number of flags possible";
  114. else
  115. return true;
  116. if (!isCheckOnly)
  117. player.sendMessage(text);
  118. return false;
  119. }
  120. public static boolean checkIfOkToPlaceFlag(L2Character activeChar, Fort fort, boolean isCheckOnly)
  121. {
  122. if (!(activeChar instanceof L2PcInstance))
  123. return false;
  124. String text = "";
  125. L2PcInstance player = (L2PcInstance)activeChar;
  126. if (fort == null || fort.getFortId() <= 0)
  127. text = "You must be on fort ground to place a flag";
  128. else if (!fort.getSiege().getIsInProgress())
  129. text = "You can only place a flag during a siege.";
  130. else if (fort.getSiege().getAttackerClan(player.getClan()) == null)
  131. text = "You must be an attacker to place a flag";
  132. else if (player.getClan() == null || !player.isClanLeader())
  133. text = "You must be a clan leader to place a flag";
  134. else if (fort.getSiege().getAttackerClan(player.getClan()).getNumFlags() >= FortSiegeManager.getInstance().getFlagMaxCount())
  135. text = "You have already placed the maximum number of flags possible";
  136. else
  137. return true;
  138. if (!isCheckOnly)
  139. player.sendMessage(text);
  140. return false;
  141. }
  142. }