From dbbbd7dcd76ad368f252d2949bcf35f40c929a35 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Mon, 25 Nov 2024 22:23:34 +0100 Subject: [PATCH] Add potion effect improvements for modern Signed-off-by: Pablo Herrera --- core/src/main/java/tc/oc/pgm/kits/PotionKit.java | 11 ++++++++--- .../main/java/tc/oc/pgm/util/platform/Platform.java | 8 ++++++++ .../java/tc/oc/pgm/util/tablist/TablistResizer.java | 4 +--- util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java | 7 ++++++- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/kits/PotionKit.java b/core/src/main/java/tc/oc/pgm/kits/PotionKit.java index 6047cf1438..88795638fc 100644 --- a/core/src/main/java/tc/oc/pgm/kits/PotionKit.java +++ b/core/src/main/java/tc/oc/pgm/kits/PotionKit.java @@ -17,16 +17,21 @@ public PotionKit(Set effects) { @Override public void applyPostEvent(MatchPlayer player, boolean force, List displacedItems) { + var pl = player.getBukkit(); if (force) { for (PotionEffect effect : this.effects) { - player.getBukkit().addPotionEffect(effect, true); + // Forced potion eff with duration = 0 is used to remove effects, however in modern versions + // due to allowing multiple of the same effect, they aren't removed. + // This makes the behavior explicit that forcing an effect with duration 0 removes it. + if (effect.getDuration() != 0) pl.addPotionEffect(effect, true); + else pl.removePotionEffect(effect.getType()); } } else { - player.getBukkit().addPotionEffects(this.effects); + pl.addPotionEffects(this.effects); } // No swirls by default, KitNode can re-enable them if it so desires - PLAYER_UTILS.setPotionParticles(player.getBukkit(), false); + PLAYER_UTILS.setPotionParticles(pl, false); } @Override diff --git a/util/src/main/java/tc/oc/pgm/util/platform/Platform.java b/util/src/main/java/tc/oc/pgm/util/platform/Platform.java index b37abda6d8..529ccf37b8 100644 --- a/util/src/main/java/tc/oc/pgm/util/platform/Platform.java +++ b/util/src/main/java/tc/oc/pgm/util/platform/Platform.java @@ -52,6 +52,14 @@ public static void init() throws Throwable { return (T) Platform.getBestSupported(clazz); } + public static boolean isLegacy() { + return VARIANT == Variant.SPORTPAPER; + } + + public static boolean isModern() { + return VARIANT == Variant.PAPER; + } + private static Iterable> getSupported(Class parent) { return REFLECTIONS.get(TypesAnnotated.with(Supports.class, Supports.List.class) .asClass() diff --git a/util/src/main/java/tc/oc/pgm/util/tablist/TablistResizer.java b/util/src/main/java/tc/oc/pgm/util/tablist/TablistResizer.java index c5eb891922..dd5b466049 100644 --- a/util/src/main/java/tc/oc/pgm/util/tablist/TablistResizer.java +++ b/util/src/main/java/tc/oc/pgm/util/tablist/TablistResizer.java @@ -1,7 +1,5 @@ package tc.oc.pgm.util.tablist; -import static tc.oc.pgm.util.platform.Supports.Variant.SPORTPAPER; - import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.ListenerPriority; @@ -13,7 +11,7 @@ public class TablistResizer { private static final int TAB_SIZE = 80; // In 1.20.6 the field to edit is 1, unsure what version exactly broke it - private static final int FIELD = Platform.VARIANT == SPORTPAPER ? 2 : 1; + private static final int FIELD = Platform.isLegacy() ? 2 : 1; public static void registerAdapter(Plugin plugin) { ProtocolLibrary.getProtocolManager().addPacketListener(new TablistResizePacketAdapter(plugin)); diff --git a/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java b/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java index 2ad7c34b27..0b1bbe8b15 100644 --- a/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java +++ b/util/src/main/java/tc/oc/pgm/util/xml/XMLUtils.java @@ -39,6 +39,7 @@ import tc.oc.pgm.util.material.matcher.AllMaterialMatcher; import tc.oc.pgm.util.material.matcher.BlockMaterialMatcher; import tc.oc.pgm.util.math.OffsetVector; +import tc.oc.pgm.util.platform.Platform; import tc.oc.pgm.util.range.Ranges; import tc.oc.pgm.util.skin.Skin; import tc.oc.pgm.util.text.TextException; @@ -833,7 +834,11 @@ public static PotionEffectType parsePotionEffectType(Node node, String text) private static PotionEffect createPotionEffect( PotionEffectType type, Duration duration, int amplifier, boolean ambient) { - return new PotionEffect(type, (int) TimeUtils.toTicks(duration), amplifier, ambient); + // Modern supports infinite durations with value -1 + int ticks = Platform.isModern() && TimeUtils.isInfinite(duration) + ? -1 + : (int) TimeUtils.toTicks(duration); + return new PotionEffect(type, ticks, amplifier, ambient); } public static PotionEffect parsePotionEffect(Element el) throws InvalidXMLException {