Advertisement
Nik

Drop protection system

Nik
Apr 20th, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 13.66 KB | None | 0 0
  1. Index: java/com/l2jserver/gameserver/model/DropProtection.java
  2. ===================================================================
  3. --- java/com/l2jserver/gameserver/model/DropProtection.java (revision 0)
  4. +++ java/com/l2jserver/gameserver/model/DropProtection.java (revision 0)
  5. @@ -0,0 +1,95 @@
  6. +/*
  7. + * This program is free software: you can redistribute it and/or modify it under
  8. + * the terms of the GNU General Public License as published by the Free Software
  9. + * Foundation, either version 3 of the License, or (at your option) any later
  10. + * version.
  11. + *
  12. + * This program is distributed in the hope that it will be useful, but WITHOUT
  13. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  15. + * details.
  16. + *
  17. + * You should have received a copy of the GNU General Public License along with
  18. + * this program. If not, see <http://www.gnu.org/licenses/>.
  19. + */
  20. +package com.l2jserver.gameserver.model;
  21. +
  22. +import java.util.concurrent.ScheduledFuture;
  23. +
  24. +import com.l2jserver.gameserver.ThreadPoolManager;
  25. +import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. +import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  27. +
  28. +/**
  29. + *
  30. + * @author DrHouse
  31. + *
  32. + */
  33. +public class DropProtection implements Runnable
  34. +{
  35. +   private volatile boolean _isProtected = false;
  36. +   private L2PcInstance _owner = null;
  37. +   private ScheduledFuture<?> _task = null;
  38. +  
  39. +   private static final long PROTECTED_MILLIS_TIME = 15000;
  40. +  
  41. +   public synchronized void run()
  42. +   {
  43. +       _isProtected = false;
  44. +       _owner = null;
  45. +       _task = null;
  46. +   }
  47. +  
  48. +   public boolean isProtected()
  49. +   {
  50. +       return _isProtected;
  51. +   }
  52. +  
  53. +   public L2PcInstance getOwner()
  54. +   {
  55. +       return _owner;
  56. +   }
  57. +  
  58. +   public synchronized boolean tryPickUp(L2PcInstance actor)
  59. +   {
  60. +       if (!_isProtected)
  61. +           return true;
  62. +      
  63. +       if (_owner == actor)
  64. +           return true;
  65. +      
  66. +       if (_owner.getParty() != null && _owner.getParty() == actor.getParty())
  67. +           return true;
  68. +      
  69. +       if (_owner.getClan() != null && _owner.getClan() == actor.getClan())
  70. +           return true;
  71. +      
  72. +       return false;
  73. +   }
  74. +  
  75. +   public boolean tryPickUp(L2PetInstance pet)
  76. +   {
  77. +       return tryPickUp(pet.getOwner());
  78. +   }
  79. +  
  80. +   public synchronized void unprotect()
  81. +   {
  82. +       if (_task != null)
  83. +           _task.cancel(false);
  84. +       _isProtected = false;
  85. +       _owner = null;
  86. +       _task = null;
  87. +   }
  88. +  
  89. +   public synchronized void protect(L2PcInstance player)
  90. +   {
  91. +       unprotect();
  92. +      
  93. +       _isProtected = true;
  94. +      
  95. +       if ((_owner = player) == null)
  96. +           throw new NullPointerException("Trying to protect dropped item to null owner");
  97. +      
  98. +       _task = ThreadPoolManager.getInstance().scheduleGeneral(this, PROTECTED_MILLIS_TIME);
  99. +   }
  100. +}
  101. Index: java/com/l2jserver/gameserver/model/L2ItemInstance.java
  102. ===================================================================
  103. --- java/com/l2jserver/gameserver/model/L2ItemInstance.java (revision 4578)
  104. +++ java/com/l2jserver/gameserver/model/L2ItemInstance.java (working copy)
  105. @@ -158,6 +158,8 @@
  106.    
  107.     private ScheduledFuture<?> itemLootShedule = null;
  108.     public ScheduledFuture<?> _lifeTimeTask;
  109. +  
  110. +   private final DropProtection _dropProtection = new DropProtection();
  111.     /**
  112.      * Constructor of the L2ItemInstance from the objectId and the itemId.
  113.      * @param objectId : int designating the ID of the object in the world
  114. @@ -1891,6 +1893,11 @@
  115.             activeChar.sendPacket(new SpawnItem(this));
  116.     }
  117.    
  118. +   public final DropProtection getDropProtection()
  119. +   {
  120. +       return _dropProtection;
  121. +   }
  122. +  
  123.     public boolean isPublished()
  124.     {
  125.         return _published;
  126. Index: java/com/l2jserver/gameserver/model/actor/L2Attackable.java
  127. ===================================================================
  128. --- java/com/l2jserver/gameserver/model/actor/L2Attackable.java (revision 4578)
  129. +++ java/com/l2jserver/gameserver/model/actor/L2Attackable.java (working copy)
  130. @@ -595,19 +595,13 @@
  131.             if (getAggroList().isEmpty())
  132.                 return;
  133.            
  134. -           // Manage Base, Quests and Sweep drops of the L2Attackable
  135. -           doItemDrop(lastAttacker);
  136. -          
  137. -           // Manage drop of Special Events created by GM for a defined period
  138. -           doEventDrop(lastAttacker);
  139. -          
  140. -           if (!getMustRewardExpSP())
  141. -               return;
  142. -          
  143.             int damage;
  144.             L2Character attacker, ddealer;
  145.             RewardInfo reward;
  146.            
  147. +           L2PcInstance maxDealer = null;
  148. +           int maxDamage = 0;
  149. +          
  150.             // While Interating over This Map Removing Object is Not Allowed
  151.             //synchronized (getAggroList())
  152.             {
  153. @@ -644,9 +638,25 @@
  154.                             reward.addDamage(damage);
  155.                        
  156.                         rewards.put(ddealer, reward);
  157. +                      
  158. +                       if (ddealer.getActingPlayer() != null && reward._dmg > maxDamage)
  159. +                       {
  160. +                           maxDealer = ddealer.getActingPlayer();
  161. +                           maxDamage = reward._dmg;
  162. +                       }
  163.                     }
  164.                 }
  165.             }
  166. +          
  167. +           // Manage Base, Quests and Sweep drops of the L2Attackable
  168. +           doItemDrop(maxDealer != null && maxDealer.isOnline() == true ? maxDealer : lastAttacker);
  169. +
  170. +           // Manage drop of Special Events created by GM for a defined period
  171. +           doEventDrop(lastAttacker);
  172. +
  173. +           if (!getMustRewardExpSP())
  174. +               return;
  175. +          
  176.             if (!rewards.isEmpty())
  177.             {
  178.                 L2Party attackerParty;
  179. @@ -1575,9 +1585,9 @@
  180.         return null;
  181.     }
  182.    
  183. -   public void doItemDrop(L2Character lastAttacker)
  184. +   public void doItemDrop(L2Character mainDamageDealer)
  185.     {
  186. -       doItemDrop(getTemplate(),lastAttacker);
  187. +       doItemDrop(getTemplate(),mainDamageDealer);
  188.     }
  189.    
  190.     /**
  191. @@ -1599,12 +1609,12 @@
  192.      *
  193.      * @param lastAttacker The L2Character that has killed the L2Attackable
  194.      */
  195. -   public void doItemDrop(L2NpcTemplate npcTemplate, L2Character lastAttacker)
  196. +   public void doItemDrop(L2NpcTemplate npcTemplate, L2Character mainDamageDealer)
  197.     {
  198. -       if (lastAttacker == null)
  199. +       if (mainDamageDealer == null)
  200.             return;
  201.        
  202. -       L2PcInstance player = lastAttacker.getActingPlayer();
  203. +       L2PcInstance player = mainDamageDealer.getActingPlayer();
  204.        
  205.         // Don't drop anything if the last attacker or owner isn't L2PcInstance
  206.         if (player == null)
  207. @@ -1780,7 +1790,7 @@
  208.     /**
  209.      * Drop reward item.
  210.      */
  211. -   public L2ItemInstance dropItem(L2PcInstance lastAttacker, RewardItem item)
  212. +   public L2ItemInstance dropItem(L2PcInstance mainDamageDealer, RewardItem item)
  213.     {
  214.         int randDropLim = 70;
  215.        
  216. @@ -1790,12 +1800,13 @@
  217.             // Randomize drop position
  218.             int newX = getX() + Rnd.get(randDropLim * 2 + 1) - randDropLim;
  219.             int newY = getY() + Rnd.get(randDropLim * 2 + 1) - randDropLim;
  220. -           int newZ = Math.max(getZ(), lastAttacker.getZ()) + 20; // TODO: temp hack, do somethign nicer when we have geodatas
  221. +           int newZ = Math.max(getZ(), mainDamageDealer.getZ()) + 20; // TODO: temp hack, do somethign nicer when we have geodatas
  222.            
  223.             if (ItemTable.getInstance().getTemplate(item.getItemId()) != null)
  224.             {
  225.                 // Init the dropped L2ItemInstance and add it in the world as a visible object at the position where mob was last
  226. -               ditem = ItemTable.getInstance().createItem("Loot", item.getItemId(), item.getCount(), lastAttacker, this);
  227. +               ditem = ItemTable.getInstance().createItem("Loot", item.getItemId(), item.getCount(), mainDamageDealer, this);
  228. +               ditem.getDropProtection().protect(mainDamageDealer);
  229.                 ditem.dropMe(this, newX, newY, newZ);
  230.                
  231.                 // Add drop to auto destroy item task
  232. Index: java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java
  233. ===================================================================
  234. --- java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java    (revision 4578)
  235. +++ java/com/l2jserver/gameserver/model/actor/instance/L2PcInstance.java    (working copy)
  236. @@ -3454,7 +3459,7 @@
  237.            
  238.             // If over capacity, drop the item
  239.             if (!isGM() && !_inventory.validateCapacity(0, item.isQuestItem()) && newitem.isDropable() && (!newitem.isStackable() || newitem.getLastChange() != L2ItemInstance.MODIFIED))
  240. -               dropItem("InvDrop", newitem, null, true);
  241. +               dropItem("InvDrop", newitem, null, true, true);
  242.            
  243.             // Cursed Weapon
  244.             else if(CursedWeaponsManager.getInstance().isCursed(newitem.getItemId()))
  245. @@ -3859,9 +3864,10 @@
  246.      * @param item : L2ItemInstance to be dropped
  247.      * @param reference : L2Object Object referencing current action like NPC selling item or previous item in transformation
  248.      * @param sendMessage : boolean Specifies whether to send message to Client about this action
  249. +    * @param protectItem: whether or not dropped item must be protected temporary against other players
  250.      * @return boolean informing if the action was successfull
  251.      */
  252. -   public boolean dropItem(String process, L2ItemInstance item, L2Object reference, boolean sendMessage)
  253. +   public boolean dropItem(String process, L2ItemInstance item, L2Object reference, boolean sendMessage, boolean protectItem)
  254.     {
  255.         item = _inventory.dropItem(process, item, this, reference);
  256.        
  257. @@ -3875,19 +3881,27 @@
  258.        
  259.         item.dropMe(this, getX() + Rnd.get(50) - 25, getY() + Rnd.get(50) - 25, getZ() + 20);
  260.        
  261. -       if (Config.AUTODESTROY_ITEM_AFTER >0 && Config.DESTROY_DROPPED_PLAYER_ITEM && !Config.LIST_PROTECTED_ITEMS.contains(item.getItemId()))
  262. -       {
  263. -           if ( (item.isEquipable() && Config.DESTROY_EQUIPABLE_PLAYER_ITEM) || !item.isEquipable())
  264. +       if (Config.AUTODESTROY_ITEM_AFTER > 0
  265. +               && Config.DESTROY_DROPPED_PLAYER_ITEM
  266. +               && !Config.LIST_PROTECTED_ITEMS.contains(item.getItemId())) {
  267. +           if ((item.isEquipable() && Config.DESTROY_EQUIPABLE_PLAYER_ITEM)
  268. +                   || !item.isEquipable())
  269.                 ItemsAutoDestroy.getInstance().addItem(item);
  270.         }
  271. -       if (Config.DESTROY_DROPPED_PLAYER_ITEM){
  272. -           if (!item.isEquipable() || (item.isEquipable()  && Config.DESTROY_EQUIPABLE_PLAYER_ITEM ))
  273. +      
  274. +       // protection against auto destroy dropped item
  275. +       if (Config.DESTROY_DROPPED_PLAYER_ITEM) {
  276. +           if (!item.isEquipable()
  277. +                   || (item.isEquipable() && Config.DESTROY_EQUIPABLE_PLAYER_ITEM))
  278.                 item.setProtected(false);
  279.             else
  280.                 item.setProtected(true);
  281. -       }
  282. -       else
  283. +       } else
  284.             item.setProtected(true);
  285. +
  286. +       // retail drop protection
  287. +       if (protectItem)
  288. +           item.getDropProtection().protect(this);
  289.        
  290.         // Send inventory update packet
  291.         if (!Config.FORCE_INVENTORY_UPDATE)
  292. @@ -3914,6 +3928,11 @@
  293.         return true;
  294.     }
  295.    
  296. +   public boolean dropItem(String process, L2ItemInstance item, L2Object reference, boolean sendMessage)
  297. +   {
  298. +       return dropItem(process, item, reference, sendMessage, false);
  299. +   }
  300. +  
  301.     /**
  302.      * Drop item from inventory by using its <B>objectID</B> and send a Server->Client InventoryUpdate packet to the L2PcInstance.
  303.      * @param process : String Identifier of process triggering this action
  304. @@ -3926,7 +3945,7 @@
  305.      * @param sendMessage : boolean Specifies whether to send message to Client about this action
  306.      * @return L2ItemInstance corresponding to the new item or the updated item in inventory
  307.      */
  308. -   public L2ItemInstance dropItem(String process, int objectId, long count, int x, int y, int z, L2Object reference, boolean sendMessage)
  309. +   public L2ItemInstance dropItem(String process, int objectId, long count, int x, int y, int z, L2Object reference, boolean sendMessage, boolean protectItem)
  310.     {
  311.         L2ItemInstance invitem = _inventory.getItemByObjectId(objectId);
  312.         L2ItemInstance item = _inventory.dropItem(process, objectId, count, this, reference);
  313. @@ -3955,6 +3974,10 @@
  314.         else
  315.             item.setProtected(true);
  316.        
  317. +       // retail drop protection
  318. +       if (protectItem)
  319. +           item.getDropProtection().protect(this);
  320. +      
  321.         // Send inventory update packet
  322.         if (!Config.FORCE_INVENTORY_UPDATE)
  323.         {
  324. @@ -4652,6 +4676,15 @@
  325.                 sendPacket(ActionFailed.STATIC_PACKET);
  326.                 return;
  327.             }
  328. +
  329. +           if (!target.getDropProtection().tryPickUp(this))
  330. +           {
  331. +               sendPacket(ActionFailed.STATIC_PACKET);
  332. +               SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.FAILED_TO_PICKUP_S1);
  333. +               smsg.addItemName(target);
  334. +               sendPacket(smsg);
  335. +               return;
  336. +           }
  337.            
  338.             if ( ((isInParty() && getParty().getLootDistribution() == L2Party.ITEM_LOOTER) || !isInParty()) && !_inventory.validateCapacity(target))
  339.             {
  340. Index: java/com/l2jserver/gameserver/model/actor/instance/L2PetInstance.java
  341. ===================================================================
  342. --- java/com/l2jserver/gameserver/model/actor/instance/L2PetInstance.java   (revision 4578)
  343. +++ java/com/l2jserver/gameserver/model/actor/instance/L2PetInstance.java   (working copy)
  344. @@ -766,16 +766,23 @@
  345.         }
  346.     }
  347.    
  348. -   public void dropItemHere(L2ItemInstance dropit)
  349. +   public void dropItemHere(L2ItemInstance dropit, boolean protect)
  350.     {
  351.         dropit = getInventory().dropItem("Drop", dropit.getObjectId(), dropit.getCount(), getOwner(), this);
  352.        
  353.         if (dropit != null)
  354.         {
  355. +           if (protect)
  356. +               dropit.getDropProtection().protect(getOwner());
  357.             _logPet.finer("Item id to drop: "+dropit.getItemId()+" amount: "+dropit.getCount());
  358.             dropit.dropMe(this, getX(), getY(), getZ()+100);
  359.         }
  360.     }
  361. +  
  362. +   public void dropItemHere(L2ItemInstance dropit)
  363. +   {
  364. +       dropItemHere(dropit, false);
  365. +   }
  366.  
  367.     /** @return Returns the mount able. */
  368.     @Override
  369. Index: java/com/l2jserver/gameserver/network/clientpackets/RequestDropItem.java
  370. ===================================================================
  371. --- java/com/l2jserver/gameserver/network/clientpackets/RequestDropItem.java    (revision 4578)
  372. +++ java/com/l2jserver/gameserver/network/clientpackets/RequestDropItem.java    (working copy)
  373. @@ -200,7 +200,7 @@
  374.             activeChar.sendPacket(il);
  375.         }
  376.        
  377. -       L2ItemInstance dropedItem = activeChar.dropItem("Drop", _objectId, _count, _x, _y, _z, null, false);
  378. +       L2ItemInstance dropedItem = activeChar.dropItem("Drop", _objectId, _count, _x, _y, _z, null, false, true);
  379.        
  380.         if (Config.DEBUG)
  381.             _log.fine("dropping " + _objectId + " item(" + _count + ") at: " + _x + " " + _y + " " + _z);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement