From fdeda365732de46e78354ae1f562996e9811d703 Mon Sep 17 00:00:00 2001 From: TheLimeGlass Date: Wed, 28 Jun 2023 07:19:19 -0600 Subject: [PATCH] Add infinite and icon to potions effect --- .../skript/conditions/CondIsInfinite.java | 51 ++++++ .../ch/njol/skript/effects/EffPotion.java | 166 ++++++++---------- .../njol/skript/util/PotionEffectUtils.java | 20 +-- 3 files changed, 134 insertions(+), 103 deletions(-) create mode 100644 src/main/java/ch/njol/skript/conditions/CondIsInfinite.java diff --git a/src/main/java/ch/njol/skript/conditions/CondIsInfinite.java b/src/main/java/ch/njol/skript/conditions/CondIsInfinite.java new file mode 100644 index 00000000000..f9031321123 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsInfinite.java @@ -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 . + * + * 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 { + + 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"; + } + +} diff --git a/src/main/java/ch/njol/skript/effects/EffPotion.java b/src/main/java/ch/njol/skript/effects/EffPotion.java index 09c7af5912a..91092bef53c 100644 --- a/src/main/java/ch/njol/skript/effects/EffPotion.java +++ b/src/main/java/ch/njol/skript/effects/EffPotion.java @@ -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 potions; - @Nullable - private Expression tier; - @SuppressWarnings("null") private Expression entities; + private Expression effects; + @Nullable private Expression duration; - @SuppressWarnings("null") - private Expression potionEffects; - private boolean apply; + + @Nullable + private Expression 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) exprs[0]; + effects = (Expression) exprs[0]; entities = (Expression) exprs[1]; - } else if (apply) { + } else if (infinite) { potions = (Expression) exprs[0]; tier = (Expression) exprs[1]; entities = (Expression) exprs[2]; - duration = (Expression) exprs[3]; } else { potions = (Expression) exprs[0]; - entities = (Expression) 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) exprs[1]; + entities = (Expression) exprs[2]; + duration = (Expression) 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) : ""); + } } } diff --git a/src/main/java/ch/njol/skript/util/PotionEffectUtils.java b/src/main/java/ch/njol/skript/util/PotionEffectUtils.java index 2e6c9037863..e23b307242f 100644 --- a/src/main/java/ch/njol/skript/util/PotionEffectUtils.java +++ b/src/main/java/ch/njol/skript/util/PotionEffectUtils.java @@ -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; @@ -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 { @@ -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; }