mirror of https://github.com/Minestom/Minestom.git
Add setInstance methods
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
9fd537bafa
commit
97d02c445c
|
@ -23,7 +23,8 @@ public class DimensionCommand extends Command {
|
|||
return;
|
||||
}
|
||||
final var newInstance = instances.get(ThreadLocalRandom.current().nextInt(instances.size()));
|
||||
player.setInstance(newInstance).thenRun(() -> player.sendMessage("Teleported"));
|
||||
player.setInstance(newInstance);
|
||||
player.sendMessage("Teleported");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
|
|||
import net.minestom.server.instance.Chunk;
|
||||
import net.minestom.server.instance.EntityTracker;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.instance.InstanceManager;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.instance.block.BlockHandler;
|
||||
import net.minestom.server.network.packet.server.CachedPacket;
|
||||
|
@ -671,17 +670,13 @@ public class Entity implements IEntity {
|
|||
}
|
||||
|
||||
@Override
|
||||
public @NotNull EntityType type() {
|
||||
return entityType;
|
||||
public @NotNull UUID uuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the entity {@link UUID}.
|
||||
*
|
||||
* @return the entity unique id
|
||||
*/
|
||||
public @NotNull UUID getUuid() {
|
||||
return uuid;
|
||||
@Override
|
||||
public @NotNull EntityType type() {
|
||||
return entityType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -754,18 +749,11 @@ public class Entity implements IEntity {
|
|||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the entity instance, i.e. spawns it.
|
||||
*
|
||||
* @param instance the new instance of the entity
|
||||
* @param spawnPosition the spawn position for the entity.
|
||||
* @return a {@link CompletableFuture} called once the entity's instance has been set,
|
||||
* this is due to chunks needing to load
|
||||
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
|
||||
*/
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
|
||||
@Override
|
||||
public @NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance, @NotNull Point point) {
|
||||
Check.stateCondition(!instance.isRegistered(),
|
||||
"Instances need to be registered, please use InstanceManager#registerInstance or InstanceManager#registerSharedInstance");
|
||||
final Pos spawnPosition = Pos.fromPoint(point);
|
||||
final Instance previousInstance = this.instance;
|
||||
if (Objects.equals(previousInstance, instance)) {
|
||||
return teleportAsync(spawnPosition); // Already in the instance, teleport to spawn point
|
||||
|
@ -797,23 +785,6 @@ public class Entity implements IEntity {
|
|||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Point spawnPosition) {
|
||||
return setInstance(instance, Pos.fromPoint(spawnPosition));
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the entity instance.
|
||||
*
|
||||
* @param instance the new instance of the entity
|
||||
* @return a {@link CompletableFuture} called once the entity's instance has been set,
|
||||
* this is due to chunks needing to load
|
||||
* @throws NullPointerException if {@code instance} is null
|
||||
* @throws IllegalStateException if {@code instance} has not been registered in {@link InstanceManager}
|
||||
*/
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance) {
|
||||
return setInstance(instance, this.position);
|
||||
}
|
||||
|
||||
private void removeFromInstance(Instance instance) {
|
||||
EventDispatcher.call(new RemoveEntityFromInstanceEvent(instance, this));
|
||||
instance.getEntityTracker().unregister(this, trackingTarget, trackingUpdate);
|
||||
|
@ -939,7 +910,7 @@ public class Entity implements IEntity {
|
|||
final Entity vehicle = entity.getVehicle();
|
||||
if (vehicle != null) vehicle.removePassenger(entity);
|
||||
if (!currentInstance.equals(entity.getInstance()))
|
||||
entity.setInstance(currentInstance, position).join();
|
||||
entity.setInstance(currentInstance, position);
|
||||
this.passengers.add(entity);
|
||||
entity.vehicle = this;
|
||||
sendPacketToViewersAndSelf(getPassengersPacket());
|
||||
|
@ -1268,7 +1239,7 @@ public class Entity implements IEntity {
|
|||
* - update the viewable chunks (load and unload)
|
||||
* - add/remove players from the viewers list if {@link #isAutoViewable()} is enabled
|
||||
* <p>
|
||||
* WARNING: unsafe, should only be used internally in Minestom. Use {@link #teleportAsync(Pos)} instead.
|
||||
* WARNING: unsafe, should only be used internally in Minestom. Use {@link #teleportAsync(Point)} instead.
|
||||
*
|
||||
* @param newPosition the new position
|
||||
*/
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package net.minestom.server.entity;
|
||||
|
||||
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.entity.ai.EntityAI;
|
||||
import net.minestom.server.entity.ai.EntityAIGroup;
|
||||
import net.minestom.server.entity.pathfinding.NavigableEntity;
|
||||
|
@ -55,10 +55,9 @@ public class EntityCreature extends LivingEntity implements NavigableEntity, Ent
|
|||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
|
||||
public @NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance, @NotNull Point point) {
|
||||
this.navigator.setPathFinder(new HydrazinePathFinder(navigator.getPathingEntity(), instance.getInstanceSpace()));
|
||||
|
||||
return super.setInstance(instance, spawnPosition);
|
||||
return super.setInstanceAsync(instance, point);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -11,7 +11,7 @@ record EntityTemplateImpl(EntityType type) implements EntityTemplate {
|
|||
@Override
|
||||
public @NotNull CompletableFuture<IEntity> spawnAsync(@NotNull Instance instance, @NotNull Point position) {
|
||||
var entity = new Entity(type);
|
||||
return entity.setInstance(instance, position).thenApply(unused -> entity);
|
||||
return entity.setInstanceAsync(instance, position).thenApply(unused -> entity);
|
||||
}
|
||||
|
||||
static final class Builder implements EntityTemplate.Builder {
|
||||
|
|
|
@ -16,6 +16,7 @@ import net.minestom.server.timer.Schedulable;
|
|||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@ApiStatus.Experimental
|
||||
|
@ -31,6 +32,8 @@ public interface IEntity extends Viewable, Tickable, Schedulable,
|
|||
*/
|
||||
int id();
|
||||
|
||||
@NotNull UUID uuid();
|
||||
|
||||
@NotNull EntityType type();
|
||||
|
||||
@NotNull EntityMeta meta();
|
||||
|
@ -47,6 +50,20 @@ public interface IEntity extends Viewable, Tickable, Schedulable,
|
|||
teleportAsync(point).join();
|
||||
}
|
||||
|
||||
@NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance, @NotNull Point point);
|
||||
|
||||
default void setInstance(@NotNull Instance instance, @NotNull Point point) {
|
||||
setInstanceAsync(instance, point).join();
|
||||
}
|
||||
|
||||
default @NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance) {
|
||||
return setInstanceAsync(instance, position());
|
||||
}
|
||||
|
||||
default void setInstance(@NotNull Instance instance) {
|
||||
setInstanceAsync(instance).join();
|
||||
}
|
||||
|
||||
void remove();
|
||||
|
||||
boolean isRemoved();
|
||||
|
@ -58,6 +75,11 @@ public interface IEntity extends Viewable, Tickable, Schedulable,
|
|||
return id();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default UUID getUuid() {
|
||||
return uuid();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default EntityType getEntityType() {
|
||||
return type();
|
||||
|
|
|
@ -513,12 +513,13 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
* Be aware that because chunk operations are expensive,
|
||||
* it is possible for this method to be non-blocking when retrieving chunks is required.
|
||||
*
|
||||
* @param instance the new player instance
|
||||
* @param spawnPosition the new position of the player
|
||||
* @param instance the new player instance
|
||||
* @param point the new position of the player
|
||||
* @return a future called once the player instance changed
|
||||
*/
|
||||
@Override
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
|
||||
public @NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance, @NotNull Point point) {
|
||||
final Pos spawnPosition = Pos.fromPoint(point);
|
||||
final Instance currentInstance = this.instance;
|
||||
Check.argCondition(currentInstance == instance, "Instance should be different than the current one");
|
||||
if (InstanceUtils.areLinked(currentInstance, instance) && spawnPosition.sameChunk(this.position)) {
|
||||
|
@ -568,18 +569,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes the player instance without changing its position (defaulted to {@link #getRespawnPoint()}
|
||||
* if the player is not in any instance).
|
||||
*
|
||||
* @param instance the new player instance
|
||||
* @return a {@link CompletableFuture} called once the entity's instance has been set,
|
||||
* this is due to chunks needing to load for players
|
||||
* @see #setInstance(Instance, Pos)
|
||||
*/
|
||||
@Override
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance) {
|
||||
return setInstance(instance, this.instance != null ? getPosition() : getRespawnPoint());
|
||||
public void setInstance(@NotNull Instance instance) {
|
||||
setInstanceAsync(instance, this.instance != null ? position() : getRespawnPoint()).join();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -587,7 +579,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
* <p>
|
||||
* Does add the player to {@code instance}, remove all viewable entities and call {@link PlayerSpawnEvent}.
|
||||
* <p>
|
||||
* UNSAFE: only called with {@link #setInstance(Instance, Pos)}.
|
||||
* UNSAFE: only called with {@link #setInstanceAsync(Instance, Point)}.
|
||||
*
|
||||
* @param spawnPosition the position to teleport the player
|
||||
* @param firstSpawn true if this is the player first spawn
|
||||
|
@ -604,7 +596,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
|
||||
if (dimensionChange) sendDimension(instance.getDimensionType());
|
||||
|
||||
super.setInstance(instance, spawnPosition);
|
||||
super.setInstanceAsync(instance, spawnPosition).join();
|
||||
|
||||
if (updateChunks) {
|
||||
sendPacket(new UpdateViewPositionPacket(spawnPosition.chunkX(), spawnPosition.chunkZ()));
|
||||
|
@ -958,6 +950,11 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
|||
return Objects.requireNonNullElse(displayName, usernameComponent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUuid() {
|
||||
return uuid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player's username.
|
||||
*
|
||||
|
|
|
@ -2,7 +2,7 @@ package net.minestom.server.entity.fakeplayer;
|
|||
|
||||
import com.extollit.gaming.ai.path.HydrazinePathFinder;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.coordinate.Pos;
|
||||
import net.minestom.server.coordinate.Point;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.pathfinding.NavigableEntity;
|
||||
import net.minestom.server.entity.pathfinding.Navigator;
|
||||
|
@ -122,10 +122,9 @@ public class FakePlayer extends Player implements NavigableEntity {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) {
|
||||
public @NotNull CompletableFuture<Void> setInstanceAsync(@NotNull Instance instance, @NotNull Point point) {
|
||||
this.navigator.setPathFinder(new HydrazinePathFinder(navigator.getPathingEntity(), instance.getInstanceSpace()));
|
||||
|
||||
return super.setInstance(instance, spawnPosition);
|
||||
return super.setInstanceAsync(instance, point);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue