Skip to content

Commit 49d5620

Browse files
🍒 [2.7] Add infinite and icon to potion effects (#5982)
Add infinite and icon to potion effects (#5777) Co-authored-by: LimeGlass <16087552+TheLimeGlass@users.noreply.github.com>
1 parent 276a191 commit 49d5620

File tree

3 files changed

+141
-110
lines changed

3 files changed

+141
-110
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* This file is part of Skript.
3+
*
4+
* Skript is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Skript is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
* Copyright Peter Güttinger, SkriptLang team and contributors
18+
*/
19+
package ch.njol.skript.conditions;
20+
21+
import org.bukkit.potion.PotionEffect;
22+
23+
import ch.njol.skript.Skript;
24+
import ch.njol.skript.conditions.base.PropertyCondition;
25+
import ch.njol.skript.doc.Description;
26+
import ch.njol.skript.doc.Examples;
27+
import ch.njol.skript.doc.Name;
28+
import ch.njol.skript.doc.Since;
29+
30+
// This class can be expanded apon for other types if needed.
31+
@Name("Is Infinite")
32+
@Description("Checks whether potion effects are infinite.")
33+
@Examples("all of the active potion effects of the player are infinite")
34+
@Since("INSERT VERSION")
35+
public class CondIsInfinite extends PropertyCondition<PotionEffect> {
36+
37+
static {
38+
if (Skript.methodExists(PotionEffect.class, "isInfinite"))
39+
register(CondIsInfinite.class, "infinite", "potioneffects");
40+
}
41+
42+
@Override
43+
public boolean check(PotionEffect potion) {
44+
return potion.isInfinite();
45+
}
46+
47+
@Override
48+
protected String getPropertyName() {
49+
return "infinite";
50+
}
51+
52+
}

src/main/java/ch/njol/skript/effects/EffPotion.java

Lines changed: 77 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -36,143 +36,128 @@
3636
import ch.njol.skript.util.Timespan;
3737
import ch.njol.util.Kleenean;
3838

39-
/**
40-
* @author Peter Güttinger
41-
*/
4239
@Name("Potion Effects")
4340
@Description("Apply or remove potion effects to/from entities.")
44-
@Examples({"apply swiftness 2 to the player",
41+
@Examples({
42+
"apply ambient swiftness 2 to the player",
4543
"remove haste from the victim",
44+
"",
4645
"on join:",
47-
"\tapply potion of strength of tier {strength.%player%} to the player for 999 days",
48-
"apply potion effects of player's tool to player"})
49-
@Since("2.0, 2.2-dev27 (ambient and particle-less potion effects), 2.5 (replacing existing effect), 2.5.2 (potion effects)")
46+
"\tapply infinite potion of strength of tier {strength::%uuid of player%} to the player",
47+
"\tapply potion of strength of tier {strength::%uuid of player%} to the player for 999 days # Before 1.19.4",
48+
"",
49+
"apply potion effects of player's tool to player",
50+
"apply haste potion of tier 3 without any particles to the player whilst hiding the potion icon # Hide potions"
51+
})
52+
@Since(
53+
"2.0, 2.2-dev27 (ambient and particle-less potion effects), " +
54+
"2.5 (replacing existing effect), 2.5.2 (potion effects), " +
55+
"INSERT VERSION (icon and infinite)"
56+
)
5057
public class EffPotion extends Effect {
58+
5159
static {
5260
Skript.registerEffect(EffPotion.class,
53-
"apply %potioneffects% to %livingentities%",
54-
"apply [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]",
55-
"apply ambient [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]",
56-
"apply [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] without [any] particles to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]"
57-
//, "apply %itemtypes% to %livingentities%"
58-
/*,"remove %potioneffecttypes% from %livingentities%"*/);
61+
"apply %potioneffects% to %livingentities%",
62+
"apply infinite [:ambient] [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] [noparticles:without [any] particles] [icon:(whilst hiding [the]|without (the|a)) [potion] icon] to %livingentities% [replacing:replacing [the] existing effect]",
63+
"apply [:ambient] [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] [noparticles:without [any] particles] [icon:(whilst hiding [the]|without (the|a)) [potion] icon] to %livingentities% [for %-timespan%] [replacing:replacing [the] existing effect]"
64+
);
5965
}
6066

67+
private final static boolean COMPATIBLE = Skript.isRunningMinecraft(1, 19, 4);
68+
6169
private final static int DEFAULT_DURATION = 15 * 20; // 15 seconds, same as EffPoison
62-
private boolean replaceExisting;
6370

64-
@SuppressWarnings("null")
6571
private Expression<PotionEffectType> potions;
66-
@Nullable
67-
private Expression<Number> tier;
68-
@SuppressWarnings("null")
6972
private Expression<LivingEntity> entities;
73+
private Expression<PotionEffect> effects;
74+
7075
@Nullable
7176
private Expression<Timespan> duration;
72-
@SuppressWarnings("null")
73-
private Expression<PotionEffect> potionEffects;
74-
private boolean apply;
77+
78+
@Nullable
79+
private Expression<Number> tier;
80+
81+
private boolean replaceExisting; // Replace the existing potion if present.
82+
private boolean potionEffect; // PotionEffects rather than PotionEffectTypes.
83+
private boolean noParticles;
84+
private boolean infinite; // 1.19.4+ has an infinite option.
7585
private boolean ambient; // Ambient means less particles
76-
private boolean particles; // Particles or no particles?
77-
private boolean potionEffect; // PotionEffects rather than PotionEffectTypes
86+
private boolean icon;
7887

79-
@SuppressWarnings({"unchecked", "null"})
8088
@Override
81-
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
82-
apply = matchedPattern > 0;
89+
@SuppressWarnings("unchecked")
90+
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
8391
potionEffect = matchedPattern == 0;
84-
replaceExisting = parseResult.mark == 1;
92+
replaceExisting = parseResult.hasTag("replacing");
93+
noParticles = parseResult.hasTag("noparticles");
94+
ambient = parseResult.hasTag("ambient");
95+
icon = !parseResult.hasTag("icon");
96+
infinite = matchedPattern == 1;
8597
if (potionEffect) {
86-
potionEffects = (Expression<PotionEffect>) exprs[0];
98+
effects = (Expression<PotionEffect>) exprs[0];
8799
entities = (Expression<LivingEntity>) exprs[1];
88-
} else if (apply) {
100+
} else {
89101
potions = (Expression<PotionEffectType>) exprs[0];
90102
tier = (Expression<Number>) exprs[1];
91103
entities = (Expression<LivingEntity>) exprs[2];
92-
duration = (Expression<Timespan>) exprs[3];
93-
} else {
94-
potions = (Expression<PotionEffectType>) exprs[0];
95-
entities = (Expression<LivingEntity>) exprs[1];
96-
}
97-
98-
// Ambience and particles
99-
switch (matchedPattern) {
100-
case 1:
101-
ambient = false;
102-
particles = true;
103-
break;
104-
case 2:
105-
ambient = true;
106-
particles = true;
107-
break;
108-
case 3:
109-
ambient = false;
110-
particles = false;
111-
break;
104+
if (infinite)
105+
duration = (Expression<Timespan>) exprs[3];
112106
}
113-
114107
return true;
115108
}
116109

117110
@Override
118-
protected void execute(final Event e) {
111+
protected void execute(Event event) {
119112
if (potionEffect) {
120-
for (LivingEntity livingEntity : entities.getArray(e)) {
121-
PotionEffect[] potionEffects = this.potionEffects.getArray(e);
122-
PotionEffectUtils.addEffects(livingEntity, potionEffects);
123-
}
113+
for (LivingEntity livingEntity : entities.getArray(event))
114+
PotionEffectUtils.addEffects(livingEntity, effects.getArray(event));
124115
} else {
125-
final PotionEffectType[] ts = potions.getArray(e);
126-
if (ts.length == 0)
127-
return;
128-
if (!apply) {
129-
for (LivingEntity en : entities.getArray(e)) {
130-
for (final PotionEffectType t : ts)
131-
en.removePotionEffect(t);
132-
}
116+
PotionEffectType[] potionEffectTypes = potions.getArray(event);
117+
if (potionEffectTypes.length == 0)
133118
return;
134-
}
135-
int a = 0;
136-
if (tier != null) {
137-
final Number amp = tier.getSingle(e);
138-
if (amp == null)
139-
return;
140-
a = amp.intValue() - 1;
141-
}
142-
int d = DEFAULT_DURATION;
143-
if (duration != null) {
144-
final Timespan dur = duration.getSingle(e);
145-
if (dur == null)
119+
int tier = 0;
120+
if (this.tier != null)
121+
tier = this.tier.getOptionalSingle(event).orElse(1).intValue() - 1;
122+
123+
int duration = infinite ? (COMPATIBLE ? -1 : Integer.MAX_VALUE) : DEFAULT_DURATION;
124+
if (this.duration != null && !infinite) {
125+
Timespan timespan = this.duration.getSingle(event);
126+
if (timespan == null)
146127
return;
147-
d = (int) (dur.getTicks_i() >= Integer.MAX_VALUE ? Integer.MAX_VALUE : dur.getTicks_i());
128+
duration = (int) Math.max(timespan.getTicks_i(), Integer.MAX_VALUE);
148129
}
149-
for (final LivingEntity en : entities.getArray(e)) {
150-
for (final PotionEffectType t : ts) {
151-
int duration = d;
152-
if (!replaceExisting) {
153-
if (en.hasPotionEffect(t)) {
154-
for (final PotionEffect eff : en.getActivePotionEffects()) {
155-
if (eff.getType() == t) {
156-
duration += eff.getDuration();
130+
for (LivingEntity entity : entities.getArray(event)) {
131+
for (PotionEffectType potionEffectType : potionEffectTypes) {
132+
int finalDuration = duration;
133+
if (!replaceExisting && !infinite) {
134+
if (entity.hasPotionEffect(potionEffectType)) {
135+
for (PotionEffect effect : entity.getActivePotionEffects()) {
136+
if (effect.getType() == potionEffectType) {
137+
finalDuration += effect.getDuration();
157138
break;
158139
}
159140
}
160141
}
161142
}
162-
en.addPotionEffect(new PotionEffect(t, duration, a, ambient, particles), true);
143+
entity.addPotionEffect(new PotionEffect(potionEffectType, finalDuration, tier, ambient, !noParticles, icon));
163144
}
164145
}
165146
}
166147
}
167148

168149
@Override
169-
public String toString(final @Nullable Event e, final boolean debug) {
170-
if (potionEffect)
171-
return "apply " + potionEffects.toString(e, debug) + " to " + entities.toString(e, debug);
172-
else if (apply)
173-
return "apply " + potions.toString(e, debug) + (tier != null ? " of tier " + tier.toString(e, debug) : "") + " to " + entities.toString(e, debug) + (duration != null ? " for " + duration.toString(e, debug) : "");
174-
else
175-
return "remove " + potions.toString(e, debug) + " from " + entities.toString(e, debug);
150+
public String toString(@Nullable Event event, boolean debug) {
151+
if (potionEffect) {
152+
// Uses PotionEffectUtils#toString
153+
return "apply " + effects.toString(event, debug) + " to " + entities.toString(event, debug);
154+
} else {
155+
return "apply " + (infinite ? " infinite " : "") +
156+
potions.toString(event, debug) +
157+
(tier != null ? " of tier " + tier.toString(event, debug) : "") +
158+
" to " + entities.toString(event, debug) +
159+
(duration != null ? " for " + duration.toString(event, debug) : "");
160+
}
176161
}
177162

178163
}

src/main/java/ch/njol/skript/util/PotionEffectUtils.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package ch.njol.skript.util;
2020

2121
import java.util.ArrayList;
22-
import java.util.Arrays;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Locale;
@@ -41,20 +40,17 @@
4140
import ch.njol.skript.localization.Language;
4241
import ch.njol.skript.localization.LanguageChangeListener;
4342

44-
/**
45-
* @author Peter Güttinger
46-
*/
4743
@SuppressWarnings("deprecation")
4844
public abstract class PotionEffectUtils {
49-
45+
5046
private static final boolean HAS_SUSPICIOUS_META = Skript.classExists("org.bukkit.inventory.meta.SuspiciousStewMeta");
51-
47+
5248
private PotionEffectUtils() {}
53-
49+
5450
final static Map<String, PotionEffectType> types = new HashMap<>();
55-
51+
5652
final static String[] names = new String[getMaxPotionId() + 1];
57-
53+
5854
// MCPC+ workaround
5955
private static int getMaxPotionId() {
6056
int i = 0;
@@ -99,32 +95,30 @@ public static PotionEffectType parseByEffectType(PotionEffectType t) {
9995
return null;
10096
}
10197

102-
@SuppressWarnings("null")
103-
public static String toString(final PotionEffectType t) {
98+
public static String toString(PotionEffectType t) {
10499
return names[t.getId()];
105100
}
106101

107102
// REMIND flags?
108-
@SuppressWarnings("null")
109-
public static String toString(final PotionEffectType t, final int flags) {
103+
public static String toString(PotionEffectType t, int flags) {
110104
return names[t.getId()];
111105
}
112-
106+
113107
public static String toString(PotionEffect potionEffect) {
114108
StringBuilder builder = new StringBuilder();
115109
if (potionEffect.isAmbient())
116110
builder.append("ambient ");
117111
builder.append("potion effect of ");
118112
builder.append(toString(potionEffect.getType()));
119-
120113
builder.append(" of tier ").append(potionEffect.getAmplifier() + 1);
121-
122114
if (!potionEffect.hasParticles())
123115
builder.append(" without particles");
124-
builder.append(" for ").append(Timespan.fromTicks_i(potionEffect.getDuration()));
116+
builder.append(" for ").append(potionEffect.getDuration() == -1 ? "infinity" : Timespan.fromTicks_i(Math.abs(potionEffect.getDuration())));
117+
if (!potionEffect.hasIcon())
118+
builder.append(" without icon");
125119
return builder.toString();
126120
}
127-
121+
128122
public static String[] getNames() {
129123
return names;
130124
}

0 commit comments

Comments
 (0)