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;
|
2022-10-29 11:02:22 +02:00
|
|
|
import net.minestom.server.network.NetworkBuffer;
|
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;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2022-10-29 11:02:22 +02:00
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
import static net.minestom.server.network.NetworkBuffer.BYTE;
|
|
|
|
import static net.minestom.server.network.NetworkBuffer.VAR_INT;
|
|
|
|
|
2023-02-26 18:55:34 +01:00
|
|
|
/**
|
|
|
|
* Represents a potion effect that can be added to an {@link net.minestom.server.entity.Entity}.
|
|
|
|
*
|
|
|
|
* @param effect the potion effect
|
|
|
|
* @param amplifier the amplifier starting at 0 (level 1)
|
|
|
|
* @param duration the duration (in ticks) that the potion will last
|
|
|
|
* @param flags the flags of the potion, see {@link #flags()}
|
|
|
|
*/
|
2021-12-20 17:30:01 +01:00
|
|
|
public record Potion(@NotNull PotionEffect effect, byte amplifier,
|
2022-10-29 11:02:22 +02:00
|
|
|
int duration, byte flags) implements NetworkBuffer.Writer {
|
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;
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* A flag indicating that this Potion has particles.
|
|
|
|
*
|
|
|
|
* @see #AMBIENT_FLAG
|
|
|
|
* @see #ICON_FLAG
|
|
|
|
* @see #flags()
|
|
|
|
*/
|
|
|
|
public static final byte PARTICLES_FLAG = 0x02;
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* 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-12-20 17:30:01 +01:00
|
|
|
|
2023-02-26 18:55:34 +01:00
|
|
|
/**
|
|
|
|
* Creates a new Potion with no flags.
|
|
|
|
*
|
|
|
|
* @see #Potion(PotionEffect, byte, int, byte)
|
|
|
|
*/
|
2021-12-20 17:30:01 +01:00
|
|
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration) {
|
|
|
|
this(effect, amplifier, duration, (byte) 0);
|
|
|
|
}
|
|
|
|
|
2022-10-29 11:02:22 +02:00
|
|
|
public Potion(@NotNull NetworkBuffer reader) {
|
|
|
|
this(Objects.requireNonNull(PotionEffect.fromId(reader.read(VAR_INT))), reader.read(BYTE),
|
|
|
|
reader.read(VAR_INT), reader.read(BYTE));
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* Returns the flags that this Potion has.
|
2021-12-20 17:30:01 +01:00
|
|
|
*
|
2021-12-03 20:08:18 +01:00
|
|
|
* @see #AMBIENT_FLAG
|
|
|
|
* @see #PARTICLES_FLAG
|
|
|
|
* @see #ICON_FLAG
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public byte flags() {
|
|
|
|
return flags;
|
|
|
|
}
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this Potion is ambient (it came from a beacon) or not.
|
2021-12-20 17:30:01 +01:00
|
|
|
*
|
2021-12-03 20:08:18 +01:00
|
|
|
* @return <code>true</code> if the Potion is ambient
|
|
|
|
*/
|
|
|
|
public boolean isAmbient() {
|
2021-12-20 17:30:01 +01:00
|
|
|
return (flags & AMBIENT_FLAG) == AMBIENT_FLAG;
|
2021-12-03 20:08:18 +01:00
|
|
|
}
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this Potion has particles or not.
|
2021-12-20 17:30:01 +01:00
|
|
|
*
|
2021-12-03 20:08:18 +01:00
|
|
|
* @return <code>true</code> if the Potion has particles
|
|
|
|
*/
|
|
|
|
public boolean hasParticles() {
|
2021-12-20 17:30:01 +01:00
|
|
|
return (flags & PARTICLES_FLAG) == PARTICLES_FLAG;
|
2021-12-03 20:08:18 +01:00
|
|
|
}
|
2021-12-20 17:30:01 +01:00
|
|
|
|
2021-12-03 20:08:18 +01:00
|
|
|
/**
|
|
|
|
* Returns whether this Potion has an icon or not.
|
2021-12-20 17:30:01 +01:00
|
|
|
*
|
2021-12-03 20:08:18 +01:00
|
|
|
* @return <code>true</code> if the Potion has an icon
|
|
|
|
*/
|
|
|
|
public boolean hasIcon() {
|
2021-12-20 17:30:01 +01:00
|
|
|
return (flags & ICON_FLAG) == ICON_FLAG;
|
2021-12-03 20:08:18 +01:00
|
|
|
}
|
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) {
|
2022-09-05 13:54:58 +02:00
|
|
|
entity.sendPacketToViewersAndSelf(new EntityEffectPacket(entity.getEntityId(), this, null));
|
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
|
2022-10-29 11:02:22 +02:00
|
|
|
public void write(@NotNull NetworkBuffer writer) {
|
|
|
|
writer.write(VAR_INT, effect.id());
|
|
|
|
writer.write(BYTE, amplifier);
|
|
|
|
writer.write(VAR_INT, duration);
|
|
|
|
writer.write(BYTE, flags);
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
}
|