DropProtection.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 java.util.concurrent.ScheduledFuture;
  17. import com.l2jserver.gameserver.ThreadPoolManager;
  18. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  19. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  20. /**
  21. *
  22. * @author DrHouse
  23. *
  24. */
  25. public class DropProtection implements Runnable
  26. {
  27. private volatile boolean _isProtected = false;
  28. private L2PcInstance _owner = null;
  29. private ScheduledFuture<?> _task = null;
  30. private static final long PROTECTED_MILLIS_TIME = 15000;
  31. public synchronized void run()
  32. {
  33. _isProtected = false;
  34. _owner = null;
  35. _task = null;
  36. }
  37. public boolean isProtected()
  38. {
  39. return _isProtected;
  40. }
  41. public L2PcInstance getOwner()
  42. {
  43. return _owner;
  44. }
  45. public synchronized boolean tryPickUp(L2PcInstance actor)
  46. {
  47. if (!_isProtected)
  48. return true;
  49. if (_owner == actor)
  50. return true;
  51. if (_owner.getParty() != null && _owner.getParty() == actor.getParty())
  52. return true;
  53. /*if (_owner.getClan() != null && _owner.getClan() == actor.getClan())
  54. return true;*/
  55. return false;
  56. }
  57. public boolean tryPickUp(L2PetInstance pet)
  58. {
  59. return tryPickUp(pet.getOwner());
  60. }
  61. public synchronized void unprotect()
  62. {
  63. if (_task != null)
  64. _task.cancel(false);
  65. _isProtected = false;
  66. _owner = null;
  67. _task = null;
  68. }
  69. public synchronized void protect(L2PcInstance player)
  70. {
  71. unprotect();
  72. _isProtected = true;
  73. if ((_owner = player) == null)
  74. throw new NullPointerException("Trying to protect dropped item to null owner");
  75. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, PROTECTED_MILLIS_TIME);
  76. }
  77. }