NPC head rotation

I’d like to rotate the villager the same way a player is when the npc is created.

my code is the following

private Entity createAndSpawn(EntityType entityType, Location<World> location, Player p, Text name) {
        World world = location.getExtent();

        Entity npc = world.createEntity(entityType, location.getPosition());
        npc.setRotation(p.getRotation());
        npc.setRotation(p.getHeadRotation());
        p.sendMessage(Text.of("Player rotation: "+p.getRotation()));
        p.sendMessage(Text.of("Player head rotation: "+p.getHeadRotation()));
        npc.setCreator(p.getUniqueId());
        p.sendMessage(Text.of("Npc rotation: "+p.getRotation()));
        p.sendMessage(Text.of("Npc head rotation: "+p.getHeadRotation()));
        npc.remove(AgentData.class);
        npc.offer(Keys.INVULNERABILITY_TICKS, Integer.MAX_VALUE);
        if (npc.getOrCreate(DisplayNameData.class).isPresent()) {
            npc.offer(npc.getOrCreate(DisplayNameData.class).get().set(Keys.DISPLAY_NAME, Text.builder(name.toPlain()).color(TextColors.RED).build()));
        }
        if (npc.getOrCreate(SkinData.class).isPresent()) {
            //Give npc a Steve skin
            npc.offer(npc.getOrCreate(SkinData.class).get().set(Keys.SKIN_UNIQUE_ID, UUID.fromString("8667ba71-b85a-4004-af54-457a9734eed7")));
        }
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
            world.spawnEntity(npc);
        }
        return npc;
    }

This is the output
image
but as the result, the npc rotates only towards south, but the pitch still applies.
Is there anything I’m doing wrong?

Use the following instead of setting the whole rotation

Keys.HEAD_ROTATION

It also seems like your doing it the long way around. You can just do the following

Text displayName;
npc.offer(Keys.DISPLAY_NAME, displayName)
1 Like

I hope that I understood you correctly, but whenever I run this piece of code

private Entity createAndSpawn(EntityType entityType, Location<World> location, Player p, Text name) {
    World world = location.getExtent();
    Entity npc = world.createEntity(entityType, location.getPosition());
    npc.offer(Keys.HEAD_ROTATION, p.getHeadRotation());
    p.sendMessage(Text.of("Player rotation: "+p.getRotation()));
    p.sendMessage(Text.of("Player head rotation: "+p.getHeadRotation()));
    npc.setCreator(p.getUniqueId());
    p.sendMessage(Text.of("Npc rotation: "+p.getRotation()));
    p.sendMessage(Text.of("Npc head rotation: "+p.getHeadRotation()));
    npc.remove(AgentData.class);
    npc.offer(Keys.INVULNERABILITY_TICKS, Integer.MAX_VALUE);
    if (npc.getOrCreate(DisplayNameData.class).isPresent()) {
        npc.offer(Keys.DISPLAY_NAME, Text.builder(name.toPlain()).color(TextColors.RED).build());
    }
    if (npc.getOrCreate(SkinData.class).isPresent()) {
        //Give npc a Steve skin
        npc.offer(npc.getOrCreate(SkinData.class).get().set(Keys.SKIN_UNIQUE_ID, UUID.fromString("8667ba71-b85a-4004-af54-457a9734eed7")));
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
        world.spawnEntity(npc);
    }
    return npc;
}

I get the following result


I still think I’m missing something. They all look in the same direction, it’s only the pitch that changes.

Are you sure the player head rotation is actually different between each run? Or maybe being modified later in the code?

Also with offer, you dont need to use getOrCreate. You can simply just offer and it will create it if its not already there. The getOrCreate seems to be mainly for creating new data manipulators, something your not doing

It’s not getting moved anywhere in the code, as the code snippet is the last command that is run when the command is issued. The direction the “NPC” is looking in, should be the same as the player’s, because I’m just copying the rotation values over. It’d seem like YAW coordinate isn’t working on Sponge’s side, as even tho it’s set, it doesn’t rotate into the correct direction.

When i get some time i test it on my end just to confirm the issue.
My own plugin called EntityDisguise does deal with copying a player’s rotation (including head) and that does work. However I haven’t worked on it nor tested for so long.

1 Like

Thanks, I’ll take a look!