[NMS] Unmoveable Mobs

Discussion in 'Resources' started by TGRHavoc, Aug 9, 2014.

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

    TGRHavoc

    Hey guys,
    Today I am going to be telling you guys how to make your custom entities unmovable. This resource is mainly only to be used to stop other entities from pushing your custom entity.

    So, the first thing you're going to want to do is create your custom entity and make sure that you have registered it with the server (TeeePeee does this in his tutorial). I personally suggest following that tutorial as it's extremely detailed and walks you through every step of how to create your custom entity if you're new to NMS.

    Here is an example of what your entitys' class will look like.

    Code:java
    1. package net.subplex.TGRHavoc.VillagerSelect.Entities;
    2.  
    3. public class CustomVillager extends EntityVillager {
    4.  
    5. public CustomVillager(World world, int i) {
    6. super(world);
    7.  
    8. this.setProfession(i);
    9. //Removes all pathfinders already registered in the villager
    10. try{
    11. Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
    12. bField.setAccessible(true);
    13. Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
    14. cField.setAccessible(true);
    15.  
    16. bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    17. bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    18. cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    19. cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    20.  
    21. }catch (Exception e){
    22. e.printStackTrace();
    23. }
    24. setUp();
    25. }
    26.  
    27. public void setUp(){
    28. // Make the villager look at players
    29. this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    30. }
    31.  
    32. @Override
    33. protected void aC(){
    34. super.aC();
    35. // Make his walk speed 0 so he doesn't walk around on his own
    36. this.getAttributeInstance(GenericAttributes.d).setValue(0.0D);
    37. // Make his health to max a double can be
    38. this.getAttributeInstance(GenericAttributes.a).setValue(Double.MAX_VALUE);
    39. // Make his knockback resistance max double can be
    40. this.getAttributeInstance(GenericAttributes.c).setValue(Double.MAX_VALUE);
    41. }
    42. }


    Now all you have to do is override the Entity's move method and put nothing inside. It should look like:
    Code:
    @Override
    public void move(double d0, double d1, double d2){
    }
    That's it! The entity should now stay in the same place. Even if you destroy the block beneath its feet (It'll look like it's teleporting but, I think that's due to how the client renders things). Hope this helps anyone out who is trying to make their entity unmovable I know it took me a while to figure this out.
     
    macboinc, bennie3211 and Plugers11 like this.
  2. Offline

    Plugers11

    Very helped
    This is also working on damage ?
     
  3. Offline

    Geekhellmc

    TGRHavoc likes this.
  4. Offline

    TGRHavoc

  5. Offline

    Geekhellmc

    TGRHavoc He can cancel the event if the damaged entity is the villager.
     
    Plugers11 likes this.
  6. Offline

    Plugers11

    Geekhellmc
    I know how to use this...
    I'm not noob like noob
    So go noob like noob and press like to noob
     
  7. Offline

    user_90854156

    what ?_?
     
    ZodiacTheories likes this.
  8. Offline

    Geekhellmc

  9. Offline

    Plugers11

  10. Offline

    rbrick

    ;o nice :)
     
  11. Offline

    SuppaTim

    Is this meant to prevent entities from moving or being pushed, as you mentioned both ("Today I am ... custom entities unmovable. This source is ... entities from pushing your custom entity")?
    In the case of unpushable only I'd override 'g' or 'collide' (depending on the situation) and do fun stuff with those instead of canceling the entity's whole movement.
     
  12. Offline

    TGRHavoc

    SuppaTim
    Both. I tried overriding the entities "collide" method before however, the entity could still be pushed. I even tried setting the variables "motX", "motY" and "motZ" to zero as I thought that they controlled the entities motion. Same happened with the "g" method.
     
  13. Offline

    SuppaTim

    Code:java
    1. @Override
    2. public void g(double d1, double d2, double d3) { return; }
    3.  

    That's it. :)
     
  14. Offline

    TGRHavoc

    SuppaTim
    Yea, like I said I tried that however, you're still able to move the entity by pushing it.
     
  15. Offline

    Geekhellmc

    Can't we just agree there are many ways to do this but TGRHavoc is showing us a way?
     
  16. Offline

    macboinc

    So, to make the villager un-movable I would do this?
    Code:java
    1. @Override
    2. public void move(double d0, double d1, double d2) { }
     
    TGRHavoc likes this.
  17. Offline

    Gater12

    TGRHavoc likes this.
  18. Offline

    macboinc

    This is not working, when I spawn a villager with a spawn egg, it keeps moving, AND I can push it. :(
    My Custom Villager class:
    Code:java
    1. package me.macboinc1.stationarymobs;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import org.bukkit.craftbukkit.v1_7_R3.util.UnsafeList;
    6.  
    7. import net.minecraft.server.v1_7_R3.EntityHuman;
    8. import net.minecraft.server.v1_7_R3.EntityVillager;
    9. import net.minecraft.server.v1_7_R3.GenericAttributes;
    10. import net.minecraft.server.v1_7_R3.PathfinderGoalLookAtPlayer;
    11. import net.minecraft.server.v1_7_R3.PathfinderGoalSelector;
    12. import net.minecraft.server.v1_7_R3.World;
    13.  
    14. public class CustomVillager extends EntityVillager {
    15.  
    16. public CustomVillager(World world, int i) {
    17. super(world);
    18.  
    19. this.setProfession(i);
    20.  
    21. try {
    22. Field bfield = PathfinderGoalSelector.class.getDeclaredField("b");
    23. bfield.setAccessible(true);
    24. Field cfield = PathfinderGoalSelector.class.getDeclaredField("c");
    25. cfield.setAccessible(true);
    26.  
    27. bfield.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    28. bfield.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    29. cfield.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
    30. cfield.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
    31. } catch (Exception e) {
    32. e.printStackTrace();
    33. }
    34. setUp();
    35. }
    36.  
    37. public void setUp() {
    38. this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
    39. }
    40.  
    41. @Override
    42. protected void aC() {
    43. super.aC();
    44. this.getAttributeInstance(GenericAttributes.d).setValue(0.0D);
    45. this.getAttributeInstance(GenericAttributes.a).setValue(Double.MAX_VALUE);
    46. this.getAttributeInstance(GenericAttributes.c).setValue(Double.MAX_VALUE);
    47.  
    48. }
    49.  
    50. @Override
    51. public void move(double d0, double d1, double d2) { }
    52.  
    53. }
    54.  


    My Main Class:
    Code:java
    1. package me.macboinc1.stationarymobs;
    2.  
    3. import java.util.logging.Level;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class StationaryMobs extends JavaPlugin {
    8.  
    9. public void onEnable() {
    10. getLogger().log(Level.INFO, "StationaryMobs has been initated!");
    11. }
    12.  
    13. }
    14.  


    What am I doing wrong? :mad:
     
  19. Offline

    TGRHavoc

    macboinc
    You need to register your custom entity to make it override the default Minecraft entities.
     
  20. Offline

    macboinc

    How would I do that?
    ░░░░░░░░░░░░░░░░░░
    ░░░░░░░░░░░░░░░░░░
    ░░░░░░░░░░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
    ░░░░░░░░░░░░░░░░░░
    ░░░░░░░░▓░░░░░░░░░
     
  21. Offline

    TGRHavoc

  22. Offline

    macboinc

  23. I'd suggest you read it again, because it says how to do it on there. :)
     
    TGRHavoc likes this.
  24. Offline

    TGRHavoc

    macboinc
    If you had read it you would know what to do..
    Code:java
    1. public static void registerEntity(Class paramClass, String paramString, int paramInt) {
    2. try {
    3. ((Map) getPrivateStatic(EntityTypes.class, "c")).put(paramString, paramClass);
    4. ((Map) getPrivateStatic(EntityTypes.class, "d")).put(paramClass, paramString);
    5. ((Map) getPrivateStatic(EntityTypes.class, "e")).put(Integer.valueOf(paramInt), paramClass);
    6. ((Map) getPrivateStatic(EntityTypes.class, "f")).put(paramClass, Integer.valueOf(paramInt));
    7. ((Map) getPrivateStatic(EntityTypes.class, "g")).put(paramString, Integer.valueOf(paramInt));
    8. } catch (Exception exc) {
    9. // Unable to register the new class.
    10. }
    11. }

    To register the entity do: registerEntity(nmsClass, MinecraftName, MinecarftID) where the nmsClass is you custom entity class, MinecraftName is the name of the entity (e.g. "Zombie") and MinecraftID is the entities ID.
    Then to unregister the entities do the same but instead of "nmsClass" use the entities actual class (e.g. EntityZombie.class).

    Edit: It's the same idea as what the tutorial says.
     
  25. Offline

    macboinc

    Error for: getPrivateStatic
     
  26. Offline

    xTrollxDudex

    Honestly, this thread is filled with a bunch of trolls in my opinion. This is a valid, well written tutorial, and cites sources, so stop posting useless content:
     
    TGRHavoc likes this.
  27. Offline

    TGRHavoc

    -sigh- Again, this is all in the tutorial...
    Code:java
    1. private static Object getPrivateStatic(Class clazz, String f) throws Exception {
    2. Field field = clazz.getDeclaredField(f);
    3. field.setAccessible(true);
    4. return field.get(null);
    5. }
     
Thread Status:
Not open for further replies.

Share This Page