RequestSetCastleSiegeTime.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.clientpackets;
  20. import java.util.Calendar;
  21. import java.util.Date;
  22. import java.util.logging.Level;
  23. import com.l2jserver.Config;
  24. import com.l2jserver.gameserver.instancemanager.CastleManager;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.entity.Castle;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.SiegeInfo;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. import com.l2jserver.gameserver.util.Broadcast;
  31. /**
  32. * @author UnAfraid
  33. */
  34. public class RequestSetCastleSiegeTime extends L2GameClientPacket
  35. {
  36. private int _castleId;
  37. private long _time;
  38. @Override
  39. protected void readImpl()
  40. {
  41. _castleId = readD();
  42. _time = readD();
  43. _time *= 1000;
  44. }
  45. @Override
  46. protected void runImpl()
  47. {
  48. final L2PcInstance activeChar = getClient().getActiveChar();
  49. final Castle castle = CastleManager.getInstance().getCastleById(_castleId);
  50. if ((activeChar == null) || (castle == null))
  51. {
  52. _log.log(Level.WARNING, getType() + ": activeChar: " + activeChar + " castle: " + castle + " castleId: " + _castleId);
  53. return;
  54. }
  55. if ((castle.getOwnerId() > 0) && (castle.getOwnerId() != activeChar.getClanId()))
  56. {
  57. _log.log(Level.WARNING, getType() + ": activeChar: " + activeChar + " castle: " + castle + " castleId: " + _castleId + " is trying to change siege date of not his own castle!");
  58. return;
  59. }
  60. else if (!activeChar.isClanLeader())
  61. {
  62. _log.log(Level.WARNING, getType() + ": activeChar: " + activeChar + " castle: " + castle + " castleId: " + _castleId + " is trying to change siege date but is not clan leader!");
  63. return;
  64. }
  65. else if (!castle.getIsTimeRegistrationOver())
  66. {
  67. if (isSiegeTimeValid(castle.getSiegeDate().getTimeInMillis(), _time))
  68. {
  69. castle.getSiegeDate().setTimeInMillis(_time);
  70. castle.setIsTimeRegistrationOver(true);
  71. castle.getSiege().saveSiegeDate();
  72. SystemMessage msg = SystemMessage.getSystemMessage(SystemMessageId.S1_ANNOUNCED_SIEGE_TIME);
  73. msg.addCastleId(_castleId);
  74. Broadcast.toAllOnlinePlayers(msg);
  75. activeChar.sendPacket(new SiegeInfo(castle));
  76. }
  77. else
  78. {
  79. _log.log(Level.WARNING, getType() + ": activeChar: " + activeChar + " castle: " + castle + " castleId: " + _castleId + " is trying to an invalid time (" + new Date(_time) + " !");
  80. }
  81. }
  82. else
  83. {
  84. _log.log(Level.WARNING, getType() + ": activeChar: " + activeChar + " castle: " + castle + " castleId: " + _castleId + " is trying to change siege date but currently not possible!");
  85. }
  86. }
  87. private static boolean isSiegeTimeValid(long siegeDate, long choosenDate)
  88. {
  89. Calendar cal1 = Calendar.getInstance();
  90. cal1.setTimeInMillis(siegeDate);
  91. cal1.set(Calendar.MINUTE, 0);
  92. cal1.set(Calendar.SECOND, 0);
  93. Calendar cal2 = Calendar.getInstance();
  94. cal2.setTimeInMillis(choosenDate);
  95. for (int hour : Config.SIEGE_HOUR_LIST)
  96. {
  97. cal1.set(Calendar.HOUR_OF_DAY, hour);
  98. if (isEqual(cal1, cal2, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND))
  99. {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. private static boolean isEqual(Calendar cal1, Calendar cal2, int... fields)
  106. {
  107. for (int field : fields)
  108. {
  109. if (cal1.get(field) != cal2.get(field))
  110. {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. @Override
  117. public String getType()
  118. {
  119. return getClass().getSimpleName();
  120. }
  121. }