diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 5af05889a..ca364ff3d 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -14,9 +14,7 @@ import net.minestom.server.event.entity.EntitySpawnEvent; 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.instance.Chunk; -import net.minestom.server.instance.Instance; -import net.minestom.server.instance.WorldBorder; +import net.minestom.server.instance.*; import net.minestom.server.instance.block.CustomBlock; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.play.*; @@ -301,6 +299,11 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { this.data = data; } + /** + * Update the entity, called every tick + * + * @param time update time in milliseconds + */ public void tick(long time) { if (instance == null) return; @@ -464,9 +467,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } /** - * Returns the number of ticks this entity has been active for + * Get the number of ticks this entity has been active for * - * @return + * @return the number of ticks this entity has been active for */ public long getAliveTicks() { return ticks; @@ -507,30 +510,73 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { return id; } + /** + * Return the entity type id, can convert using {@link EntityType#fromId(int)} + * + * @return the entity type id + */ public int getEntityType() { return entityType; } + /** + * Get the entity UUID + * + * @return the entity UUID + */ public UUID getUuid() { return uuid; } + /** + * Return false just after instantiation, set to true after calling {@link #setInstance(Instance)} + * + * @return true if the entity has been linked to an instance, false otherwise + */ public boolean isActive() { return isActive; } + /** + * Is used to check collision with coordinates or other blocks/entities + * + * @return the entity bounding box + */ public BoundingBox getBoundingBox() { return boundingBox; } + /** + * Change the internal entity bounding box + *
+ * WARNING: this does not change the entity hit-box which is client-side + * + * @param x the bounding box X size + * @param y the bounding box Y size + * @param z the bounding box Z size + */ public void setBoundingBox(float x, float y, float z) { this.boundingBox = new BoundingBox(this, x, y, z); } + /** + * Get the entity current instance + * + * @return the entity instance + */ public Instance getInstance() { return instance; } + /** + * Change the entity instance + * + * @param instance the new instance of the entity + * @throws NullPointerException if {@code instance} is null + * @throws IllegalStateException if {@code instance} has not been registered in + * {@link InstanceManager#createInstanceContainer()} or + * {@link InstanceManager#createSharedInstance(InstanceContainer)} + */ public void setInstance(Instance instance) { Check.notNull(instance, "instance cannot be null!"); Check.stateCondition(!MinecraftServer.getInstanceManager().getInstances().contains(instance), @@ -548,16 +594,22 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { callEvent(EntitySpawnEvent.class, entitySpawnEvent); } + /** + * Get the entity current velocity + * + * @return the entity current velocity + */ public Vector getVelocity() { return velocity; } - public boolean hasVelocity() { - return velocity.getX() != 0 || - velocity.getY() != 0 || - velocity.getZ() != 0; - } - + /** + * Change the entity velocity and calls {@link EntityVelocityEvent}. + *
+ * The final velocity can be cancelled or modified by the event
+ *
+ * @param velocity the new entity velocity
+ */
public void setVelocity(Vector velocity) {
EntityVelocityEvent entityVelocityEvent = new EntityVelocityEvent(this, velocity);
callCancellableEvent(EntityVelocityEvent.class, entityVelocityEvent, () -> {
@@ -566,19 +618,51 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
});
}
+ /**
+ * @return true if velocity is not set to 0
+ */
+ public boolean hasVelocity() {
+ return velocity.getX() != 0 ||
+ velocity.getY() != 0 ||
+ velocity.getZ() != 0;
+ }
+
+ /**
+ * Change the gravity of the entity
+ *
+ * @param gravityDragPerTick
+ */
public void setGravity(float gravityDragPerTick) {
this.gravityDragPerTick = gravityDragPerTick;
}
+ /**
+ * Get the distance between two entities
+ *
+ * @param entity the entity to get the distance from
+ * @return the distance between this and {@code entity}
+ */
public float getDistance(Entity entity) {
Check.notNull(entity, "Entity cannot be null");
return getPosition().getDistance(entity.getPosition());
}
+ /**
+ * Get the entity vehicle or null
+ *
+ * @return the entity vehicle, or null if there is not any
+ */
public Entity getVehicle() {
return vehicle;
}
+ /**
+ * Add a new passenger to this entity
+ *
+ * @param entity the new passenger
+ * @throws NullPointerException if {@code entity} is null
+ * @throws IllegalStateException if {@link #getInstance()} returns null
+ */
public void addPassenger(Entity entity) {
Check.notNull(entity, "Passenger cannot be null");
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
@@ -593,6 +677,13 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
sendPacketToViewersAndSelf(getPassengersPacket());
}
+ /**
+ * Remove a passenger to this entity
+ *
+ * @param entity the passenger to remove
+ * @throws NullPointerException if {@code entity} is null
+ * @throws IllegalStateException if {@link #getInstance()} returns null
+ */
public void removePassenger(Entity entity) {
Check.notNull(entity, "Passenger cannot be null");
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
@@ -603,11 +694,18 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
sendPacketToViewersAndSelf(getPassengersPacket());
}
+ /**
+ * Get if the entity has any passenger
+ *
+ * @return true if the entity has any passenger, false otherwise
+ */
public boolean hasPassenger() {
return !passengers.isEmpty();
}
/**
+ * Get the entity passengers
+ *
* @return an unmodifiable list containing all the entity passengers
*/
public Set
+ * Set to null to show the player username
+ *
+ * @param displayName the display name
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
@@ -887,15 +893,34 @@ public class Player extends LivingEntity {
sendPacketToViewersAndSelf(infoPacket);
}
+ /**
+ * Get if the player has the respawn screen enabled or disabled
+ *
+ * @return true if the player has the respawn screen, false if he didn't
+ */
public boolean isEnableRespawnScreen() {
return enableRespawnScreen;
}
+ /**
+ * Enable or disable the respawn screen
+ *
+ * @param enableRespawnScreen true to enable the respawn screen, false to disable it
+ */
public void setEnableRespawnScreen(boolean enableRespawnScreen) {
this.enableRespawnScreen = enableRespawnScreen;
sendChangeGameStatePacket(ChangeGameStatePacket.Reason.ENABLE_RESPAWN_SCREEN, enableRespawnScreen ? 0 : 1);
}
+ /**
+ * Get the player username
+ *
+ * @return the player username
+ */
+ public String getUsername() {
+ return username;
+ }
+
private void sendChangeGameStatePacket(ChangeGameStatePacket.Reason reason, float value) {
ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket();
changeGameStatePacket.reason = reason;
@@ -1097,15 +1122,20 @@ public class Player extends LivingEntity {
teleport(position, null);
}
- public String getUsername() {
- return username;
- }
-
+ /**
+ * Get the player connection
+ *
+ * Used to send packets and get relatives stuff to the connection
+ *
+ * @return the player connection
+ */
public PlayerConnection getPlayerConnection() {
return playerConnection;
}
/**
+ * Get if the player is online or not
+ *
* @return true if the player is online, false otherwise
*/
public boolean isOnline() {
@@ -1113,12 +1143,23 @@ public class Player extends LivingEntity {
}
/**
+ * Get the player settings
+ *
* @return the player settings
*/
public PlayerSettings getSettings() {
return settings;
}
+ /**
+ * Get the player dimension
+ *
+ * @return the player current dimension
+ */
+ public Dimension getDimension() {
+ return dimension;
+ }
+
public PlayerInventory getInventory() {
return inventory;
}
@@ -1134,19 +1175,29 @@ public class Player extends LivingEntity {
}
/**
- * @return the player current dimension
- */
- public Dimension getDimension() {
- return dimension;
- }
-
- /**
+ * Get the player GameMode
+ *
* @return the player current gamemode
*/
public GameMode getGameMode() {
return gameMode;
}
+ /**
+ * Change the player GameMode
+ *
+ * @param gameMode the new player GameMode
+ */
+ public void setGameMode(GameMode gameMode) {
+ Check.notNull(gameMode, "GameMode cannot be null");
+ this.gameMode = gameMode;
+ sendChangeGameStatePacket(ChangeGameStatePacket.Reason.CHANGE_GAMEMODE, gameMode.getId());
+
+ PlayerInfoPacket infoPacket = new PlayerInfoPacket(PlayerInfoPacket.Action.UPDATE_GAMEMODE);
+ infoPacket.playerInfos.add(new PlayerInfoPacket.UpdateGamemode(getUuid(), gameMode));
+ sendPacketToViewersAndSelf(infoPacket);
+ }
+
/**
* Returns true iff this player is in creative. Used for code readability
*
@@ -1174,13 +1225,23 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(respawnPacket);
}
- public void kick(TextComponent message) {
+ /**
+ * Kick the player with a reason
+ *
+ * @param textComponent the kick reason
+ */
+ public void kick(TextComponent textComponent) {
DisconnectPacket disconnectPacket = new DisconnectPacket();
- disconnectPacket.message = Chat.toJsonString(message);
+ disconnectPacket.message = Chat.toJsonString(textComponent);
playerConnection.sendPacket(disconnectPacket);
playerConnection.disconnect();
}
+ /**
+ * Kick the player with a reason
+ *
+ * @param message the kick reason
+ */
public void kick(String message) {
kick(Chat.fromLegacyText(message));
}
@@ -1205,6 +1266,8 @@ public class Player extends LivingEntity {
}
/**
+ * Get the player held slot (0-8)
+ *
* @return the current held slot for the player
*/
public short getHeldSlot() {
@@ -1243,6 +1306,8 @@ public class Player extends LivingEntity {
}
/**
+ * Get the player open inventory
+ *
* @return the currently open inventory, null if there is not (player inventory is not detected)
*/
public Inventory getOpenInventory() {
@@ -1332,6 +1397,8 @@ public class Player extends LivingEntity {
}
/**
+ * Get the player viewable chunks
+ *
* @return an unmodifiable {@link Set} containing all the chunks that the player sees
*/
public Set