DropProtection.java 2.4 KB

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