DropProtection.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. @Override
  32. public synchronized void run()
  33. {
  34. _isProtected = false;
  35. _owner = null;
  36. _task = null;
  37. }
  38. public boolean isProtected()
  39. {
  40. return _isProtected;
  41. }
  42. public L2PcInstance getOwner()
  43. {
  44. return _owner;
  45. }
  46. public synchronized boolean tryPickUp(L2PcInstance actor)
  47. {
  48. if (!_isProtected)
  49. return true;
  50. if (_owner == actor)
  51. return true;
  52. if (_owner.getParty() != null && _owner.getParty() == actor.getParty())
  53. return true;
  54. /*if (_owner.getClan() != null && _owner.getClan() == actor.getClan())
  55. return true;*/
  56. return false;
  57. }
  58. public boolean tryPickUp(L2PetInstance pet)
  59. {
  60. return tryPickUp(pet.getOwner());
  61. }
  62. public synchronized void unprotect()
  63. {
  64. if (_task != null)
  65. _task.cancel(false);
  66. _isProtected = false;
  67. _owner = null;
  68. _task = null;
  69. }
  70. public synchronized void protect(L2PcInstance player)
  71. {
  72. unprotect();
  73. _isProtected = true;
  74. if ((_owner = player) == null)
  75. throw new NullPointerException("Trying to protect dropped item to null owner");
  76. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, PROTECTED_MILLIS_TIME);
  77. }
  78. }