Solved Arrow Trails Spawns Zombies

Discussion in 'Spigot Plugin Development' started by Anshu, Jun 29, 2021.

  1. Hi, I am working on a small plugin. That is whenever a player shoots an arrow from bow or crossbow than it's get detected and a line of zombies spawns as a trail of arrow.... and I have no idea about this I only learned about making custom commands and now I am moving on to listeners.... and please answer me with full explanation and code about this...
     
  2. I am not sure if spigot allows you to release server killing plugins.
    But yeah, mistakes are the best way to learn.
    So hereby this server-destroyer-2.0

    Code (Java):
    Map<Integer, Integer> arrowMap = new LinkedHashMap<>();

    @EventHandler
    private void onArrowHitEntity(ProjectileHitEvent event) {
        if(event.getEntity() instanceof Arrow) {
            if(arrowMap.containsKey(event.getEntity().getEntityId())) {
                Bukkit.getScheduler().cancelTask(arrowMap.get(event.getEntity().getEntityId()));
                arrowMap.remove(event.getEntity().getEntityId())
            }
        }
    }

    @EventHandler
    public void onItemSpawn(ItemSpawnEvent event)
    {
        if(event.getEntity() instanceof Arrow) {
            int taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
                event.getEntity().getLocation().getWorld().spawnEntity(event.getEntity().getLocation(), EntityType.ZOMBIE);
            }, 10, 10);
            arrowMap.put(event.getEntity().getEntityId(), taskId);
        }
    }
    ItemSpawnEvent to check if arrow is launched. then it adds a schedule repeat event where it triggers every 1/2 second (10 ticks).
    When it triggers it will spawn a zombie. Then when ProjectileHitEvent checks if it is an arrow. Then it cancel task and remove the map entry. Here you have it all.
    Please dont destroy the server :cry:
     
    • Funny Funny x 1
  3. Code (Java):
        @EventHandler
        public void onItemSpawn(ItemSpawnEvent event)
        {
            if(!(event.getEntity() instanceof Arrow)) return;
            new BukkitRunnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if(event.getEntity().isOnGround() || event.getEntity().isEmpty()) this.cancel();
                    event.getEntity().getLocation().getWorld().spawnEntity(event.getEntity().getLocation(), EntityType.ZOMBIE);
                }
            }.runTaskTimer(plugin, 10, 10);
        }
    This will make it easier.

    edit: And this don't even need listeners
     
    #3 Dru_TNT, Jun 29, 2021
    Last edited: Jun 29, 2021
    • Like Like x 1
  4. There it is, I knew there was a function called Entity#isOnGround
     
  5. Nevermind I found a tutorial here https://bukkit.org/threads/tutorial-particle-effects-for-arrows.251835/ I just replaced showing particles to every player with summoning zombie on that location...

    By The Way Thank You everyone for helping....
    If there any mistakes please don't mind because I am Indian and I don't know perfect English...

    Here is my code

    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityShootBowEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import java.util.ArrayList;

    public class Main extends JavaPlugin implements Listener {

        public ArrayList<Projectile> arrows = new ArrayList<Projectile>();

        @Override
        public void onEnable() {
            Bukkit.broadcastMessage("[Arrows Trail] Plugin is enabled");
            Bukkit.getPluginManager().registerEvents(this, this);
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    for (Projectile arrow : arrows) {
                        if (arrow.getShooter() instanceof Player) {
                            arrow.getWorld().spawnEntity(arrow.getLocation(), EntityType.ZOMBIE);
                        }
                    }
                }
            },0,2);
        }

        @Override
        public void onDisable() {
            Bukkit.broadcastMessage("[Arrows Trail] Plugin is disabled");
        }

        @EventHandler
        public void onArrowFire(EntityShootBowEvent event) {
            arrows.add((Projectile) event.getProjectile());
            Player shooter = (Player) ((Projectile) event.getProjectile()).getShooter();
            shooter.sendMessage("You Fire A Arrow");
        }

        @EventHandler
        public void onArrowHit(ProjectileHitEvent event) {
            arrows.remove(event.getEntity());
            Player shooter = (Player) event.getEntity().getShooter();
            shooter.sendMessage("Your Arrow Hit Something");
        }
    }