Commits

Parker Hawke authored and md_5 committed 3741079b38c
#824: Expand upon PotionEffect API to better accommodate infinite durations
No tags

src/main/java/org/bukkit/potion/PotionEffect.java

Modified
13 13 import org.jetbrains.annotations.Nullable;
14 14
15 15 /**
16 16 * Represents a potion effect, that can be added to a {@link LivingEntity}. A
17 17 * potion effect has a duration that it will last for, an amplifier that will
18 18 * enhance its effects, and a {@link PotionEffectType}, that represents its
19 19 * effect on an entity.
20 20 */
21 21 @SerializableAs("PotionEffect")
22 22 public class PotionEffect implements ConfigurationSerializable {
23 + /**
24 + * A constant denoting infinite potion duration.
25 + */
26 + public static final int INFINITE_DURATION = -1;
27 +
23 28 private static final String AMPLIFIER = "amplifier";
24 29 private static final String DURATION = "duration";
25 30 private static final String TYPE = "effect";
26 31 private static final String AMBIENT = "ambient";
27 32 private static final String PARTICLES = "has-particles";
28 33 private static final String ICON = "has-icon";
29 34 private final int amplifier;
30 35 private final int duration;
31 36 private final PotionEffectType type;
32 37 private final boolean ambient;
172 177 * @return The effect amplifier
173 178 */
174 179 public int getAmplifier() {
175 180 return amplifier;
176 181 }
177 182
178 183 /**
179 184 * Returns the duration (in ticks) that this effect will run for when
180 185 * applied to a {@link LivingEntity}.
181 186 *
182 - * @return The duration of the effect
187 + * @return The duration of the effect, or {@value #INFINITE_DURATION} if
188 + * this effect is infinite
189 + * @see #isInfinite()
183 190 */
184 191 public int getDuration() {
185 192 return duration;
186 193 }
187 194
195 + /**
196 + * Returns whether or not this potion effect has an infinite duration. Potion
197 + * effects with infinite durations will display an infinite symbol and never
198 + * expire unless manually removed.
199 + *
200 + * @return whether this duration is infinite or not
201 + */
202 + public boolean isInfinite() {
203 + return duration == INFINITE_DURATION;
204 + }
205 +
206 + /**
207 + * Returns whether or not this potion effect has a shorter duration than the
208 + * provided potion effect.
209 + * <p>
210 + * An infinite duration is considered longer than non-infinite durations. If
211 + * both potion effects have infinite durations, then neither is shorter than
212 + * the other and this method will return false.
213 + *
214 + * @param other the other effect
215 + * @return true if this effect is shorter than the other, false if longer or equal
216 + */
217 + public boolean isShorterThan(@NotNull PotionEffect other) {
218 + return !isInfinite() && (duration < other.duration || other.isInfinite());
219 + }
220 +
188 221 /**
189 222 * Returns the {@link PotionEffectType} of this effect.
190 223 *
191 224 * @return The potion type of this effect
192 225 */
193 226 @NotNull
194 227 public PotionEffectType getType() {
195 228 return type;
196 229 }
197 230

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut