Removing Pig When It hits the ground

Discussion in 'Spigot Plugin Help' started by MaxNatural, May 27, 2016.

Thread Status:
Not open for further replies.
  1. I want to remove the pig when it hits the ground... I tried this code but it doesn't remove the pig.

    Code (Text):
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                if (e.getItem().getType() == Material.PORK) {
                    if (Cooldown.isOnCD(e.getPlayer().getName())) {
                        e.getPlayer().sendMessage(Hub.prefix + "This is still on cooldown!");
                        e.setCancelled(true);
                    } else {

                        Location start = p.getLocation();
                        start.add(0,1,0);
                        Vector increase = start.getDirection();
                        for (int counter = 0; counter < 1; counter++) {
                            Location point = start.add(increase);

                            Pig pig = (Pig) p.getWorld().spawnEntity(p.getEyeLocation(), EntityType.PIG);
                            pig.setVelocity(p.getLocation().getDirection().multiply(throwForce));
                           
                            if (pig.isOnGround()) {
                                pig.remove();
                            }
                            Cooldown.add(e.getPlayer().getName());
                        }
                    }
                }
            }
        }
    }
     
     
  2. I hate to say it, but.. learn to program.
    Did you really expect the pig to travel all the indeterminiate distance to the ground between these two lines of code?

    Code (Java):
    pig.setVelocity(p.getLocation().getDirection().multiply(throwForce));
                           // magic ??
     if (pig.isOnGround())
    I would suggest you use falling damage to detect it, if the height is enough, or create a BukkitRunnable with the pig entity as an argument, and check if it has landed every 30 ticks or so, then cancel the runnable (easy and effective, no real lag).
     
    • Agree Agree x 1
  3. So you're throwing a pig and wanting to remove it when it hits the ground?

    You can create a repeating task to handle this. Your for loop does nothing, and checking if the pig is on ground immediately after throwing it is not going to do anything either.

    I recommend you create a anonymous runnable that continuously checks if the pig is on the ground or not, if it is, cancel the task and remove the pig.
     
  4. I got the scheduler working now. I searched some stuff up and learned a bunch.
    How would I get the exact pig that the player threw to remove? Right now it removes all the pigs on the map xD

    Code (Text):
            Location start = p.getLocation();
                        start.add(0,1,0);
                        Vector increase = start.getDirection();
                        for (int counter = 0; counter < 1; counter++) {
                            Location point = start.add(increase);

                            final Pig pig = (Pig) p.getWorld().spawnEntity(p.getEyeLocation(), EntityType.PIG);
                            pig.setVelocity(p.getLocation().getDirection().multiply(throwForce));
                            Cooldown.add(e.getPlayer().getName());
                        }

                        Bukkit.getScheduler().scheduleSyncDelayedTask(Hub.plugin, new Runnable()
                        {
                            public void run()
                            {
                             
                                for (Entity entities : p.getWorld().getEntities()) {
                                    if (entities instanceof Pig) {
                                        Pig pig = (Pig) entities;
                                        pig.remove();
                                    }

                                }

                            }
                        },10);


                    }
                }
            }
        }
    }
    I do understand it gets all of the pig entities in the world.
     
  5. That's because you are removing all the pigs in the world. :p
    Code (Text):

    Bukkit.getScheduler().scheduleSyncDelayedTask(Hub.plugin, new Runnable()
    {
        public void run()
        {
            //this says that if is a pig, kill it!
            for (Entity entities : p.getWorld().getEntities()) {
                if (entities instanceof Pig) {
                    Pig pig = (Pig) entities;
                    pig.remove();
                }

            }

        }
    },10);
     
    I would do some thing like this:
    Code (Text):

    new BukkitRunnable() {
        @Override
        public void run() {
            if (pig.isOnGround()) {
                cancel();
                pig.remove();
            }
        }
     
    }.runTaskTimer(plugin, delay, interval);
     
     
  6. Thank you so much! Worked perfectly! :D
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.