TerritoryWard.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 com.l2jserver.gameserver.model;
  16. import com.l2jserver.Config;
  17. import com.l2jserver.gameserver.datatables.ItemTable;
  18. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  19. import com.l2jserver.gameserver.model.actor.L2Npc;
  20. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  21. import com.l2jserver.gameserver.network.SystemMessageId;
  22. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  23. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  24. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  25. public class TerritoryWard
  26. {
  27. //private static final Logger _log = Logger.getLogger(CombatFlag.class.getName());
  28. protected L2PcInstance _player = null;
  29. public int playerId = 0;
  30. private L2ItemInstance _item = null;
  31. private L2Npc _npc = null;
  32. private Location _location;
  33. public L2ItemInstance itemInstance;
  34. private int _itemId;
  35. private int _ownerCastleId;
  36. @SuppressWarnings("unused")
  37. private int _heading;
  38. private int _territoryId;
  39. // =========================================================
  40. // Constructor
  41. public TerritoryWard(int territory_id, int x, int y, int z, int heading, int item_id, int castleId, L2Npc npc)
  42. {
  43. _territoryId = territory_id;
  44. _location = new Location(x,y,z,heading);
  45. _heading = heading;
  46. _itemId = item_id;
  47. _ownerCastleId = castleId;
  48. _npc = npc;
  49. }
  50. public int getTerritoryId()
  51. {
  52. return _territoryId;
  53. }
  54. public int getOwnerCastleId()
  55. {
  56. return _ownerCastleId;
  57. }
  58. public void setOwnerCastleId(int newOwner)
  59. {
  60. _ownerCastleId = newOwner;
  61. }
  62. public L2Npc getNpc()
  63. {
  64. return _npc;
  65. }
  66. public void setNpc(L2Npc npc)
  67. {
  68. _npc = npc;
  69. }
  70. public L2PcInstance getPlayer()
  71. {
  72. return _player;
  73. }
  74. public L2ItemInstance getItemInstance()
  75. {
  76. return itemInstance;
  77. }
  78. public synchronized void spawnMe()
  79. {
  80. if ( _player != null )
  81. dropIt();
  82. L2ItemInstance i;
  83. // Init the dropped L2ItemInstance and add it in the world as a visible object at the position where mob was last
  84. i = ItemTable.getInstance().createItem("Combat", _itemId, 1, null, null);
  85. i.spawnMe(_location.getX(), _location.getY(), _location.getZ());
  86. itemInstance = i;
  87. _npc = null;
  88. }
  89. public synchronized void unSpawnMe()
  90. {
  91. if ( _player != null )
  92. dropIt();
  93. if ( itemInstance != null )
  94. {
  95. itemInstance.decayMe();
  96. }
  97. }
  98. public boolean activate(L2PcInstance player, L2ItemInstance item)
  99. {
  100. if (player.isMounted())
  101. {
  102. player.sendPacket(new SystemMessage(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION));
  103. player.destroyItem("CombatFlag", item, null, true);
  104. spawnMe();
  105. return false;
  106. }
  107. else if (TerritoryWarManager.getInstance().getRegisteredTerritoryId(player) == 0)
  108. {
  109. player.sendMessage("Non participants can't pickup Territory Wards!");
  110. player.destroyItem("CombatFlag", item, null, true);
  111. spawnMe();
  112. return false;
  113. }
  114. // Player holding it data
  115. _player = player;
  116. playerId = _player.getObjectId();
  117. itemInstance = null;
  118. _npc = null;
  119. // Equip with the weapon
  120. if (item == null)
  121. _item = ItemTable.getInstance().createItem("Combat", _itemId, 1, null, null);
  122. else
  123. _item = item;
  124. _player.getInventory().equipItemAndRecord(_item);
  125. SystemMessage sm = new SystemMessage(SystemMessageId.S1_EQUIPPED);
  126. sm.addItemName(_item);
  127. _player.sendPacket(sm);
  128. // Refresh inventory
  129. if (!Config.FORCE_INVENTORY_UPDATE)
  130. {
  131. InventoryUpdate iu = new InventoryUpdate();
  132. iu.addItem(_item);
  133. _player.sendPacket(iu);
  134. }
  135. else _player.sendPacket(new ItemList(_player, false));
  136. // Refresh player stats
  137. _player.broadcastUserInfo();
  138. _player.setCombatFlagEquipped(true);
  139. _player.sendPacket(new SystemMessage(SystemMessageId.YOU_VE_ACQUIRED_THE_WARD));
  140. TerritoryWarManager.getInstance().giveTWPoint(player, _territoryId, 5);
  141. return true;
  142. }
  143. public void dropIt()
  144. {
  145. // Reset player stats
  146. _player.setCombatFlagEquipped(false);
  147. int slot = _player.getInventory().getSlotFromItem(_item);
  148. _player.getInventory().unEquipItemInBodySlotAndRecord(slot);
  149. _player.destroyItem("CombatFlag", _item, null, true);
  150. _item = null;
  151. _player.broadcastUserInfo();
  152. _location = new Location(_player.getX(),_player.getY(),_player.getZ(),_player.getHeading());
  153. _player = null;
  154. playerId = 0;
  155. }
  156. }