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;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
public class Potion {
|
2020-12-31 12:05:36 +01:00
|
|
|
|
2020-12-31 02:07:07 +01:00
|
|
|
private final PotionEffect effect;
|
|
|
|
private final byte amplifier;
|
|
|
|
private final int duration;
|
|
|
|
private final byte flags;
|
2020-12-31 00:12:03 +01:00
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Creates a new potion.
|
|
|
|
*
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param effect The type of potion.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param amplifier The strength of the potion.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param duration The length of the potion in ticks.
|
2020-12-31 02:27:29 +01:00
|
|
|
*/
|
2020-12-31 12:05:36 +01:00
|
|
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration) {
|
2020-12-31 00:12:03 +01:00
|
|
|
this(effect, amplifier, duration, true, true, false);
|
|
|
|
}
|
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Creates a new potion.
|
|
|
|
*
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param effect The type of potion.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param amplifier The strength of the potion.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param duration The length of the potion in ticks.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param particles If the potion has particles.
|
|
|
|
*/
|
2020-12-31 12:05:36 +01:00
|
|
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles) {
|
2020-12-31 00:12:03 +01:00
|
|
|
this(effect, amplifier, duration, particles, true, false);
|
|
|
|
}
|
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Creates a new potion.
|
|
|
|
*
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param effect The type of potion.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param amplifier The strength of the potion.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param duration The length of the potion in ticks.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param particles If the potion has particles.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param icon If the potion has an icon.
|
2020-12-31 02:27:29 +01:00
|
|
|
*/
|
2020-12-31 12:05:36 +01:00
|
|
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) {
|
2020-12-31 00:12:03 +01:00
|
|
|
this(effect, amplifier, duration, particles, icon, false);
|
|
|
|
}
|
|
|
|
|
2020-12-31 02:27:29 +01:00
|
|
|
/**
|
|
|
|
* Creates a new potion.
|
|
|
|
*
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param effect The type of potion.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param amplifier The strength of the potion.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param duration The length of the potion in ticks.
|
2020-12-31 02:27:29 +01:00
|
|
|
* @param particles If the potion has particles.
|
2020-12-31 12:05:36 +01:00
|
|
|
* @param icon If the potion has an icon.
|
|
|
|
* @param ambient If the potion came from a beacon.
|
2020-12-31 02:27:29 +01:00
|
|
|
*/
|
2020-12-31 12:05:36 +01:00
|
|
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) {
|
2020-12-31 00:12:03 +01:00
|
|
|
this.effect = effect;
|
|
|
|
this.amplifier = amplifier;
|
|
|
|
this.duration = duration;
|
2020-12-31 02:07:07 +01:00
|
|
|
byte flags = 0;
|
2020-12-31 02:27:29 +01:00
|
|
|
if (ambient) {
|
|
|
|
flags = (byte) (flags | 0x01);
|
|
|
|
}
|
|
|
|
if (particles) {
|
|
|
|
flags = (byte) (flags | 0x02);
|
|
|
|
}
|
|
|
|
if (icon) {
|
|
|
|
flags = (byte) (flags | 0x04);
|
|
|
|
}
|
2020-12-31 02:07:07 +01:00
|
|
|
this.flags = flags;
|
|
|
|
}
|
|
|
|
|
2020-12-31 12:05:36 +01:00
|
|
|
@NotNull
|
2020-12-31 02:07:07 +01:00
|
|
|
public PotionEffect getEffect() {
|
|
|
|
return effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte getAmplifier() {
|
|
|
|
return amplifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getDuration() {
|
|
|
|
return duration;
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte getFlags() {
|
2020-12-31 02:07:07 +01:00
|
|
|
return flags;
|
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) {
|
2020-12-31 12:05:36 +01:00
|
|
|
EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
|
|
|
|
entityEffectPacket.entityId = entity.getEntityId();
|
|
|
|
entityEffectPacket.potion = this;
|
|
|
|
entity.sendPacketToViewersAndSelf(entityEffectPacket);
|
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) {
|
2020-12-31 12:05:36 +01:00
|
|
|
RemoveEntityEffectPacket removeEntityEffectPacket = new RemoveEntityEffectPacket();
|
|
|
|
removeEntityEffectPacket.entityId = entity.getEntityId();
|
|
|
|
removeEntityEffectPacket.effect = effect;
|
|
|
|
entity.sendPacketToViewersAndSelf(removeEntityEffectPacket);
|
2020-12-31 00:12:03 +01:00
|
|
|
}
|
|
|
|
}
|