Making Particles Last Forever

Discussion in 'Plugin Development' started by Datmusic, Jan 19, 2015.

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

    Datmusic

    Hey, I am new to the whole coding thing. I've been trying to figure out how to 1 make it so I can have other classes but it seems the code has to be in the main for it to work for Bukkit (I know I am wrong but I'm not sure). Secondly, how can I make my code so when I do /pfire the fire particles stay on that player until they log off or they switch to a different particle command or they just turn it off in general? (Im unable to get the particles to keep going they disappear after a second).

    http://gyazo.com/c2639b7962c511438c21fb6a3e1bb42d

    Here is my code:

    package datplugin;

    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;


    public class DatMusic extends JavaPlugin {

    @Override
    public void onEnable() {

    }


    @Override
    public void onDisable() {

    }
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("pfire")) {
    Player player = (Player) sender;

    for(int i = 0; i < 100; i++) {
    player.getWorld().playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 10);

    }
    }
    else if(cmd.getName().equalsIgnoreCase("glimmer")){
    for(int i = 0; i < 100; i++){
    Player player = (Player) sender;
    player.getWorld().playEffect(player.getLocation(), Effect.POTION_BREAK, 10);

    }

    }




    return false;

    }
    }
     
  2. @Datmusic What you want to do is schedule the particles being sent to the player in a repeating task with a small delay. For help using the scheduler, you can check out its link here.
     
  3. Offline

    Skionz

    @Datmusic You can't. A scheduler would be your best option.
     
  4. Offline

    Zandor300

    You have to create a repeating task where you keep spawning the particles. You need to save the BukkitTask in a HashMap<String(Playername), BukkitTask> so you can cancel it when they want to disable the particles. Remember to remove the BukkitTask from the HashMap after you cancel it.

    EDIT: Too late :)
     
  5. Offline

    Datmusic

    So can someone give me a little demonstration of what it would look like. (Where the particles keep going until they do for example /dparticles off or /pfire off or w/e.
     
  6. Offline

    Zandor300

    HashMap<String(playername), BukkitTask> hashmap

    Method startParticles(player) {
    start repeating task {
    spawnparticles
    }
    hashmap.put(bukkittask from repeatingtask, player.getName());
    }

    Method stopParticles(player) {
    hashmap.get(player.getName()).cancel();
    hasmap.remove(player.getName());
    }
     
  7. @Zandor300 It would be better to store the BukkitTask as an integer (TaskID).
     
  8. Offline

    Datmusic

    @DJSkepter Can you show me a example and put it in my code so I could see what you mean? Or at least just give me a example either or.
     
  9. Offline

    Skionz

    If you want someone to write your code for you then go here.
     
  10. Offline

    Datmusic

    @Skionz I don't I simply wanted to see what his alternative was.
     
  11. Offline

    Zandor300

    SPOILER ALERT: Spoonfeeding
    You don't learn anything if you just copy it!


    Create a HashMap:
    Code:
    private static HashMap<String, Integer> playerTask = new HashMap<String, Integer>();
    Then in your onCommand:
    Code:
            if(command.getName().equalsIgnoreCase("mycommand")) {
                if(playerTask.containsKey(sender.getName())) {
                    Bukkit.getScheduler().cancelTask(playerTask.get(sender.getName()));
                    playerTask.remove(sender.getName());
                } else {
                    playerTask.put(sender.getName(), Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                        @Override
                        public void run() {
                            // <SPAWN PARTICLES>
                        }
                    }, 0l, 20l).getTaskId());
                }
            }
            return false;
     
  12. Offline

    Datmusic

    @Zandor300 I almost have everything working except these little things

    package datplugin;

    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;


    public class DatMusic extends JavaPlugin {

    @Override
    public void onEnable() {

    }


    @Override
    public void onDisable() {

    }
    private static HashMap<String, Integer> playerTask = new HashMap<String, Integer>();
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("pfire")) {
    Player player = (Player) sender;
    if (playerTask.containsKey(sender.getName())) {
    Bukkit.getScheduler().cancelTask(playerTask.get(sender.getName()));
    } else {
    playerTask.put(sender.getName(), Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
    @Override
    public void run(){
    for(int i = 0; i < 100; i++) {
    player.getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 10);
    }
    }, 01, 201).getTaskId()));

    }
    }







    return false;

    }
    }
    http://prntscr.com/5v032n
    http://prntscr.com/5v036k
    http://prntscr.com/5v03ib

    After this is fixed I'll finally be done and can work on what the long time plan for this is.
     
  13. @Datmusic Try doing what the error tells you. When it says, "insert "}" to complete class body", do it. Same when it tells you, "delete this token ")" ". Also, please use the "insert" button then select "code" when you want to insert code into your post. :)
     
  14. Offline

    Datmusic

  15. Offline

    1Rogue

    Highlight everything and hit Ctrl+I, you'll probably see what the problem is in no time. (Hint, you open a lot of braces and don't close them).
     
  16. Offline

    Skionz

    @Datmusic Your IDE is telling you exactly how to fix it lol
     
  17. Offline

    Datmusic

    Fixed it but it doesn't work. It will do the particle once, and than stop, and if you try to do the command again it does nothing.

    http://gyazo.com/04256833c82b92950bca3d22b92969d6

    Code:
    package datplugin;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Effect;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class DatMusic extends JavaPlugin {
       
        @Override
        public void onEnable() {
    
        }
    
       
        @Override
        public void onDisable() {
           
        }
        private static HashMap<String, Integer> playerTask = new HashMap<String, Integer>();
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("pfire")) {
                final Player player = (Player) sender;
            if (playerTask.containsKey(sender.getName())) {
                Bukkit.getScheduler().cancelTask(playerTask.get(sender.getName()));
            } else {
                playerTask.put(sender.getName(), Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
                    @Override
                    public void run(){
                        for(int i = 0; i < 100; i++) {
                            player.getWorld().playEffect(player.getLocation(), Effect.ENDER_SIGNAL, 10);
                        }
                    }}, 01, 201).getTaskId()); 
                   
                }
            }
                   
               
               
           
           
       
       
            return false;
       
       
        }
    }
    
       
    
     
    Last edited: Jan 22, 2015
  18. @Datmusic It's because you're returning false on line 49. Replace false with true.
     
  19. Offline

    Datmusic

    Nope, only works once and than does nothing. If I wanted a command that only did the particles once I wouldn't have added all that code. I wanted it to continue going until they did the command again, like Mineplex has it. Any clue how to do this for this code isn't doing it.
     
Thread Status:
Not open for further replies.

Share This Page