Minestom/src/main/java/net/minestom/server/potion/Potion.java

119 lines
3.6 KiB
Java
Raw Normal View History

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 {
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.
*
* @param effect The type of potion.
* @param amplifier The strength of the potion.
* @param duration The length of the potion in ticks.
*/
2020-12-31 00:12:03 +01:00
public Potion(PotionEffect effect, byte amplifier, int duration) {
this(effect, amplifier, duration, true, true, false);
}
2020-12-31 02:27:29 +01:00
/**
* Creates a new potion.
*
* @param effect The type of potion.
* @param amplifier The strength of the potion.
* @param duration The length of the potion in ticks.
* @param particles If the potion has particles.
*/
2020-12-31 00:12:03 +01:00
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles) {
this(effect, amplifier, duration, particles, true, false);
}
2020-12-31 02:27:29 +01:00
/**
* Creates a new potion.
*
* @param effect The type of potion.
* @param amplifier The strength of the potion.
* @param duration The length of the potion in ticks.
* @param particles If the potion has particles.
* @param icon If the potion has an icon.
*/
2020-12-31 00:12:03 +01:00
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) {
this(effect, amplifier, duration, particles, icon, false);
}
2020-12-31 02:27:29 +01:00
/**
* Creates a new potion.
*
* @param effect The type of potion.
* @param amplifier The strength of the potion.
* @param duration The length of the potion in ticks.
* @param particles If the potion has particles.
* @param icon If the potion has an icon.
* @param ambient If the potion came from a beacon.
*/
2020-12-31 00:12:03 +01:00
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) {
this.effect = effect;
this.amplifier = amplifier;
this.duration = duration;
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);
}
this.flags = flags;
}
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() {
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)}
* @param entity
*/
2020-12-31 01:29:07 +01:00
public void sendAddPacket(@NotNull Entity entity) {
2020-12-31 00:12:03 +01:00
EntityEffectPacket eep = new EntityEffectPacket();
2020-12-31 01:29:07 +01:00
eep.entityId = entity.getEntityId();
2020-12-31 00:12:03 +01:00
eep.potion = this;
2020-12-31 01:29:07 +01:00
entity.sendPacketToViewersAndSelf(eep);
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)}
* @param entity
*/
2020-12-31 01:29:07 +01:00
public void sendRemovePacket(@NotNull Entity entity) {
2020-12-31 00:12:03 +01:00
RemoveEntityEffectPacket reep = new RemoveEntityEffectPacket();
2020-12-31 01:29:07 +01:00
reep.entityId = entity.getEntityId();
2020-12-31 00:12:03 +01:00
reep.effect = effect;
2020-12-31 01:29:07 +01:00
entity.sendPacketToViewersAndSelf(reep);
2020-12-31 00:12:03 +01:00
}
}