[Tutorial]Particle effects for arrows

Discussion in 'Resources' started by Funergy, Apr 2, 2014.

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

    Funergy

    Slikey I've made this when I was learning java/bukkit for like 3 months or something. obviously I can do better than this.
     
  2. Offline

    Funergy

    Slikey I've updated my code :). no runnables running when theres no arrow getting shot
    Thanks for the method. I'll give you credit at the original post
    Code:
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
     
    import net.minecraft.server.v1_7_R4.Packet;
    import net.minecraft.server.v1_7_R4.PacketPlayOutWorldParticles;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Entity;
    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 org.bukkit.scheduler.BukkitRunnable;
     
    /**
    * @author Funergy
    *
    */
    public class ParticleArrows extends JavaPlugin implements Listener{
            public ArrayList<Projectile> arrows = new ArrayList<Projectile>();
       
            public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);//Removed the Bukkit Runnable
            }
       
            @EventHandler
            public void onShootBow(EntityShootBowEvent e){
                arrows.add((Projectile) e.getProjectile());
                addParticleEffect((Projectile)e.getProjectile());
               
       
       
            }
            @EventHandler
            public void onProjectileHit(ProjectileHitEvent e){
                if(e.getEntity() instanceof Arrow){
                arrows.remove(e.getEntity());
     
                }
            }
          //moved the Bukkit runnable to a single method and not in the onEnable
            @SuppressWarnings("deprecation")
            public void addParticleEffect(final Projectile entity){
                new BukkitRunnable(){
     
                    @Override
                    public void run() {
                        if(arrows.contains(entity)){//if the arrow still is in the air
                            Map<Player, Location> locationCache = new HashMap<>();
                            for (Player online : Bukkit.getOnlinePlayers()) {
                                  locationCache.put(online, online.getLocation());
                               
                            }
                              Location loc = entity.getLocation();
                              Packet packet = new PacketPlayOutWorldParticles("flame",
                                  (float) loc.getX(),  (float) loc.getY(), (float) loc.getZ(),
                                  0, 0, 0,
                                  0, 1);
                             
                              for (Map.Entry<Player, Location> entry : locationCache.entrySet()) {
                                  if(entry.getKey().isOnline()){
                                   
                                      //If the player has left when the arrow was going. we should remove him from the map to avoid NPE's
                                if (entry.getValue().getWorld() == loc.getWorld() &&
                                    entry.getValue().distanceSquared(loc) <= 16*16) {
                                  ((CraftPlayer) entry.getKey()).getHandle().playerConnection.sendPacket(packet);
     
                                }
                   
                              }
                        }
                        }else{//we cancel the event when the arrow isn't in the air anymore.
                            this.cancel();
                            return;
                        }
            }
                }.runTaskTimer(this, 0, 1);
            }
       
     
    }
    
     
Thread Status:
Not open for further replies.

Share This Page