TerritoryWard.java 5.0 KB

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