TerritoryWard.java 5.1 KB

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