Expand PotionMeta Api to allow getting effective potion colour and effects (#12390)

This commit is contained in:
Isaac - The456
2025-05-02 21:31:39 +01:00
committed by GitHub
parent 825685f82f
commit 2bd84f6f0e
4 changed files with 83 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap.Builder;
import java.util.ArrayList;
@@ -25,6 +26,7 @@ import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;
@DelegateDeserialization(SerializableMeta.class)
class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
@@ -202,6 +204,19 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
return ImmutableList.of();
}
@Override
@NotNull
public List<PotionEffect> getAllEffects() {
final ImmutableList.Builder<PotionEffect> builder = ImmutableList.builder();
if (this.hasBasePotionType()) {
builder.addAll(this.getBasePotionType().getPotionEffects());
}
if (this.hasCustomEffects()) {
builder.addAll(this.customEffects);
}
return builder.build();
}
@Override
public boolean addCustomEffect(PotionEffect effect, boolean overwrite) {
Preconditions.checkArgument(effect != null, "Potion effect cannot be null");
@@ -305,6 +320,17 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
this.color = color == null ? null : color.asRGB();
}
@Override
@NotNull
public Color computeEffectiveColor() {
if (hasColor()) return getColor();
return Color.fromRGB(
PotionContents.getColorOptional(Collections2.transform(getAllEffects(), CraftPotionUtil::fromBukkit))
.orElse(PotionContents.BASE_POTION_COLOR) & 0xFFFFFF
);
}
@Override
public boolean hasCustomPotionName() {
return this.customName != null;