Newer
Older
ultramine_bukkit / src / main / java / org / bukkit / craftbukkit / potion / CraftPotionBrewer.java
@vlad20012 vlad20012 on 24 Feb 2017 1 KB initial
package org.bukkit.craftbukkit.potion;

import com.google.common.collect.Maps;
import org.bukkit.potion.PotionBrewer;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class CraftPotionBrewer implements PotionBrewer
{
	private static final Map<Integer, Collection<PotionEffect>> cache = Maps.newHashMap();

	public Collection<PotionEffect> getEffectsFromDamage(int damage)
	{
		if(cache.containsKey(damage))
			return cache.get(damage);

		List<?> mcEffects = net.minecraft.potion.PotionHelper.getPotionEffects(damage, false);
		List<PotionEffect> effects = new ArrayList<PotionEffect>();
		if(mcEffects == null)
			return effects;

		for(Object raw : mcEffects)
		{
			if(raw == null || !(raw instanceof net.minecraft.potion.PotionEffect))
				continue;
			net.minecraft.potion.PotionEffect mcEffect = (net.minecraft.potion.PotionEffect) raw;
			PotionEffect effect = new PotionEffect(PotionEffectType.getById(mcEffect.getPotionID()),
					mcEffect.getDuration(), mcEffect.getAmplifier());
			// Minecraft PotionBrewer applies duration modifiers automatically.
			effects.add(effect);
		}

		cache.put(damage, effects);

		return effects;
	}

	public PotionEffect createEffect(PotionEffectType potion, int duration, int amplifier)
	{
		return new PotionEffect(potion, potion.isInstant() ? 1 : (int) (duration * potion.getDurationModifier()),
				amplifier);
	}
}