9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-27 02:29:10 +00:00

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
This commit is contained in:
William
2024-05-29 00:08:40 +01:00
parent 107238360c
commit c4adec3082
2 changed files with 20 additions and 15 deletions

View File

@@ -237,21 +237,20 @@ public abstract class BukkitData implements Data {
@NotNull
public static BukkitData.PotionEffects adapt(@NotNull Collection<Effect> 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

View File

@@ -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 extends Keyed> T getRegistryValue(@NotNull Registry<T> registry, @NotNull String keyString) {
final NamespacedKey key = NamespacedKey.fromString(keyString);
return key != null ? registry.get(key) : null;