TerritoryWard.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2004-2014 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.model;
  20. import com.l2jserver.Config;
  21. import com.l2jserver.gameserver.datatables.ItemTable;
  22. import com.l2jserver.gameserver.instancemanager.TerritoryWarManager;
  23. import com.l2jserver.gameserver.model.actor.L2Npc;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  28. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  29. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  30. public class TerritoryWard
  31. {
  32. // private static final Logger _log = Logger.getLogger(CombatFlag.class.getName());
  33. protected L2PcInstance _player = null;
  34. public int playerId = 0;
  35. private L2ItemInstance _item = null;
  36. private L2Npc _npc = null;
  37. private Location _location;
  38. private Location _oldLocation;
  39. private final int _itemId;
  40. private int _ownerCastleId;
  41. private final int _territoryId;
  42. public TerritoryWard(int territory_id, int x, int y, int z, int heading, int item_id, int castleId, L2Npc npc)
  43. {
  44. _territoryId = territory_id;
  45. _location = new Location(x, y, z, 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 synchronized void spawnBack()
  75. {
  76. if (_player != null)
  77. {
  78. dropIt();
  79. }
  80. // Init the dropped L2WardInstance and add it in the world as a visible object at the position where last Pc got it
  81. _npc = TerritoryWarManager.getInstance().spawnNPC(36491 + _territoryId, _oldLocation);
  82. }
  83. public synchronized void spawnMe()
  84. {
  85. if (_player != null)
  86. {
  87. dropIt();
  88. }
  89. // Init the dropped L2WardInstance and add it in the world as a visible object at the position where Pc was last
  90. _npc = TerritoryWarManager.getInstance().spawnNPC(36491 + _territoryId, _location);
  91. }
  92. public synchronized void unSpawnMe()
  93. {
  94. if (_player != null)
  95. {
  96. dropIt();
  97. }
  98. if ((_npc != null) && !_npc.isDecayed())
  99. {
  100. _npc.deleteMe();
  101. }
  102. }
  103. public boolean activate(L2PcInstance player, L2ItemInstance item)
  104. {
  105. if (player.isMounted())
  106. {
  107. player.sendPacket(SystemMessageId.CANNOT_EQUIP_ITEM_DUE_TO_BAD_CONDITION);
  108. player.destroyItem("CombatFlag", item, null, true);
  109. spawnMe();
  110. return false;
  111. }
  112. else if (TerritoryWarManager.getInstance().getRegisteredTerritoryId(player) == 0)
  113. {
  114. player.sendMessage("Non participants can't pickup Territory Wards!");
  115. player.destroyItem("CombatFlag", item, null, true);
  116. spawnMe();
  117. return false;
  118. }
  119. // Player holding it data
  120. _player = player;
  121. playerId = _player.getObjectId();
  122. _oldLocation = new Location(_npc.getX(), _npc.getY(), _npc.getZ(), _npc.getHeading());
  123. _npc = null;
  124. // Equip with the weapon
  125. if (item == null)
  126. {
  127. _item = ItemTable.getInstance().createItem("Combat", _itemId, 1, null, null);
  128. }
  129. else
  130. {
  131. _item = item;
  132. }
  133. _player.getInventory().equipItem(_item);
  134. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_EQUIPPED);
  135. sm.addItemName(_item);
  136. _player.sendPacket(sm);
  137. // Refresh inventory
  138. if (!Config.FORCE_INVENTORY_UPDATE)
  139. {
  140. InventoryUpdate iu = new InventoryUpdate();
  141. iu.addItem(_item);
  142. _player.sendPacket(iu);
  143. }
  144. else
  145. {
  146. _player.sendPacket(new ItemList(_player, false));
  147. }
  148. // Refresh player stats
  149. _player.broadcastUserInfo();
  150. _player.setCombatFlagEquipped(true);
  151. _player.sendPacket(SystemMessageId.YOU_VE_ACQUIRED_THE_WARD);
  152. TerritoryWarManager.getInstance().giveTWPoint(player, _territoryId, 5);
  153. return true;
  154. }
  155. public void dropIt()
  156. {
  157. // Reset player stats
  158. _player.setCombatFlagEquipped(false);
  159. int slot = _player.getInventory().getSlotFromItem(_item);
  160. _player.getInventory().unEquipItemInBodySlot(slot);
  161. _player.destroyItem("CombatFlag", _item, null, true);
  162. _item = null;
  163. _player.broadcastUserInfo();
  164. _location = new Location(_player.getX(), _player.getY(), _player.getZ(), _player.getHeading());
  165. _player = null;
  166. playerId = 0;
  167. }
  168. }