DropProtection.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model;
  20. import java.util.concurrent.ScheduledFuture;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
  24. /**
  25. * @author DrHouse
  26. */
  27. public class DropProtection implements Runnable
  28. {
  29. private volatile boolean _isProtected = false;
  30. private L2PcInstance _owner = null;
  31. private ScheduledFuture<?> _task = null;
  32. private static final long PROTECTED_MILLIS_TIME = 15000;
  33. @Override
  34. public synchronized void run()
  35. {
  36. _isProtected = false;
  37. _owner = null;
  38. _task = null;
  39. }
  40. public boolean isProtected()
  41. {
  42. return _isProtected;
  43. }
  44. public L2PcInstance getOwner()
  45. {
  46. return _owner;
  47. }
  48. public synchronized boolean tryPickUp(L2PcInstance actor)
  49. {
  50. if (!_isProtected)
  51. {
  52. return true;
  53. }
  54. if (_owner == actor)
  55. {
  56. return true;
  57. }
  58. if ((_owner.getParty() != null) && (_owner.getParty() == actor.getParty()))
  59. {
  60. return true;
  61. }
  62. /*
  63. * if (_owner.getClan() != null && _owner.getClan() == actor.getClan()) return true;
  64. */
  65. return false;
  66. }
  67. public boolean tryPickUp(L2PetInstance pet)
  68. {
  69. return tryPickUp(pet.getOwner());
  70. }
  71. public synchronized void unprotect()
  72. {
  73. if (_task != null)
  74. {
  75. _task.cancel(false);
  76. }
  77. _isProtected = false;
  78. _owner = null;
  79. _task = null;
  80. }
  81. public synchronized void protect(L2PcInstance player)
  82. {
  83. unprotect();
  84. _isProtected = true;
  85. if ((_owner = player) == null)
  86. {
  87. throw new NullPointerException("Trying to protect dropped item to null owner");
  88. }
  89. _task = ThreadPoolManager.getInstance().scheduleGeneral(this, PROTECTED_MILLIS_TIME);
  90. }
  91. }