Essentials/providers/1_12Provider/src/main/java/net/ess3/provider/providers/LegacyPotionMetaProvider.java

117 lines
3.9 KiB
Java
Raw Normal View History

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.Potion;
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();
2020-10-03 19:46:05 +02:00
private static int getBit(final int n, final int k) {
return (n >> k) & 1;
}
2016-03-29 03:51:16 +02:00
@Override
2020-10-03 19:46:05 +02:00
public ItemStack createPotionItem(final Material initial, final int effectId) {
2016-04-02 04:52:20 +02:00
ItemStack potion = new ItemStack(initial, 1);
if (effectId == 0) {
2016-04-02 04:52:20 +02:00
return potion;
}
2020-10-03 19:46:05 +02:00
final int damageValue = getBit(effectId, 0) +
2 * getBit(effectId, 1) +
4 * getBit(effectId, 2) +
8 * getBit(effectId, 3);
2020-10-03 19:46:05 +02:00
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);
}
2020-10-03 19:46:05 +02:00
final PotionMeta meta = (PotionMeta) potion.getItemMeta();
//getBit(s) are extended and upgraded respectfully
2020-10-03 19:46:05 +02:00
final PotionData data = new PotionData(type, getBit(effectId, 6) == 1, getBit(effectId, 5) == 1);
2016-03-29 01:42:33 +02:00
meta.setBasePotionData(data); // this method is exclusive to recent 1.9+
2016-03-29 01:31:25 +02:00
potion.setItemMeta(meta);
return potion;
}
@Override
2024-04-26 17:48:24 +02:00
public AbstractPotionData getPotionData(ItemStack stack) {
return new AbstractPotionData() {
final Potion potion = Potion.fromDamage(stack.getDurability());
@Override
public boolean isSplash() {
return potion.isSplash();
}
@Override
public Collection<PotionEffect> getEffects() {
return potion.getEffects();
}
@Override
public PotionType getType() {
return ((PotionMeta) stack.getItemMeta()).getBasePotionData().getType();
}
@Override
public void setType(PotionType type) {
final PotionMeta itemMeta = (PotionMeta) stack.getItemMeta();
final PotionData data = itemMeta.getBasePotionData();
itemMeta.setBasePotionData(new PotionData(type, data.isExtended(), data.isUpgraded()));
stack.setItemMeta(itemMeta);
}
@Override
public int hashCode() {
return (31 * stack.getType().hashCode()) ^ ((PotionMeta) stack.getItemMeta()).getBasePotionData().hashCode();
}
};
}
@Override
2024-04-26 17:48:24 +02:00
public void updatePotionStack(ItemStack stack, AbstractPotionData data) {
return;
//todo
return;
}
2016-03-29 01:42:33 +02:00
@Override
2020-05-28 21:06:03 +02:00
public String getDescription() {
return "1.9-1.20.4 Potion Meta Provider";
2016-03-29 01:42:33 +02:00
}
}