Despawn/Kill a mob after a certain amount of time?

Discussion in 'Plugin Development' started by yankeesblocks, Apr 30, 2015.

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

    yankeesblocks

    Hi, i'm working on a plugin, and for a part of it, a bat needs to spawn and die a certain amount of time after spawning. I originally had the bat spawn with the wither effect, but that creates annoying noises and particles, which ruins the effect. Anyone have any help?
     
  2. Offline

    Hex_27

    @yankeesblocks try using a scheduler. Then after your decided time, do bat.setHealth(0.0);
     
  3. Offline

    ImaTimelord7

    @yankeesblocks if you still need help with this, Tahg me and I will give you a hand. Otherwise mark this thread as solved.
     
  4. Offline

    yankeesblocks

  5. Offline

    SuperOriginal

    Show your code.
     
  6. Offline

    yankeesblocks

    @SuperOriginal
    Code:
    package me.yankeesblock.gasBombs;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import net.minecraft.server.v1_7_R3.PacketPlayOutWorldParticles;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    import org.bukkit.entity.Bat;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityTeleportEvent;
    import org.bukkit.event.entity.PotionSplashEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapelessRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class MainClass extends JavaPlugin implements Listener {
    
        ArrayList<Bat> bats = new ArrayList<Bat>();
    
        private int counter;
    
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            recipe();
        }
    
        private void recipe() {
            getServer().getPluginManager().registerEvents(this, this);
            ItemStack bomb = new ItemStack(Material.POTION, 1, (short) 16420);
            ItemMeta bombMeta = bomb.getItemMeta();
            bombMeta.setDisplayName("§d§lGas Bomb");
            bombMeta.setLore(Arrays.asList("§5Creates a cloud of toxic gas"));
            bomb.setItemMeta(bombMeta);
            ShapelessRecipe recipe = new ShapelessRecipe(bomb);
            recipe.addIngredient(1, Material.POTION, (short) 16420);
            recipe.addIngredient(1, Material.TNT);
            Bukkit.addRecipe(recipe);
        }
    
        @EventHandler
        public void onPotionSplash(final PotionSplashEvent potion) {
            ItemStack bomb = new ItemStack(Material.POTION, 1, (short) 16420);
            ItemMeta bombMeta = bomb.getItemMeta();
            bombMeta.setDisplayName("§d§lGas Bomb");
            bombMeta.setLore(Arrays.asList("§5Creates a cloud of toxic gas"));
            bomb.setItemMeta(bombMeta);
            if (potion.getPotion().getItem().equals(bomb)) {
                Location loc = potion.getPotion().getLocation();
                loc.getWorld().playSound(loc, Sound.FIZZ, 5, 1);
                new BukkitRunnable() {
                    private Object plugin;
                    private int counter;
                    public void run() {
                        this.plugin = plugin;
                        if (counter < 50) {
                            Location loc2 = potion.getPotion().getLocation();
                            Bat b = loc2.getWorld().spawn(loc2, Bat.class);
                            bats.add(b);
                            b.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 99999, 1, false));
                            b.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 99999, 0, false));
                            b.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 300, 3, false));
                            b.setCustomName("batBomb1");
                            counter = counter +1;
                        } else {
                            this.counter = counter;
                        }
                    }
                }.runTaskTimer(this, 0, 10);
            }
            new BukkitRunnable() {
                public void run() {
                    for (Bat batList : bats) {
                        if (!(batList.isDead())) {
                            for (Player online : Bukkit.getOnlinePlayers()) {
                                ((CraftPlayer) online).getHandle().playerConnection.sendPacket(new PacketPlayOutWorldParticles("witchMagic", (float) batList.getLocation().getX(), (float) batList.getLocation().getY(), (float) batList.getLocation().getZ(), 1,1, 1,(float) 0, 6));
                                for (Entity poison : batList.getNearbyEntities(3, 3, 3)) {
                                    if (poison instanceof Player) {
                                        Player p = (Player) poison;
                                        p.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 300, 3, false));
                                        p.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 300, 3, false));
                                        p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 300, 3, false));
                                    }
                                }
                            }
                        }
                    }
                }
            }.runTaskTimer(this, 0, 1);
        }
    }
    
     
    Last edited: May 1, 2015
  7. Offline

    ImaTimelord7

    I had problems with Schedules when I first used them, they are a bit weird in my opinion, from your description here:
    Here is some code from one of my plugins, using a delay:
    Code:
    @EventHandler
        public void PlayerBreakBlock(BlockBreakEvent e) {
            Player p = e.getPlayer();
            Block b = e.getBlock();
            Material Pick = e.getPlayer().getItemInHand().getType();
            if (p.isOp() == false) {
           
                if (Pick == Material.WOOD_PICKAXE ||
                    Pick == Material.STONE_PICKAXE ||
                    Pick == Material.IRON_PICKAXE ||
                    Pick == Material.DIAMOND_PICKAXE) {
               
                    if (b.getType() == Material.IRON_ORE) {
                        e.setCancelled(true);
                        b.setType(Material.DIRT);
                        p.getInventory().addItem(IronOre());
                        delayiron(b);
                   
                    }else if (b.getType() == Material.GOLD_ORE) {
                        e.setCancelled(true);
                        b.setType(Material.DIRT);
                        p.getInventory().addItem(GoldOre());
                        delaygold(b);
                   
                    }else e.setCancelled(true);
                }else e.setCancelled(true);
            }
        }
    
        public void delayiron(final Block b) {
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
           
            public void run() {
                b.setType(Material.IRON_ORE);
            }
            }, 1200L);
        }
    So there are two Methods, the First is my BlockBreak method which calls the delay, so that once iron ore is broken and turned into dirt, it later turns back into Iron Ore.
    You would replace that method with your one to spawn your bat, and add the delayiron(b); the b is Block, which is the block mined, so that in the 2nd method for the delay, the delay knows what block to replace.
    You would need to change the Block to Entity, so that you can kill that Bat.

    In the 2nd method I have the block turned into iron ore again, at the bottom I have the delay time for it, 20L = 1 second, so I have a minute delay.
     
Thread Status:
Not open for further replies.

Share This Page