From 8116ce39d7f60e5e1883f59214a10f92a4b2f513 Mon Sep 17 00:00:00 2001 From: vemacs Date: Mon, 28 Mar 2016 16:54:17 -0600 Subject: [PATCH] [Experimental] Attempt restoring potion effect ID compatibility to 1.9 --- .../src/com/earth2me/essentials/ItemDb.java | 3 ++ .../essentials/utils/PotionMetaUtil.java | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Essentials/src/com/earth2me/essentials/utils/PotionMetaUtil.java diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 4d061ae16..9ecdb0c08 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -1,6 +1,7 @@ package com.earth2me.essentials; import com.earth2me.essentials.utils.NumberUtil; +import com.earth2me.essentials.utils.PotionMetaUtil; import com.earth2me.essentials.utils.StringUtil; import net.ess3.api.IEssentials; import org.bukkit.Bukkit; @@ -151,6 +152,8 @@ public class ItemDb implements IConf, net.ess3.api.IItemDb { throw new Exception("Can't spawn entity ID " + metaData + " from spawn eggs."); } retval = ess.getSpawnEggProvider().createEggItem(type); + } else if (mat == Material.POTION) { + retval = PotionMetaUtil.createPotionItem(metaData); } else { retval.setDurability(metaData); } diff --git a/Essentials/src/com/earth2me/essentials/utils/PotionMetaUtil.java b/Essentials/src/com/earth2me/essentials/utils/PotionMetaUtil.java new file mode 100644 index 000000000..1fdd68c05 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/utils/PotionMetaUtil.java @@ -0,0 +1,34 @@ +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 static 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 static int getBit(int n, int k) { + return (n >> k) & 1; + } +}