[Experimental] Attempt restoring potion effect ID compatibility to 1.9

This commit is contained in:
vemacs 2016-03-28 16:54:17 -06:00
parent 8167002092
commit 8116ce39d7
2 changed files with 37 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.NumberUtil;
import com.earth2me.essentials.utils.PotionMetaUtil;
import com.earth2me.essentials.utils.StringUtil; import com.earth2me.essentials.utils.StringUtil;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.Bukkit; 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."); throw new Exception("Can't spawn entity ID " + metaData + " from spawn eggs.");
} }
retval = ess.getSpawnEggProvider().createEggItem(type); retval = ess.getSpawnEggProvider().createEggItem(type);
} else if (mat == Material.POTION) {
retval = PotionMetaUtil.createPotionItem(metaData);
} else { } else {
retval.setDurability(metaData); retval.setDurability(metaData);
} }

View File

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