mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 14:13:24 +02:00
Potion cleanup
This commit is contained in:
parent
4d8bd8430b
commit
fd920ffdfe
@ -134,7 +134,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
protected boolean noGravity;
|
protected boolean noGravity;
|
||||||
protected Pose pose = Pose.STANDING;
|
protected Pose pose = Pose.STANDING;
|
||||||
|
|
||||||
private CopyOnWriteArrayList<TimedPotion> effects = new CopyOnWriteArrayList<>();
|
private final List<TimedPotion> effects = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
// list of scheduled tasks to be executed during the next entity tick
|
// list of scheduled tasks to be executed during the next entity tick
|
||||||
protected final Queue<Consumer<Entity>> nextTick = Queues.newConcurrentLinkedQueue();
|
protected final Queue<Consumer<Entity>> nextTick = Queues.newConcurrentLinkedQueue();
|
||||||
@ -404,9 +404,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
|
|
||||||
// remove expired effects
|
// remove expired effects
|
||||||
{
|
{
|
||||||
effects.removeIf(timedPotion -> time
|
this.effects.removeIf(timedPotion -> {
|
||||||
>=
|
final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS;
|
||||||
(timedPotion.getStartingTime() + timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS));
|
return timedPotion.getStartingTime() + potionTime <= time;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// scheduled tasks
|
// scheduled tasks
|
||||||
@ -1455,8 +1456,14 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
DYING
|
DYING
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the potion effect of this entity.
|
||||||
|
*
|
||||||
|
* @return an unmodifiable list of all this entity effects
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
public List<TimedPotion> getActiveEffects() {
|
public List<TimedPotion> getActiveEffects() {
|
||||||
return effects;
|
return Collections.unmodifiableList(effects);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1465,7 +1472,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
* @param effect The effect to remove
|
* @param effect The effect to remove
|
||||||
*/
|
*/
|
||||||
public void removeEffect(@NotNull PotionEffect effect) {
|
public void removeEffect(@NotNull PotionEffect effect) {
|
||||||
effects.removeIf(timedPotion -> {
|
this.effects.removeIf(timedPotion -> {
|
||||||
if (timedPotion.getPotion().getEffect() == effect) {
|
if (timedPotion.getPotion().getEffect() == effect) {
|
||||||
timedPotion.getPotion().sendRemovePacket(this);
|
timedPotion.getPotion().sendRemovePacket(this);
|
||||||
return true;
|
return true;
|
||||||
@ -1481,7 +1488,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
*/
|
*/
|
||||||
public void addEffect(@NotNull Potion potion) {
|
public void addEffect(@NotNull Potion potion) {
|
||||||
removeEffect(potion.getEffect());
|
removeEffect(potion.getEffect());
|
||||||
effects.add(new TimedPotion(potion, System.currentTimeMillis()));
|
this.effects.add(new TimedPotion(potion, System.currentTimeMillis()));
|
||||||
potion.sendAddPacket(this);
|
potion.sendAddPacket(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class Potion {
|
public class Potion {
|
||||||
|
|
||||||
private final PotionEffect effect;
|
private final PotionEffect effect;
|
||||||
private final byte amplifier;
|
private final byte amplifier;
|
||||||
private final int duration;
|
private final int duration;
|
||||||
@ -14,50 +15,50 @@ public class Potion {
|
|||||||
/**
|
/**
|
||||||
* Creates a new potion.
|
* Creates a new potion.
|
||||||
*
|
*
|
||||||
* @param effect The type of potion.
|
* @param effect The type of potion.
|
||||||
* @param amplifier The strength of the potion.
|
* @param amplifier The strength of the potion.
|
||||||
* @param duration The length of the potion in ticks.
|
* @param duration The length of the potion in ticks.
|
||||||
*/
|
*/
|
||||||
public Potion(PotionEffect effect, byte amplifier, int duration) {
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration) {
|
||||||
this(effect, amplifier, duration, true, true, false);
|
this(effect, amplifier, duration, true, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new potion.
|
* Creates a new potion.
|
||||||
*
|
*
|
||||||
* @param effect The type of potion.
|
* @param effect The type of potion.
|
||||||
* @param amplifier The strength of the potion.
|
* @param amplifier The strength of the potion.
|
||||||
* @param duration The length of the potion in ticks.
|
* @param duration The length of the potion in ticks.
|
||||||
* @param particles If the potion has particles.
|
* @param particles If the potion has particles.
|
||||||
*/
|
*/
|
||||||
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles) {
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles) {
|
||||||
this(effect, amplifier, duration, particles, true, false);
|
this(effect, amplifier, duration, particles, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new potion.
|
* Creates a new potion.
|
||||||
*
|
*
|
||||||
* @param effect The type of potion.
|
* @param effect The type of potion.
|
||||||
* @param amplifier The strength of the potion.
|
* @param amplifier The strength of the potion.
|
||||||
* @param duration The length of the potion in ticks.
|
* @param duration The length of the potion in ticks.
|
||||||
* @param particles If the potion has particles.
|
* @param particles If the potion has particles.
|
||||||
* @param icon If the potion has an icon.
|
* @param icon If the potion has an icon.
|
||||||
*/
|
*/
|
||||||
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) {
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon) {
|
||||||
this(effect, amplifier, duration, particles, icon, false);
|
this(effect, amplifier, duration, particles, icon, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new potion.
|
* Creates a new potion.
|
||||||
*
|
*
|
||||||
* @param effect The type of potion.
|
* @param effect The type of potion.
|
||||||
* @param amplifier The strength of the potion.
|
* @param amplifier The strength of the potion.
|
||||||
* @param duration The length of the potion in ticks.
|
* @param duration The length of the potion in ticks.
|
||||||
* @param particles If the potion has particles.
|
* @param particles If the potion has particles.
|
||||||
* @param icon If the potion has an icon.
|
* @param icon If the potion has an icon.
|
||||||
* @param ambient If the potion came from a beacon.
|
* @param ambient If the potion came from a beacon.
|
||||||
*/
|
*/
|
||||||
public Potion(PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) {
|
public Potion(@NotNull PotionEffect effect, byte amplifier, int duration, boolean particles, boolean icon, boolean ambient) {
|
||||||
this.effect = effect;
|
this.effect = effect;
|
||||||
this.amplifier = amplifier;
|
this.amplifier = amplifier;
|
||||||
this.duration = duration;
|
this.duration = duration;
|
||||||
@ -74,6 +75,7 @@ public class Potion {
|
|||||||
this.flags = flags;
|
this.flags = flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public PotionEffect getEffect() {
|
public PotionEffect getEffect() {
|
||||||
return effect;
|
return effect;
|
||||||
}
|
}
|
||||||
@ -94,25 +96,27 @@ public class Potion {
|
|||||||
* Sends a packet that a potion effect has been applied to the entity.
|
* Sends a packet that a potion effect has been applied to the entity.
|
||||||
* <p>
|
* <p>
|
||||||
* Used internally by {@link net.minestom.server.entity.Player#addEffect(Potion)}
|
* Used internally by {@link net.minestom.server.entity.Player#addEffect(Potion)}
|
||||||
* @param entity
|
*
|
||||||
|
* @param entity the entity to add the effect to
|
||||||
*/
|
*/
|
||||||
public void sendAddPacket(@NotNull Entity entity) {
|
public void sendAddPacket(@NotNull Entity entity) {
|
||||||
EntityEffectPacket eep = new EntityEffectPacket();
|
EntityEffectPacket entityEffectPacket = new EntityEffectPacket();
|
||||||
eep.entityId = entity.getEntityId();
|
entityEffectPacket.entityId = entity.getEntityId();
|
||||||
eep.potion = this;
|
entityEffectPacket.potion = this;
|
||||||
entity.sendPacketToViewersAndSelf(eep);
|
entity.sendPacketToViewersAndSelf(entityEffectPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a packet that a potion effect has been removed from the entity.
|
* Sends a packet that a potion effect has been removed from the entity.
|
||||||
* <p>
|
* <p>
|
||||||
* Used internally by {@link net.minestom.server.entity.Player#removeEffect(PotionEffect)}
|
* Used internally by {@link net.minestom.server.entity.Player#removeEffect(PotionEffect)}
|
||||||
* @param entity
|
*
|
||||||
|
* @param entity the entity to remove the effect from
|
||||||
*/
|
*/
|
||||||
public void sendRemovePacket(@NotNull Entity entity) {
|
public void sendRemovePacket(@NotNull Entity entity) {
|
||||||
RemoveEntityEffectPacket reep = new RemoveEntityEffectPacket();
|
RemoveEntityEffectPacket removeEntityEffectPacket = new RemoveEntityEffectPacket();
|
||||||
reep.entityId = entity.getEntityId();
|
removeEntityEffectPacket.entityId = entity.getEntityId();
|
||||||
reep.effect = effect;
|
removeEntityEffectPacket.effect = effect;
|
||||||
entity.sendPacketToViewersAndSelf(reep);
|
entity.sendPacketToViewersAndSelf(removeEntityEffectPacket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package net.minestom.server.potion;
|
package net.minestom.server.potion;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class TimedPotion {
|
public class TimedPotion {
|
||||||
|
|
||||||
private final Potion potion;
|
private final Potion potion;
|
||||||
private final long startingTime;
|
private final long startingTime;
|
||||||
|
|
||||||
public TimedPotion(Potion potion, long startingTime) {
|
public TimedPotion(@NotNull Potion potion, long startingTime) {
|
||||||
this.potion = potion;
|
this.potion = potion;
|
||||||
this.startingTime = startingTime;
|
this.startingTime = startingTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public Potion getPotion() {
|
public Potion getPotion() {
|
||||||
return potion;
|
return potion;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user