diff --git a/demo/src/main/java/net/minestom/demo/Main.java b/demo/src/main/java/net/minestom/demo/Main.java index d69703ec8..11a7c65f6 100644 --- a/demo/src/main/java/net/minestom/demo/Main.java +++ b/demo/src/main/java/net/minestom/demo/Main.java @@ -63,6 +63,7 @@ public class Main { commandManager.register(new TestCommand2()); commandManager.register(new ConfigCommand()); commandManager.register(new SidebarCommand()); + commandManager.register(new SetEntityType()); commandManager.setUnknownCommandCallback((sender, command) -> sender.sendMessage(Component.text("Unknown command", NamedTextColor.RED))); diff --git a/demo/src/main/java/net/minestom/demo/commands/SetEntityType.java b/demo/src/main/java/net/minestom/demo/commands/SetEntityType.java new file mode 100644 index 000000000..365db971f --- /dev/null +++ b/demo/src/main/java/net/minestom/demo/commands/SetEntityType.java @@ -0,0 +1,29 @@ +package net.minestom.demo.commands; + +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType; +import net.minestom.server.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class SetEntityType extends Command { + private final ArgumentEntityType entityTypeArg = ArgumentType.EntityType("type"); + + public SetEntityType() { + super("setentitytype"); + + addSyntax(this::execute, entityTypeArg); + } + + private void execute(@NotNull CommandSender sender, @NotNull CommandContext context) { + if (!(sender instanceof Player player)) { + return; + } + + var entityType = context.get(entityTypeArg); + player.switchEntityType(entityType); + player.sendMessage("set entity type to " + entityType); + } +} diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 88328dc9e..eadda47de 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -819,6 +819,9 @@ public class Player extends LivingEntity implements CommandSender, Localizable, Pose oldPose = getPose(); Pose newPose; + // If they are not a player, do nothing + if (!getEntityType().equals(EntityType.PLAYER)) return; + // Figure out their expected state var meta = Objects.requireNonNull(getLivingEntityMeta()); if (meta.isFlyingWithElytra()) { @@ -1014,27 +1017,39 @@ public class Player extends LivingEntity implements CommandSender, Localizable, sendPacket(new UpdateHealthPacket(health, food, foodSaturation)); } - @Override - public @NotNull PlayerMeta getEntityMeta() { + /** + * Gets the entity meta for the player. + * + *

Note that this method will throw an exception if the player's entity type has + * been changed with {@link #switchEntityType(EntityType)}. It is wise to check + * {@link #getEntityType()} first.

+ */ + public @NotNull PlayerMeta getPlayerMeta() { return (PlayerMeta) super.getEntityMeta(); } /** * Gets the player additional hearts. * + *

Note that this function is uncallable if the player has their entity type switched + * with {@link #switchEntityType(EntityType)}.

+ * * @return the player additional hearts */ public float getAdditionalHearts() { - return getEntityMeta().getAdditionalHearts(); + return getPlayerMeta().getAdditionalHearts(); } /** * Changes the amount of additional hearts shown. * + *

Note that this function is uncallable if the player has their entity type switched + * with {@link #switchEntityType(EntityType)}.

+ * * @param additionalHearts the count of additional hearts */ public void setAdditionalHearts(float additionalHearts) { - getEntityMeta().setAdditionalHearts(additionalHearts); + getPlayerMeta().setAdditionalHearts(additionalHearts); } /** diff --git a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java index 47414120f..9c3beec2c 100644 --- a/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java +++ b/src/main/java/net/minestom/server/listener/PlayerDiggingListener.java @@ -4,7 +4,7 @@ import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Pos; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; -import net.minestom.server.entity.metadata.PlayerMeta; +import net.minestom.server.entity.metadata.LivingEntityMeta; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.item.ItemUpdateStateEvent; import net.minestom.server.event.player.PlayerCancelDiggingEvent; @@ -145,8 +145,8 @@ public final class PlayerDiggingListener { } private static void updateItemState(Player player) { - PlayerMeta meta = player.getEntityMeta(); - if (!meta.isHandActive()) return; + LivingEntityMeta meta = player.getLivingEntityMeta(); + if (meta == null || !meta.isHandActive()) return; Player.Hand hand = meta.getActiveHand(); player.refreshEating(null);