mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 14:13:24 +02:00
Added EntityCreature#switchEntityType
This commit is contained in:
parent
cd44732cbc
commit
6bd6d22651
@ -103,7 +103,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer, P
|
|||||||
|
|
||||||
private final Set<Entity> passengers = new CopyOnWriteArraySet<>();
|
private final Set<Entity> passengers = new CopyOnWriteArraySet<>();
|
||||||
private long lastUpdate;
|
private long lastUpdate;
|
||||||
private final EntityType entityType;
|
protected EntityType entityType; // UNSAFE to change, modify at your own risk
|
||||||
|
|
||||||
// Network synchronization, send the absolute position of the entity each X milliseconds
|
// Network synchronization, send the absolute position of the entity each X milliseconds
|
||||||
private static final UpdateOption SYNCHRONIZATION_COOLDOWN = new UpdateOption(1500, TimeUnit.MILLISECOND);
|
private static final UpdateOption SYNCHRONIZATION_COOLDOWN = new UpdateOption(1500, TimeUnit.MILLISECOND);
|
||||||
|
@ -21,7 +21,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public abstract class EntityCreature extends LivingEntity implements NavigableEntity {
|
public abstract class EntityCreature extends LivingEntity implements NavigableEntity {
|
||||||
@ -40,6 +42,11 @@ public abstract class EntityCreature extends LivingEntity implements NavigableEn
|
|||||||
|
|
||||||
private Entity target;
|
private Entity target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock used to support #switchEntityType
|
||||||
|
*/
|
||||||
|
private Object entityTypeLock = new Object();
|
||||||
|
|
||||||
// Equipments
|
// Equipments
|
||||||
private ItemStack mainHandItem;
|
private ItemStack mainHandItem;
|
||||||
private ItemStack offHandItem;
|
private ItemStack offHandItem;
|
||||||
@ -148,33 +155,63 @@ public abstract class EntityCreature extends LivingEntity implements NavigableEn
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addViewer(@NotNull Player player) {
|
public boolean addViewer(@NotNull Player player) {
|
||||||
final boolean result = super.addViewer(player);
|
synchronized (entityTypeLock) {
|
||||||
|
final boolean result = super.addViewer(player);
|
||||||
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
final PlayerConnection playerConnection = player.getPlayerConnection();
|
||||||
|
|
||||||
EntityMovementPacket entityMovementPacket = new EntityMovementPacket();
|
EntityMovementPacket entityMovementPacket = new EntityMovementPacket();
|
||||||
entityMovementPacket.entityId = getEntityId();
|
entityMovementPacket.entityId = getEntityId();
|
||||||
|
|
||||||
SpawnLivingEntityPacket spawnLivingEntityPacket = new SpawnLivingEntityPacket();
|
SpawnLivingEntityPacket spawnLivingEntityPacket = new SpawnLivingEntityPacket();
|
||||||
spawnLivingEntityPacket.entityId = getEntityId();
|
spawnLivingEntityPacket.entityId = getEntityId();
|
||||||
spawnLivingEntityPacket.entityUuid = getUuid();
|
spawnLivingEntityPacket.entityUuid = getUuid();
|
||||||
spawnLivingEntityPacket.entityType = getEntityType().getId();
|
spawnLivingEntityPacket.entityType = getEntityType().getId();
|
||||||
spawnLivingEntityPacket.position = getPosition();
|
spawnLivingEntityPacket.position = getPosition();
|
||||||
spawnLivingEntityPacket.headPitch = 0;
|
spawnLivingEntityPacket.headPitch = 0;
|
||||||
|
|
||||||
playerConnection.sendPacket(entityMovementPacket);
|
playerConnection.sendPacket(entityMovementPacket);
|
||||||
playerConnection.sendPacket(spawnLivingEntityPacket);
|
playerConnection.sendPacket(spawnLivingEntityPacket);
|
||||||
playerConnection.sendPacket(getVelocityPacket());
|
playerConnection.sendPacket(getVelocityPacket());
|
||||||
playerConnection.sendPacket(getMetadataPacket());
|
playerConnection.sendPacket(getMetadataPacket());
|
||||||
|
|
||||||
// Equipments synchronization
|
// Equipments synchronization
|
||||||
syncEquipments(playerConnection);
|
syncEquipments(playerConnection);
|
||||||
|
|
||||||
if (hasPassenger()) {
|
if (hasPassenger()) {
|
||||||
playerConnection.sendPacket(getPassengersPacket());
|
playerConnection.sendPacket(getPassengersPacket());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
@Override
|
||||||
|
public boolean removeViewer(@NotNull Player player) {
|
||||||
|
synchronized (entityTypeLock) {
|
||||||
|
return super.removeViewer(player);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Changes the entity type of this entity.
|
||||||
|
* <p>
|
||||||
|
* Works by changing the internal entity type field and by calling {@link #removeViewer(Player)}
|
||||||
|
* followed by {@link #addViewer(Player)} to all current viewers.
|
||||||
|
* <p>
|
||||||
|
* Be aware that this only change the visual of the entity, the {@link net.minestom.server.collision.BoundingBox}
|
||||||
|
* will not be modified.
|
||||||
|
*
|
||||||
|
* @param entityType the new entity type
|
||||||
|
*/
|
||||||
|
public void switchEntityType(@NotNull EntityType entityType) {
|
||||||
|
synchronized (entityTypeLock) {
|
||||||
|
this.entityType = entityType;
|
||||||
|
|
||||||
|
Set<Player> viewers = new HashSet<>(getViewers());
|
||||||
|
getViewers().forEach(this::removeViewer);
|
||||||
|
viewers.forEach(this::addViewer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user