Reflect Potion API Changes

This commit is contained in:
Josh Roy 2024-05-13 16:55:10 -04:00
parent b9b3e4430e
commit 4c5a97bf00
11 changed files with 473 additions and 167 deletions

View File

@ -81,7 +81,6 @@ import net.ess3.provider.SyncCommandsProvider;
import net.ess3.provider.WorldInfoProvider;
import net.ess3.provider.providers.BaseBannerDataProvider;
import net.ess3.provider.providers.BaseLoggerProvider;
import net.ess3.provider.providers.BasePotionDataProvider;
import net.ess3.provider.providers.BlockMetaSpawnerItemProvider;
import net.ess3.provider.providers.BukkitMaterialTagProvider;
import net.ess3.provider.providers.BukkitSpawnerBlockProvider;
@ -98,6 +97,7 @@ import net.ess3.provider.providers.ModernDataWorldInfoProvider;
import net.ess3.provider.providers.ModernItemUnbreakableProvider;
import net.ess3.provider.providers.ModernPersistentDataProvider;
import net.ess3.provider.providers.ModernPlayerLocaleProvider;
import net.ess3.provider.providers.ModernPotionMetaProvider;
import net.ess3.provider.providers.ModernSignDataProvider;
import net.ess3.provider.providers.PaperBiomeKeyProvider;
import net.ess3.provider.providers.PaperContainerProvider;
@ -106,6 +106,7 @@ import net.ess3.provider.providers.PaperMaterialTagProvider;
import net.ess3.provider.providers.PaperRecipeBookListener;
import net.ess3.provider.providers.PaperSerializationProvider;
import net.ess3.provider.providers.PaperServerStateProvider;
import net.ess3.provider.providers.PrehistoricPotionMetaProvider;
import net.essentialsx.api.v2.services.BalanceTop;
import net.essentialsx.api.v2.services.mail.MailService;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
@ -409,10 +410,12 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
}
//Potion Meta Provider
if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_9_R01)) {
potionMetaProvider = new LegacyPotionMetaProvider();
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_20_6_R01)) {
potionMetaProvider = new ModernPotionMetaProvider();
} else if (VersionUtil.getServerBukkitVersion().isLowerThan(VersionUtil.v1_9_R01)) {
potionMetaProvider = new PrehistoricPotionMetaProvider();
} else {
potionMetaProvider = new BasePotionDataProvider();
potionMetaProvider = new LegacyPotionMetaProvider();
}
//Banner Meta Provider

View File

@ -30,7 +30,6 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
@ -541,17 +540,7 @@ public class MetaItemStack {
}
pmeta.addCustomEffect(pEffect, true);
stack.setItemMeta(pmeta);
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_9_R01)) {
if (isSplashPotion && stack.getType() == Material.POTION) {
stack.setType(Material.SPLASH_POTION);
} else if (!isSplashPotion && stack.getType() == Material.SPLASH_POTION) {
stack.setType(Material.POTION);
}
} else {
final Potion potion = Potion.fromDamage(stack.getDurability());
potion.setSplash(isSplashPotion);
potion.apply(stack);
}
ess.getPotionMetaProvider().setSplashPotion(stack, isSplashPotion);
resetPotionMeta();
}
}
@ -633,6 +622,7 @@ public class MetaItemStack {
PatternType patternType = null;
try {
//noinspection removal
patternType = PatternType.getByIdentifier(split[0]);
} catch (final Exception ignored) {
}
@ -642,6 +632,7 @@ public class MetaItemStack {
final Color color = Color.fromRGB(Integer.parseInt(split[1]));
ess.getBannerDataProvider().setBaseColor(stack, DyeColor.getByColor(color));
} else if (patternType != null) {
//noinspection removal
final PatternType type = PatternType.getByIdentifier(split[0]);
final DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.parseInt(split[1])));
final org.bukkit.block.banner.Pattern pattern = new org.bukkit.block.banner.Pattern(color, type);
@ -658,6 +649,7 @@ public class MetaItemStack {
PatternType patternType = null;
try {
//noinspection removal
patternType = PatternType.getByIdentifier(split[0]);
} catch (final Exception ignored) {
}
@ -669,6 +661,7 @@ public class MetaItemStack {
final Color color = Color.fromRGB(Integer.parseInt(split[1]));
banner.setBaseColor(DyeColor.getByColor(color));
} else if (patternType != null) {
//noinspection removal
final PatternType type = PatternType.getByIdentifier(split[0]);
final DyeColor color = DyeColor.getByColor(Color.fromRGB(Integer.parseInt(split[1])));
final org.bukkit.block.banner.Pattern pattern = new org.bukkit.block.banner.Pattern(color, type);

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials;
import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.RegistryUtil;
import org.bukkit.potion.PotionEffectType;
import java.util.HashMap;
@ -21,50 +22,68 @@ public final class Potions {
ALIASPOTIONS.put("sprint", PotionEffectType.SPEED);
ALIASPOTIONS.put("swift", PotionEffectType.SPEED);
POTIONS.put("slowness", PotionEffectType.SLOW);
ALIASPOTIONS.put("slow", PotionEffectType.SLOW);
ALIASPOTIONS.put("sluggish", PotionEffectType.SLOW);
final PotionEffectType SLOWNESS = RegistryUtil.valueOf(PotionEffectType.class, "SLOW", "SLOWNESS");
POTIONS.put("haste", PotionEffectType.FAST_DIGGING);
ALIASPOTIONS.put("superpick", PotionEffectType.FAST_DIGGING);
ALIASPOTIONS.put("quickmine", PotionEffectType.FAST_DIGGING);
ALIASPOTIONS.put("digspeed", PotionEffectType.FAST_DIGGING);
ALIASPOTIONS.put("digfast", PotionEffectType.FAST_DIGGING);
ALIASPOTIONS.put("sharp", PotionEffectType.FAST_DIGGING);
POTIONS.put("slowness", SLOWNESS);
ALIASPOTIONS.put("slow", SLOWNESS);
ALIASPOTIONS.put("sluggish", SLOWNESS);
POTIONS.put("fatigue", PotionEffectType.SLOW_DIGGING);
ALIASPOTIONS.put("slow", PotionEffectType.SLOW_DIGGING);
ALIASPOTIONS.put("dull", PotionEffectType.SLOW_DIGGING);
final PotionEffectType HASTE = RegistryUtil.valueOf(PotionEffectType.class, "FAST_DIGGING", "HASTE");
POTIONS.put("strength", PotionEffectType.INCREASE_DAMAGE);
ALIASPOTIONS.put("strong", PotionEffectType.INCREASE_DAMAGE);
ALIASPOTIONS.put("bull", PotionEffectType.INCREASE_DAMAGE);
ALIASPOTIONS.put("attack", PotionEffectType.INCREASE_DAMAGE);
POTIONS.put("haste", HASTE);
ALIASPOTIONS.put("superpick", HASTE);
ALIASPOTIONS.put("quickmine", HASTE);
ALIASPOTIONS.put("digspeed", HASTE);
ALIASPOTIONS.put("digfast", HASTE);
ALIASPOTIONS.put("sharp", HASTE);
POTIONS.put("heal", PotionEffectType.HEAL);
ALIASPOTIONS.put("healthy", PotionEffectType.HEAL);
ALIASPOTIONS.put("instaheal", PotionEffectType.HEAL);
final PotionEffectType MINING_FATIGUE = RegistryUtil.valueOf(PotionEffectType.class, "SLOW_DIGGING", "MINING_FATIGUE");
POTIONS.put("harm", PotionEffectType.HARM);
ALIASPOTIONS.put("harming", PotionEffectType.HARM);
ALIASPOTIONS.put("injure", PotionEffectType.HARM);
ALIASPOTIONS.put("damage", PotionEffectType.HARM);
ALIASPOTIONS.put("inflict", PotionEffectType.HARM);
POTIONS.put("fatigue", MINING_FATIGUE);
ALIASPOTIONS.put("slow", MINING_FATIGUE);
ALIASPOTIONS.put("dull", MINING_FATIGUE);
POTIONS.put("jump", PotionEffectType.JUMP);
ALIASPOTIONS.put("leap", PotionEffectType.JUMP);
final PotionEffectType STRENGTH = RegistryUtil.valueOf(PotionEffectType.class, "INCREASE_DAMAGE", "STRENGTH");
POTIONS.put("nausea", PotionEffectType.CONFUSION);
ALIASPOTIONS.put("sick", PotionEffectType.CONFUSION);
ALIASPOTIONS.put("sickness", PotionEffectType.CONFUSION);
ALIASPOTIONS.put("confusion", PotionEffectType.CONFUSION);
POTIONS.put("strength", STRENGTH);
ALIASPOTIONS.put("strong", STRENGTH);
ALIASPOTIONS.put("bull", STRENGTH);
ALIASPOTIONS.put("attack", STRENGTH);
final PotionEffectType INSTANT_HEALTH = RegistryUtil.valueOf(PotionEffectType.class, "HEAL", "INSTANT_HEALTH");
POTIONS.put("heal", INSTANT_HEALTH);
ALIASPOTIONS.put("healthy", INSTANT_HEALTH);
ALIASPOTIONS.put("instaheal", INSTANT_HEALTH);
final PotionEffectType INSTANT_DAMAGE = RegistryUtil.valueOf(PotionEffectType.class, "HARM", "INSTANT_DAMAGE");
POTIONS.put("harm", INSTANT_DAMAGE);
ALIASPOTIONS.put("harming", INSTANT_DAMAGE);
ALIASPOTIONS.put("injure", INSTANT_DAMAGE);
ALIASPOTIONS.put("damage", INSTANT_DAMAGE);
ALIASPOTIONS.put("inflict", INSTANT_DAMAGE);
final PotionEffectType JUMP_BOOST = RegistryUtil.valueOf(PotionEffectType.class, "JUMP", "JUMP_BOOST");
POTIONS.put("jump", JUMP_BOOST);
ALIASPOTIONS.put("leap", JUMP_BOOST);
final PotionEffectType NAUSEA = RegistryUtil.valueOf(PotionEffectType.class, "CONFUSION", "NAUSEA");
POTIONS.put("nausea", NAUSEA);
ALIASPOTIONS.put("sick", NAUSEA);
ALIASPOTIONS.put("sickness", NAUSEA);
ALIASPOTIONS.put("confusion", NAUSEA);
POTIONS.put("regeneration", PotionEffectType.REGENERATION);
ALIASPOTIONS.put("regen", PotionEffectType.REGENERATION);
POTIONS.put("resistance", PotionEffectType.DAMAGE_RESISTANCE);
ALIASPOTIONS.put("dmgresist", PotionEffectType.DAMAGE_RESISTANCE);
ALIASPOTIONS.put("armor", PotionEffectType.DAMAGE_RESISTANCE);
final PotionEffectType RESISTANCE = RegistryUtil.valueOf(PotionEffectType.class, "DAMAGE_RESISTANCE", "RESISTANCE");
POTIONS.put("resistance", RESISTANCE);
ALIASPOTIONS.put("dmgresist", RESISTANCE);
ALIASPOTIONS.put("armor", RESISTANCE);
POTIONS.put("fireresist", PotionEffectType.FIRE_RESISTANCE);
ALIASPOTIONS.put("fireresistance", PotionEffectType.FIRE_RESISTANCE);

View File

@ -24,10 +24,8 @@ import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import java.util.ArrayList;
@ -285,16 +283,8 @@ public abstract class AbstractItemDb implements IConf, net.ess3.api.IItemDb {
serializeEffectMeta(sb, fireworkEffectMeta.getEffect());
}
} else if (MaterialUtil.isPotion(material)) {
final boolean splash;
final Collection<PotionEffect> effects;
if (VersionUtil.PRE_FLATTENING) {
final Potion potion = Potion.fromDamage(is.getDurability());
splash = potion.isSplash();
effects = potion.getEffects();
} else {
splash = is.getType() == Material.SPLASH_POTION;
effects = ((PotionMeta) is.getItemMeta()).getCustomEffects();
}
final boolean splash = ess.getPotionMetaProvider().isSplashPotion(is);
final Collection<PotionEffect> effects = ess.getPotionMetaProvider().getCustomEffects(is);
for (final PotionEffect e : effects) {
// long but needs to be effect:speed power:2 duration:120 in that order.

View File

@ -9,13 +9,14 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.ess3.api.IEssentials;
import net.ess3.api.TranslatableException;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import java.util.ArrayList;
import java.util.Collection;
@ -23,6 +24,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -127,14 +129,14 @@ public class FlatItemDb extends AbstractItemDb {
final ItemStack stack = new ItemStack(material);
stack.setAmount(material.getMaxStackSize());
final PotionData potionData = data.getPotionData();
final ItemMeta meta = stack.getItemMeta();
final ItemData.EssentialPotionData potionData = data.getPotionData();
if (potionData != null && meta instanceof PotionMeta) {
final PotionMeta potionMeta = (PotionMeta) meta;
potionMeta.setBasePotionData(potionData);
if (potionData != null && stack.getItemMeta() instanceof PotionMeta) {
ess.getPotionMetaProvider().setBasePotionType(stack, potionData.getType(), potionData.isExtended(), potionData.isUpgraded());
}
final ItemMeta meta = stack.getItemMeta();
// For some reason, Damageable doesn't extend ItemMeta but CB implements them in the same
// class. As to why, your guess is as good as mine.
if (split.length > 1 && meta instanceof Damageable) {
@ -200,14 +202,14 @@ public class FlatItemDb extends AbstractItemDb {
throw new UnsupportedOperationException("Legacy IDs aren't supported on this version.");
}
private ItemData lookup(final ItemStack item) {
final Material type = item.getType();
private ItemData lookup(final ItemStack is) {
final Material type = is.getType();
if (MaterialUtil.isPotion(type) && item.getItemMeta() instanceof PotionMeta) {
final PotionData potion = ((PotionMeta) item.getItemMeta()).getBasePotionData();
return new ItemData(type, potion);
if (MaterialUtil.isPotion(type) && is.getItemMeta() instanceof PotionMeta) {
final PotionMetaProvider provider = ess.getPotionMetaProvider();
return new ItemData(type, new ItemData.EssentialPotionData(provider.getBasePotionType(is), provider.isUpgraded(is), provider.isExtended(is)));
} else if (type.toString().contains("SPAWNER")) {
final EntityType entity = ess.getSpawnerItemProvider().getEntityType(item);
final EntityType entity = ess.getSpawnerItemProvider().getEntityType(is);
return new ItemData(type, entity);
} else {
return new ItemData(type);
@ -224,14 +226,14 @@ public class FlatItemDb extends AbstractItemDb {
public static class ItemData {
private Material material;
private String[] fallbacks = null;
private PotionData potionData = null;
private EssentialPotionData potionData = null;
private EntityType entity = null;
ItemData(final Material material) {
this.material = material;
}
ItemData(final Material material, final PotionData potionData) {
ItemData(final Material material, final EssentialPotionData potionData) {
this.material = material;
this.potionData = potionData;
}
@ -267,7 +269,7 @@ public class FlatItemDb extends AbstractItemDb {
return material;
}
public PotionData getPotionData() {
public EssentialPotionData getPotionData() {
return this.potionData;
}
@ -294,5 +296,51 @@ public class FlatItemDb extends AbstractItemDb {
return false;
}
}
public static class EssentialPotionData {
private PotionType type;
private String fallbackType;
private final boolean upgraded;
private final boolean extended;
EssentialPotionData(PotionType type, boolean upgraded, boolean extended) {
this.type = type;
this.upgraded = upgraded;
this.extended = extended;
}
public PotionType getType() {
if (type == null && fallbackType != null) {
type = EnumUtil.valueOf(PotionType.class, fallbackType);
fallbackType = null; // If fallback fails, don't keep trying to look up fallbacks
}
return type;
}
public boolean isUpgraded() {
return upgraded;
}
public boolean isExtended() {
return extended;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final EssentialPotionData that = (EssentialPotionData) o;
return upgraded == that.upgraded &&
extended == that.extended &&
// Use the getters here to ensure the fallbacks are being used
getType() == that.getType();
}
@Override
public int hashCode() {
return Objects.hash(getType(), upgraded, extended);
}
}
}
}

View File

@ -0,0 +1,134 @@
package net.ess3.provider.providers;
import com.google.common.collect.ImmutableMap;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
import java.util.Map;
public class LegacyPotionMetaProvider implements PotionMetaProvider {
private static final Map<Integer, PotionType> damageValueToType = ImmutableMap.<Integer, PotionType>builder()
.put(1, PotionType.REGEN)
.put(2, PotionType.SPEED)
.put(3, PotionType.FIRE_RESISTANCE)
.put(4, PotionType.POISON)
.put(5, PotionType.INSTANT_HEAL)
.put(6, PotionType.NIGHT_VISION)
// Skip 7
.put(8, PotionType.WEAKNESS)
.put(9, PotionType.STRENGTH)
.put(10, PotionType.SLOWNESS)
.put(11, PotionType.JUMP)
.put(12, PotionType.INSTANT_DAMAGE)
.put(13, PotionType.WATER_BREATHING)
.put(14, PotionType.INVISIBILITY)
.build();
private static int getBit(final int n, final int k) {
return (n >> k) & 1;
}
@Override
public ItemStack createPotionItem(final Material initial, final int effectId) {
ItemStack potion = new ItemStack(initial, 1);
if (effectId == 0) {
return potion;
}
final int damageValue = getBit(effectId, 0) +
2 * getBit(effectId, 1) +
4 * getBit(effectId, 2) +
8 * getBit(effectId, 3);
final PotionType type = damageValueToType.get(damageValue);
if (type == null) {
throw new IllegalArgumentException("Unable to process potion effect ID " + effectId + " with damage value " + damageValue);
}
//getBit is splash here
if (getBit(effectId, 14) == 1 && initial == Material.POTION) {
potion = new ItemStack(Material.SPLASH_POTION, 1);
}
final PotionMeta meta = (PotionMeta) potion.getItemMeta();
//getBit(s) are extended and upgraded respectfully
final PotionData data = new PotionData(type, getBit(effectId, 6) == 1, getBit(effectId, 5) == 1);
meta.setBasePotionData(data); // this method is exclusive to recent 1.9+
potion.setItemMeta(meta);
return potion;
}
@Override
public void setSplashPotion(final ItemStack stack, final boolean isSplash) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
if (isSplash && stack.getType() == Material.POTION) {
stack.setType(Material.SPLASH_POTION);
} else if (!isSplash && stack.getType() == Material.SPLASH_POTION) {
stack.setType(Material.POTION);
}
}
@Override
public boolean isSplashPotion(final ItemStack stack) {
return stack != null && stack.getType() == Material.SPLASH_POTION;
}
@Override
public Collection<PotionEffect> getCustomEffects(final ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
return meta.getCustomEffects();
}
@Override
public boolean isExtended(final ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
final PotionData data = meta.getBasePotionData();
return data.isExtended();
}
@Override
public boolean isUpgraded(final ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
final PotionData data = meta.getBasePotionData();
return data.isUpgraded();
}
@Override
public PotionType getBasePotionType(final ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
final PotionData data = meta.getBasePotionData();
return data.getType();
}
@Override
public void setBasePotionType(final ItemStack stack, final PotionType type, final boolean extended, final boolean upgraded) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
if (extended && upgraded) {
throw new IllegalArgumentException("Potion cannot be both extended and upgraded");
}
final PotionData data = new PotionData(type, extended, upgraded);
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
meta.setBasePotionData(data);
stack.setItemMeta(meta);
}
@Override
public String getDescription() {
return "1.9-1.20.4 Potion Meta Provider";
}
}

View File

@ -0,0 +1,85 @@
package net.ess3.provider.providers;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
public class PrehistoricPotionMetaProvider implements PotionMetaProvider {
@Override
public ItemStack createPotionItem(final Material initial, final int effectId) {
final ItemStack potion = new ItemStack(initial, 1);
potion.setDurability((short) effectId);
return potion;
}
@Override
public void setSplashPotion(final ItemStack stack, final boolean isSplash) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
final Potion potion = Potion.fromItemStack(stack);
potion.setSplash(isSplash);
potion.apply(stack);
}
@Override
public boolean isSplashPotion(ItemStack stack) {
return Potion.fromItemStack(stack).isSplash();
}
@Override
public Collection<PotionEffect> getCustomEffects(ItemStack stack) {
return Potion.fromItemStack(stack).getEffects();
}
@Override
public boolean isExtended(final ItemStack stack) {
throw new UnsupportedOperationException();
}
@Override
public boolean isUpgraded(final ItemStack stack) {
throw new UnsupportedOperationException();
}
@Override
public PotionType getBasePotionType(final ItemStack stack) {
throw new UnsupportedOperationException();
}
@Override
public void setBasePotionType(final ItemStack stack, final PotionType type, final boolean extended, final boolean upgraded) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
if (extended && upgraded) {
throw new IllegalArgumentException("Potion cannot be both extended and upgraded");
}
final Potion potion = Potion.fromItemStack(stack);
if (extended && !potion.getType().isInstant()) {
potion.setHasExtendedDuration(true);
potion.setLevel(Math.min(potion.getLevel(), 1));
}
if (upgraded && type.getMaxLevel() == 2) {
potion.setLevel(2);
potion.setHasExtendedDuration(false);
}
potion.apply(stack);
}
@Override
public String getDescription() {
return "Legacy 1.8 Potion Meta Provider";
}
}

View File

@ -2,7 +2,25 @@ package net.ess3.provider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
public interface PotionMetaProvider extends Provider {
ItemStack createPotionItem(Material initial, int effectId);
void setSplashPotion(ItemStack stack, boolean isSplash);
boolean isSplashPotion(ItemStack stack);
Collection<PotionEffect> getCustomEffects(ItemStack stack);
boolean isExtended(ItemStack stack);
boolean isUpgraded(ItemStack stack);
PotionType getBasePotionType(ItemStack stack);
void setBasePotionType(ItemStack stack, PotionType type, boolean extended, boolean upgraded);
}

View File

@ -1,71 +0,0 @@
package net.ess3.provider.providers;
import com.google.common.collect.ImmutableMap;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import java.util.Map;
public class BasePotionDataProvider implements PotionMetaProvider {
private static final Map<Integer, PotionType> damageValueToType = ImmutableMap.<Integer, PotionType>builder()
.put(1, PotionType.REGEN)
.put(2, PotionType.SPEED)
.put(3, PotionType.FIRE_RESISTANCE)
.put(4, PotionType.POISON)
.put(5, PotionType.INSTANT_HEAL)
.put(6, PotionType.NIGHT_VISION)
// Skip 7
.put(8, PotionType.WEAKNESS)
.put(9, PotionType.STRENGTH)
.put(10, PotionType.SLOWNESS)
.put(11, PotionType.JUMP)
.put(12, PotionType.INSTANT_DAMAGE)
.put(13, PotionType.WATER_BREATHING)
.put(14, PotionType.INVISIBILITY)
.build();
private static int getBit(final int n, final int k) {
return (n >> k) & 1;
}
@Override
public ItemStack createPotionItem(final Material initial, final int effectId) {
ItemStack potion = new ItemStack(initial, 1);
if (effectId == 0) {
return potion;
}
final int damageValue = getBit(effectId, 0) +
2 * getBit(effectId, 1) +
4 * getBit(effectId, 2) +
8 * getBit(effectId, 3);
final PotionType type = damageValueToType.get(damageValue);
if (type == null) {
throw new IllegalArgumentException("Unable to process potion effect ID " + effectId + " with damage value " + damageValue);
}
//getBit is splash here
if (getBit(effectId, 14) == 1 && initial == Material.POTION) {
potion = new ItemStack(Material.SPLASH_POTION, 1);
}
final PotionMeta meta = (PotionMeta) potion.getItemMeta();
//getBit(s) are extended and upgraded respectfully
final PotionData data = new PotionData(type, getBit(effectId, 6) == 1, getBit(effectId, 5) == 1);
meta.setBasePotionData(data); // this method is exclusive to recent 1.9+
potion.setItemMeta(meta);
return potion;
}
@Override
public String getDescription() {
return "1.9+ Potion Meta Provider";
}
}

View File

@ -1,20 +0,0 @@
package net.ess3.provider.providers;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
@SuppressWarnings("deprecation")
public class LegacyPotionMetaProvider implements PotionMetaProvider {
@Override
public ItemStack createPotionItem(final Material initial, final int effectId) {
final ItemStack potion = new ItemStack(initial, 1);
potion.setDurability((short) effectId);
return potion;
}
@Override
public String getDescription() {
return "Legacy 1.8 Potion Meta Provider";
}
}

View File

@ -0,0 +1,107 @@
package net.ess3.provider.providers;
import net.ess3.provider.PotionMetaProvider;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionType;
import java.util.Collection;
public class ModernPotionMetaProvider implements PotionMetaProvider {
@Override
public ItemStack createPotionItem(Material initial, int effectId) {
throw new UnsupportedOperationException("This should never happen, if this happens please submit a bug report!");
}
@Override
public void setBasePotionType(final ItemStack stack, PotionType type, final boolean extended, final boolean upgraded) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
if (extended && upgraded) {
throw new IllegalArgumentException("Potion cannot be both extended and upgraded");
}
final String name = type.name();
if (name.startsWith("LONG_")) {
type = PotionType.valueOf(name.substring(5));
} else if (name.startsWith("STRONG_")) {
type = PotionType.valueOf(name.substring(7));
}
if (extended && type.isExtendable()) {
type = PotionType.valueOf("LONG_" + type.name());
}
if (upgraded && type.isUpgradeable()) {
type = PotionType.valueOf("STRONG_" + type.name());
}
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
//noinspection DataFlowIssue
meta.setBasePotionType(type);
stack.setItemMeta(meta);
}
@Override
public Collection<PotionEffect> getCustomEffects(ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
//noinspection DataFlowIssue
return meta.getCustomEffects();
}
@Override
public boolean isSplashPotion(ItemStack stack) {
return stack != null && stack.getType() == Material.SPLASH_POTION;
}
@Override
public boolean isExtended(ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
//noinspection DataFlowIssue
return meta.getBasePotionType().name().startsWith("LONG_");
}
@Override
public boolean isUpgraded(ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
//noinspection DataFlowIssue
return meta.getBasePotionType().name().startsWith("STRONG_");
}
@Override
public PotionType getBasePotionType(ItemStack stack) {
final PotionMeta meta = (PotionMeta) stack.getItemMeta();
//noinspection DataFlowIssue
PotionType type = meta.getBasePotionType();
//noinspection DataFlowIssue
final String name = type.name();
if (name.startsWith("LONG_")) {
type = PotionType.valueOf(name.substring(5));
} else if (name.startsWith("STRONG_")) {
type = PotionType.valueOf(name.substring(7));
}
return type;
}
@Override
public void setSplashPotion(ItemStack stack, boolean isSplash) {
if (stack == null) {
throw new IllegalArgumentException("ItemStack cannot be null");
}
if (isSplash && stack.getType() == Material.POTION) {
stack.setType(Material.SPLASH_POTION);
} else if (!isSplash && stack.getType() == Material.SPLASH_POTION) {
stack.setType(Material.POTION);
}
}
@Override
public String getDescription() {
return "1.20.5+ Potion Meta Provider";
}
}