FortUpdater.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.model.L2Clan;
  24. import com.l2jserver.gameserver.model.entity.Fort;
  25. import com.l2jserver.gameserver.model.itemcontainer.Inventory;
  26. /**
  27. * Class managing periodical events with castle
  28. * @author Vice - 2008
  29. */
  30. public class FortUpdater implements Runnable
  31. {
  32. private static final Logger _log = LoggerFactory.getLogger(FortUpdater.class);
  33. private final L2Clan _clan;
  34. private final Fort _fort;
  35. private int _runCount;
  36. private final UpdaterType _updaterType;
  37. public enum UpdaterType
  38. {
  39. MAX_OWN_TIME, // gives fort back to NPC clan
  40. PERIODIC_UPDATE // raise blood oath/supply level
  41. }
  42. public FortUpdater(Fort fort, L2Clan clan, int runCount, UpdaterType ut)
  43. {
  44. _fort = fort;
  45. _clan = clan;
  46. _runCount = runCount;
  47. _updaterType = ut;
  48. }
  49. @Override
  50. public void run()
  51. {
  52. try
  53. {
  54. switch (_updaterType)
  55. {
  56. case PERIODIC_UPDATE:
  57. {
  58. _runCount++;
  59. if ((_fort.getOwnerClan() == null) || (_fort.getOwnerClan() != _clan))
  60. {
  61. return;
  62. }
  63. _fort.getOwnerClan().increaseBloodOathCount();
  64. if (_fort.getFortState() == 2)
  65. {
  66. if (_clan.getWarehouse().getAdena() >= Config.FS_FEE_FOR_CASTLE)
  67. {
  68. _clan.getWarehouse().destroyItemByItemId("FS_fee_for_Castle", Inventory.ADENA_ID, Config.FS_FEE_FOR_CASTLE, null, null);
  69. _fort.getContractedCastle().addToTreasuryNoTax(Config.FS_FEE_FOR_CASTLE);
  70. _fort.raiseSupplyLvL();
  71. }
  72. else
  73. {
  74. _fort.setFortState(1, 0);
  75. }
  76. }
  77. _fort.saveFortVariables();
  78. break;
  79. }
  80. case MAX_OWN_TIME:
  81. {
  82. if ((_fort.getOwnerClan() == null) || (_fort.getOwnerClan() != _clan))
  83. {
  84. return;
  85. }
  86. if (_fort.getOwnedTime() > (Config.FS_MAX_OWN_TIME * 3600))
  87. {
  88. _fort.removeOwner(true);
  89. _fort.setFortState(0, 0);
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. catch (Exception e)
  96. {
  97. _log.error("There has been a problem updating forts!", e);
  98. }
  99. }
  100. public int getRunCount()
  101. {
  102. return _runCount;
  103. }
  104. }