Stop a villager from moving?

Discussion in 'Plugin Development' started by stuntguy3000, Apr 14, 2013.

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

    stuntguy3000

    Hi,

    In my plugin i spawn a villager, i need to stop it from moving around and have it always look at a location. There is no event for this, so i am confused on how to do it! Thanks
     
  2. Offline

    caseif

    Er... You might be able to use NMS code to get the Craft version of that entity, then set its- er... the word escapes me right now. You know, like the path it follows- to null.
     
  3. Offline

    Tzeentchful

    The only way to do this is through NMS and refelection.
    Here is how i did it.

    First off you will need to get the NMS version of the villager.
    Code:java
    1.  
    2.  
    3. EntityVillagernmsVillager = ((CraftVillager) villager).getHandle();
    4.  


    Now the movement tof an entity is defined in a variable called "goalSelector" in the EntityLiving superclass.
    New we ant to edit it but it has the "protected" and "final" modifiers so we are going to need to use reflection to be able to access the field.

    Code:java
    1.  
    2.  
    3. //Get the field
    4. Field goal = nmsVillager.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("goalSelector");
    5. goal.setAccessible(true);//remove the protected modifier
    6.  
    7.  
    8. //Evilly Remove the final modifier
    9. FieldmodifiersField = Field.class.getDeclaredField("modifiers");
    10. modifiersField.setAccessible(true);
    11. modifiersField.setInt(goal, goal.getModifiers() & ~Modifier.FINAL);//Evilness shall ensue
    12.  

    So now we can easly set the field to whet we want.
    Since you want it to basically do nothing you will want to create a new PathfinderGoalSelector
    with nothing in it.
    Code:java
    1.  
    2.  
    3. PathfinderGoalSelectorgoalSelector = newPathfinderGoalSelector(nmsVillager.world != null && nmsVillager.world.methodProfiler != null ? nmsVillager.world.methodProfiler : null);
    4.  


    Then set the variable.
    Code:java
    1.  
    2.  
    3. goal.set(nmsVillager, goalSelector);
    4.  


    And you should be good to go.
     
Thread Status:
Not open for further replies.

Share This Page