Skip to content

Commit

Permalink
Add infinite and icon to potions effect
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass committed Jun 28, 2023
1 parent 011c91e commit fdeda36
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 103 deletions.
51 changes: 51 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsInfinite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.conditions;

import org.bukkit.potion.PotionEffect;

import ch.njol.skript.Skript;
import ch.njol.skript.conditions.base.PropertyCondition;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;

@Name("Is Infinite")
@Description("Checks whether potion effects are infinite.") // Can be expanded apon.
@Examples("all of the active potion effects of the player are infinite")
@Since("INSERT VERSION")
public class CondIsInfinite extends PropertyCondition<PotionEffect> {

static {
if (Skript.methodExists(PotionEffect.class, "isInfinite"))
register(CondIsInfinite.class, "infinite", "potioneffects");
}

@Override
public boolean check(PotionEffect potion) {
return potion.isInfinite();
}

@Override
protected String getPropertyName() {
return "infinite";
}

}
166 changes: 76 additions & 90 deletions src/main/java/ch/njol/skript/effects/EffPotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,143 +36,129 @@
import ch.njol.skript.util.Timespan;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Potion Effects")
@Description("Apply or remove potion effects to/from entities.")
@Examples({"apply swiftness 2 to the player",
@Examples({
"apply ambient swiftness 2 to the player",
"remove haste from the victim",
"",
"on join:",
"\tapply potion of strength of tier {strength.%player%} to the player for 999 days",
"apply potion effects of player's tool to player"})
@Since("2.0, 2.2-dev27 (ambient and particle-less potion effects), 2.5 (replacing existing effect), 2.5.2 (potion effects)")
"\tapply infinite potion of strength of tier {strength::%uuid of player%} to the player",
"\tapply potion of strength of tier {strength::%uuid of player%} to the player for 999 days # Before 1.19.4",
"",
"apply potion effects of player's tool to player",
"apply haste potion of tier 3 without any particles to the player whilst hiding the potion icon # Hide potions"
})
@Since(
"2.0, 2.2-dev27 (ambient and particle-less potion effects), " +
"2.5 (replacing existing effect), 2.5.2 (potion effects), " +
"INSERT VERSION (icon and infinite)"
)
public class EffPotion extends Effect {

static {
Skript.registerEffect(EffPotion.class,
"apply %potioneffects% to %livingentities%",
"apply [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]",
"apply ambient [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]",
"apply [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] without [any] particles to %livingentities% [for %-timespan%] [(1¦replacing [the] existing effect)]"
//, "apply %itemtypes% to %livingentities%"
/*,"remove %potioneffecttypes% from %livingentities%"*/);
"apply %potioneffects% to %livingentities%",
"apply infinit(e|y) [:ambient] [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] [particles:without [any] particles] to %livingentities% [replacing:replacing [the] existing effect] [icon:hide [the] [potion] icon]",
"apply [:ambient] [potion of] %potioneffecttypes% [potion] [[[of] tier] %-number%] [particles:without [any] particles] to %livingentities% [for %-timespan%] [replacing:replacing [the] existing effect] [icon:[whilst] hid(e|ing) [the] [potion] icon]"
);
}

private final static int DEFAULT_DURATION = 15 * 20; // 15 seconds, same as EffPoison
private boolean replaceExisting;

@SuppressWarnings("null")
private Expression<PotionEffectType> potions;
@Nullable
private Expression<Number> tier;
@SuppressWarnings("null")
private Expression<LivingEntity> entities;
private Expression<PotionEffect> effects;

@Nullable
private Expression<Timespan> duration;
@SuppressWarnings("null")
private Expression<PotionEffect> potionEffects;
private boolean apply;

@Nullable
private Expression<Number> tier;

private boolean replaceExisting; // Replace the existing potion if present.
private boolean potionEffect; // PotionEffects rather than PotionEffectTypes.
private boolean particles;
private boolean infinite; // 1.19.4+ has an infinite option.
private boolean ambient; // Ambient means less particles
private boolean particles; // Particles or no particles?
private boolean potionEffect; // PotionEffects rather than PotionEffectTypes
private boolean icon;

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
apply = matchedPattern > 0;
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
potionEffect = matchedPattern == 0;
replaceExisting = parseResult.mark == 1;
replaceExisting = parseResult.hasTag("replacing");
particles = !parseResult.hasTag("particles");
ambient = parseResult.hasTag("ambient");
icon = !parseResult.hasTag("icon");
infinite = matchedPattern == 1;
if (potionEffect) {
potionEffects = (Expression<PotionEffect>) exprs[0];
effects = (Expression<PotionEffect>) exprs[0];
entities = (Expression<LivingEntity>) exprs[1];
} else if (apply) {
} else if (infinite) {
potions = (Expression<PotionEffectType>) exprs[0];
tier = (Expression<Number>) exprs[1];
entities = (Expression<LivingEntity>) exprs[2];
duration = (Expression<Timespan>) exprs[3];
} else {
potions = (Expression<PotionEffectType>) exprs[0];
entities = (Expression<LivingEntity>) exprs[1];
}

// Ambience and particles
switch (matchedPattern) {
case 1:
ambient = false;
particles = true;
break;
case 2:
ambient = true;
particles = true;
break;
case 3:
ambient = false;
particles = false;
break;
tier = (Expression<Number>) exprs[1];
entities = (Expression<LivingEntity>) exprs[2];
duration = (Expression<Timespan>) exprs[3];
}

return true;
}

@Override
protected void execute(final Event e) {
protected void execute(Event event) {
if (potionEffect) {
for (LivingEntity livingEntity : entities.getArray(e)) {
PotionEffect[] potionEffects = this.potionEffects.getArray(e);
PotionEffectUtils.addEffects(livingEntity, potionEffects);
}
for (LivingEntity livingEntity : entities.getArray(event))
PotionEffectUtils.addEffects(livingEntity, effects.getArray(event));
} else {
final PotionEffectType[] ts = potions.getArray(e);
if (ts.length == 0)
return;
if (!apply) {
for (LivingEntity en : entities.getArray(e)) {
for (final PotionEffectType t : ts)
en.removePotionEffect(t);
}
PotionEffectType[] potionEffectTypes = potions.getArray(event);
if (potionEffectTypes.length == 0)
return;
}
int a = 0;
if (tier != null) {
final Number amp = tier.getSingle(e);
if (amp == null)
return;
a = amp.intValue() - 1;
}
int d = DEFAULT_DURATION;
if (duration != null) {
final Timespan dur = duration.getSingle(e);
if (dur == null)
int tier = 0;
if (this.tier != null)
tier = this.tier.getOptionalSingle(event).orElse(1).intValue() - 1;

int duration = infinite ? (Skript.isRunningMinecraft(1, 19, 4) ? -1 : Integer.MAX_VALUE) : DEFAULT_DURATION;
if (this.duration != null && !infinite) {
Timespan timespan = this.duration.getSingle(event);
if (timespan == null)
return;
d = (int) (dur.getTicks_i() >= Integer.MAX_VALUE ? Integer.MAX_VALUE : dur.getTicks_i());
duration = (int) Math.max(timespan.getTicks_i(), Integer.MAX_VALUE);
}
for (final LivingEntity en : entities.getArray(e)) {
for (final PotionEffectType t : ts) {
int duration = d;
if (!replaceExisting) {
if (en.hasPotionEffect(t)) {
for (final PotionEffect eff : en.getActivePotionEffects()) {
if (eff.getType() == t) {
duration += eff.getDuration();
for (LivingEntity entity : entities.getArray(event)) {
for (PotionEffectType potionEffectType : potionEffectTypes) {
int finalDuration = duration;
if (!replaceExisting && !infinite) {
if (entity.hasPotionEffect(potionEffectType)) {
for (PotionEffect effect : entity.getActivePotionEffects()) {
if (effect.getType() == potionEffectType) {
finalDuration += effect.getDuration();
break;
}
}
}
}
en.addPotionEffect(new PotionEffect(t, duration, a, ambient, particles), true);
entity.addPotionEffect(new PotionEffect(potionEffectType, finalDuration, tier, ambient, particles, icon));
}
}
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
if (potionEffect)
return "apply " + potionEffects.toString(e, debug) + " to " + entities.toString(e, debug);
else if (apply)
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) : "");
else
return "remove " + potions.toString(e, debug) + " from " + entities.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
if (potionEffect) {
// Uses PotionEffectUtils#toString
return "apply " + effects.toString(event, debug) + " to " + entities.toString(event, debug);
} else {
return "apply " + (infinite ? " infinite " : "") +
potions.toString(event, debug) +
(tier != null ? " of tier " + tier.toString(event, debug) : "") +
" to " + entities.toString(event, debug) +
(duration != null ? " for " + duration.toString(event, debug) : "");
}
}

}
20 changes: 7 additions & 13 deletions src/main/java/ch/njol/skript/util/PotionEffectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ch.njol.skript.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -41,9 +40,6 @@
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.LanguageChangeListener;

/**
* @author Peter Güttinger
*/
@SuppressWarnings("deprecation")
public abstract class PotionEffectUtils {

Expand Down Expand Up @@ -99,32 +95,30 @@ public static PotionEffectType parseByEffectType(PotionEffectType t) {
return null;
}

@SuppressWarnings("null")
public static String toString(final PotionEffectType t) {
public static String toString(PotionEffectType t) {
return names[t.getId()];
}

// REMIND flags?
@SuppressWarnings("null")
public static String toString(final PotionEffectType t, final int flags) {
public static String toString(PotionEffectType t, int flags) {
return names[t.getId()];
}

public static String toString(PotionEffect potionEffect) {
StringBuilder builder = new StringBuilder();
if (potionEffect.isAmbient())
builder.append("ambient ");
builder.append("potion effect of ");
builder.append(toString(potionEffect.getType()));

builder.append(" of tier ").append(potionEffect.getAmplifier() + 1);

if (!potionEffect.hasParticles())
builder.append(" without particles");
builder.append(" for ").append(Timespan.fromTicks_i(potionEffect.getDuration()));
builder.append(" for ").append(potionEffect.isInfinite() ? "infinity" : Timespan.fromTicks_i(Math.abs(potionEffect.getDuration())));
if (!potionEffect.hasIcon())
builder.append(" without icon");
return builder.toString();
}

public static String[] getNames() {
return names;
}
Expand Down

0 comments on commit fdeda36

Please sign in to comment.