mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-29 04:28:21 +01:00
Make Potions use time instead of nanoTime
This commit is contained in:
parent
6a154eb15a
commit
fa41aabaed
@ -24,6 +24,7 @@ import net.minestom.server.permission.Permission;
|
|||||||
import net.minestom.server.permission.PermissionHandler;
|
import net.minestom.server.permission.PermissionHandler;
|
||||||
import net.minestom.server.potion.Potion;
|
import net.minestom.server.potion.Potion;
|
||||||
import net.minestom.server.potion.PotionEffect;
|
import net.minestom.server.potion.PotionEffect;
|
||||||
|
import net.minestom.server.potion.TimedPotion;
|
||||||
import net.minestom.server.thread.ThreadProvider;
|
import net.minestom.server.thread.ThreadProvider;
|
||||||
import net.minestom.server.utils.BlockPosition;
|
import net.minestom.server.utils.BlockPosition;
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.Position;
|
||||||
@ -43,6 +44,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -132,8 +134,8 @@ 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 ArrayList<Potion> effects = new ArrayList<>();
|
private CopyOnWriteArrayList<TimedPotion> effects = new CopyOnWriteArrayList<>();
|
||||||
private ArrayList<Long> effectTimes = new ArrayList<>();
|
private CopyOnWriteArrayList<Potion> scheduledPotions = 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();
|
||||||
@ -403,25 +405,17 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
|
|
||||||
// remove expired effects
|
// remove expired effects
|
||||||
{
|
{
|
||||||
if (effects.size() > 0) {
|
effects.removeIf(timedPotion -> time >=
|
||||||
boolean foundToRemove = true;
|
(timedPotion.startingTime + timedPotion.potion.duration * 50));
|
||||||
while (foundToRemove) {
|
}
|
||||||
foundToRemove = false;
|
|
||||||
int i = 0;
|
// add queued effects
|
||||||
while (i < effects.size()) {
|
{
|
||||||
if (effects.get(i).duration * 50 +
|
for (Potion potion : scheduledPotions) {
|
||||||
effectTimes.get(i) <= System.nanoTime() / 1000000) {
|
effects.add(new TimedPotion(potion, time));
|
||||||
foundToRemove = true;
|
potion.sendAddPacket(this);
|
||||||
break;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (foundToRemove) {
|
|
||||||
effects.remove(i);
|
|
||||||
effectTimes.remove(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
scheduledPotions.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// scheduled tasks
|
// scheduled tasks
|
||||||
@ -1470,7 +1464,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
DYING
|
DYING
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Potion> getActiveEffects() {
|
public List<TimedPotion> getActiveEffects() {
|
||||||
return effects;
|
return effects;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1480,21 +1474,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) {
|
||||||
if (effects.size() == 0) return;
|
effects.removeIf(timedPotion -> timedPotion.potion.effect == effect);
|
||||||
int i = 0;
|
|
||||||
boolean found = false;
|
|
||||||
while (i < effects.size()) {
|
|
||||||
if (effects.get(i).effect == effect) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (found) {
|
|
||||||
effects.get(i).sendRemovePacket(this);
|
|
||||||
effects.remove(i);
|
|
||||||
effectTimes.remove(i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1504,9 +1484,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
*/
|
*/
|
||||||
public void addEffect(@NotNull Potion potion) {
|
public void addEffect(@NotNull Potion potion) {
|
||||||
removeEffect(potion.effect);
|
removeEffect(potion.effect);
|
||||||
effects.add(potion);
|
scheduledPotions.add(potion);
|
||||||
effectTimes.add(System.nanoTime() / 1000000);
|
|
||||||
potion.sendAddPacket(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean shouldRemove() {
|
protected boolean shouldRemove() {
|
||||||
|
11
src/main/java/net/minestom/server/potion/TimedPotion.java
Normal file
11
src/main/java/net/minestom/server/potion/TimedPotion.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package net.minestom.server.potion;
|
||||||
|
|
||||||
|
public class TimedPotion {
|
||||||
|
public Potion potion;
|
||||||
|
public Long startingTime;
|
||||||
|
|
||||||
|
public TimedPotion(Potion potion, Long startingTime) {
|
||||||
|
this.potion = potion;
|
||||||
|
this.startingTime = startingTime;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user