Condition.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2004-2015 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.conditions;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.items.L2Item;
  22. import com.l2jserver.gameserver.model.skills.Skill;
  23. /**
  24. * The Class Condition.
  25. * @author mkizub
  26. */
  27. public abstract class Condition implements ConditionListener
  28. {
  29. private ConditionListener _listener;
  30. private String _msg;
  31. private int _msgId;
  32. private boolean _addName = false;
  33. private boolean _result;
  34. /**
  35. * Sets the message.
  36. * @param msg the new message
  37. */
  38. public final void setMessage(String msg)
  39. {
  40. _msg = msg;
  41. }
  42. /**
  43. * Gets the message.
  44. * @return the message
  45. */
  46. public final String getMessage()
  47. {
  48. return _msg;
  49. }
  50. /**
  51. * Sets the message id.
  52. * @param msgId the new message id
  53. */
  54. public final void setMessageId(int msgId)
  55. {
  56. _msgId = msgId;
  57. }
  58. /**
  59. * Gets the message id.
  60. * @return the message id
  61. */
  62. public final int getMessageId()
  63. {
  64. return _msgId;
  65. }
  66. /**
  67. * Adds the name.
  68. */
  69. public final void addName()
  70. {
  71. _addName = true;
  72. }
  73. /**
  74. * Checks if is adds the name.
  75. * @return true, if is adds the name
  76. */
  77. public final boolean isAddName()
  78. {
  79. return _addName;
  80. }
  81. /**
  82. * Sets the listener.
  83. * @param listener the new listener
  84. */
  85. void setListener(ConditionListener listener)
  86. {
  87. _listener = listener;
  88. notifyChanged();
  89. }
  90. /**
  91. * Gets the listener.
  92. * @return the listener
  93. */
  94. final ConditionListener getListener()
  95. {
  96. return _listener;
  97. }
  98. public final boolean test(L2Character caster, L2Character target, Skill skill)
  99. {
  100. return test(caster, target, skill, null);
  101. }
  102. public final boolean test(L2Character caster, L2Character target, L2Item item)
  103. {
  104. return test(caster, target, null, null);
  105. }
  106. public final boolean test(L2Character caster, L2Character target, Skill skill, L2Item item)
  107. {
  108. boolean res = testImpl(caster, target, skill, item);
  109. if ((_listener != null) && (res != _result))
  110. {
  111. _result = res;
  112. notifyChanged();
  113. }
  114. return res;
  115. }
  116. /**
  117. * Test the condition.
  118. * @param effector the effector
  119. * @param effected the effected
  120. * @param skill the skill
  121. * @param item the item
  122. * @return {@code true} if successful, {@code false} otherwise
  123. */
  124. public abstract boolean testImpl(L2Character effector, L2Character effected, Skill skill, L2Item item);
  125. @Override
  126. public void notifyChanged()
  127. {
  128. if (_listener != null)
  129. {
  130. _listener.notifyChanged();
  131. }
  132. }
  133. }