Need help with a directional beam!

Discussion in 'Plugin Development' started by MCTV, Jan 9, 2016.

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

    MCTV

    Ok, as the title states I have a problem adding a direction/rotation vector to my beam, if anyone is good with this stuff, please PM me because I don't want to release the code itself, only the solution! Thanks in advance!
     
  2. Offline

    Evonoucono

    We will not PM you with anything regarding your plugin. If you want help from bukkit, it will be done so on this forum.
    If you want to change the velocity of an object (such as a rotation) this might help:
    Code:
    ENTITY1.setVelocity(SOMELOCATION.toVector().subtract(ENTITY1.getLocation().toVector()).normalize().multiply(NUMBER));
    ENTITY1 is the object you are trying to change velocity, and SOMELOCATION is pretty obvious
    Instead of subtract you could also use add to invert the effect.
     
  3. Offline

    MCTV

    Ok, well since you won't help me through PM, I'll post the code here just please if you will use it give me credit.

    Code:
    package me.snipars;
    
    import java.awt.Color;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.SmallFireball;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerToggleSneakEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.Vector;
    
    
    public class PlayerListener implements Listener{ ///changed this
        public static Boolean active = false;
        public static int uses = 0;
       
        public PlayerListener(EventHandle plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            final Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.STICK) {
                new BukkitRunnable() {
                    Location loc = player.getLocation();
                    public boolean gotTarget = false;
                    public Entity target = null;
                double t = 0;
                double r = 1;
                double n = 0;
                double counter = 0;
                   
                    public void run() {
                       
                        t = t + Math.PI/12;
                        n = n + Math.PI/10;
                        int j = 0;
                        for(double i = 0; i <= 2*Math.PI; i = i + 2*Math.PI/2){
                            double x = 2*0.3*t+0.5;
                            double y = Math.cos(t+i) + 2;
                        double z = Math.sin(t+i);
                        loc.add(x,y,z);
                        ParticleEffect.FLAME.display(loc, 0, 0, 0, 0, 1);
                        loc.subtract(x,y,z);
                        loc.add(2*0.3*t, 2, 0);
                            ParticleEffect.FIREWORKS_SPARK.display(loc, 0, 0, 0, 0, 1);
                            loc.subtract(2*0.3*t, 2, 0);
                       
                        if (t > 60) {
                            this.cancel();
                        }
                    }
                       
                    }
                }.runTaskTimer(EventHandle.getInstance(), 0, 1);
            }
            }
        }
    How do you think I can add it there?
     
  4. Offline

    Evonoucono

    @MCTV It depends on what you are trying to do... Are you trying to make the beam curve and lock-on to an entity? Are you trying to make it curve to where your player is looking? You should be extremely specific.
     
  5. Offline

    MCTV

    I'd like the beam to start the way the player is looking, and continue to go that way. Thanks for replying by the way!
     
  6. Offline

    Zombie_Striker

    No one is hear to steal your code (trust me, you will most likely never be the first one to think of the idea).

    Also, it's against bukkits rules to take anything from a thread (a problem or idea) off of the thread (E.g. into a PM)

    To get a beam:

    • Create a location that starts at where the player is looking.
    • Get the direction the player is looking (it's under the lines of Player.getLocation.getDirection())
    • Create a for loop, which will trigger maybe 100 times (i < 100)
    • Inside that for loop, use Location.add(The direction).
    • Summon a particle at that location.
    That's it.
     
  7. Offline

    MCTV

    Thanks!
     
  8. Offline

    Zombie_Striker

    Because you asked in the PM. All of the lines with " * " are the lines that I have modified/added.
    Code:
    @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            final Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.STICK) {
                new BukkitRunnable() {
            *        Location loc = player.getLocation().add(0,1,0);//Get the head's location
                    Vector direction = player.getLocation().getDirection();
                    public boolean gotTarget = false;
                    public Entity target = null;
                double t = 0;
                double r = 1;
                double n = 0;
                double counter = 0;
                   
                    public void run() {
                       
                        t = t + Math.PI/12;
                        n = n + Math.PI/10;
                        int j = 0;
                *        for(double i = 0; i <= 2*Math.PI; i = i + Math.PI){ // You where multiplying and then deviding by 2. Is this what you meant?
                            double x = 2*0.3*t+0.5;
                            double y = Math.cos(t+i) + 2;
                        double z = Math.sin(t+i);
                        loc.add(x,y,z);
                        ParticleEffect.FLAME.display(loc, 0, 0, 0, 0, 1);
                        loc.subtract(x,y,z);
                        loc.add(2*0.3*t, 2, 0);
                            ParticleEffect.FIREWORKS_SPARK.display(loc, 0, 0, 0, 0, 1);
                            loc.subtract(2*0.3*t, 2, 0);
    
                  *        loc.add(direction);//Adding the actual direction. Now it should  move
    
                       
                        if (t > 60) {
                            this.cancel();
                        }
                    }
                       
                    }
                }.runTaskTimer(EventHandle.getInstance(), 0, 1);
            }
            }
    
     
  9. Offline

    MCTV

    Hey what do I do now? Please tell me what I did wrong :)

    Sorry, that message just got verified. Oh and now it shoots very fast to the point where its like half a second
     
    Last edited by a moderator: Jan 9, 2016
  10. Offline

    Zombie_Striker

    @MCTV
    I assumed that is what you wanted. To slow it down, make a new runnable for each particle, each one increasing the delay.
     
  11. Offline

    MCTV

    How would I do that? Also I just wanted the particle effect I made to go in the direction the player is facing, not faster or anything like that. Thanks in advance!
     
  12. Offline

    Zombie_Striker

    • Create a for loop (which will be all the amount of particles you want to spawn)
    • Inside the loop, create a new runnble (like the one you have)
    • Put all the code for adding the direction, spin, and the spawning the particle inside that runnable.
    • Set the delay of the runnable to be "Delay*Number-of-particle" (get the Number-of-particle from the loop)
     
  13. Offline

    AppleBabies

    @MCTV Eewwww...SteeZyy's code...
     
  14. Offline

    MCTV

    No. its not.
     
  15. Offline

    MCTV

    I've been having trouble adding it in, should I post what I got so far? Or can you show me how it would look in code?
     
  16. Offline

    Zombie_Striker

    @MCTV
    Since I'm not going to spoonfeed you code, let see what you got.
     
  17. Offline

    MCTV

    Ok this is what I got (Sry, im new to coding)
    Code:
    @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            final Player player = event.getPlayer();
            if (player.getItemInHand().getType() == Material.STICK) {
                for loop(90);
                new BukkitRunnable(Delay 90) {
                    Location loc = player.getLocation();
                    Vector direction = loc.getDirection().normalize();
                    public boolean gotTarget = false;
                    public Entity target = null;
                double t = 0;
                double r = 2.5;
                double n = 0;
                double counter = 0;
                   
                    public void run() {
                       
                        t = t + Math.PI/12;
                        n = n + Math.PI/10;
                        int j = 0;
                        for(double i = 0; i <= 1.5*Math.PI; i = i + 1.5*Math.PI/2){
                            double x = 2*0.3*t+0.5;
                            double y = Math.cos(t+i) + 2;
                        double z = Math.sin(t+i);
                        loc.add(x,y,z);
                        ParticleEffect.FLAME.display(loc, 0, 0, 0, 0, 1);
                        loc.subtract(x,y,z);
                        loc.add(2*0.3*t, 2, 0);
                            ParticleEffect.FIREWORKS_SPARK.display(loc, 0, 0, 0, 0, 1);
                            loc.subtract(2*0.3*t, 2, 0);
                       
                        if (t > 60) {
                            this.cancel();
                        }
                    }
                       
                    }
                }.runTaskTimer(EventHandle.getInstance(), 0, 1);
            }
            }
        }
     
  18. Offline

    Zombie_Striker

    I can see that. Right now, I think what you really should do is first learn Java and then come back to this. It seems you do not have a full grasp of Java's syntax, and that most of this code was copied and pasted. Please find a good tutorial from this page, take a few weeks to learn Java, and then come back. You should be able to fix your mistakes and understand what you would need to do at that point.
     
  19. Offline

    MCTV

    Did you say I'm using someone elses code?

    Also, I'm not looking to get into coding. I just wanted to fix this because I do math for particle effects, That's all. How could I fix the code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 12, 2016
  20. Offline

    Zombie_Striker

    @MCTV
    1. If you have no intention on getting into coding, nor do you wish to learn Java, why are you making plugins?
    2. At this point, I can't help you understand any further of what you need to do without spoonfeeding you code. You may want to asking the the Plugin Request Forums for code.
    3. The reason why I said you copied and pasted code was because of the following:
    • You do not understand how to create a for loop in the first example, yet you managed to correctly create the for loop for the second example. Also, you added a semicolon at the beginning of the first for loop (which stops it from working) and added brackets correctly for the second.
    • The spacing on each of these blocks show that they came from separate places (whether it be from other places of your code, or from other links)
    • You still have the world "Delay" in here.
    • Another example of the formatting issue.
     
  21. Offline

    MCTV

    Sorry for the misunderstanding, all I meant is that I do MATH for plugins, not all the code. I have a template made by my friend that I just edit some of the code so the math works correctly, and I edit the math aswell. If the math works then it to my friend who re-creates the whole code from scratch but implements the same math so the particle works. Thanks for all your help.
     
Thread Status:
Not open for further replies.

Share This Page