Convert PotionType

This commit is contained in:
DerFrZocker 2023-05-30 13:28:22 +02:00
parent eacaa45dbe
commit ffbf67a145
No known key found for this signature in database
GPG key ID: 713F71FFFE1DDF91
7 changed files with 206 additions and 52 deletions

View file

@ -26,6 +26,7 @@ import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern; import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.loot.LootTables; import org.bukkit.loot.LootTables;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -152,6 +153,12 @@ public interface Registry<T extends Keyed> extends Iterable<T> {
* @see Particle * @see Particle
*/ */
Registry<Particle> PARTICLE_TYPE = Objects.requireNonNull(Bukkit.getRegistry(Particle.class), "No registry present for Particle. This is a bug."); Registry<Particle> PARTICLE_TYPE = Objects.requireNonNull(Bukkit.getRegistry(Particle.class), "No registry present for Particle. This is a bug.");
/**
* Server potion types.
*
* @see PotionType
*/
Registry<PotionType> POTION = Objects.requireNonNull(Bukkit.getRegistry(PotionType.class), "No registry present for PotionType. This is a bug.");
/** /**
* Server statistics. * Server statistics.
* *

View file

@ -6,6 +6,7 @@ import org.bukkit.Particle;
import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.bukkit.projectiles.ProjectileSource; import org.bukkit.projectiles.ProjectileSource;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -149,17 +150,36 @@ public interface AreaEffectCloud extends Entity {
* Sets the underlying potion data * Sets the underlying potion data
* *
* @param data PotionData to set the base potion state to * @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); void setBasePotionData(@NotNull PotionData data);
/** /**
* Returns the potion data about the base potion * Returns the potion data about the base potion
* *
* @return a PotionData object * @return a PotionData object
* @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead.
*/ */
@NotNull @NotNull
@Deprecated
PotionData getBasePotionData(); 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. * Checks for the presence of custom potion effects.
* *

View file

@ -5,6 +5,7 @@ import org.bukkit.Color;
import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -14,17 +15,36 @@ public interface Arrow extends AbstractArrow {
* Sets the underlying potion data * Sets the underlying potion data
* *
* @param data PotionData to set the base potion state to * @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); void setBasePotionData(@NotNull PotionData data);
/** /**
* Returns the potion data about the base potion * Returns the potion data about the base potion
* *
* @return a PotionData object * @return a PotionData object
* @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead.
*/ */
@NotNull @NotNull
@Deprecated
PotionData getBasePotionData(); 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. * Gets the color of this arrow.
* *

View file

@ -5,6 +5,7 @@ import org.bukkit.Color;
import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -17,17 +18,36 @@ public interface PotionMeta extends ItemMeta {
* Sets the underlying potion data * Sets the underlying potion data
* *
* @param data PotionData to set the base potion state to * @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); void setBasePotionData(@NotNull PotionData data);
/** /**
* Returns the potion data about the base potion * Returns the potion data about the base potion
* *
* @return a PotionData object * @return a PotionData object
* @deprecated Upgraded / extended potions are now their own {@link PotionType} use {@link #getBasePotionType()} instead.
*/ */
@NotNull @NotNull
@Deprecated
PotionData getBasePotionData(); 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. * Checks for the presence of custom potion effects.
* *

View file

@ -294,10 +294,10 @@ public class Potion {
type = PotionType.WATER; type = PotionType.WATER;
break; break;
case 1: case 1:
type = PotionType.REGEN; type = PotionType.REGENERATION;
break; break;
case 2: case 2:
type = PotionType.SPEED; type = PotionType.SWIFTNESS;
break; break;
case 3: case 3:
type = PotionType.FIRE_RESISTANCE; type = PotionType.FIRE_RESISTANCE;
@ -306,7 +306,7 @@ public class Potion {
type = PotionType.POISON; type = PotionType.POISON;
break; break;
case 5: case 5:
type = PotionType.INSTANT_HEAL; type = PotionType.HEALING;
break; break;
case 6: case 6:
type = PotionType.NIGHT_VISION; type = PotionType.NIGHT_VISION;
@ -321,10 +321,10 @@ public class Potion {
type = PotionType.SLOWNESS; type = PotionType.SLOWNESS;
break; break;
case 11: case 11:
type = PotionType.JUMP; type = PotionType.LEAPING;
break; break;
case 12: case 12:
type = PotionType.INSTANT_DAMAGE; type = PotionType.HARMING;
break; break;
case 13: case 13:
type = PotionType.WATER_BREATHING; type = PotionType.WATER_BREATHING;

View file

@ -3,6 +3,10 @@ package org.bukkit.potion;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/**
* @deprecated Upgraded / extended potions are now their own {@link PotionType} use them instead.
*/
@Deprecated
public final class PotionData { public final class PotionData {
private final PotionType type; private final PotionType type;
@ -24,6 +28,8 @@ public final class PotionData {
Preconditions.checkArgument(!upgraded || type.isUpgradeable(), "Potion Type is not upgradable"); Preconditions.checkArgument(!upgraded || type.isUpgradeable(), "Potion Type is not upgradable");
Preconditions.checkArgument(!extended || type.isExtendable(), "Potion Type is not extendable"); Preconditions.checkArgument(!extended || type.isExtendable(), "Potion Type is not extendable");
Preconditions.checkArgument(!upgraded || !extended, "Potion cannot be both extended and upgraded"); 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.type = type;
this.extended = extended; this.extended = extended;
this.upgraded = upgraded; this.upgraded = upgraded;

View file

@ -1,53 +1,93 @@
package org.bukkit.potion; 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; import org.jetbrains.annotations.Nullable;
/** /**
* This enum reflects and matches each potion state that can be obtained from * This enum reflects and matches each potion state that can be obtained from
* the Creative mode inventory * the Creative mode inventory
*/ */
public enum PotionType { public abstract class PotionType extends OldEnum<PotionType> implements Keyed {
UNCRAFTABLE(null, false, false), public static final PotionType EMPTY = getPotionType("empty");
WATER(null, false, false), public static final PotionType WATER = getPotionType("water");
MUNDANE(null, false, false), public static final PotionType MUNDANE = getPotionType("mundane");
THICK(null, false, false), public static final PotionType THICK = getPotionType("thick");
AWKWARD(null, false, false), public static final PotionType AWKWARD = getPotionType("awkward");
NIGHT_VISION(PotionEffectType.NIGHT_VISION, false, true), public static final PotionType NIGHT_VISION = getPotionType("night_vision");
INVISIBILITY(PotionEffectType.INVISIBILITY, false, true), public static final PotionType LONG_NIGHT_VISION = getPotionType("long_night_vision");
JUMP(PotionEffectType.JUMP_BOOST, true, true), public static final PotionType INVISIBILITY = getPotionType("invisibility");
FIRE_RESISTANCE(PotionEffectType.FIRE_RESISTANCE, false, true), public static final PotionType LONG_INVISIBILITY = getPotionType("long_invisibility");
SPEED(PotionEffectType.SPEED, true, true), public static final PotionType LEAPING = getPotionType("leaping");
SLOWNESS(PotionEffectType.SLOWNESS, true, true), public static final PotionType LONG_LEAPING = getPotionType("long_leaping");
WATER_BREATHING(PotionEffectType.WATER_BREATHING, false, true), public static final PotionType STRONG_LEAPING = getPotionType("strong_leaping");
INSTANT_HEAL(PotionEffectType.INSTANT_HEALTH, true, false), public static final PotionType FIRE_RESISTANCE = getPotionType("fire_resistance");
INSTANT_DAMAGE(PotionEffectType.INSTANT_DAMAGE, true, false), public static final PotionType LONG_FIRE_RESISTANCE = getPotionType("long_fire_resistance");
POISON(PotionEffectType.POISON, true, true), public static final PotionType SWIFTNESS = getPotionType("swiftness");
REGEN(PotionEffectType.REGENERATION, true, true), public static final PotionType LONG_SWIFTNESS = getPotionType("long_swiftness");
STRENGTH(PotionEffectType.STRENGTH, true, true), public static final PotionType STRONG_SWIFTNESS = getPotionType("strong_swiftness");
WEAKNESS(PotionEffectType.WEAKNESS, false, true), public static final PotionType SLOWNESS = getPotionType("slowness");
LUCK(PotionEffectType.LUCK, false, false), public static final PotionType LONG_SLOWNESS = getPotionType("long_slowness");
TURTLE_MASTER(PotionEffectType.SLOWNESS, true, true), // TODO: multiple effects public static final PotionType STRONG_SLOWNESS = getPotionType("strong_slowness");
SLOW_FALLING(PotionEffectType.SLOW_FALLING, false, true), 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; @NotNull
private final boolean upgradeable; private static PotionType getPotionType(@NotNull String key) {
private final boolean extendable; NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
PotionType potionType = Registry.POTION.get(namespacedKey);
PotionType(/*@Nullable*/ PotionEffectType effect, boolean upgradeable, boolean extendable) { Preconditions.checkNotNull(potionType, "No potion type found for %s. This is a bug.", namespacedKey);
this.effect = effect; return potionType;
this.upgradeable = upgradeable;
this.extendable = extendable;
} }
/**
* @return the potion effect type of this potion type
* @deprecated Potions can have multiple effects use {@link #getPotionEffects()}
*/
@Nullable @Nullable
public PotionEffectType getEffectType() { @Deprecated
return effect; 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<PotionEffect> 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. * 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; * @return true if the potion type can be upgraded;
*/ */
public boolean isUpgradeable() { public abstract boolean isUpgradeable();
return upgradeable;
}
/** /**
* Checks if the potion type has an extended state. * 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 * @return true if the potion type can be extended
*/ */
public boolean isExtendable() { public abstract boolean isExtendable();
return extendable;
}
public int getMaxLevel() { public abstract int getMaxLevel();
return upgradeable ? 2 : 1;
}
/** /**
* @param effectType the effect to get by * @param effectType the effect to get by
@ -85,9 +119,56 @@ public enum PotionType {
if (effectType == null) if (effectType == null)
return WATER; return WATER;
for (PotionType type : PotionType.values()) { for (PotionType type : PotionType.values()) {
if (effectType.equals(type.effect)) if (effectType.equals(type.getEffectType()))
return type; return type;
} }
return null; 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;
}
} }