Non-Pushable/Moving Villager

Discussion in 'Spigot Plugin Development' started by ckamps2020, May 18, 2016.

  1. Hey! So I have managed to figure out to make a noAI villager. What I am not too sure of what to do is make: A) Make the villager non-pushable and B) How to make the villager only face one direction. If you could help me that would be wonderful!

    Here is how I create the noAI
    Code (Text):
    public boolean setNoAI(Entity entity, boolean yesNo) {
                net.minecraft.server.v1_8_R3.Entity a = ((CraftEntity)entity).getHandle();
                int b = 1;
                if (yesNo) {
                    b = 0;
                }
                NBTTagCompound c = a.getNBTTag();
                if (c == null) {
                    c = new NBTTagCompound();
                }
                if (c.getInt("NoAI") == b) {
                    return false;
                } else {
                    a.c(c);
                    c.setInt("NoAI", b);
                    a.f(c);
                    return true;
                }
            }
    And here is how I spawn the villager

    Code (Text):
    if (cmd.getName().equalsIgnoreCase("test")) {
            Villager villager = (Villager) player.getWorld().spawnEntity(player.getLocation(), EntityType.VILLAGER);
            setNoAI(villager, true);
        }
     
  2. There's already an AI setter in the api if you didnt know

    Sent from my SGH-I747M using Tapatalk
     
  3. Thanks!
     
  4. Each nms Entity has it's own bounding box (it's hit box) and if you override the current one with a new one of the size 0, you can't push this entity.
    Code (Text):

    c = (Villager) loc.getWorld().spawnEntity(loc, EntityType.VILLAGER);
            ec = ((CraftVillager)c).getHandle();
         
            ec.a(new AxisAlignedBB(ec.locX, ec.locY, ec.locZ, ec.locX, ec.locY, ec.locZ));
         
     
    To set the bounding box to 0 make sure you use the same coordinates on both sides

    Edit:
    make sure to update the box from time to time as it happened to me that it resetted itself
     
    • Informative Informative x 1
  5. Thank you! I will try this out!
     
  6. or set the villager speed to 0
     
  7. Spigot: 1_8_R3

    Villager e = p.getWorld().spawn(p.getLocation(), Villager.class);
    CraftVillager v = (CraftVillager) e;
    EntityVillager handle = ((CraftVillager) e).getHandle();

    handle.getDataWatcher().watch(15, (byte) 1); // NoAI

    /* You can also silence the entity. */
    handle.getDataWatcher().watch(4, (byte) 1); // Silent
     
    #7 DevLeeo, May 18, 2016
    Last edited: May 18, 2016
    • Like Like x 1