Entity teleport problem

Discussion in 'Plugin Development' started by Cheesepro, Dec 16, 2014.

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

    Cheesepro

    So I just found a great lightweighted PetAPI from the resource section, but it missed a important feature which teleport the pet to a player when the player is not in the same world as the pet. I was trying to code that feature for the API but I am getting some really weird problems which it displays no errors in the console... Here is the code that I edited:
    Code:
        public void petFollow(final Player player , final Entity pet , final double speed){
            new BukkitRunnable(){
                @SuppressWarnings("deprecation")
                public void run(){
                    if ((!pet.isValid() || (!player.isOnline()))){
                        this.cancel();
                    }
                    net.minecraft.server.v1_7_R4.Entity pett = ((CraftEntity) pet).getHandle();
                    Object petf = ((CraftEntity) pet).getHandle();
                    Location targetLocation = player.getLocation();
                    PathEntity path;
                    path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
                    if (path != null) {
                        ((EntityInsentient) petf).getNavigation().a(path, 1.0D);
                        ((EntityInsentient) petf).getNavigation().a(2.0D);
                    }
                    //My edit starts from here
                    if(Bukkit.getWorld(player.getLocation().getWorld().getName()).equals(Bukkit.getWorld(pet.getLocation().getWorld().getName()))){
                        distance = (int) Bukkit.getPlayer(player.getName()).getLocation().distance(pet.getLocation());
                        if (distance > 10 && !pet.isDead() && player.isOnGround()) {
                            pet.teleport(player.getLocation());
                        }
                    }else{
                        pet.teleport(player.getLocation());
                    }
                    //My edit ends here
                    AttributeInstance attributes = ((EntityInsentient)((CraftEntity)pet).getHandle()).getAttributeInstance(GenericAttributes.d);
                    attributes.setValue(speed);
                }}.runTaskTimer(plugin, 0L, 20L);}
    Here is the original code from HERE:
    Code:
    public void PetFollow(final Player player , final Entity pet , final double speed){
    new BukkitRunnable(){
    public void run(){
    if ((!pet.isValid() || (!player.isOnline()))){
    this.cancel();}
    net.minecraft.server.v1_8_R1.Entity pett = ((CraftEntity) pet).getHandle();
    ((EntityInsentient) pett).getNavigation().a(2);
    Object petf = ((CraftEntity) pet).getHandle();
    Location targetLocation = player.getLocation();
    PathEntity path;
    path = ((EntityInsentient) petf).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
    if (path != null) {
    ((EntityInsentient) petf).getNavigation().a(path, 1.0D);
    ((EntityInsentient) petf).getNavigation().a(2.0D);}
    int distance = (int) Bukkit.getPlayer(player.getName()).getLocation().distance(pet.getLocation());
    if (distance > 10 && !pet.isDead() && player.isOnGround()) {
    pet.teleport(player.getLocation());}
    AttributeInstance attributes = ((EntityInsentient)((CraftEntity)pet).getHandle()).getAttributeInstance(GenericAttributes.d);
    attributes.setValue(speed);}}.runTaskTimer(this, 0L, 20L);}
    So if you guys know where did I got wrong or know how to add the feature please help, Thanks :D
     
  2. Offline

    97WaterPolo

    @Cheesepro
    Lot of repetitive code, you can get the world straight from the player/entity without using Bukkit#.getWorld(). You also can use just your player variable instead of calling Bukkit#getPlayer() on the player variable's name.

    PHP:
                if(player.getWorld().getName().equalsIgnoreCase(pet.getWorld().getName())){
                    
    int distance = (int) player.getLocation().distance(pet.getLocation());
                    if (
    distance 10 && !pet.isDead() && player.isOnGround())
                        
    pet.teleport(player.getLocation());
                }else
                    
    pet.teleport(player.getLocation());
     
  3. Offline

    Cheesepro

    @97WaterPolo
    Thanks but still the pet is not teleported nor any errors in the Console

    Just to adding on I think is line's problem, since the debug message I put right below it works.
    Code:
    else{
         pet.teleport(player.getLocation());
         player.sendMessage("debug msg");
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  4. Offline

    97WaterPolo

    @Cheesepro
    Not sure why it isn't working, try just teleporting them to the entity.

    Code:
    pet.teleport(player);
    
     
  5. Offline

    Cheesepro

    @97WaterPolo
    :( nope

    @97WaterPolo
    HOLY!!! I know why! All the code are right just that I used 1.7_R4 as the library but not as the actual server... So Problem fixed... xD Sorry for the mistake.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
Thread Status:
Not open for further replies.

Share This Page