|
36 | 36 | import ch.njol.skript.util.Timespan;
|
37 | 37 | import ch.njol.util.Kleenean;
|
38 | 38 |
|
39 |
| -/** |
40 |
| - * @author Peter Güttinger |
41 |
| - */ |
42 | 39 | @Name("Potion Effects")
|
43 | 40 | @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", |
45 | 43 | "remove haste from the victim",
|
| 44 | + "", |
46 | 45 | "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 | +) |
50 | 57 | public class EffPotion extends Effect {
|
| 58 | + |
51 | 59 | static {
|
52 | 60 | 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 | + ); |
59 | 65 | }
|
60 | 66 |
|
| 67 | + private final static boolean COMPATIBLE = Skript.isRunningMinecraft(1, 19, 4); |
| 68 | + |
61 | 69 | private final static int DEFAULT_DURATION = 15 * 20; // 15 seconds, same as EffPoison
|
62 |
| - private boolean replaceExisting; |
63 | 70 |
|
64 |
| - @SuppressWarnings("null") |
65 | 71 | private Expression<PotionEffectType> potions;
|
66 |
| - @Nullable |
67 |
| - private Expression<Number> tier; |
68 |
| - @SuppressWarnings("null") |
69 | 72 | private Expression<LivingEntity> entities;
|
| 73 | + private Expression<PotionEffect> effects; |
| 74 | + |
70 | 75 | @Nullable
|
71 | 76 | 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. |
75 | 85 | 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; |
78 | 87 |
|
79 |
| - @SuppressWarnings({"unchecked", "null"}) |
80 | 88 | @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) { |
83 | 91 | 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; |
85 | 97 | if (potionEffect) {
|
86 |
| - potionEffects = (Expression<PotionEffect>) exprs[0]; |
| 98 | + effects = (Expression<PotionEffect>) exprs[0]; |
87 | 99 | entities = (Expression<LivingEntity>) exprs[1];
|
88 |
| - } else if (apply) { |
| 100 | + } else { |
89 | 101 | potions = (Expression<PotionEffectType>) exprs[0];
|
90 | 102 | tier = (Expression<Number>) exprs[1];
|
91 | 103 | 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]; |
112 | 106 | }
|
113 |
| - |
114 | 107 | return true;
|
115 | 108 | }
|
116 | 109 |
|
117 | 110 | @Override
|
118 |
| - protected void execute(final Event e) { |
| 111 | + protected void execute(Event event) { |
119 | 112 | 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)); |
124 | 115 | } 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) |
133 | 118 | 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) |
146 | 127 | 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); |
148 | 129 | }
|
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(); |
157 | 138 | break;
|
158 | 139 | }
|
159 | 140 | }
|
160 | 141 | }
|
161 | 142 | }
|
162 |
| - en.addPotionEffect(new PotionEffect(t, duration, a, ambient, particles), true); |
| 143 | + entity.addPotionEffect(new PotionEffect(potionEffectType, finalDuration, tier, ambient, !noParticles, icon)); |
163 | 144 | }
|
164 | 145 | }
|
165 | 146 | }
|
166 | 147 | }
|
167 | 148 |
|
168 | 149 | @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 | + } |
176 | 161 | }
|
177 | 162 |
|
178 | 163 | }
|
0 commit comments