2020-12-31 00:12:03 +01:00
|
|
|
package net.minestom.server.potion;
|
|
|
|
|
2020-12-31 01:29:07 +01:00
|
|
|
import net.minestom.server.entity.Entity;
|
2020-12-31 00:12:03 +01:00
|
|
|
import net.minestom.server.network.packet.server.play.EntityEffectPacket;
|
|
|
|
import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket;
|
2021-11-30 17:49:41 +01:00
|
|
|
import net.minestom.server.utils.binary.BinaryReader;
|
|
|
|
import net.minestom.server.utils.binary.BinaryWriter;
|
|
|
|
import net.minestom.server.utils.binary.Writeable;
|
2020-12-31 00:12:03 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2021-11-30 17:49:41 +01:00
|
|
|
public record Potion(PotionEffect effect, byte amplifier, int duration, byte flags)
|
|
|
|
implements Writeable {
|
|
|
|
public Potion(BinaryReader reader) {
|
|
|
|
this(PotionEffect.fromId(reader.readVarInt()), reader.readByte(),
|
|
|
|
reader.readVarInt(), reader.readByte());
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Sends a packet that a potion effect has been applied to the entity.
|
|
|
|
* <p>
|
|
|
|
* Used internally by {@link net.minestom.server.entity.Player#addEffect(Potion)}
|
2020-12-31 12:05:36 +01:00
|
|
|
*
|
|
|
|
* @param entity the entity to add the effect to
|
2020-12-31 02:27:29 +01:00
|
|
|
*/
|
2020-12-31 01:29:07 +01:00
|
|
|
public void sendAddPacket(@NotNull Entity entity) {
|
2021-11-30 17:49:41 +01:00
|
|
|
entity.sendPacketToViewersAndSelf(new EntityEffectPacket(entity.getEntityId(), this));
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Sends a packet that a potion effect has been removed from the entity.
|
|
|
|
* <p>
|
|
|
|
* Used internally by {@link net.minestom.server.entity.Player#removeEffect(PotionEffect)}
|
2020-12-31 12:05:36 +01:00
|
|
|
*
|
|
|
|
* @param entity the entity to remove the effect from
|
2020-12-31 02:27:29 +01:00
|
|
|
*/
|
2020-12-31 01:29:07 +01:00
|
|
|
public void sendRemovePacket(@NotNull Entity entity) {
|
2021-11-30 17:49:41 +01:00
|
|
|
entity.sendPacketToViewersAndSelf(new RemoveEntityEffectPacket(entity.getEntityId(), effect));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(@NotNull BinaryWriter writer) {
|
|
|
|
writer.writeByte((byte) effect.id());
|
|
|
|
writer.writeByte(amplifier);
|
|
|
|
writer.writeVarInt(duration);
|
|
|
|
writer.writeByte(flags);
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
}
|