Deprecated Entity constructors accepting spawnPosition and introduces Entity#setInstance(Instance, Position)

This commit is contained in:
Konstantin Shandurenko 2021-02-25 13:56:10 +03:00
parent 3c5b76c0a8
commit 23ee4c7fdf
5 changed files with 88 additions and 22 deletions

View File

@ -123,6 +123,30 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
*/
private final Object entityTypeLock = new Object();
public Entity(@NotNull EntityType entityType, @NotNull UUID uuid) {
this.id = generateId();
this.entityType = entityType;
this.uuid = uuid;
this.position = new Position();
this.lastX = this.position.getX();
this.lastY = this.position.getY();
this.lastZ = this.position.getZ();
setBoundingBox(entityType.getWidth(), entityType.getHeight(), entityType.getWidth());
this.entityMeta = entityType.getMetaConstructor().apply(this, this.metadata);
setAutoViewable(true);
Entity.entityById.put(id, this);
Entity.entityByUuid.put(uuid, this);
}
public Entity(@NotNull EntityType entityType) {
this(entityType, UUID.randomUUID());
}
@Deprecated
public Entity(@NotNull EntityType entityType, @NotNull UUID uuid, @NotNull Position spawnPosition) {
this.id = generateId();
this.entityType = entityType;
@ -142,14 +166,11 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
Entity.entityByUuid.put(uuid, this);
}
@Deprecated
public Entity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
this(entityType, UUID.randomUUID(), spawnPosition);
}
public Entity(@NotNull EntityType entityType) {
this(entityType, new Position());
}
/**
* Schedules a task to be run during the next entity tick.
* It ensures that the task will be executed in the same thread as the entity (depending of the {@link ThreadProvider}).
@ -805,13 +826,13 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
}
/**
* Changes the entity instance.
* Changes the entity instance, i.e. spawns it.
*
* @param instance the new instance of the entity
* @throws NullPointerException if {@code instance} is null
* @param spawnPosition the spawn position for the entity.
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
*/
public void setInstance(@NotNull Instance instance) {
public void setInstance(@NotNull Instance instance, @NotNull Position spawnPosition) {
Check.stateCondition(!instance.isRegistered(),
"Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance");
@ -819,6 +840,11 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
this.instance.UNSAFE_removeEntity(this);
}
this.position.set(spawnPosition);
this.lastX = this.position.getX();
this.lastY = this.position.getY();
this.lastZ = this.position.getZ();
this.isActive = true;
this.instance = instance;
instance.UNSAFE_addEntity(this);
@ -827,6 +853,19 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
callEvent(EntitySpawnEvent.class, entitySpawnEvent);
}
/**
* Changes the entity instance.
*
* @param instance the new instance of the entity
* @deprecated Use {@link Entity#setInstance(Instance, Position)} instead.
* @throws NullPointerException if {@code instance} is null
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
*/
@Deprecated
public void setInstance(@NotNull Instance instance) {
setInstance(instance, this.position);
}
/**
* Gets the entity current velocity.
*

View File

@ -16,6 +16,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class EntityCreature extends LivingEntity implements NavigableEntity, EntityAI {
@ -29,15 +30,27 @@ public class EntityCreature extends LivingEntity implements NavigableEntity, Ent
private Entity target;
public EntityCreature(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
super(entityType, spawnPosition);
/**
* Constructor which allows to specify an UUID. Only use if you know what you are doing!
*/
public EntityCreature(@NotNull EntityType entityType, @NotNull UUID uuid) {
super(entityType, uuid);
heal();
}
public EntityCreature(@NotNull EntityType entityType) {
this(entityType, UUID.randomUUID());
}
@Deprecated
public EntityCreature(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
super(entityType, spawnPosition);
heal();
}
@Deprecated
public EntityCreature(@NotNull EntityType entityType, @NotNull Position spawnPosition, @Nullable Instance instance) {
this(entityType, spawnPosition);
if (instance != null) {
setInstance(instance);
}

View File

@ -82,16 +82,24 @@ public class LivingEntity extends Entity implements EquipmentHandler {
private ItemStack leggings;
private ItemStack boots;
public LivingEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
super(entityType, spawnPosition);
/**
* Constructor which allows to specify an UUID. Only use if you know what you are doing!
*/
public LivingEntity(@NotNull EntityType entityType, @NotNull UUID uuid) {
this(entityType, uuid, new Position());
setupAttributes();
setGravity(0.02f, 0.08f, 3.92f);
initEquipments();
}
public LivingEntity(@NotNull EntityType entityType) {
this(entityType, UUID.randomUUID());
}
/**
* Constructor which allows to specify an UUID. Only use if you know what you are doing!
*/
@Deprecated
public LivingEntity(@NotNull EntityType entityType, @NotNull UUID uuid, @NotNull Position spawnPosition) {
super(entityType, uuid, spawnPosition);
setupAttributes();
@ -99,15 +107,9 @@ public class LivingEntity extends Entity implements EquipmentHandler {
initEquipments();
}
public LivingEntity(@NotNull EntityType entityType) {
this(entityType, new Position());
}
/**
* Constructor which allows to specify an UUID. Only use if you know what you are doing!
*/
public LivingEntity(@NotNull EntityType entityType, @NotNull UUID uuid) {
this(entityType, uuid, new Position());
@Deprecated
public LivingEntity(@NotNull EntityType entityType, @NotNull Position spawnPosition) {
this(entityType, UUID.randomUUID(), spawnPosition);
}
private void initEquipments() {

View File

@ -661,6 +661,7 @@ public class Player extends LivingEntity implements CommandSender {
* @param instance the new player instance
* @param spawnPosition the new position of the player
*/
@Override
public void setInstance(@NotNull Instance instance, @NotNull Position spawnPosition) {
Check.argCondition(this.instance == instance, "Instance should be different than the current one");

View File

@ -336,6 +336,17 @@ public class Position implements PublicCloneable<Position> {
this.z = z;
}
/**
* Changes the position to the given one.
*
* @param position the new position.
*/
public void set(Position position) {
this.x = position.x;
this.y = position.y;
this.z = position.z;
}
/**
* Gets the position yaw.
*