From 97d02c445c024d0392510c0630b51355a0aabd68 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 9 Feb 2022 07:05:24 +0100 Subject: [PATCH] Add setInstance methods Signed-off-by: TheMode --- .../demo/commands/DimensionCommand.java | 3 +- .../net/minestom/server/entity/Entity.java | 49 ++++--------------- .../server/entity/EntityCreature.java | 7 ++- .../server/entity/EntityTemplateImpl.java | 2 +- .../net/minestom/server/entity/IEntity.java | 22 +++++++++ .../net/minestom/server/entity/Player.java | 29 +++++------ .../server/entity/fakeplayer/FakePlayer.java | 7 ++- 7 files changed, 54 insertions(+), 65 deletions(-) diff --git a/demo/src/main/java/net/minestom/demo/commands/DimensionCommand.java b/demo/src/main/java/net/minestom/demo/commands/DimensionCommand.java index c14d0e053..06c229ad8 100644 --- a/demo/src/main/java/net/minestom/demo/commands/DimensionCommand.java +++ b/demo/src/main/java/net/minestom/demo/commands/DimensionCommand.java @@ -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"); }); } } diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 070511aa7..6b517d249 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -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 setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { + @Override + public @NotNull CompletableFuture 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 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 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 *

- * 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 */ diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index fdbf64d3d..77d8d2319 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -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 setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { + public @NotNull CompletableFuture 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 diff --git a/src/main/java/net/minestom/server/entity/EntityTemplateImpl.java b/src/main/java/net/minestom/server/entity/EntityTemplateImpl.java index f614f3099..1fbde8750 100644 --- a/src/main/java/net/minestom/server/entity/EntityTemplateImpl.java +++ b/src/main/java/net/minestom/server/entity/EntityTemplateImpl.java @@ -11,7 +11,7 @@ record EntityTemplateImpl(EntityType type) implements EntityTemplate { @Override public @NotNull CompletableFuture 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 { diff --git a/src/main/java/net/minestom/server/entity/IEntity.java b/src/main/java/net/minestom/server/entity/IEntity.java index 8a3fb5303..4c038d4a6 100644 --- a/src/main/java/net/minestom/server/entity/IEntity.java +++ b/src/main/java/net/minestom/server/entity/IEntity.java @@ -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 setInstanceAsync(@NotNull Instance instance, @NotNull Point point); + + default void setInstance(@NotNull Instance instance, @NotNull Point point) { + setInstanceAsync(instance, point).join(); + } + + default @NotNull CompletableFuture 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(); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 0a1b9a907..b7120e16f 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -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 setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { + public @NotNull CompletableFuture 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 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, *

* Does add the player to {@code instance}, remove all viewable entities and call {@link PlayerSpawnEvent}. *

- * 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. * diff --git a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java index 0e8b3de1b..51fd2778a 100644 --- a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java +++ b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java @@ -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 setInstance(@NotNull Instance instance, @NotNull Pos spawnPosition) { + public @NotNull CompletableFuture 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