Entity cleanup

This commit is contained in:
themode 2021-01-03 00:23:41 +01:00
parent 2152ef64cf
commit 317432460e
4 changed files with 77 additions and 78 deletions

View File

@ -10,12 +10,7 @@ import net.minestom.server.data.Data;
import net.minestom.server.data.DataContainer; import net.minestom.server.data.DataContainer;
import net.minestom.server.event.Event; import net.minestom.server.event.Event;
import net.minestom.server.event.EventCallback; import net.minestom.server.event.EventCallback;
import net.minestom.server.event.entity.EntityDeathEvent; import net.minestom.server.event.entity.*;
import net.minestom.server.event.entity.EntitySpawnEvent;
import net.minestom.server.event.entity.EntityPotionAddEvent;
import net.minestom.server.event.entity.EntityPotionRemoveEvent;
import net.minestom.server.event.entity.EntityTickEvent;
import net.minestom.server.event.entity.EntityVelocityEvent;
import net.minestom.server.event.handler.EventHandler; import net.minestom.server.event.handler.EventHandler;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
@ -404,24 +399,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
return; return;
} }
// remove expired effects
{
this.effects.removeIf(timedPotion -> {
final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS;
// Remove if the potion should be expired
if (time >= timedPotion.getStartingTime() + potionTime) {
// Send the packet that the potion should no longer be applied
timedPotion.getPotion().sendRemovePacket(this);
callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
});
}
// scheduled tasks // scheduled tasks
if (!nextTick.isEmpty()) { if (!nextTick.isEmpty()) {
Consumer<Entity> callback; Consumer<Entity> callback;
@ -572,7 +549,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
sendSynchronization(); sendSynchronization();
// Verify if velocity packet has to be sent // Verify if velocity packet has to be sent
if (hasVelocity() || gravityTickCount > 0) { if (hasVelocity() || gravityTickCount > 0) {
sendVelocityPacket(); sendPacketsToViewers(getVelocityPacket());
} }
} }
@ -614,6 +591,24 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
ticks++; ticks++;
callEvent(EntityTickEvent.class, tickEvent); // reuse tickEvent to avoid recreating it each tick callEvent(EntityTickEvent.class, tickEvent); // reuse tickEvent to avoid recreating it each tick
// remove expired effects
{
this.effects.removeIf(timedPotion -> {
final long potionTime = (long) timedPotion.getPotion().getDuration() * MinecraftServer.TICK_MS;
// Remove if the potion should be expired
if (time >= timedPotion.getStartingTime() + potionTime) {
// Send the packet that the potion should no longer be applied
timedPotion.getPotion().sendRemovePacket(this);
callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
});
}
} }
// Scheduled synchronization // Scheduled synchronization
@ -627,13 +622,6 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
} }
} }
/**
* Equivalent to <code>sendPacketsToViewers(getVelocityPacket());</code>.
*/
public void sendVelocityPacket() {
sendPacketsToViewers(getVelocityPacket());
}
/** /**
* Gets the number of ticks this entity has been active for. * Gets the number of ticks this entity has been active for.
* *
@ -694,6 +682,10 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
* @param uuid the new entity uuid * @param uuid the new entity uuid
*/ */
protected void setUuid(@NotNull UUID uuid) { protected void setUuid(@NotNull UUID uuid) {
// Refresh internal map
Entity.entityByUuid.remove(this.uuid);
Entity.entityByUuid.put(uuid, this);
this.uuid = uuid; this.uuid = uuid;
} }
@ -1242,6 +1234,47 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
return boundingBox.getHeight() * 0.85f; return boundingBox.getHeight() * 0.85f;
} }
/**
* Gets all the potion effect of this entity.
*
* @return an unmodifiable list of all this entity effects
*/
@NotNull
public List<TimedPotion> getActiveEffects() {
return Collections.unmodifiableList(effects);
}
/**
* Removes effect from entity, if it has it.
*
* @param effect The effect to remove
*/
public void removeEffect(@NotNull PotionEffect effect) {
this.effects.removeIf(timedPotion -> {
if (timedPotion.getPotion().getEffect() == effect) {
timedPotion.getPotion().sendRemovePacket(this);
callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
});
}
/**
* Adds an effect to an entity.
*
* @param potion The potion to add
*/
public void addEffect(@NotNull Potion potion) {
removeEffect(potion.getEffect());
this.effects.add(new TimedPotion(potion, System.currentTimeMillis()));
potion.sendAddPacket(this);
callEvent(EntityPotionAddEvent.class, new EntityPotionAddEvent(this, potion));
}
/** /**
* Removes the entity from the server immediately. * Removes the entity from the server immediately.
* <p> * <p>
@ -1468,50 +1501,6 @@ 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() {
return Collections.unmodifiableList(effects);
}
/**
* Removes effect from entity, if it has it.
*
* @param effect The effect to remove
*/
public void removeEffect(@NotNull PotionEffect effect) {
this.effects.removeIf(timedPotion -> {
if (timedPotion.getPotion().getEffect() == effect) {
timedPotion.getPotion().sendRemovePacket(this);
callEvent(EntityPotionRemoveEvent.class, new EntityPotionRemoveEvent(
this,
timedPotion.getPotion()
));
return true;
}
return false;
});
}
/**
* Adds an effect to an entity.
*
* @param potion The potion to add
*/
public void addEffect(@NotNull Potion potion) {
removeEffect(potion.getEffect());
this.effects.add(new TimedPotion(potion, System.currentTimeMillis()));
potion.sendAddPacket(this);
callEvent(EntityPotionAddEvent.class, new EntityPotionAddEvent(
this,
potion
));
}
protected boolean shouldRemove() { protected boolean shouldRemove() {
return shouldRemove; return shouldRemove;
} }

View File

@ -107,8 +107,14 @@ public final class EntityManager {
final String username = asyncPlayerPreLoginEvent.getUsername(); final String username = asyncPlayerPreLoginEvent.getUsername();
final UUID uuid = asyncPlayerPreLoginEvent.getPlayerUuid(); final UUID uuid = asyncPlayerPreLoginEvent.getPlayerUuid();
player.setUsername(username); if (!player.getUsername().equals(username)) {
player.setUuid(uuid); player.setUsername(username);
}
if (!player.getUuid().equals(uuid)) {
player.setUuid(uuid);
}
} }
// Add the player to the waiting list // Add the player to the waiting list

View File

@ -6,6 +6,7 @@ import net.minestom.server.potion.Potion;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class EntityPotionAddEvent extends EntityEvent { public class EntityPotionAddEvent extends EntityEvent {
private final Potion potion; private final Potion potion;
public EntityPotionAddEvent(@NotNull Entity entity, @NotNull Potion potion) { public EntityPotionAddEvent(@NotNull Entity entity, @NotNull Potion potion) {
@ -18,6 +19,7 @@ public class EntityPotionAddEvent extends EntityEvent {
* *
* @return the added potion. * @return the added potion.
*/ */
@NotNull
public Potion getPotion() { public Potion getPotion() {
return potion; return potion;
} }

View File

@ -6,6 +6,7 @@ import net.minestom.server.potion.Potion;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class EntityPotionRemoveEvent extends EntityEvent { public class EntityPotionRemoveEvent extends EntityEvent {
private final Potion potion; private final Potion potion;
public EntityPotionRemoveEvent(@NotNull Entity entity, @NotNull Potion potion) { public EntityPotionRemoveEvent(@NotNull Entity entity, @NotNull Potion potion) {
@ -18,6 +19,7 @@ public class EntityPotionRemoveEvent extends EntityEvent {
* *
* @return the removed potion. * @return the removed potion.
*/ */
@NotNull
public Potion getPotion() { public Potion getPotion() {
return potion; return potion;
} }