Entity navigation problem

Discussion in 'Plugin Development' started by CheesyFreezy, Feb 1, 2015.

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

    CheesyFreezy

    Heey developers,

    I was making a plugin where you can spawn an irongolem that will go to the spawn location of the world. Whenever i spawn this iron golem it's just not walking but standing still and sometimes it walks away.

    This is the piece of code where the problem is (I checked it):
    Code:
    Navigation nav = (Navigation) ((EntityInsentient) ((CraftEntity) e).getHandle()).getNavigation();
    nav.a(Bukkit.getServer().getWorld("world").getSpawnLocation().getX(), Bukkit.getServer().getWorld("world").getSpawnLocation().getY(), Bukkit.getServer().getWorld("world").getSpawnLocation().getZ(), 5D);
                                              
    e.getLocation().setPitch(0);
    e.getLocation().setYaw(0);
     
  2. @CheesyFreezy When do you use this code? Are there any errors?
     
  3. Offline

    CheesyFreezy

    @Lionhard,
    Code:
    if(command.equalsIgnoreCase("portals")) {
                if(sender instanceof Player) {
                    final Player player = (Player)sender;
                  
                    if(player.hasPermission("mineshine.portals") || player.isOp()) {
                        if(args.length == 0) {
                            player.sendMessage(Portals.prefix + ChatColor.YELLOW + "/Portals [Stalker]");
                        } else if(args.length == 1) {
                            if(args[0].equalsIgnoreCase("Stalker")) {
                                IronGolem portalNPC = (IronGolem) player.getWorld().spawnEntity(player.getLocation(), EntityType.IRON_GOLEM);
                                portalNPC.setCanPickupItems(false);
                              
                                portalNPC.setCustomName(ChatColor.GREEN + "" + ChatColor.BOLD + "Stalker");
                                portalNPC.setCustomNameVisible(true);
                              
                                Bukkit.getServer().getScheduler().runTaskTimer(Portals.core, new Runnable() {
                                    public void run() {
                                        World world = Bukkit.getServer().getWorld("world");
                                        for(Entity e : world.getEntities()) {
                                            if(e instanceof IronGolem) {
                                                Navigation nav = (Navigation) ((EntityInsentient) ((CraftEntity) e).getHandle()).getNavigation();
                                                nav.a(Bukkit.getServer().getWorld("world").getSpawnLocation().getX(), Bukkit.getServer().getWorld("world").getSpawnLocation().getY(), Bukkit.getServer().getWorld("world").getSpawnLocation().getZ(), 5D);
                                              
                                                e.getLocation().setPitch(0);
                                                e.getLocation().setYaw(0);
                                            }
                                        }
                                    }
                                }, 0, 1);
                            }
                        }
                    } else {
                        player.sendMessage(Portals.prefix + ChatColor.RED + "You're not permitted to use this command!");
                    }
                }
            }
    There are no errors
     
  4. Offline

    crolemol

    @CheesyFreezy
    just make portalnpc final so you can use it in your runnable. then remove the for loop en just get the navigaotr of the final portalnpc
     
  5. Offline

    CheesyFreezy

    @crolemol, normally i would do this to. But if you reload the server the loop will stop. That's why i made that it's searching for a villager.
     
  6. Offline

    crolemol

    @CheesyFreezy
    the loop will not stop. that is not how programming works. if you run this in the main thread (normally this is the case) then the server will freeze until the loop is done and after that it reloads the server so it is completely useless to do it such a difficult way which gives you 90% of the times a bad result
     
  7. Offline

    CheesyFreezy

    @crolemol
    So i have to make a loop/scheduler in the main class?
     
  8. Offline

    crolemol

    @CheesyFreezy
    in the main class is just putting that loop in your main class file. the main thread is something different. a thread is an order of methods. it waits until a method is finished to start the next one so the normally every line of code is executed in the main thread but when you run something async it will be in another list so methods can overlap and clear each other's data
     
  9. Offline

    CheesyFreezy

    @crolemol, so..... what do i have to do now?
     
  10. Offline

    crolemol

  11. Offline

    CheesyFreezy

    @crolemol
    It's plugin where you can place portalNPC, they'll lead to a minigame.

    • type /portal stalker - to spawn a npc.
    • the npc will have to stand still and may be not pushed, that's why i made the navigation part
    • When the server reloads it's going to search for all Iron Golems in the world and make them walk to themselfs
    That's what i'm trying to do
     
  12. Offline

    crolemol

    @CheesyFreezy
    sorry i thought you were doing something different. in that case your code is pretty good except of the bukkit runnable. it is completly useless and will cause huuge lag. and i think it is not working because the a() method has a distance limitation (just a thougth) because pathfinding is a cpu intensive thing. Also if your plugin is not pretty big it is maybe better to not use nms (navigator and handle stuff) and I recommend using citizens. it has a very good path finding method and is update proof
     
  13. Offline

    CheesyFreezy

    @crolemol, i'll try other ways to maybe fix this because i don't want to use other plugins or API i want to do this all on my own to see how good i am and maybe learn more stuff
     
  14. Offline

    crolemol

    @CheesyFreezy
    you could let debug messages show up in the for loop so you know there is found one and for the pathfinding I should start with something easy because nms is pretty hard and has also limitations of you are not a professional at it.
    if an iron golem is found it is pretty sure the navigator method is not working
     
Thread Status:
Not open for further replies.

Share This Page