mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-19 13:51:22 +01:00
feat: use ticks for potion duration instead of system time (#2050)
* feat: use ticks for potion duration instead of system time * chore: remove redudant getter
This commit is contained in:
parent
59ea880d26
commit
204b447cdb
@ -574,7 +574,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
EventDispatcher.call(new EntityTickEvent(this));
|
EventDispatcher.call(new EntityTickEvent(this));
|
||||||
|
|
||||||
// remove expired effects
|
// remove expired effects
|
||||||
effectTick(time);
|
effectTick();
|
||||||
}
|
}
|
||||||
// Scheduled synchronization
|
// Scheduled synchronization
|
||||||
if (ticks >= nextSynchronizationTick) {
|
if (ticks >= nextSynchronizationTick) {
|
||||||
@ -640,18 +640,17 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void effectTick(long time) {
|
private void effectTick() {
|
||||||
final List<TimedPotion> effects = this.effects;
|
final List<TimedPotion> effects = this.effects;
|
||||||
if (effects.isEmpty()) return;
|
if (effects.isEmpty()) return;
|
||||||
effects.removeIf(timedPotion -> {
|
effects.removeIf(timedPotion -> {
|
||||||
long duration = timedPotion.getPotion().duration();
|
long duration = timedPotion.potion().duration();
|
||||||
if (duration == Potion.INFINITE_DURATION) return false;
|
if (duration == Potion.INFINITE_DURATION) return false;
|
||||||
final long potionTime = duration * MinecraftServer.TICK_MS;
|
|
||||||
// Remove if the potion should be expired
|
// Remove if the potion should be expired
|
||||||
if (time >= timedPotion.getStartingTime() + potionTime) {
|
if (getAliveTicks() >= timedPotion.startingTicks() + duration) {
|
||||||
// Send the packet that the potion should no longer be applied
|
// Send the packet that the potion should no longer be applied
|
||||||
timedPotion.getPotion().sendRemovePacket(this);
|
timedPotion.potion().sendRemovePacket(this);
|
||||||
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.getPotion()));
|
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.potion()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1422,7 +1421,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
*/
|
*/
|
||||||
public void addEffect(@NotNull Potion potion) {
|
public void addEffect(@NotNull Potion potion) {
|
||||||
removeEffect(potion.effect());
|
removeEffect(potion.effect());
|
||||||
this.effects.add(new TimedPotion(potion, System.currentTimeMillis()));
|
this.effects.add(new TimedPotion(potion, getAliveTicks()));
|
||||||
potion.sendAddPacket(this);
|
potion.sendAddPacket(this);
|
||||||
EventDispatcher.call(new EntityPotionAddEvent(this, potion));
|
EventDispatcher.call(new EntityPotionAddEvent(this, potion));
|
||||||
}
|
}
|
||||||
@ -1434,9 +1433,9 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
*/
|
*/
|
||||||
public void removeEffect(@NotNull PotionEffect effect) {
|
public void removeEffect(@NotNull PotionEffect effect) {
|
||||||
this.effects.removeIf(timedPotion -> {
|
this.effects.removeIf(timedPotion -> {
|
||||||
if (timedPotion.getPotion().effect() == effect) {
|
if (timedPotion.potion().effect() == effect) {
|
||||||
timedPotion.getPotion().sendRemovePacket(this);
|
timedPotion.potion().sendRemovePacket(this);
|
||||||
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.getPotion()));
|
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.potion()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -1449,7 +1448,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
* @param effect the effect to check
|
* @param effect the effect to check
|
||||||
*/
|
*/
|
||||||
public boolean hasEffect(@NotNull PotionEffect effect) {
|
public boolean hasEffect(@NotNull PotionEffect effect) {
|
||||||
return this.effects.stream().anyMatch(timedPotion -> timedPotion.getPotion().effect() == effect);
|
return this.effects.stream().anyMatch(timedPotion -> timedPotion.potion().effect() == effect);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1459,7 +1458,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
* @return the effect, null if not found
|
* @return the effect, null if not found
|
||||||
*/
|
*/
|
||||||
public @Nullable TimedPotion getEffect(@NotNull PotionEffect effect) {
|
public @Nullable TimedPotion getEffect(@NotNull PotionEffect effect) {
|
||||||
return this.effects.stream().filter(timedPotion -> timedPotion.getPotion().effect() == effect).findFirst().orElse(null);
|
return this.effects.stream().filter(timedPotion -> timedPotion.potion().effect() == effect).findFirst().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1470,7 +1469,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
*/
|
*/
|
||||||
public int getEffectLevel(@NotNull PotionEffect effect) {
|
public int getEffectLevel(@NotNull PotionEffect effect) {
|
||||||
TimedPotion timedPotion = getEffect(effect);
|
TimedPotion timedPotion = getEffect(effect);
|
||||||
return timedPotion == null ? 0 : timedPotion.getPotion().amplifier();
|
return timedPotion == null ? 0 : timedPotion.potion().amplifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1478,8 +1477,8 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
|
|||||||
*/
|
*/
|
||||||
public void clearEffects() {
|
public void clearEffects() {
|
||||||
for (TimedPotion timedPotion : effects) {
|
for (TimedPotion timedPotion : effects) {
|
||||||
timedPotion.getPotion().sendRemovePacket(this);
|
timedPotion.potion().sendRemovePacket(this);
|
||||||
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.getPotion()));
|
EventDispatcher.call(new EntityPotionRemoveEvent(this, timedPotion.potion()));
|
||||||
}
|
}
|
||||||
this.effects.clear();
|
this.effects.clear();
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,3 @@
|
|||||||
package net.minestom.server.potion;
|
package net.minestom.server.potion;
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
public record TimedPotion(Potion potion, long startingTicks) {}
|
||||||
|
|
||||||
public class TimedPotion {
|
|
||||||
|
|
||||||
private final Potion potion;
|
|
||||||
private final long startingTime;
|
|
||||||
|
|
||||||
public TimedPotion(@NotNull Potion potion, long startingTime) {
|
|
||||||
this.potion = potion;
|
|
||||||
this.startingTime = startingTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public Potion getPotion() {
|
|
||||||
return potion;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getStartingTime() {
|
|
||||||
return startingTime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user