Search in sources :

Example 36 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project LanternServer by LanternPowered.

the class PotionEffectHelper method merge.

public static List<PotionEffect> merge(Collection<PotionEffect> effectsA, Collection<PotionEffect> effectsB) {
    final Map<PotionEffectType, PotionEffect> effectsByType = new HashMap<>();
    for (PotionEffect effect : effectsA) {
        effectsByType.put(effect.getType(), effect);
    }
    final List<PotionEffect> result = new ArrayList<>();
    for (PotionEffect effect : effectsB) {
        final PotionEffect potionEffect = effectsByType.remove(effect.getType());
        if (potionEffect != null) {
            result.add(merge(effect, potionEffect));
        } else {
            result.add(effect);
        }
    }
    result.addAll(effectsByType.values());
    return result;
}
Also used : PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) HashMap(java.util.HashMap) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType) ArrayList(java.util.ArrayList)

Example 37 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project LanternServer by LanternPowered.

the class LivingEntityProtocol method update.

@Override
protected void update(EntityProtocolUpdateContext context) {
    super.update(context);
    final List<PotionEffect> potionEffects = this.entity.get(Keys.POTION_EFFECTS).orElse(Collections.emptyList());
    final Map<PotionEffectType, PotionEffect> potionEffectMap = new HashMap<>();
    for (PotionEffect potionEffect : potionEffects) {
        if (potionEffect.getDuration() > 0) {
            potionEffectMap.put(potionEffect.getType(), potionEffect);
        }
    }
    final long time = LanternGame.currentTimeTicks();
    if (this.lastPotionSendTime == -1L) {
        potionEffects.forEach(potionEffect -> context.sendToAll(() -> createAddMessage(potionEffect)));
    } else {
        final int delay = (int) (time - this.lastPotionSendTime);
        for (PotionEffect potionEffect : potionEffectMap.values()) {
            // noinspection ConstantConditions
            final PotionEffect oldEntry = this.lastPotionEffects.remove(potionEffect.getType());
            if (oldEntry == null || oldEntry.getDuration() - delay != potionEffect.getDuration() || oldEntry.getAmplifier() != potionEffect.getAmplifier() || oldEntry.isAmbient() != potionEffect.isAmbient() || oldEntry.getShowParticles() != potionEffect.getShowParticles()) {
                context.sendToAll(() -> createAddMessage(potionEffect));
            }
        }
        this.lastPotionEffects.values().forEach(potionEffect -> context.sendToAll(() -> new MessagePlayOutRemovePotionEffect(getRootEntityId(), potionEffect.getType())));
    }
    this.lastPotionSendTime = time;
    this.lastPotionEffects = potionEffectMap;
}
Also used : MessagePlayOutRemovePotionEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRemovePotionEffect) MessagePlayOutRemovePotionEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRemovePotionEffect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) MessagePlayOutAddPotionEffect(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutAddPotionEffect) HashMap(java.util.HashMap) PotionEffectType(org.spongepowered.api.effect.potion.PotionEffectType)

Aggregations

PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)37 Entity (org.spongepowered.api.entity.Entity)11 Player (org.spongepowered.api.entity.living.player.Player)11 ArrayList (java.util.ArrayList)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 List (java.util.List)5 PotionEffectType (org.spongepowered.api.effect.potion.PotionEffectType)5 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)4 MobEffectInstance (net.minecraft.world.effect.MobEffectInstance)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 IntegratedRunnable (com.skelril.nitro.time.IntegratedRunnable)3 TimedRunnable (com.skelril.nitro.time.TimedRunnable)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 BlockType (org.spongepowered.api.block.BlockType)3 Keys (org.spongepowered.api.data.Keys)3 Keys (org.spongepowered.api.data.key.Keys)3 Listener (org.spongepowered.api.event.Listener)3 Cause (org.spongepowered.api.event.cause.Cause)3