Make mobs go to a location???

Discussion in 'Plugin Development' started by pasow, May 28, 2013.

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

    pasow

    So, I've seen that there has been some threads about similar ideas, but they're referring to outdated APIs, which dont work the way they used to.


    My idea is to spawn a mob (preferably villagers and cats) and then make them go to a certain location. (known x,y,z coordinates)

    I havent seen anything in the bukkit api and i even scratched the surface of mincraft-server code for a second, but i backed off, since it was scary :p (nothing works)


    I wanted to know if there is any way to do this?


    Another idea would be to have another mob at the destination and make the spawned mob target the other one. (maybe with aggressive mobs)


    however, i would really prefer to just send the mob to a given location, so a player can see where it's going and maybe follow it.
     
  2. Offline

    ZachBora

    The only thing I can help you with is that if you Teleport an entity, it will look like they walked there. My CloneMe plugin teleports NPCs at the same time as you move and the NPC look like they are walking.
     
    pasow and blablubbabc like this.
  3. Offline

    Windy Day

  4. Offline

    pasow


    do they walk at normal speed or extremely fast though? :confused:
     
  5. Offline

    ZachBora

    In my plugin they move each time I move, so same speed as myself. They copy movements, so they'll move as fast as you tell them.
     
  6. Offline

    pasow


    my plan is to have villagers walk to places all the time, and a player can follow them if he wants.

    so basically they ae supposed to help people who dont know where to go.
     
  7. Offline

    pasow

    I got to this point where the spawned villagers dont do anything special:


    separate PathFinderGoal class:
    Code:
    public class PathfinderGoalWalktoTile extends PathfinderGoal{
       
        float speed;
        private EntityCreature entitycreature;
        public PathfinderGoalWalktoTile(EntityCreature entitycreature, float speed){
            this.entitycreature = entitycreature;
            this.speed = speed;
        }
       
        @Override
        public boolean a(){
            return true;
        }
       
        @Override
        public void c(){
            this.entitycreature.getNavigation().a(0.0, 66.0, 0.0, speed);
        }
     
    }
    separate villager-constructor class:
    Code:
    public class PathEntityVillager extends net.minecraft.server.v1_5_R3.EntityVillager{
       
        public PathEntityVillager(World world, int i){
            super(world);
            this.setProfession(i);
           
            try{
                Field gsa = net.minecraft.server.v1_5_R3.PathfinderGoalSelector.class.getDeclaredField("a");
                gsa.setAccessible(true);
                gsa.set(this.goalSelector, new UnsafeList());
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
           
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(1, new PathfinderGoalWalktoTile(this, 0.4F));
            this.goalSelector.a(2, new PathfinderGoalRestrictOpenDoor(this));
            this.goalSelector.a(3, new PathfinderGoalOpenDoor(this, true));
            this.goalSelector.a(4, new PathfinderGoalMoveTowardsRestriction(this, 0.3F));
            this.goalSelector.a(5, new PathfinderGoalRandomStroll(this, 0.3F));
        }
     
    }
    on Enable:
    Code:
    public void onEnable(){
            System.out.println("[EntityControl] ON!!!");
           
            try{
                @SuppressWarnings("rawtypes")
                Class[] args = new Class[3];
                args[0] = Class.class;
                args[1] = String.class;
                args[2] = int.class;
                Method a = net.minecraft.server.v1_5_R3.EntityTypes.class.getDeclaredMethod("a", args);
                a.setAccessible(true);
                a.invoke(a, PathEntityVillager.class, "Villager", 120);
            }catch(Exception e){
                e.printStackTrace();
                this.setEnabled(false);
            }
        }
    onCommand (its in the main class, because this is just a small plugin for testing :p):
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player p = (Player) sender;
            if (label.equalsIgnoreCase("spawnvilg")){
                    Location loc = p.getLocation();
                    World w = loc.getWorld();
                    net.minecraft.server.v1_5_R3.World mcW = ((CraftWorld) w).getHandle();
                    PathEntityVillager pv = new PathEntityVillager(mcW, 0);
                    Entity b = pv.getBukkitEntity();
                    w.spawnEntity(loc, b.getType());
                }
            return true;
        }
     
  8. Offline

    Wingzzz

    • So from here, what functionality is missing?
    • How much do you not understand?
     
  9. Offline

    pasow

    The villagers spawn and act like regular villagers, but they dont go to 0,66,0.

    I basically dont understand the part in the onEnable method, but I think that I can just use it like it is, since it spawns the villager (which Jacek said that that part is for)


    EDIT:

    I made the onCommand method myself without any tutorial, but i think it works like it should.

    The problem lies in the pathfindergoal or the pathentityvillager class i think.

    also im not sure if i should use the arguments ("Villager", 120) in the onEnable
     
  10. Offline

    pasow

    any ideas anyone???
     
  11. Offline

    pixelzee

    I have the same problem
     
  12. Offline

    Ivan

    pasow Your spawning a regular villager. You spawn the npc type of the villager reread my tutorial.
     
  13. Offline

    pasow



    the .getBukkitEntity() might be messing it up, right?

    i'll try out some changes. if nothing works, i'll come back for help.
     
  14. Offline

    pasow

    I deleted the randomStroll goal out of curiosity and now i have villagers that stand still and dont do anything :p (btw, I get Severe errors on the console when logging out and back in)

    So that at least proves that I am spawning custom villagers, but the walkToTile is still not working.

    I guess I'll need to find the error there.

    again, here's the class:

    Code:
    public class PathfinderGoalWalktoTile extends PathfinderGoal{
     
        float speed;
        private EntityCreature entitycreature;
        public PathfinderGoalWalktoTile(EntityCreature entitycreature, float speed){
            this.entitycreature = entitycreature;
            this.speed = speed;
        }
     
        @Override
        public boolean a(){
            if (this.entitycreature.aH() >=100){  //I still dont know, what this aH-thing is...
                return false;
            } else {
                return true;
            }
        }
     
        @Override
        public void c(){
            this.entitycreature.getNavigation().a(0.0, 66.0, 0.0, speed);
        }
     
    }
     
  15. Offline

    pasow

    OK!

    I got things to work! :D

    The villagers now go to the specified location, when I spawn them.


    Now the main problem is that they only go there, if the destination is in a loaded chunk.
    Which is pretty logical, but I would like to know if there is any way to make them go to unloaded destinations???

    Maybe just make them go into the right direction?
     
  16. Offline

    Wingzzz

    I'd say think of the location, then break it into multiple locations. The NPC will then begin to load chunks as it goes.
     
  17. Offline

    pasow


    Do villagers actually load chunks?

    Also I am not that familiar with how chunks are loaded on servers. What is view-distance for example?
    And do the render distance settings on someones client affect the servers chunk loading?


    But yeah, I also had the idea to create a mathematical fomula to break the path into several parts. In the end there will be up to 50 different spawn locations for the villagers (or cats) and 18 destinations, so the paths will have to be done automatically.
    (I might not be good at java, but luckily I know quite some math)

    However more knowledge about chunk-loading could be helpful. :)
     
  18. Offline

    Jatoc


    I know I'm not really helping, but would you mind sharing your code? I want to accomplish a similar effect, but have mobs walk into a REGION, and not leave the region. I would love to see anyone help me get started on this.
     
  19. Offline

    pasow


    I posted my code above.

    First check out Jacek's and Ivan's tutorials which were mentioned above.


    Also I fixed my custom pathfindergoal by just copying the original code for the randomstroll which is linked in ivan's tutorial and then changing the setting of the coordinates to what i wanted them to be.
     
  20. Offline

    pasow

    wait...

    I could actually just have the mobs be stacked on underground minecarts which would go on a track leading them to their destination!

    no NMS needed and easy to maintain! :D

    #ThinkOutsideTheBox
     
  21. Offline

    Wingzzz

    Could always use Citizens 2. A lot of functionality and extremely well done, comes with an API as well.
     
Thread Status:
Not open for further replies.

Share This Page