From ffbf67a1456fe680d2ee0fe9287fd97a1e0a98ad Mon Sep 17 00:00:00 2001 From: DerFrZocker Date: Tue, 30 May 2023 13:28:22 +0200 Subject: [PATCH] Convert PotionType --- src/main/java/org/bukkit/Registry.java | 7 + .../org/bukkit/entity/AreaEffectCloud.java | 20 ++ src/main/java/org/bukkit/entity/Arrow.java | 20 ++ .../org/bukkit/inventory/meta/PotionMeta.java | 20 ++ src/main/java/org/bukkit/potion/Potion.java | 10 +- .../java/org/bukkit/potion/PotionData.java | 6 + .../java/org/bukkit/potion/PotionType.java | 175 +++++++++++++----- 7 files changed, 206 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java index 6da99bf0..2fee2227 100644 --- a/src/main/java/org/bukkit/Registry.java +++ b/src/main/java/org/bukkit/Registry.java @@ -26,6 +26,7 @@ import org.bukkit.inventory.meta.trim.TrimMaterial; import org.bukkit.inventory.meta.trim.TrimPattern; import org.bukkit.loot.LootTables; import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -152,6 +153,12 @@ public interface Registry extends Iterable { * @see Particle */ Registry PARTICLE_TYPE = Objects.requireNonNull(Bukkit.getRegistry(Particle.class), "No registry present for Particle. This is a bug."); + /** + * Server potion types. + * + * @see PotionType + */ + Registry POTION = Objects.requireNonNull(Bukkit.getRegistry(PotionType.class), "No registry present for PotionType. This is a bug."); /** * Server statistics. * diff --git a/src/main/java/org/bukkit/entity/AreaEffectCloud.java b/src/main/java/org/bukkit/entity/AreaEffectCloud.java index d145db6b..c0ed1e00 100644 --- a/src/main/java/org/bukkit/entity/AreaEffectCloud.java +++ b/src/main/java/org/bukkit/entity/AreaEffectCloud.java @@ -6,6 +6,7 @@ import org.bukkit.Particle; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; import org.bukkit.projectiles.ProjectileSource; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -149,17 +150,36 @@ public interface AreaEffectCloud extends Entity { * Sets the underlying potion data * * @param data PotionData to set the base potion state to + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #setBasePotionType} instead. */ + @Deprecated void setBasePotionData(@NotNull PotionData data); /** * Returns the potion data about the base potion * * @return a PotionData object + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead. */ @NotNull + @Deprecated PotionData getBasePotionData(); + /** + * Sets the underlying potion type + * + * @param type PotionType to set the base potion state to + */ + void setBasePotionType(@NotNull PotionType type); + + /** + * Returns the potion type about the base potion + * + * @return a PotionType object + */ + @NotNull + PotionType getBasePotionType(); + /** * Checks for the presence of custom potion effects. * diff --git a/src/main/java/org/bukkit/entity/Arrow.java b/src/main/java/org/bukkit/entity/Arrow.java index 8814519d..8eb0cf31 100644 --- a/src/main/java/org/bukkit/entity/Arrow.java +++ b/src/main/java/org/bukkit/entity/Arrow.java @@ -5,6 +5,7 @@ import org.bukkit.Color; 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; import org.jetbrains.annotations.Nullable; @@ -14,17 +15,36 @@ public interface Arrow extends AbstractArrow { * Sets the underlying potion data * * @param data PotionData to set the base potion state to + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #setBasePotionType} instead. */ + @Deprecated void setBasePotionData(@NotNull PotionData data); /** * Returns the potion data about the base potion * * @return a PotionData object + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead. */ @NotNull + @Deprecated PotionData getBasePotionData(); + /** + * Sets the underlying potion type + * + * @param type PotionType to set the base potion state to + */ + void setBasePotionType(@NotNull PotionType type); + + /** + * Returns the potion type about the base potion + * + * @return a PotionType object + */ + @NotNull + PotionType getBasePotionType(); + /** * Gets the color of this arrow. * diff --git a/src/main/java/org/bukkit/inventory/meta/PotionMeta.java b/src/main/java/org/bukkit/inventory/meta/PotionMeta.java index 8e2f474a..1113c40f 100644 --- a/src/main/java/org/bukkit/inventory/meta/PotionMeta.java +++ b/src/main/java/org/bukkit/inventory/meta/PotionMeta.java @@ -5,6 +5,7 @@ import org.bukkit.Color; 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; import org.jetbrains.annotations.Nullable; @@ -17,17 +18,36 @@ public interface PotionMeta extends ItemMeta { * Sets the underlying potion data * * @param data PotionData to set the base potion state to + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #setBasePotionType} instead. */ + @Deprecated void setBasePotionData(@NotNull PotionData data); /** * Returns the potion data about the base potion * * @return a PotionData object + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead. */ @NotNull + @Deprecated PotionData getBasePotionData(); + /** + * Sets the underlying potion type + * + * @param type PotionType to set the base potion state to + */ + void setBasePotionType(@NotNull PotionType type); + + /** + * Returns the potion type about the base potion + * + * @return a PotionType object + */ + @NotNull + PotionType getBasePotionType(); + /** * Checks for the presence of custom potion effects. * diff --git a/src/main/java/org/bukkit/potion/Potion.java b/src/main/java/org/bukkit/potion/Potion.java index b79c6757..9a670f91 100644 --- a/src/main/java/org/bukkit/potion/Potion.java +++ b/src/main/java/org/bukkit/potion/Potion.java @@ -294,10 +294,10 @@ public class Potion { type = PotionType.WATER; break; case 1: - type = PotionType.REGEN; + type = PotionType.REGENERATION; break; case 2: - type = PotionType.SPEED; + type = PotionType.SWIFTNESS; break; case 3: type = PotionType.FIRE_RESISTANCE; @@ -306,7 +306,7 @@ public class Potion { type = PotionType.POISON; break; case 5: - type = PotionType.INSTANT_HEAL; + type = PotionType.HEALING; break; case 6: type = PotionType.NIGHT_VISION; @@ -321,10 +321,10 @@ public class Potion { type = PotionType.SLOWNESS; break; case 11: - type = PotionType.JUMP; + type = PotionType.LEAPING; break; case 12: - type = PotionType.INSTANT_DAMAGE; + type = PotionType.HARMING; break; case 13: type = PotionType.WATER_BREATHING; diff --git a/src/main/java/org/bukkit/potion/PotionData.java b/src/main/java/org/bukkit/potion/PotionData.java index ef5fe586..3852a092 100644 --- a/src/main/java/org/bukkit/potion/PotionData.java +++ b/src/main/java/org/bukkit/potion/PotionData.java @@ -3,6 +3,10 @@ package org.bukkit.potion; import com.google.common.base.Preconditions; import org.jetbrains.annotations.NotNull; +/** + * @deprecated Upgraded / extended potions are now their own {@link PotionType} use them instead. + */ +@Deprecated public final class PotionData { private final PotionType type; @@ -24,6 +28,8 @@ public final class PotionData { Preconditions.checkArgument(!upgraded || type.isUpgradeable(), "Potion Type is not upgradable"); Preconditions.checkArgument(!extended || type.isExtendable(), "Potion Type is not extendable"); Preconditions.checkArgument(!upgraded || !extended, "Potion cannot be both extended and upgraded"); + Preconditions.checkArgument(!type.getKey().getKey().startsWith("strong_"), "Strong potion type cannot be used directly, got %s", type.getKey()); + Preconditions.checkArgument(!type.getKey().getKey().startsWith("long_"), "Extended potion type cannot be used directly, got %s", type.getKey()); this.type = type; this.extended = extended; this.upgraded = upgraded; diff --git a/src/main/java/org/bukkit/potion/PotionType.java b/src/main/java/org/bukkit/potion/PotionType.java index 3ec3dafa..f0f4849b 100644 --- a/src/main/java/org/bukkit/potion/PotionType.java +++ b/src/main/java/org/bukkit/potion/PotionType.java @@ -1,53 +1,93 @@ package org.bukkit.potion; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import java.util.List; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; +import org.bukkit.util.OldEnum; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * This enum reflects and matches each potion state that can be obtained from * the Creative mode inventory */ -public enum PotionType { - UNCRAFTABLE(null, false, false), - WATER(null, false, false), - MUNDANE(null, false, false), - THICK(null, false, false), - AWKWARD(null, false, false), - NIGHT_VISION(PotionEffectType.NIGHT_VISION, false, true), - INVISIBILITY(PotionEffectType.INVISIBILITY, false, true), - JUMP(PotionEffectType.JUMP_BOOST, true, true), - FIRE_RESISTANCE(PotionEffectType.FIRE_RESISTANCE, false, true), - SPEED(PotionEffectType.SPEED, true, true), - SLOWNESS(PotionEffectType.SLOWNESS, true, true), - WATER_BREATHING(PotionEffectType.WATER_BREATHING, false, true), - INSTANT_HEAL(PotionEffectType.INSTANT_HEALTH, true, false), - INSTANT_DAMAGE(PotionEffectType.INSTANT_DAMAGE, true, false), - POISON(PotionEffectType.POISON, true, true), - REGEN(PotionEffectType.REGENERATION, true, true), - STRENGTH(PotionEffectType.STRENGTH, true, true), - WEAKNESS(PotionEffectType.WEAKNESS, false, true), - LUCK(PotionEffectType.LUCK, false, false), - TURTLE_MASTER(PotionEffectType.SLOWNESS, true, true), // TODO: multiple effects - SLOW_FALLING(PotionEffectType.SLOW_FALLING, false, true), - ; +public abstract class PotionType extends OldEnum implements Keyed { + public static final PotionType EMPTY = getPotionType("empty"); + public static final PotionType WATER = getPotionType("water"); + public static final PotionType MUNDANE = getPotionType("mundane"); + public static final PotionType THICK = getPotionType("thick"); + public static final PotionType AWKWARD = getPotionType("awkward"); + public static final PotionType NIGHT_VISION = getPotionType("night_vision"); + public static final PotionType LONG_NIGHT_VISION = getPotionType("long_night_vision"); + public static final PotionType INVISIBILITY = getPotionType("invisibility"); + public static final PotionType LONG_INVISIBILITY = getPotionType("long_invisibility"); + public static final PotionType LEAPING = getPotionType("leaping"); + public static final PotionType LONG_LEAPING = getPotionType("long_leaping"); + public static final PotionType STRONG_LEAPING = getPotionType("strong_leaping"); + public static final PotionType FIRE_RESISTANCE = getPotionType("fire_resistance"); + public static final PotionType LONG_FIRE_RESISTANCE = getPotionType("long_fire_resistance"); + public static final PotionType SWIFTNESS = getPotionType("swiftness"); + public static final PotionType LONG_SWIFTNESS = getPotionType("long_swiftness"); + public static final PotionType STRONG_SWIFTNESS = getPotionType("strong_swiftness"); + public static final PotionType SLOWNESS = getPotionType("slowness"); + public static final PotionType LONG_SLOWNESS = getPotionType("long_slowness"); + public static final PotionType STRONG_SLOWNESS = getPotionType("strong_slowness"); + public static final PotionType WATER_BREATHING = getPotionType("water_breathing"); + public static final PotionType LONG_WATER_BREATHING = getPotionType("long_water_breathing"); + public static final PotionType HEALING = getPotionType("healing"); + public static final PotionType STRONG_HEALING = getPotionType("strong_healing"); + public static final PotionType HARMING = getPotionType("harming"); + public static final PotionType STRONG_HARMING = getPotionType("strong_harming"); + public static final PotionType POISON = getPotionType("poison"); + public static final PotionType LONG_POISON = getPotionType("long_poison"); + public static final PotionType STRONG_POISON = getPotionType("strong_poison"); + public static final PotionType REGENERATION = getPotionType("regeneration"); + public static final PotionType LONG_REGENERATION = getPotionType("long_regeneration"); + public static final PotionType STRONG_REGENERATION = getPotionType("strong_regeneration"); + public static final PotionType STRENGTH = getPotionType("strength"); + public static final PotionType LONG_STRENGTH = getPotionType("long_strength"); + public static final PotionType STRONG_STRENGTH = getPotionType("strong_strength"); + public static final PotionType WEAKNESS = getPotionType("weakness"); + public static final PotionType LONG_WEAKNESS = getPotionType("long_weakness"); + public static final PotionType LUCK = getPotionType("luck"); + public static final PotionType TURTLE_MASTER = getPotionType("turtle_master"); + public static final PotionType LONG_TURTLE_MASTER = getPotionType("long_turtle_master"); + public static final PotionType STRONG_TURTLE_MASTER = getPotionType("strong_turtle_master"); + public static final PotionType SLOW_FALLING = getPotionType("slow_falling"); + public static final PotionType LONG_SLOW_FALLING = getPotionType("long_slow_falling"); - private final PotionEffectType effect; - private final boolean upgradeable; - private final boolean extendable; - - PotionType(/*@Nullable*/ PotionEffectType effect, boolean upgradeable, boolean extendable) { - this.effect = effect; - this.upgradeable = upgradeable; - this.extendable = extendable; + @NotNull + private static PotionType getPotionType(@NotNull String key) { + NamespacedKey namespacedKey = NamespacedKey.minecraft(key); + PotionType potionType = Registry.POTION.get(namespacedKey); + Preconditions.checkNotNull(potionType, "No potion type found for %s. This is a bug.", namespacedKey); + return potionType; } + /** + * @return the potion effect type of this potion type + * @deprecated Potions can have multiple effects use {@link #getPotionEffects()} + */ @Nullable - public PotionEffectType getEffectType() { - return effect; - } + @Deprecated + public abstract PotionEffectType getEffectType(); - public boolean isInstant() { - return effect != null && effect.isInstant(); - } + /** + * @return a list of all effects this potion type has + */ + @NotNull + public abstract List getPotionEffects(); + + /** + * @return if this potion type is instant + * @deprecated PotionType can have multiple effects, some of which can be instant and others not. + * Use {@link PotionEffectType#isInstant()} in combination with {@link #getPotionEffects()} and {@link PotionEffect#getType()} + */ + @Deprecated + public abstract boolean isInstant(); /** * Checks if the potion type has an upgraded state. @@ -56,9 +96,7 @@ public enum PotionType { * * @return true if the potion type can be upgraded; */ - public boolean isUpgradeable() { - return upgradeable; - } + public abstract boolean isUpgradeable(); /** * Checks if the potion type has an extended state. @@ -66,13 +104,9 @@ public enum PotionType { * * @return true if the potion type can be extended */ - public boolean isExtendable() { - return extendable; - } + public abstract boolean isExtendable(); - public int getMaxLevel() { - return upgradeable ? 2 : 1; - } + public abstract int getMaxLevel(); /** * @param effectType the effect to get by @@ -85,9 +119,56 @@ public enum PotionType { if (effectType == null) return WATER; for (PotionType type : PotionType.values()) { - if (effectType.equals(type.effect)) + if (effectType.equals(type.getEffectType())) return type; } return null; } + + /** + * @param name of the potion type. + * @return the potion type with the given name. + * @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead. + */ + @NotNull + @Deprecated + public static PotionType valueOf(@NotNull String name) { + name = convertLegacy(name); + PotionType potionType = Registry.POTION.get(NamespacedKey.fromString(name.toLowerCase())); + Preconditions.checkArgument(potionType != null, "No potion type found with the name %s", name); + return potionType; + } + + /** + * @return an array of all known potion types. + * @deprecated use {@link Registry#iterator()}. + */ + @NotNull + @Deprecated + public static PotionType[] values() { + return Lists.newArrayList(Registry.POTION).toArray(new PotionType[0]); + } + + private static String convertLegacy(String from) { + if (from == null) { + return null; + } + + switch (from.toLowerCase()) { + case "uncraftable": + return "empty"; + case "jump": + return "leaping"; + case "speed": + return "swiftness"; + case "instant_heal": + return "healing"; + case "instant_damage": + return "harming"; + case "regen": + return "regeneration"; + } + + return from; + } }