Solved Controlling Entities with movement

Discussion in 'Plugin Development' started by Mr. Sandwich, Jun 4, 2016.

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

    Mr. Sandwich

    Hey so I am trying to control an entity by moving, examples: if you move left then the entity moves left, if you look to the right then the entity looks to the right and if you jump then the entity jumps.
    I got the server to detect the entity but I dont know where to go from here
     
    Last edited: Jun 4, 2016
  2. Offline

    I Al Istannen

    @Mr. Sandwich
    You can set the velocity of the entity or teleport it (latter can also change it's pitch and yaw).
    Constantly teleporting would also work, but that may glitch Minecraft. Very annoying. The server and client will get out of sync, so the server has the entities at the right location, but the client not.
     
  3. Offline

    Mr. Sandwich

    few problems.. 1. it can tp the entity through blocks 2. I need help detecting when a player moves forward, looks down, jumps, etc.

    EDIT: I guess I can detect if there's a block in the way of the entity but I still need help with number 2
     
  4. Offline

    I Al Istannen

    @Mr. Sandwich
    PlayerMoveEvent. Then just compare the getFrom and the getTo.
    You can also set the velocity, which won't move them through blocks. Depends on what you want to achieve with it in the end.
     
  5. Offline

    Mr. Sandwich

    what do you mean compare the getFrom and the getTo?

    EDIT: I meant if you can show me an example because its really confusing
     
    Last edited: Jun 8, 2016
  6. Offline

    I Al Istannen

    @Mr. Sandwich
    I can't do much at the momemt, so I would be glad if somebody else could answer this Fürther.

    But generally you need to calculate the difference vector between getto and getFrom. If the y is positive, thr player jumped. Might need to implement a minimum value, to prevent it from triggering multiple times. Vor cancel the move event.

    Forward and stuff is Mord complicated, as the direction the player is facing is important. It depends in what you want to do with it in the end, whether there is an easier solution.
     
  7. Online

    timtower Administrator Administrator Moderator

    @I Al Istannen Won't do well when stepping up half slabs or stairs.
     
  8. Offline

    I Al Istannen

    @timtower
    While this is true, it depends on what he wants to achieve. I suggested cancelling the move event to prevent a double detection, which implies his character will stand still. If this is the case, it should work.

    What I did dome time ago was checking for the move event y and then checking the player velocity to approximately (it's a floating point number) match the normal speed at the start of a jump. With some tweaks for Jump boost it worked quit well, I think.
     
  9. Offline

    Mr. Sandwich

    Okay so I did it but there are 2 problems, 1. the entity can go through blocks 2. the entity doesnt rotate it's head like the player
     
  10. Offline

    I Al Istannen

    @Mr. Sandwich
    You would need to set the direction of the location (or yaw and pitch) for ther entity's head to rotate, I think.

    To prevent passing through blocks, set the velocity of the animal and don't teleport it.

    BUT:
    What do you want to do? Why do you need this?
     
  11. Offline

    Mr. Sandwich

    setting yaw and pitch doesn't rotate the skeleton's head just moving it up and down, and if I set the velocity without the teleporting the skeleton doesn't move at all(except when I jump then it jumps too), I am doing some kind of "Mind Control" so when I move the skeleton moves to the same direction and when I look somewhere it looks to
     
  12. Offline

    I Al Istannen

    @Mr. Sandwich
    So you want it to look like this:
    Poor skeleton.gif

    Sorry if I killed somebody's phone :p
    And sorry for the rain, but I was too lazy to turn it to night ;)
     
  13. Offline

    Mr. Sandwich

    I can't really get from the video if the Skeleton's head rotation is like your head rotation but if it is then yes
     
  14. Offline

    I Al Istannen

    @Mr. Sandwich
    So, I updated it a bit. It now sets the velocity, meaning that it won't phase through blocks and it mirrors your movement, in a <5> block distance. If you jump, it does and can actually go up blocks. If you move it does it too.

    The code is the following, but so short I don't know if I should consider it spoonfeeding. It are just four, easily understandable lines. I think code is a bit more clear here.
    Code:
    Location loc = e.getPlayer().getLocation();  // just save the player's location. Normally unneeded, but just to show it a bit better
    Location skeleLoc = skeleton.getLocation(); // the current location of the skeleton. Same as above
    skeleLoc = loc.clone().add(5, 0, 0); // add 5 on the x axis to the skeletons location. This means it will be offset by 5 on the x axis
    // Calculates the difference between the current skeleton location and the skeleLoc it should have. Then divides this by two, just to smooth it out a bit. After that is sets the resulting vector as the velocity of the skeleton.
    skeleton.setVelocity(skeleLoc.toVector().subtract(skeleton.getLocation().toVector()).multiply(0.5));
    
    If you want it to follow the mouse pointer, it is also possible, but slightly different. I thought you wanted that.

    If you want it to change it's facing direction, it looks like you will need to teleport it. This will make it phase through blocks, so I don't know if it is worth it.

    And don't forget to apply slowness to the skeleton or it will move away.
     
  15. Offline

    Mr. Sandwich

    I dont need slowness I can just disable it'a AI also I need it to look to the direction I am looking and teleporting doesn't change the skeleton's rotation and I really need the skeleton's rotation to change like the player's head rotation
     
  16. Offline

    I Al Istannen

    @Mr. Sandwich
    Well, I could only figure out a solution with the EntityData command, which I translated to NMS code, to not spam the console. It will break with every minor release and if you don't use the "v1_8_R3" the names of the methods (e and f) might be different. You can get them if you decompile your server jar and look in the entityData command. This is a really bad solution, as it needs constant maintanaince for every version. I just couldn't find another.

    First way would be saving the entity to NBT, changing the Rotation tag and then restoring it from NBT. Bad on too many ways to mention, but here it is:
    Code:
    CraftSkeleton craftSkele = (CraftSkeleton) skeleton;
    EntitySkeleton handle = craftSkele.getHandle();
    NBTTagCompound nbt = new NBTTagCompound();
    handle.e(nbt); // load to NBT tag "nbt"
    NBTTagList list = new NBTTagList();
    list.add(new NBTTagFloat(e.getPlayer().getLocation().getYaw()));
    list.add(new NBTTagFloat(e.getPlayer().getLocation().getPitch()));
    nbt.set("Rotation", list);
    nbt.setBoolean("NoAI", true);
    handle.f(nbt); // save to NBT
    In my version calling the methods here is enough. Then you don't need to read write the entity to NBT:
    Code:
    handle.f(e.getPlayer().getLocation().getYaw());
    handle.g(e.getPlayer().getLocation().getYaw());
    You can get these from the save method, which you can get from the entityData command. They take this.yaw as parameter.
    These methods are implemented in EntityLiving, and change one variable each.
    You may be able to find the methods in EntityLiving by looping through all declared methods of EntityLiving, then checking if they are a "public void" taking one float as parameter and then making sure it also exists in the super class Entity. This will only result in the two needed method in my server jar. They may have changed that already. But it would hopefully make it a bit more version proof.

    tl;dr:
    I couldn't find an easy way not involving NMS code with obfuscated methods. <profanity>

    I would be really glad if anybody knew a better way and would post it!
     
  17. Offline

    Mr. Sandwich

    Thanks! Solved
     
Thread Status:
Not open for further replies.

Share This Page