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 {
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* A flag indicating that this Potion is ambient (it came from a beacon).
|
|
|
|
*
|
|
|
|
* @see #PARTICLES_FLAG
|
|
|
|
* @see #ICON_FLAG
|
|
|
|
* @see #flags()
|
|
|
|
*/
|
|
|
|
public static final byte AMBIENT_FLAG = 0x01;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A flag indicating that this Potion has particles.
|
|
|
|
*
|
|
|
|
* @see #AMBIENT_FLAG
|
|
|
|
* @see #ICON_FLAG
|
|
|
|
* @see #flags()
|
|
|
|
*/
|
|
|
|
public static final byte PARTICLES_FLAG = 0x02;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A flag indicating that this Potion has an icon.
|
|
|
|
*
|
|
|
|
* @see #AMBIENT_FLAG
|
|
|
|
* @see #PARTICLES_FLAG
|
|
|
|
* @see #flags()
|
|
|
|
*/
|
|
|
|
public static final byte ICON_FLAG = 0x04;
|
|
|
|
|
2021-11-30 17:49:41 +01:00
|
|
|
public Potion(BinaryReader reader) {
|
|
|
|
this(PotionEffect.fromId(reader.readVarInt()), reader.readByte(),
|
|
|
|
reader.readVarInt(), reader.readByte());
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
2021-12-03 20:08:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the flags that this Potion has.
|
|
|
|
*
|
|
|
|
* @see #AMBIENT_FLAG
|
|
|
|
* @see #PARTICLES_FLAG
|
|
|
|
* @see #ICON_FLAG
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public byte flags() {
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this Potion is ambient (it came from a beacon) or not.
|
|
|
|
* @return <code>true</code> if the Potion is ambient
|
|
|
|
*/
|
|
|
|
public boolean isAmbient() {
|
|
|
|
return (flags & AMBIENT_FLAG) == AMBIENT_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this Potion has particles or not.
|
|
|
|
* @return <code>true</code> if the Potion has particles
|
|
|
|
*/
|
|
|
|
public boolean hasParticles() {
|
|
|
|
return (flags & PARTICLES_FLAG) == PARTICLES_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this Potion has an icon or not.
|
|
|
|
* @return <code>true</code> if the Potion has an icon
|
|
|
|
*/
|
|
|
|
public boolean hasIcon() {
|
|
|
|
return (flags & ICON_FLAG) == ICON_FLAG;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|