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.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<T extends Keyed> extends Iterable<T> {
* @see Particle
*/
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.
*

View file

@ -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.
*

View file

@ -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.
*

View file

@ -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.
*

View file

@ -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;

View file

@ -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;

View file

@ -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<PotionType> 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<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.
@ -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;
}
}