Search in sources :

Example 1 with MessagePlayOutRemovePotionEffect

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRemovePotionEffect 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

HashMap (java.util.HashMap)1 MessagePlayOutAddPotionEffect (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutAddPotionEffect)1 MessagePlayOutRemovePotionEffect (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutRemovePotionEffect)1 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)1 PotionEffectType (org.spongepowered.api.effect.potion.PotionEffectType)1