Skip to content

Instantly share code, notes, and snippets.

@vemacs
Created March 28, 2016 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vemacs/fef1501081fc9ae92fcb to your computer and use it in GitHub Desktop.
Save vemacs/fef1501081fc9ae92fcb to your computer and use it in GitHub Desktop.
package com.earth2me.essentials.utils;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionType;
public class PotionMetaUtil {
@SuppressWarnings("deprecation")
public ItemStack createPotionItem(int effectId) throws IllegalArgumentException {
int damageValue = getBit(effectId, 0) +
2 * getBit(effectId, 1) +
4 * getBit(effectId, 2) +
8 * getBit(effectId, 3);
PotionType type = PotionType.getByDamageValue(damageValue);
if (getBit(effectId, 15) != 1 || type == null) {
throw new IllegalArgumentException("Unable to process potion effect ID " + effectId);
}
int level = getBit(effectId, 5) + 1;
boolean extended = getBit(effectId, 6) == 1;
boolean splash = getBit(effectId, 14) == 1;
Potion potion = new Potion(type, level);
potion.setHasExtendedDuration(extended);
potion.setSplash(splash);
return potion.toItemStack(1);
}
private int getBit(int n, int k) {
return (n >> k) & 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment