Solved How to rotate an entity - while player is riding it?

Discussion in 'Plugin Development' started by westjet, Mar 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    westjet

    OK, the title asks it all. No matter what I've tried, I can't rotate a mob so that it looks as if the player is controlling it. I've tried this on a scheduler:

    Code:
    entity.getLocation().setDirection(entity.getPassenger().getLocation().getDirection());
                Location location = entity.getLocation();
                        Double speed = 0.3;
                        Vector dir = location.getDirection().multiply(1.0D);
                
                        dir.setY(0);
                
                        entity.setVelocity(dir.multiply(speed));
                        entity.getLocation().setPitch(entity.getPassenger().getLocation().getPitch());
                        entity.getLocation().setYaw(entity.getPassenger().getLocation().getYaw());

    It doesn't work. The sheep is facing backwards and poor Steve looks like his head is twisted. Any ideas?

    NOTE: The above code is also supposed to make the animal the player is riding walk at a constant speed. That works just fine. It's the animal's body that is not facing the way the player is facing.
    Also, I'm not getting any errors.
     
  2. Offline

    NathanWolf

    Try to teleport the Entity to a new Location - just setting it's location doesn't do anything (in fact, I believe getLocation returns a clone anyway).

    This might work, but the Entity might want to go back to facing whatever direction it's AI tells it to, if it's a LivingEntity, so it might look/feel really glitchy.
     
  3. Offline

    westjet

    Even that would be nice. I'm using custom mobs that have their AI stripped out of them.
    This is the new code i'm using, that implements your idea:
    Code:
    Entity eeee = Followers.get(player).get(0);
                eeee.getLocation().setDirection(eeee.getPassenger().getLocation().getDirection());
                Location location = player.getLocation();
                        Double speed = 0.3;
                        Vector dir = location.getDirection().multiply(1.0D);
                       
                        dir.setY(0);
                       
                        eeee.setVelocity(dir.multiply(speed));
                        Location itWontWork = eeee.getLocation();
                        itWontWork.setYaw(eeee.getPassenger().getLocation().getYaw());
                        itWontWork.setDirection(eeee.getPassenger().getLocation().getDirection());
                        eeee.teleport(itWontWork);
    And this is the best I can get:
    [​IMG]

    Does setvelocity do anything with the rotation of an entity?

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Mar 26, 2015
  4. Offline

    NathanWolf

    setVelocity doesn't have anything to do with entity rotation. setDirection and then tp should do what you want, though.. not sure why it's not working.

    You might need to put some debugging in- like log when you tp the entity, and what direction it's tp'ing to- then log after tp what the entity's direction is.

    And you don't need to setYaw if you setDirection, btw, it's redundant :)

    EDIT: I assume you've set up a repeating task that does this as long as the player is riding?
     
  5. Offline

    westjet


    yes I put it on a scheduler and I'm 100% that part is working.

    I'll do that debugging and compare the direction the entity is told to go, the direction the entity is going, and the direction the player is facing.

    I'm basically having the same problem as this:
    https://bukkit.org/threads/how-to-set-an-entitys-rotation.20629/

    Notice the last post, where OP is saying that he thinks he is fighting Minecraft's code. I'm tempted to necro that thread and ask OP if he ever found the solution.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @westjet Thank you for letting us know the thread exists. Locked that one to prevent necro's.
     
  7. Offline

    westjet

    I noticed OP wasn't on for 2 or 3 years. Necroing it wouldn't help.

    @NathanWolf

    Wow this is crazy!!

    [​IMG]

    My code has a mind of it's own!
    This is what I'm using:
    Code:
    Entity eeee = invincibleFollowers.get(player).get(0);
                Location location = eeee.getLocation();
                Bukkit.getLogger().info("The direction of the sheep is: " + eeee.getLocation().getDirection());
                location.setDirection(player.getLocation().getDirection());
                Bukkit.getLogger().info("The direction of the sheep should be: " + location.getDirection());
                        Double speed = 0.3;
                        Vector dir = location.getDirection().multiply(1.0D);
                      
                        dir.setY(0);
                        eeee.setPassenger(null);
                        eeee.teleport(location);
                        eeee.setPassenger(player);
                        eeee.setVelocity(dir.multiply(speed));
                        Bukkit.getLogger().info("The direction of the sheep is now: " + eeee.getLocation().getDirection());
                      
                

    EDIT 2: I got it working! I changed "eeee.setPassenger(null);" to "eeee.getPassenger().leaveVehicle();". But it is REALLY glitchy to the point that the screen is giving me nausia.
     
    Last edited: Mar 26, 2015
  8. Offline

    NathanWolf

    I could be wrong about velocity not making them face a different direction... or it could be something your custom entity AI is doing?

    In my experience entity facing is really hard to control and always overridden by the entity AI, so not sure what to say- sorry!
     
  9. Offline

    mine-care

    Hmm i am not sure if it will work... try your luck with a litle nms, Specifically PacketPlayOutEntityHeadRotation to rotate the entity's head acording to the rotation of riders head and then it will probably turn that way
    Otherwise you can add to the entity being ridden the goal that pigs have and that makes them follow carrots on sticks,
    In class "EntityPig" you can find an example
    (goalSelector.a(4, new PathfinderGoalTempt(this, 1.2D, Items.CARROT_STICK, false));)
    Hope that helped.

    EDIT: packet thingy will not work because it is a client change, my fault sorry!
     
  10. Offline

    westjet

    @mine-care
    I'm using this for other parts of my game:
    http://bukkit.org/threads/1-8-pet-following-pet-speed-1-7-too.325828/

    I tried that with my problem, didn't work. But I don't think ive tried PathfinderGoalTempt.
    How would I make it so that only certain custom sheep that have been spawned are using this?

    EDIT:
    Tried this, Eclipse giving me an error, something about PathfinderGoalTempt being undefined.
    Code:
    ((EntityInsentient) petf).goalSelector.a(4, new PathfinderGoalTempt(running, 1.2D, player.getItemInHand(), false));
     
    Last edited: Mar 26, 2015
  11. Offline

    mine-care

    @westjet yes eclipse is right, the class goalSelector is protected so it is not visible.
    Use reflection to acess it, for me it work :p you can use tempt.
     
  12. Offline

    DemKazoo

    @mine-care
    Code:
    (goalSelector.a(4, new PathfinderGoalTempt(this, 1.2D, Items.CARROT_STICK, false))
    Should actually be:
    Code:
    (goalSelector.a(6, new PathfinderGoalTempt(this, 1.2D, Items.CARROT_STICK, true))
     
  13. Offline

    westjet

    How do I use reflection for this?
    I'm new at Java.
     
  14. Offline

    nverdier

  15. Offline

    westjet

    Last edited: Mar 26, 2015
  16. Offline

    mine-care

    @DemKazoo Hmm realy? i dont think it is so, i tryed with the code i had above and it wored perfectly, umm why is the change above?

    @westjet a is not a field it is a method, you need to get a field called "goalSelector" that is an object from class EntityInsentient and then get its value from the EntitySheep you have.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  17. Offline

    westjet

    @mine-care
    So if I get it - do I have to set something using reflection as well?

    Could you show me a commented example?

    EDIT: This is what I have so far:
    Code:
    try {
                            Field f = EntityInsentient.class.getDeclaredField("goalSelector");
                            f.setAccessible(true);
                            Object o = f.get(entity);
                        } catch (SecurityException e) {
                        
                        
                        } catch (IllegalArgumentException e) {
                       
                       
                        } catch (NoSuchFieldException e) {
                        
                      
                        }
                        
    What next?
     
    Last edited: Mar 27, 2015
  18. Offline

    Funergy

    @westjet If you are familiar with custom mob creation in my tutorial for 1.8 or the one for 1.7.5 from @DSH105 We have implemented that the entity automatically rotates with the player. You can just leave the moving part.
     
  19. Offline

    westjet

    Yes, but I don't want to create another custom mob because I have already created a custom one. All I was looking for in "custom" mobs is to delete their AI. It would be great if I can make the sheep rotate the way the player is facing, but I would only want to do so on certain custom sheep, not all of them.

    Also, I created a new thread for the problem I'm having. If I can get that fixed, then both threads can be marked as solved.

    ______________________________________________________________________

    EDIT: I FOUND THE SOLUTION TO THE ORIGINAL PROBLEM!
    Code:
    ((CraftSheep) entity).getHandle().yaw = player.getLocation().getYaw();
    EDIT 2: I ALSO FOUND A SOLUTION TO THE 2ND PROBLEM:
    Code:
    ((EntityInsentient) ((CraftSheep) entity).getHandle()).goalSelector.a(4, new PathfinderGoalTempt(((EntityCreature) ((CraftSheep) entity).getHandle()), 1.2D, Item.getById(1), false));
    Requesting Lock
     
    Last edited: Mar 27, 2015
  20. Offline

    mrCookieSlime

    Locked on request.
     
Thread Status:
Not open for further replies.

Share This Page