[Solved] Shoot a particle beam

Discussion in 'Spigot Plugin Development' started by KingFaris10, May 22, 2015.

  1. Hi, I'm trying to make a beam (of particles using the ParticleEffect Lib) shoot out from a location towards a player. However, I can't figure out how to make it so once the beam collides with the player, exit out of the loop. This 'distanceSquared' thing in the following current piece of code I've made doesn't work (I was testing out to see if it detects a 1 block radius), it just freezes the main thread.

    Code (Text):

        public static void shootBeam(Location fromLocation, Player player) {
            try {
                if (player != null && player.isOnline()) {
                    if (fromLocation != null) {
                        Location toLocation = player.getEyeLocation().clone().subtract(0D, 0.1D, 0D);
                        Vector from = fromLocation.toVector(), to = toLocation.toVector();
                        if (from.getX() != to.getX() || from.getY() != to.getY() || from.getZ() != to.getZ()) {
                            Vector direction = to.subtract(from).normalize();
                            while (from.distanceSquared(to) > 1F) {
                                ParticleEffect.REDSTONE.display(ParticleEffect.ParticleColor.WHITE, from.toLocation(fromLocation.getWorld()), player);
                                from.add(direction);
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
    If I limit it to a certain number, it works, but I think this is an inefficient method.
    Code (Text):

    int shots = 0, maxShots = (int) from.distance(to) + 5;
    while (shots < maxShots && from.distanceSquared(to) > 1F) {
        // Do code
        maxShots++;
    }
     
     
  2. Ask the author of the 'Lasers' plugin it shoots beams from the picture.
     
  3. Thanks, but I just figured it out. I decided to check the distance between the original start position, and the current position of the beam, and then check if it's within the range of the beam, if not, exit out of the while loop.
     
    • Like Like x 1
  4. If trying to learn how to make particle beams etc what did you end up having it, I got a particle beam to work but it relied on the player after they shote a beam they couldnt move their mouse haahaha,
     
  5. I'm not really sure what project I was working on when making this, but I think it was this:
    Code (Text):

        private static Location getTargetLocation(Player to) {
            return to.getEyeLocation().clone().subtract(0D, 0.2D, 0D);
        }

        public static void shootBeam(Location fromOrigin, Player player, double maxRange) {
            try {
                if (player != null && player.isOnline()) {
                    if (fromOrigin != null) {
                        Location toLocation = getTargetLocation(player);
                        if (fromOrigin.getX() != toLocation.getX() || fromOrigin.getY() != toLocation.getY() || fromOrigin.getZ() != toLocation.getZ()) {
                            Location fromNew = fromOrigin.clone();
                            Vector direction = toLocation.toVector().subtract(fromOrigin.toVector()).normalize();
                            double range = Math.min(fromOrigin.distanceSquared(toLocation), maxRange * maxRange);
                            while (fromOrigin.distanceSquared(fromNew) <= range) {
                                ParticleEffect.REDSTONE.display(ParticleEffect.ParticleColor.WHITE, fromNew, player);
                                fromNew.add(direction);
                            }
                        }
                        // Do something to 'player' here, e.g. player.setHealth(player.getHealth() - 2D);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
     
     
  6. Also would you be able to link me to the lib? I have been trying to make one for a while now you should see my attempts the best I got was to spawn particles 1 after the other 1 block futher away from player ( didn't work as player could move hahah)
     
  7. I'm not sure but i think it's this:

    https://bukkit.org/threads/1-8-particleeffect-v1-7.154406/
     
    • Friendly Friendly x 2
  8. Thanks o.o :p
     
  9. did it work for you?
     
  10. I have this:

    Code (Text):

    package me.Goldiedog123.Classes.Air;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    import me.Goldiedog123.Main;

    public class Airblast implements Listener {
       
        public Airblast(Main plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }
       
         @EventHandler
           public void Airblasting(PlayerInteractEvent event) {

             Action eventAction = event.getAction();
             final Player player = event.getPlayer();

             ItemStack item = player.getItemInHand();
             if ((eventAction == Action.RIGHT_CLICK_AIR) || (eventAction == Action.RIGHT_CLICK_BLOCK)){
             if(item.getType() != Material.GOLD_INGOT) {
                 return;
             }
             
             String displayName = item.getItemMeta().getDisplayName();
             
             if(displayName != null && displayName.equalsIgnoreCase("Airblast")) {
                 
    // what should I put here?
                 
       }
      }
    }    
    }
     
     
  11. You would have to modify my code to adjust your needs. My code was to shoot a beam from a location to a player, but yours seems to be, from a location to anywhere. What I'd recommend is, adding the direction of the from Location to the clone of the from Location in a while loop and check if the block at the new from is in a block, if so, return out, if not, continue to add. Also, I'd recommend every so often checking nearby players/entities, and if in their collision box, return out too.