From c4adec30824d48bc3241f29b78903a2f0d4e4914 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 29 May 2024 00:08:40 +0100 Subject: [PATCH] refactor: make more resilient against invalid effect types Spigot has a potion effect API lookup mismatch bug (SPIGOT-7674) due to the deprecated methods we use to support 1.17 --- .../william278/husksync/data/BukkitData.java | 29 +++++++++---------- .../husksync/util/BukkitKeyedAdapter.java | 6 ++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java index 07f05bb6..568a30e6 100644 --- a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java +++ b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java @@ -237,21 +237,20 @@ public abstract class BukkitData implements Data { @NotNull public static BukkitData.PotionEffects adapt(@NotNull Collection effects) { - return from( - effects.stream() - .map(effect -> new PotionEffect( - Objects.requireNonNull( - PotionEffectType.getByName(effect.type()), - "Invalid potion effect type" - ), - effect.duration(), - effect.amplifier(), - effect.isAmbient(), - effect.showParticles(), - effect.hasIcon() - )) - .toList() - ); + return from(effects.stream() + .map(effect -> { + final PotionEffectType type = matchEffectType(effect.type()); + return type != null ? new PotionEffect( + type, + effect.duration(), + effect.amplifier(), + effect.isAmbient(), + effect.showParticles(), + effect.hasIcon() + ) : null; + }) + .filter(Objects::nonNull) + .toList()); } @NotNull diff --git a/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java b/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java index c427bf49..94bdfcce 100644 --- a/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java +++ b/bukkit/src/main/java/net/william278/husksync/util/BukkitKeyedAdapter.java @@ -22,6 +22,7 @@ package net.william278.husksync.util; import org.bukkit.*; import org.bukkit.attribute.Attribute; import org.bukkit.entity.EntityType; +import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -48,6 +49,11 @@ public final class BukkitKeyedAdapter { return getRegistryValue(Registry.ATTRIBUTE, key); } + @Nullable + public static PotionEffectType matchEffectType(@NotNull String key) { + return PotionEffectType.getByName(key); // No registry for this in 1.17 API + } + private static T getRegistryValue(@NotNull Registry registry, @NotNull String keyString) { final NamespacedKey key = NamespacedKey.fromString(keyString); return key != null ? registry.get(key) : null;